Scale Mapper

I have played guitar for a while, but always struggled with music theory.  I play my guitar for fun and learning theory always seems the opposite of fun to me.  In an attempt to better understand scales, I made a little program in Java a couple of years ago that shows where the notes of a few scales are on a guitar fretboard.  Initially, it was just a program that let the user pick the scale and key and the program would plot the notes on the fretboard using color codes for the 1st, 2nd, 3rd, etc.  My dad looked at it and said, “where is the musical notation?”  Drawing a fretboard with colored dots and computing notes and locations was a bit of work.  Adding musical notation was not something I was keen on.  Years passed.  I was now working long hours at a pharma plant in New Jersey.  I would be at the plant 12-16 hours a day, but I would often have an hour or two here and there where I was waiting for approvals of prior work.  I decided to fill this time by adding musical notation to my program.  I thought there might be an existing Java package that would accomplish this.  There was something called JFugue, but I did not want to use a third party package to program.  I decided to just try to draw a simple note and a staff and take it from there.

Each note is simply a 10×15 oval with a line on one side pointing up or down.  I learned that any note below the third staff line goes up and any line above the third staff line goes down.  I found that I could plot the 𝄞 symbol as a font on my graphics window.  This was a relief.  Drawing this thing pixel by pixel did not sound like fun.  I then drew five evenly spaced horizontal lines through it.  It took a little fiddling to get the sizing and spacing of everything correct, but I got it.

My program exploits the intervals between notes to determine which notes belong to a particular scale and which do not.  I have an array that contains the notes of a chromatic scale.  This is a scale that has every possible note in order  A, A♯, B, C, C♯, D, D♯, E, F, F♯, G, and G♯.  There is an interval of half a step between each one of these notes.  Two half steps is a whole step.  A major scale – Do, Re, Mi, Fa, So, La, Ti, Do has intervals of whole, whole, half, whole, whole, whole.   Grabbing notes from the chromatic scale at these intervals means the A Major scale would be A, B, C♯, D, E, F♯, G♯.  A minor scale has an interval of whole, half, whole, whole, half, whole, so an A minor scale would be A, B, C, D, E, F, G.  To determine other scales, I just need to know the intervals between the notes and grab them from the chromatic scale.  This is fairly simple, but the chromatic scale can also be written as A, B♭, B, C, D♭, D, E♭, E, F, G♭, G, A♭, so I had to figure out which chromatic scale to pick from.  I used string arrays with the different keys to determine if I needed to use the chromatic scale with the sharps or the chromatic scale with the flats to build the new scale.

For Major scales:
HasFlats={“G♭”,”D♭”,”A♭”,”E♭”,”B♭”,”F”};
HasSharps={“C”,”G”,”D”,”A”,”E”,”B”,”F♯”,”G♯”};
For minor scales:
HasFlatsMinor={“D”,”G”,”C”,”F”,”B♭”};
HasSharpsMinor={“E”,”B”,”C♯”,”F♯”,”G♯”,”D♯”};

When the user picks the key and the scale, I know which chromatic scale to use.  I had to figure this out by looking at the key signature of every possible key.  Adding a scale was now as simple as just adding an array with the correct intervals from the scale:

MajorInterval = Whole,Whole, Half, Whole, Whole, Whole
MinorInterval = Whole,Half, Whole, Whole, Half, Whole

I then came up with the routine to plot the notes on the staff.  Adding a new scale simply means knowing the interval and adding another array of corresponding intervals like those above for the Major and minor scales.  So I finish this and am asked, “Why can’t we hear the notes?”

This actually was not as hard as I though it would be.  Java has a synthesizer package, but instead of notes it takes in numbers that correspond to notes to play a tone.  The low, open E string on the guitar is tone 64.  For every half step up, you just have to add one, so the first fret of the low E string is F, or 65, and so on.  I used the chromatic scale again to make a routine that converted a note to a corresponding number to play.  The synthesizer is not perfect.  The tone is very accurate, but if the computer is running something else (it’s always running something else) while playing notes, it alters the timing somewhat between notes.  Maybe I will figure out how to clean this up later.  To convert a note to a number, I take the element number from the chromatic scale, 0-11, where E=0, F=1, F♯=2 and so forth

“E”, “F”, “F♯”, “G”, “G♯”, “A”, “A♯”, “B”, “C”, “C♯”, “D”,”D♯”

NumberOfChromaticScale +64+ octave * 12 = Note number for synthesizer

So E=64, F=65, and so on.  I increment the octave every time the root note is encountered again.

There are many different instrument sounds that can be played, but since this is a guitar program, I chose the classical guitar sound.  Oddly, the code to play a note takes an INSTRUMENT parameter, but this does not change the instrument.  There is a parameter called programChange that changes the instrument.  It took a bunch of Googling to figure this out.  Here is the, Yawn!, code:

channels[INSTRUMENT].programChange(25);// 0-8 piano, 9-16 percussion, 25 Classical, 26 acoustic, 27-31 electric

When a program is compiled in Java a .jar file executable is created.  This just has the Java Coffee Cup logo icon.   I ran the jar through a program called launch4j and was able to convert it to an exe file and give it a custom icon.  I compiled the program on both a Mac and a PC and was surprised to find out that it did not matter which platform it was compiled on, the resulting jar file would run on either platform.  My goal in writing this program was to learn a little more about music theory.  I think I have done that.  If someone else happens to use and benefit from this program that is just icing on the cake.

http://erikboisen.net/ScaleMapper.html

Leave a Reply

Your email address will not be published. Required fields are marked *