Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Need Some Help With Logic and Some Music Theory

  1. #1
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default 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#")) {
    
                        notes.add(note);
    
                    } 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();
            aMajor.add("A");
            aMajor.add("B");
            aMajor.add("C#");
            aMajor.add("D");
            aMajor.add("E");
            aMajor.add("F#");
            aMajor.add("G#");
    
            ArrayList bMajor = new ArrayList();
            bMajor.add("A#");
            bMajor.add("B");
            bMajor.add("C#");
            bMajor.add("D#");
            bMajor.add("E");
            bMajor.add("F#");
            bMajor.add("G#");
    
            ArrayList cMajor = new ArrayList();
            cMajor.add("A");
            cMajor.add("B");
            cMajor.add("C");
            cMajor.add("D");
            cMajor.add("E");
            cMajor.add("F");
            cMajor.add("G");
    
            ArrayList dMajor = new ArrayList();
            dMajor.add("A");
            dMajor.add("B");
            dMajor.add("C#");
            dMajor.add("D");
            dMajor.add("E");
            dMajor.add("F#");
            dMajor.add("G");
    
            ArrayList eMajor = new ArrayList();
            eMajor.add("A");
            eMajor.add("B");
            eMajor.add("C#");
            eMajor.add("D#");
            eMajor.add("E");
            eMajor.add("F#");
            eMajor.add("G#");
    
            ArrayList fMajor = new ArrayList();
            fMajor.add("A");
            fMajor.add("Bb");
            fMajor.add("C");
            fMajor.add("D");
            fMajor.add("E");
            fMajor.add("F");
            fMajor.add("G");
    
            ArrayList gMajor = new ArrayList();
            gMajor.add("A");
            gMajor.add("B");
            gMajor.add("C");
            gMajor.add("D");
            gMajor.add("E");
            gMajor.add("F#");
            gMajor.add("G");
    
            ArrayList allScales = new ArrayList();
            allScales.add(aMajor);
            allScales.add(bMajor);
            allScales.add(cMajor);
            allScales.add(dMajor);
            allScales.add(eMajor);
            allScales.add(fMajor);
            allScales.add(gMajor);
    
            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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    Norm likes this.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Rough set theory in java
    By sylvia in forum Advanced Java
    Replies: 13
    Last Post: 09-27-2014, 05:07 AM
  2. Check my JAVA theory HW?
    By tripline in forum New To Java
    Replies: 5
    Last Post: 10-28-2011, 01:04 AM
  3. OO Class Theory Question
    By Dark in forum New To Java
    Replies: 22
    Last Post: 04-20-2011, 02:16 PM
  4. Minimax game theory with mancala
    By jigglywiggly in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 12:04 PM

Posting Permissions

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