# Thread: Need Some Help With Logic and Some Music Theory

1. Member
Join Date
Dec 2015
Posts
6
Rep Power
0

## Need Some Help With Logic and Some Music Theory

Hey, I only started using Java a few days ago and I need some help with the logic of a console app I am writing. Not sure if it should go in this forum or even if it's an actual Java question, but here it goes.

The app I am writing will (hopefully) find what musical key a user's song is in. It starts by asking them to type in their notes. Once they are done with that, it should cycle through an array of arrays that contain each scale and the notes that fall under them. For example, the C Major scale contains the notes A, B, C, D, E, F, G. If the user types in the notes A, E, D, G, they are probably using the C Major Scale. So what I have to do is find which scale has the most notes that the user provided. Which scale is most in common with those notes.

I have stored the Major scales of A-G natural(not sharp or flat) in arrays. I also collected the user input and validated it. So all I have to do now is figure out what to do when I cycle through the notes they provide and then the scales.

Here's what I have so far...
Java Code:
```package com.example.java;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to the musical key calculator!");
System.out.println("Enter your notes one at a time.");
System.out.println("Use uppercase letters A-G and # for sharp and b for flat.(Ex. Eb)");
System.out.println("Remember that E# and B# do not exist in music!");
System.out.println("You can enter up to 7 notes or When you have entered all of your notes, type 'done'");
System.out.println("------------------------------------------------------------------");

ArrayList notes = new ArrayList();
for (int i = 0; notes.size() < 7; i++) {

System.out.print("Enter a note: ");

String note = scanner.nextLine();

if (note.equals("done")) {

break;

} else {

String rPattern = "([A-G]{1})(#|b{1})?";
Pattern regex = Pattern.compile(rPattern);
Matcher match = regex.matcher(note);

if(match.find() && !note.equals("E#") && !note.equals("B#")) {

} else {

System.out.println("The string '" + note + "' is not a valid note and was not added.  Use uppercase A-G and if necessary followed by # for sharp or b for flat.");
System.out.println("Your current notes are " + notes);

}
}

}

ArrayList aMajor = new ArrayList();

ArrayList bMajor = new ArrayList();

ArrayList cMajor = new ArrayList();

ArrayList dMajor = new ArrayList();

ArrayList eMajor = new ArrayList();

ArrayList fMajor = new ArrayList();

ArrayList gMajor = new ArrayList();

ArrayList allScales = new ArrayList();

foreach(String note : notes){

foreach(String scale : allScales){

if(scale.contains(note)){

//This is as far as I have gotten.
}
}
}

System.out.println("Your notes are " + notes);

}
}```

2. ## Re: Need Some Help With Logic and Some Music Theory

There are 12 (major) scales; ignore (and convert) the notes with a flat; e.g. convert Bb to a A#. Every scale contains a subset of 12 different notes. Every note can be represented by a unique bit, so each scale can be represented by a unique bitsequence (a 12 bit word). If the notes from a user supplied sequence is a subsequence of a the bits of a scale, you can assume the scale, i.e. if (userSupplied&scale == userSupplied) // 'scale' is the used scale.

kind regards,

Jos

3. ## Re: Need Some Help With Logic and Some Music Theory

Perhaps an explanation of the magic of the AND(&) operator would be useful for the OP to show the beauty of the solution.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need Some Help With Logic and Some Music Theory

Of course, there is a "minor" problem. The relative minor of any major scale uses the same key signature (example, C major and A minor). So you won't be able to discern whether the key is in a minor or major scale by the provided notes (this is normally figured out by looking at the first or last measure - but that doesn't always work either). And you may need extra logic to derive the proper enharmonic scale (e.g. Db major and C# major).

Regards,
Jim

5. ## Re: Need Some Help With Logic and Some Music Theory

True, but the OP only wants to recognize the major scales; wrt to the Db major and C# major differences: if you keep the 'b' separate from the '#' (the representations still fit in a single 32 bit word: A ... G A# ... G# Ab ... Gb), one is able to distinguish between, say Db and C#; only for violins and the like, there is a difference between the two scales (except for JS Bach and his 'wohl temperierter klavier', but JS Bach was a creep ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•