5
2
Your mission is to create a function for transposing music chords.
Copied from SO with permission of the original asker, I just really wanted to see what you guys could do with this one:
Since I don't expect everyone to be a musician here, I'll try to explain how it works in music theory. I hope I don't forget something. If yes, musicians, please, correct me.
1) The simple chords
The simple chords are almost as simple as an alphabet and it goes like this:
C, C#, D, D#, E, F, F#, G, G#, A, A# B
From B it loops all over again to C. Therefore, If the original chord is E
and we want to transpose +1, the resulting chord is F
. If we transpose +4, the resulting chord is G#
.
2) Expanded chords
They work almost like the simple chords, but contain a few more characters, which can safely be ignored when transposing. For example:
Cmi, C#7, Dsus7, Emi, Fsus4, F#mi, G ...
So again, as with the simple chords, if we transpose Dsus7
+ 3 = Fsus7
3) Non-root bass tone
A problem arises when the bass plays a different tone than the chord root tone. This is marked by a slash after the chord and also needs to be transposed. Examples:
C/G, Dmi/A, F#sus7/A#
As with examples 1 and 2, everything is the same, but the part after the slash needs transpose too, therefore:
C/G
+ 5 = F/C
F#sus7/A#
+ 1 = Gsus7/B
I think this should be all, unless I forgot something.
So basically, imagine you have a javascript variable called chord
and the transpose value transpose
. What code would transpose the chord?
Example:
var chord = 'F#sus7/C#';
var transpose = 3; // remember this value also may be negative, like "-4"
... code here ...
var result; // expected result = 'Asus7/E';
original question here: https://stackoverflow.com/questions/7936843/how-do-i-transpose-music-chords-using-javascript (note that he's asking for JS, for this challenge I don't care what language its in)
I think flats and stuff like E# etc isn't really useful when simply "sharing" chords with friends and playing music this way. Of course they are very important in sheet music etc, but for people transposing music to play for their own pleasure, this seems like a very satisfying solution. +1 – Richard Rodriguez – 2011-10-29T04:46:33.403
@RIMMER - Even with a simple chord chart I find it much harder to follow when the author has ignored flats. Do you never play in the key of F (which has a Bb, not an A#)? I'd like to see a full solution (using my function or not) that transposes a whole chord chart and not just one chord. Thanks for the up-vote. – nnnnnn – 2011-10-29T04:56:30.303
Oh OK, I see what you mean now. Well, I play the piano and I know the scales perfectly, so even if the chords say A# and I'm in the F scale, I always know that I can play A in that scale and that the A# notation is actually Bb. Anyway, maybe you can re-define the problem to consider all this in the equation? I don't know how to explain the whole theory to reach the full solution, but I'd be happy to see the code! – Richard Rodriguez – 2011-10-29T05:07:58.043
A full solution is beyond me (and would probably be incomprehensible in a code-golf setting). I agree that yes, if I see an A# chord in the key of F I know that it should be a Bb, but it's a distraction so I'd rather correct the chart before I play from it. – nnnnnn – 2011-10-29T05:34:53.610
If I write down all the chords and exactly specify which scale has what sharps and what flats and would offer you a money reward, how much would it cost to code the full solution? (this is rather a reply for your stackoverflow comment, but I didn't want to put it there). anyway, my mail is rimmer@rimmer.sk ... – Richard Rodriguez – 2011-10-29T05:37:17.007
I'm not sure how long it would take to write a solution that would satisfy me, certainly much longer than it would take me to just transpose manually whenever I happen to need it. I'd suggest extending the above function to accept another parameter for whether to use sharps or flats for the output key, and to look for sharps and flats in the input, and you'd probably be most of the way there for most normal cases - probably wouldn't take too long to get that far. Then just manually double-check the output for edge cases. – nnnnnn – 2011-10-29T10:01:42.927
P.S. Google found a few websites that offer transposition on a par with what I coded above, some of which also show guitar chord fingerboard diagrams for the appropriate key, e.g.: http://www.transposechords.com/ and http://www.chordie.com.
– nnnnnn – 2011-10-29T10:04:07.337I've got 186 as the count for this solution, please let me know if I got it wrong. – kekekela – 2011-10-31T00:53:17.133