cmMidi.h/c : Added cmSciPitchToMidiPitch().

This commit is contained in:
kevin 2016-07-14 19:07:20 -04:00
parent 7be4937965
commit a4ed9ba98b
2 changed files with 33 additions and 18 deletions

View File

@ -210,18 +210,11 @@ const char* cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labe
} }
cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr ) cmMidiByte_t cmSciPitchToMidiPitch( cmChar_t pitch, int acc, int octave )
{ {
const char* cp = sciPitchStr; int idx = -1;
bool sharpFl = false;
bool flatFl = false;
int octave;
int idx = -1;
if( sciPitchStr==NULL || strlen(sciPitchStr) > 5 ) switch(tolower(pitch))
return kInvalidMidiPitch;
switch(tolower(*cp))
{ {
case 'a': idx = 9; break; case 'a': idx = 9; break;
case 'b': idx = 11; break; case 'b': idx = 11; break;
@ -234,6 +227,27 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr )
return kInvalidMidiPitch; return kInvalidMidiPitch;
} }
unsigned rv = (octave*12) + idx + acc + 12;
if( rv <= 127 )
return rv;
return kInvalidMidiPitch;
}
cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr )
{
const char* cp = sciPitchStr;
bool sharpFl = false;
bool flatFl = false;
int octave;
int acc = 0;
if( sciPitchStr==NULL || strlen(sciPitchStr) > 5 )
return kInvalidMidiPitch;
// skip over leading letter
++cp; ++cp;
if( !(*cp) ) if( !(*cp) )
@ -241,10 +255,10 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr )
if((sharpFl = *cp=='#') == true ) if((sharpFl = *cp=='#') == true )
++idx; acc = 1;
else else
if((flatFl = *cp=='b') == true ) if((flatFl = *cp=='b') == true )
--idx; acc = -1;
if( sharpFl || flatFl ) if( sharpFl || flatFl )
{ {
@ -259,11 +273,7 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr )
octave = atoi(cp); octave = atoi(cp);
unsigned rv = (octave*12) + idx + 12;
if( rv <= 127 ) return cmSciPitchToMidiPitch( *sciPitchStr, acc, octave );
return rv;
return kInvalidMidiPitch;
} }

View File

@ -153,6 +153,11 @@ extern "C" {
// If 'pitch' is outside of the range 0-127 then a blank string is returned. // If 'pitch' is outside of the range 0-127 then a blank string is returned.
const char* cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt ); const char* cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt );
// Convert a scientific pitch to MIDI pitch. acc == 1 == sharp, acc == -1 == flat.
// The pitch character must be in the range 'A' to 'G'. Upper or lower case is valid.
// Return kInvalidMidiPitch if the arguments are not valid.
cmMidiByte_t cmSciPitchToMidiPitch( cmChar_t pitch, int acc, int octave );
// Scientific pitch string: [A-Ga-g][#b][#] where # may be -1 to 9. // Scientific pitch string: [A-Ga-g][#b][#] where # may be -1 to 9.
// Return kInvalidMidiPitch if sciPtichStr does not contain a valid // Return kInvalidMidiPitch if sciPtichStr does not contain a valid