19
1
The year is 930, and the Gregorian Church is having a problem. They have thousands of pages of chant music, but the problem is that all the sheet music was simply thrown onto a pile instead of having any real organisation system:
Image by user gamerprinter at Cartographers' Guild.
The Church needs to organise all the sheet music, so they have hired a medieval software engineer to write a program to organise it for them. You are the software engineer that has been hired. However, the compilation process in medieval times involves the program being written onto paper by a team of slow biblical scribes. To decrease the time it takes for the team of scribes to compile your code, you must make the program as small as possible.
The Church wants the chant music to be organised based off the musical scale they are written in. All of the Church's chant music is written in Dorian scales. Given the notes of a certain piece of music, your program will output the Dorian scale that it is in. Here, I will explain exactly what a Dorian scale is. If you already know, you may skip this section.
There are 12 possible notes in any melody. Here they are in order:
C C# D D# E F F# G G# A A# B
A semitone (represented using a S
) is incrementing one step to the right, wrapping around (so a semitone up from B would be back to C). A tone (represented using a T
) is two semitones. For instance, a semitone up from F# would be G. A tone up from F# would be G#.
To create a Dorian scale, we start from any note in the list, and then move up in the following pattern, listing the notes that we encounter:
T, S, T, T, T, S
An example. I start from A. The notes of my Dorian scale becomes:
A
B (up a tone)
C (up a semitone)
D (up a tone)
E (up a tone)
F# (up a tone)
G (up a semitone)
The scale has the notes A, B, C, D, E, F#, and G. Because I started from A, we will call this the Dorian scale in A. There are therefore 12 different Dorian scales, each of which are named after the note that they started from. Each of them use the same pattern of tones and semitones, just starting from a different position. If my explanation is not coherent you may also consult Wikipedia.
The input of the program can be given from whatever is appropriate for your program (e.g. STDIN, command line argument, raw_input()
). It may be not pre-initialised in a variable. The input will be a list of comma seperated notes, representing the melody of the piece. There may be repeated notes. There will always be enough different notes in the input to be able to decisively deduce the scale of the piece. An example input:
B,B,D,E,D,B,A,G#,A,G#,E,D,F#,E,F#,E,F#,G#,A
The output of the program should be the string Dorian scale in X
, where X is the starting note of the scale. The output of the example input:
Dorian scale in B
Comparing this with the Dorian scale in B (B C# D E F# G# A
) we see that all the notes of the melody are within this scale. The note C# is unused in this case. However there are sufficient notes to unambiguously identify B Dorian as the correct key. No other Dorian scale fits, because whatever other scale we try, there is always at least one note of the melody that does not belong to the scale.
This is code golf, so the entry with the shortest number of characters wins. Ask in the comments if you have questions.
So, what we should do is to only interpret the first tone/semitone? – avall – 2014-08-20T06:58:59.280
@avall I'm sorry, I don't understand your question. The input won't always start with the tonic, if that's what you're asking. – absinthe – 2014-08-20T07:02:22.110
Please provide us more examples. Especially those, which don't start with the tonic. – avall – 2014-08-20T07:03:21.163
@avall
C#,D#,A,B,A,F#,E
==>Dorian scale in F#
– absinthe – 2014-08-20T07:06:23.640@avall
F,A,C,B,E
==>Dorian scale in D
– absinthe – 2014-08-20T07:07:42.703I still don't understand how the sequence of notes could even hint at the scale.
F, A
are two tones apart.B, E
are 2.5 tones apart in one direction, 3.5 in the other one. Should we assume the tone never changes by more than 5 semitones, spans exactly 12 semitones, and output the lowest of them? – John Dvorak – 2014-08-20T07:23:57.633@JanDvorak We know that there are 12 dorian scales, the notes of which may be obtained using the method described in the question. The only Dorian scale that contains the notes
F, A, C, B, E
within it is the one that starts from D. I have a feeling I wasn't clear at all in my explanation. – absinthe – 2014-08-20T07:26:18.4671
The inverse problem is Scale from key and mode
– Peter Taylor – 2014-08-20T07:35:13.903I see nothing unclear about this question. For interest, the min number of notes needed to define a scale unambiguously is 3. For D Dorian B,F and either C or E suffice. On the other hand it's possible to give 6 notes and still be ambiguous: C,G,D,A,E,B could be either D or A dorian, depending if the F is natural or sharp. @VonIlya, what's the flaw you refer to? I notice if you name all scales as sharps you get into some horrible double-sharp situations, but non-musicians will just use the corresponding natural and not notice a problem. I'm willing to vote to reopen and help if you explain. – Level River St – 2014-08-20T19:57:15.400
Sounds like a problem with your generator, not the question. Both examples in your comments have a tritone in them (
– Level River St – 2014-08-20T21:02:01.390B-F
andA-D#
) and a semitone in them, either of which on its own is sufficient to narrow it down to 2 possibilities. Some of these diagrams would really help: http://en.wikipedia.org/wiki/Pitch_constellation. If you delete your own negative comments I'll open vote and possibly edit. Musically it's better to say Bb dorian than A# dorian (the latter contains E# B# and F## if written correctly) but it's best to stick with the sharps now. I guessB C C#
is undefined behaviourI suppose so. I've deleted the comments now. – absinthe – 2014-08-20T21:02:50.567
Reopen vote cast. It's too late here for me to help you with an edit, but I may do so tomorrow. – Level River St – 2014-08-20T21:05:22.990
@steveverrill, I think the flaw is that this is modern Dorian, not mediaeval. – Peter Taylor – 2014-08-20T22:23:53.113
@Peter Taylor Hmm, that's true as well. Although I already threw historical accuracy out the window with the medieval software engineering part... – absinthe – 2014-08-20T22:24:46.847
Can I use space separated list of notes as input? – Ray – 2014-08-21T14:35:25.213
@Ray Yes. aaaaaaa – absinthe – 2014-08-21T20:33:07.590
By awarding a win so quickly, you discourage others from posting. You also commit to an answer that may well not be the best (in this case, the shortest). – DavidC – 2014-09-01T11:37:29.963
1
@David As per this meta question, I awarded the accept to the shortest answer after a waiting period of 12 days since I started the challenge. It just happened the CJam answer was posted right when I was going to accept the next shortest one.
– absinthe – 2014-09-01T11:39:23.497@David Also, I considered not giving accepts for any of my questions, but I would think that would also discourage people from posting ("have you heard of that Ilya person that doesn't give +15 accepts out...?") Waiting for 12 days to let posts come in before accepting seemed like the best solution. – absinthe – 2014-09-01T11:42:54.697