Page 1 of 1

Human Pitch to Midi Pitch (uncommenting a comment)

PostPosted: Mon Aug 06, 2012 12:47 pm
by DelphiGuy
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?

Re: Human Pitch to Midi Pitch (uncommenting a comment)

PostPosted: Mon Aug 06, 2012 1:39 pm
by nerdzero
You mean use the '#' character in a variable or constant name? I don't think it's possible.

Re: Human Pitch to Midi Pitch (uncommenting a comment)

PostPosted: Wed Aug 08, 2012 11:52 am
by Charles
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:
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.