Browse Source

cmMidi.h/c : Added cmSciPitchToMidiPitch().

master
kevin 8 years ago
parent
commit
a4ed9ba98b
2 changed files with 33 additions and 18 deletions
  1. 28
    18
      cmMidi.c
  2. 5
    0
      cmMidi.h

+ 28
- 18
cmMidi.c View File

210
 }
210
 }
211
 
211
 
212
 
212
 
213
-cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr )
213
+cmMidiByte_t    cmSciPitchToMidiPitch( cmChar_t pitch, int acc, int octave )
214
 {
214
 {
215
-  const char* cp      = sciPitchStr;
216
-  bool        sharpFl = false;
217
-  bool        flatFl  = false;
218
-  int         octave;
219
-  int         idx     = -1;
220
-
221
-  if( sciPitchStr==NULL || strlen(sciPitchStr) > 5 )
222
-    return kInvalidMidiPitch;
223
-
224
-  switch(tolower(*cp))
215
+  int idx = -1;
216
+  
217
+  switch(tolower(pitch))
225
   {
218
   {
226
     case 'a': idx = 9;  break;
219
     case 'a': idx = 9;  break;
227
     case 'b': idx = 11; break;
220
     case 'b': idx = 11; break;
234
       return kInvalidMidiPitch;
227
       return kInvalidMidiPitch;
235
   }
228
   }
236
 
229
 
230
+  unsigned rv =  (octave*12) + idx + acc + 12;
231
+
232
+  if( rv <= 127 )
233
+    return rv;
234
+
235
+  return kInvalidMidiPitch;
236
+
237
+}
238
+
239
+cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr )
240
+{
241
+  const char* cp      = sciPitchStr;
242
+  bool        sharpFl = false;
243
+  bool        flatFl  = false;
244
+  int         octave;
245
+  int         acc     = 0;
246
+
247
+  if( sciPitchStr==NULL || strlen(sciPitchStr) > 5 )
248
+    return kInvalidMidiPitch;
249
+
250
+  // skip over leading letter
237
   ++cp;
251
   ++cp;
238
 
252
 
239
   if( !(*cp) )
253
   if( !(*cp) )
241
 
255
 
242
   
256
   
243
   if((sharpFl = *cp=='#') == true )
257
   if((sharpFl = *cp=='#') == true )
244
-    ++idx;
258
+    acc = 1;
245
   else
259
   else
246
     if((flatFl  = *cp=='b') == true )
260
     if((flatFl  = *cp=='b') == true )
247
-      --idx;
261
+      acc = -1;
248
 
262
 
249
   if( sharpFl || flatFl )
263
   if( sharpFl || flatFl )
250
   {
264
   {
259
 
273
 
260
   octave = atoi(cp);
274
   octave = atoi(cp);
261
 
275
 
262
-  unsigned rv =  (octave*12) + idx + 12;
263
-  
264
-  if( rv <= 127 )
265
-    return rv;
266
 
276
 
267
-  return kInvalidMidiPitch;
277
+  return cmSciPitchToMidiPitch( *sciPitchStr, acc, octave );
268
   
278
   
269
 }
279
 }

+ 5
- 0
cmMidi.h View File

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

Loading…
Cancel
Save