Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate MIDI note names/numbers in YAML format
# generate MIDI note names/numbers in YAML format
octaves = (0..10)
range = (0..127)
naturals = [
{ :name => "C", :val => 0 },
{ :name => "D", :val => 2 },
{ :name => "E", :val => 4 },
{ :name => "F", :val => 5 },
{ :name => "G", :val => 7 },
{ :name => "A", :val => 9 },
{ :name => "B", :val => 11 }
]
notes = []
# add accidentals and enharmonic equivalents
naturals.each do |note|
notes.push(note)
ind = notes.index(note)
notes.insert(ind, { :name => "#{note[:name]}b", :val => note[:val]-1 })
notes.insert(ind+2, { :name => "#{note[:name]}#", :val => note[:val]+1 })
end
# create full list
list = []
octaves.each do |oct|
notes.each do |note|
note_name = note[:name] + ((oct - 1).to_s)
note_num = (note[:val] + (oct * 12))
list << { :name => note_name, :val => note_num } if range.include?(note_num)
end
end
# sort putting unusual enharmonic equivalents last
list.sort! do |a,b|
c = (a[:val] <=> b[:val])
aa = a[:name].include?("b") || a[:name].include?("#") ? 1 : 0
ba = b[:name].include?("b") || b[:name].include?("#") ? 1 : 0
c.zero? ? aa <=> ba : c
end
# output
puts("Notes:\n")
list.each do |note|
puts(" #{note[:name]}: #{note[:val].to_s}\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment