From a4ed9ba98b0c287fa28d7b1a17913a14bb4b46b7 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 14 Jul 2016 19:07:20 -0400 Subject: [PATCH] cmMidi.h/c : Added cmSciPitchToMidiPitch(). --- cmMidi.c | 46 ++++++++++++++++++++++++++++------------------ cmMidi.h | 5 +++++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/cmMidi.c b/cmMidi.c index 6a375c2..830fb38 100644 --- a/cmMidi.c +++ b/cmMidi.c @@ -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; - bool sharpFl = false; - bool flatFl = false; - int octave; - int idx = -1; - - if( sciPitchStr==NULL || strlen(sciPitchStr) > 5 ) - return kInvalidMidiPitch; - - switch(tolower(*cp)) + int idx = -1; + + switch(tolower(pitch)) { case 'a': idx = 9; break; case 'b': idx = 11; break; @@ -234,6 +227,27 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr ) 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; if( !(*cp) ) @@ -241,10 +255,10 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr ) if((sharpFl = *cp=='#') == true ) - ++idx; + acc = 1; else if((flatFl = *cp=='b') == true ) - --idx; + acc = -1; if( sharpFl || flatFl ) { @@ -259,11 +273,7 @@ cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr ) octave = atoi(cp); - unsigned rv = (octave*12) + idx + 12; - - if( rv <= 127 ) - return rv; - return kInvalidMidiPitch; + return cmSciPitchToMidiPitch( *sciPitchStr, acc, octave ); } diff --git a/cmMidi.h b/cmMidi.h index b6f4859..653941e 100644 --- a/cmMidi.h +++ b/cmMidi.h @@ -153,6 +153,11 @@ extern "C" { // 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 ); + // 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. // Return kInvalidMidiPitch if sciPtichStr does not contain a valid