Created
October 20, 2014 16:54
-
-
Save jeriko/3498e4ef8fee3a55c956 to your computer and use it in GitHub Desktop.
e-chords fixer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#header').remove() | |
function transpose_page(semitone_shift) { | |
$('u > a').each(function() { | |
old_chord = $(this).attr('rel'); | |
new_chord = transpose_chord(old_chord, semitone_shift); | |
$(this).html(new_chord); | |
}); | |
function transpose_chord(chord, semitone_shift) { | |
function note_from_chord(chord) { | |
matches = chord.match(/^[A-G|a-g][#|b]?/); | |
return !!matches ? matches[0] : null | |
} | |
old_note = note_from_chord(chord); | |
new_note = transpose_note(old_note, semitone_shift); | |
return chord.replace(old_note, new_note); // TODO this only replaces first occurrence? | |
} | |
function transpose_note(note, semitone_shift) { | |
function chromatic_scale() { return chromatic_scale_sharps(); } | |
function chromatic_scale_sharps() { return ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']; } | |
function chromatic_scale_flats() { return ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']; } | |
function note_sharp_to_corresponding_flat(note) { return chromatic_scale_flats()[chromatic_scale_sharps().indexOf(note)] } | |
function note_flat_to_corresponding_sharp(note) { return chromatic_scale_sharps()[chromatic_scale_flats().indexOf(note)] } | |
function note_is_sharp(note) { return !!note.match(/#$/); } | |
function note_is_flat(note) { return !!note.match(/b$/); } | |
function note_to_semitone(note) { | |
scale = note_is_sharp(note) ? chromatic_scale_sharps() : chromatic_scale_flats(); | |
return scale.indexOf(note); | |
} | |
function semitone_to_note(semitone, inflector) { | |
if(inflector == undefined) { inflector = '#' } | |
scale = (inflector == '#') ? chromatic_scale_sharps() : chromatic_scale_flats(); | |
return scale[normalize(semitone)]; | |
} | |
function normalize(semitone) { | |
if(semitone < 0) { | |
correction = Math.ceil(Math.abs(semitone/12)) * 12 | |
return semitone + correction; | |
} else { | |
return semitone % 12; | |
} | |
} | |
inflector = note_is_sharp(note) ? '#' : 'b' | |
return semitone_to_note(note_to_semitone(note) + semitone_shift, inflector); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment