10
1
Background
In Western music, every single music note has an assigned name. Within each octave, there are twelve unique notes in the following order: "C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B C", where the final C is one octave above the first.
To tell the difference between notes of different octaves, a number (for this challenge restricted to a single digit) is appended to the end of the note name. Thus, C5 is the note that is one octave above C4. Bb6 is above B5.
An important fact is that B5 and C6 are notes that are right next to each other, and that C0 and B9 are the lowest and highest notes.
Between any two notes, there is a distance which is the number of semitones between them. Bb4 is one semitone below B4, which is itself one semitone below C5. There are twelve semitones in an octave, so Bb4 is a distance of 12 from A#3 since it is an octave above it (notice how a single note can have up to two names).
The Challenge
Your challenge is to write the shortest possible program that can take a list of music notes from STDIN and print the list of interval changes to STDOUT.
Input will be a space-separated list of music notes. Each note will consist of an uppercase letter A-G, an optional b or # sign, and a single digit number. You will not have to deal with E#/Fb or B#/Cb. Example input:
C4 D4 E4 F4 G4 A4 B4 C5 C4
Output will be a space-separated list of integers which represent the distance between each successive note, always prefixed with an + or - to show whether the note was ascending or descending relative to the one before it. There will always be one less number outputted than notes inputted. Example output for the above input:
+2 +2 +1 +2 +2 +2 +1 -12
Some more example inputs:
E5 D#5 E5 B4 E5 F#5 E5 B4
C0 B0 Bb1 A2 G#3 G4 F#5 F6
G4 Ab4 Gb4 A4 F4 A#4
And their corresponding outputs:
-1 +1 -5 +5 +2 -2 -5
+11 +11 +11 +11 +11 +11 +11
+1 -2 +3 -4 +5
Rules and Restrictions
The winner is determined by the number of characters in the source code
Your program should consist of only printable ASCII characters
You are not allowed to use any sort of built-in function that is related to music or sound
Other than that, standard code golf rules apply
Should it print
+0
or-0
or0
for two identical notes? – Howard – 2012-04-07T08:53:16.387@Howard Since I didn't specify, either one is acceptable. – PhiNotPi – 2012-04-07T11:47:44.267
1"Bb4 is one semitone below B4, which is itself one semitone below C4". You mean C5 at the end of that, right? – Keith Randall – 2012-04-08T02:36:01.840
Wow, never noticed that. Thanks for spotting the error. It's fixed now. – PhiNotPi – 2012-04-08T02:43:58.927