Просмотр исходного кода

cmMidi.h/c : Added cmSciPitchToMidiPitch().

master
kevin 8 лет назад
Родитель
Сommit
a4ed9ba98b
2 измененных файлов: 33 добавлений и 18 удалений
  1. 28
    18
      cmMidi.c
  2. 5
    0
      cmMidi.h

+ 28
- 18
cmMidi.c Просмотреть файл

@@ -210,18 +210,11 @@ const char*     cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labe
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 219
     case 'a': idx = 9;  break;
227 220
     case 'b': idx = 11; break;
@@ -234,6 +227,27 @@ cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr )
234 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 251
   ++cp;
238 252
 
239 253
   if( !(*cp) )
@@ -241,10 +255,10 @@ cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr )
241 255
 
242 256
   
243 257
   if((sharpFl = *cp=='#') == true )
244
-    ++idx;
258
+    acc = 1;
245 259
   else
246 260
     if((flatFl  = *cp=='b') == true )
247
-      --idx;
261
+      acc = -1;
248 262
 
249 263
   if( sharpFl || flatFl )
250 264
   {
@@ -259,11 +273,7 @@ cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr )
259 273
 
260 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 Просмотреть файл

@@ -153,6 +153,11 @@ extern "C" {
153 153
   // If 'pitch' is outside of the range 0-127 then a blank string is returned.
154 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 162
   // Scientific pitch string: [A-Ga-g][#b][#] where  # may be -1 to 9.
158 163
   // Return kInvalidMidiPitch if sciPtichStr does not contain a valid 

Загрузка…
Отмена
Сохранить