|
-
Music notes - Octave - Code Explanation
Hi;
I have the following code for calculate music notes and an Octave; however, i don't understand it. I appretiated if someone could explain me.
Code:
public class Notes {
public static void main (String [] args) {
String notes = "C C#D D#E F F#G G#A A#B ";
int octave;
String note;
for (int noteNum = 0; noteNum < 128; noteNum++) {
octave = noteNum / 12 - 1;
note = notes.substring(
(noteNum % 12) * 2,
(noteNum % 12) * 2 + 2);
System.out.println (
"Note number " + noteNum +
" is octave " + octave +
" and note " + note);
}
}
}
-
Heya,
1. Is this homework?
2. Are you having difficulty with the code itself or with the music theory?
If you're stumbling on the theory, you're in luck. David Buck recently put up a blog post that wound up making the rounds on music theory for programmers. It's a really good read. Check out http://simberon.blogspot.nl/2013/04/...ic-part-1.html
Last edited by Vindir; 10-11-2013 at 02:17 AM.
Insert ancient Sharky sig here
[
Prince Vindir of the OC Crusaders
Holding Boundaries and Breaking Barriers
]
-
 Originally Posted by Vindir
Heya,
1. Is this homework?
2. Are you having difficulty with the code itself or with the music theory?
If you're stumbling on the theory, you're in luck. David Buck recently put up a blog post that wound up making the rounds on music theory for programmers. It's a really good read. Check out http://simberon.blogspot.nl/2013/04/...ic-part-1.html
Sorry for the late response.
Actually a understand the music theory (I think) mi problem is with the coding:
I have this mathematical operation ((Oct+1) *12 ) + note) that gets the MIDI values where the octave is from -1 to 9 and the note is 1-12 ( if I remember correctly) however I am not sure how to asing this values for the code to recognize it. For example:
for C# the value of the note will be 1 for the octave 2 so the MIDI value is: 37
-
Alright, we can break it down a little. Not sure exactly which piece you're curious about so I'll go through it all since it is short, ignore the Java hate and anything oversimplified at will.
Code:
public class Notes {
public static void main (String [] args) {
This is a nonsense boilerplate method signature we have to write out as punishment for choosing Java.
Code:
int octave;
String note;
Declaring our types for variables because Java hates everyone.
Code:
for (int noteNum = 0; noteNum < 128; noteNum++) {
Just a quick loop from 0 to 127 that assigns noteNum with the current value to be processed. This sets noteNum to each of the 128 MIDI notes and executes everything below(within) the for loop against each value.
Code:
octave = noteNum / 12 - 1;
Java rounds down when doing integer division so assigning the octave takes advantage of this by just dividing the current MIDI value by 12, which rounds down to the nearest digit, then subtracting 1, ie. MIDI 11 becomes octave -1 and MIDI 12 becomes octave 1. Rinse and repeat assigning octave for each MIDI value.
Code:
String notes = "C C#D D#E F F#G G#A A#B ";
This sets up the string "note" with all of the notes ascending in order. It is defined as a simple way to figure out the note for each MIDI value using the substring method as below....
Code:
note = notes.substring(
(noteNum % 12) * 2,
(noteNum % 12) * 2 + 2);
This uses the notes string defined above and calls the substring method against it using a quick modulo trick to define the indices.
Substring in Java will return a portion of a string using a lower and upper index as the two arguments with the upper bound being non-inclusive.
The noteNum modulo against 12 gives you a value of 0 to 11 depending on noteNum's value and multiplies it by 2 for the lower index and multiplies it by 2 then adds 2 for the upper index. Your final indices become every even value from 0 to 22 for the lower bound and 2 to 24 for the upper bound. All this achieves is that you end up returning 2 successive characters from the string as the for loop steps through each MIDI value walking from note to note and looping right back around every 12 iterations.
For example, the above gives you "C " for MIDI 0, "C#" for MIDI 1, "D" for MIDI 2, etcetera and each iteration assigns the result to the note variable.
Code:
System.out.println (
"Note number " + noteNum +
" is octave " + octave +
" and note " + note);
}
}
}
The rest just prints out the MIDI note index, the octave, and the note that we defined above for each iteration.
...and voila, you have an app that prints out the MIDI value, octave, and note for all of the MIDI values.
Insert ancient Sharky sig here
[
Prince Vindir of the OC Crusaders
Holding Boundaries and Breaking Barriers
]
-
 Originally Posted by shinju
Sorry for the late response.
Actually a understand the music theory (I think) mi problem is with the coding:
I have this mathematical operation ((Oct+1) *12 ) + note) that gets the MIDI values where the octave is from -1 to 9 and the note is 1-12 ( if I remember correctly) however I am not sure how to asing this values for the code to recognize it. For example:
for C# the value of the note will be 1 for the octave 2 so the MIDI value is: 37
I think you may be referring to a piece of the code you're looking at that you haven't posted. C# in octave 2 has a MIDI value of 25 typically. I'm guessing that whatever you're looking at has assigned the octaves to an array and you're seeing 2 as the octave index so it's actually octave 3 in most charts I hit on a quick google.
Somewhere near that chunk of code you're looking at you should find another array or hash or whatever that ties each of the notes to a value with C# being 1 and each note after it incrementing by 1 so D is 2, D# is 3, and so on. I'm pretty sure you'll find the range for notes is 0-11 as well since that's the only way the formula will work for C on octave 0
It's just an quick and easy way to figure out your midi value given an octave and note.
This also tells us that the original code is probably giving an off-by-one error when printing out the octave. It would be make more sense and be slightly more readable to label them 0 to 10 and it fits better with the MIDI spec since apparently MIDI 60 is defined as middle C and everything else is relative, but it seems to be arbitrary.
Last edited by Vindir; 10-17-2013 at 11:24 PM.
Insert ancient Sharky sig here
[
Prince Vindir of the OC Crusaders
Holding Boundaries and Breaking Barriers
]
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|