I need to be able to use musical pitch names to represent midi pitch numbers.
Here's a couple of typical examples:
Middle C is typically referred to in shorthand as "C4" (C from the 4th octave on a piano keyboard) and the usual corresponding MIDI pitch representation is the integer 60.
Similarly, human "C#4" is the same-sounding pitch as Midi "61".
In my code, it would be quite convenient to use the shorthand "C#4" interchangeably with the integer 61. Without creating a table of strings and an obligatory function to convert back and forth, is there a simple way to overcome "#" being interpreted as a Cobra comment?
No doubt I'm overthinking this trivial matter and should just have human "C#4" equal Midi "61" equal Cobra "Cs4", which would easily permit constants or enums to be set up. But is there a better way that allows use of the # symbol?
Forums
Human Pitch to Midi Pitch (uncommenting a comment)
3 posts
• Page 1 of 1
Re: Human Pitch to Midi Pitch (uncommenting a comment)
You mean use the '#' character in a variable or constant name? I don't think it's possible.
- nerdzero
- Posts: 286
- Location: Chicago, IL
Re: Human Pitch to Midi Pitch (uncommenting a comment)
The # symbol is used for single line comments and cannot be used in identifiers. So you need a different approach like you mentioned (Cs4) or you need to use strings like:
But when using strings, you won't get any compile-time error checking on the name. On the other hand, you could provide conveniences like making the name insensitive to case and spaces, or deriving the midi pitch with a computation against the string contents if there is such a formula.
Whichever works best for you.
trace pitches['C#4']
But when using strings, you won't get any compile-time error checking on the name. On the other hand, you could provide conveniences like making the name insensitive to case and spaces, or deriving the midi pitch with a computation against the string contents if there is such a formula.
Whichever works best for you.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 11 guests