Midi number to note name / debuging

hello,
is there also native way how to translate midi number to its note representation/name - for debugging purposes.
like
print name(60) # would print C4

coz what i do now is bit overkill yet not precise
$name = [‘C’, ‘Db’, ‘D’, ‘Eb’, ‘E’, ‘F’, ‘Gb’, ‘G’, ‘Ab’, ‘A’, ‘Bb’, ‘B’].ring
print $name[value]

Hey @2046 :slight_smile:

if you’re just debugging, then see note_info in the ‘Lang’ help reference - it has the note name in its output:

puts note_info(60)

which prints out:
#<SonicPi::Note :C4>

That is hopefully enough for your needs? :slight_smile:
(You could write a little extra code on top of that and get just the midi note name itself, if necessary)

Following on from Ethan’s answer try this.

define :ntosym do |n|
  note_info(n).to_s.split(" ")[1][1..-2].to_sym
end

define :notestosyms do |list|
  list.map {|x| ntosym(x)}
end

puts notestosyms [48,68,64,53,89,43,36]

This prints out [:C3, :Ab4, :E4, :F3, :F6, :G2, :C2]

to understand how ntosym works try this:

puts note_info(60)
puts note_info(60).to_s
puts note_info(60).to_s.split(" ")
puts note_info(60).to_s.split(" ")[1]
puts note_info(60).to_s.split(" ")[1][1..-2]
puts note_info(60).to_s.split(" ")[1][1..-2].to_sym

which splits it up stage by stage by stage printing:

 ├─ "#<SonicPi::Note :C4>"
 ├─ ["#<SonicPi::Note", ":C4>"]
 ├─ ":C4>"
 ├─ "C4"
 ├─ :C4

The list.map function takes each element in turm, and applies in place the ntosym function to each element.

@robin.newman/@2046 - If you specifically wanted only the note name such as C4, instead of the full output of note_info, I was actually thinking of this one:

puts note_info(60).midi_string

which gives you just C4.
(Keeping in mind that using .midi_string on a Sonic Pi note object is not officially supported as part of the set of Sonic Pi user functions, it just happens to be (for the moment) one of the internal functions that Sonic Pi uses - so using it in your own code will only work so long as that doesn’t change).

2 Likes

Nice one! I got carried away a bit the way I did it!

such a storm of responses :slight_smile:
my favorite so far

puts note_info(60).midi_string

thanks a lot, I’m more than saturated