0
|
1 /*
|
|
2 * midiutil.c - Auxiliary MIDI functionality. Requires Steevs MIDI Library
|
|
3 * (midiinfo.h) for enumerations used in name mapping.
|
|
4 * Version 1.4
|
|
5 *
|
|
6 * AUTHOR: Steven Goodwin (StevenGoodwin@gmail.com)
|
|
7 * Copyright 2010, Steven Goodwin.
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or
|
|
10 * modify it under the terms of the GNU General Public License as
|
|
11 * published by the Free Software Foundation; either version 2 of
|
|
12 * the License,or (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22 */
|
|
23
|
|
24 #include <stdio.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <string.h>
|
|
27 #include <ctype.h>
|
|
28 #include "midifile.h"
|
|
29 #include "midiutil.h"
|
|
30
|
|
31 /*
|
|
32 ** Data Tables
|
|
33 */
|
|
34 static const char *szPatchList[128] = {
|
|
35 /*Pianos*/
|
|
36 "Acoustic Grand Piano",
|
|
37 "Bright Acoustic Piano",
|
|
38 "Electric Grand Piano",
|
|
39 "Honky-tonk Piano",
|
|
40 "Electric Piano 1",
|
|
41 "Electric Piano 2",
|
|
42 "Harpsichord",
|
|
43 "Clavinet",
|
|
44 /*Chromatic Percussion*/
|
|
45 "Celesta",
|
|
46 "Glockenspiel",
|
|
47 "Music Box",
|
|
48 "Vibraphone",
|
|
49 "Marimba",
|
|
50 "Xylophone",
|
|
51 "Tubular Bells",
|
|
52 "Dulcimer",
|
|
53 /*Organs*/
|
|
54 "Draw Organ",
|
|
55 "Percussive Organ",
|
|
56 "Rock Organ",
|
|
57 "Church Organ",
|
|
58 "Reed Organ",
|
|
59 "Accordian",
|
|
60 "Harmonica",
|
|
61 "Tango Accordian",
|
|
62 /*Guitars*/
|
|
63 "Acoustic Guitar (nylon)",
|
|
64 "Acoustic Guitar (steel)",
|
|
65 "Electric Guitar (jazz)",
|
|
66 "Electric Guitar (clean)",
|
|
67 "Electric Guitar (muted)",
|
|
68 "Overdriven Guitar",
|
|
69 "Distortion Guitar",
|
|
70 "Guitar harmonics",
|
|
71 /*Basses*/
|
|
72 "Acoustic bass",
|
|
73 "Electric Bass (finger)",
|
|
74 "Electric Bass (picked)",
|
|
75 "Fretless Bass",
|
|
76 "Slap Bass 1",
|
|
77 "Slap Bass 2",
|
|
78 "Synth bass 1",
|
|
79 "Synth bass 2",
|
|
80 /*Strings*/
|
|
81 "Violin",
|
|
82 "Viola",
|
|
83 "Cello",
|
|
84 "Contrabass",
|
|
85 "Tremolo strings",
|
|
86 "Pizzicato strings",
|
|
87 "Orchestral harp",
|
|
88 "Timpani",
|
|
89 /*Ensembles*/
|
|
90 "String ensemble 1",
|
|
91 "String ensemble 2",
|
|
92 "Synth strings 1",
|
|
93 "Synth strings 2",
|
|
94 "Choir Ahhs",
|
|
95 "Voice oohs",
|
|
96 "Synth voice",
|
|
97 "Orchestra hit",
|
|
98 /*Brass*/
|
|
99 "Trumpet",
|
|
100 "Trombone",
|
|
101 "Tuba",
|
|
102 "Muted trumpet",
|
|
103 "French horn",
|
|
104 "Brass section",
|
|
105 "Synth brass 1",
|
|
106 "Synth brass 2",
|
|
107 /*Reeds*/
|
|
108 "Soprano sax",
|
|
109 "Alto sax",
|
|
110 "Tenor sax",
|
|
111 "Baritone sax",
|
|
112 "Oboe",
|
|
113 "English horn",
|
|
114 "Bassoon",
|
|
115 "Clarinet",
|
|
116 /*Pipes*/
|
|
117 "Picclo",
|
|
118 "Flute",
|
|
119 "Recorder",
|
|
120 "Pan Flute",
|
|
121 "Bottle Blow",
|
|
122 "Shakuhachi",
|
|
123 "Whistle",
|
|
124 "Ocarina",
|
|
125 /*Synth Lead*/
|
|
126 "Lead 5 (Square)",
|
|
127 "Lead 5 (Sawtooth)",
|
|
128 "Lead 5 (Calliope)",
|
|
129 "Lead 5 (Chiff)",
|
|
130 "Lead 5 (Charang)",
|
|
131 "Lead 5 (Voice)",
|
|
132 "Lead 5 (Fifths)",
|
|
133 "Lead 5 (Bass+lead)",
|
|
134 /*Synth Pads*/
|
|
135 "Pad 1 (New age)",
|
|
136 "Pad 2 (Warm)",
|
|
137 "Pad 3 (Polysynth)",
|
|
138 "Pad 4 (Choir)",
|
|
139 "Pad 5 (Bowed)",
|
|
140 "Pad 6 (Metallic)",
|
|
141 "Pad 7 (Halo)",
|
|
142 "Pad 8 (Sweep)",
|
|
143 /*Synth FX*/
|
|
144 "FX 1 (Rain)",
|
|
145 "FX 2 (Soundtrack)",
|
|
146 "FX 3 (Crystal)",
|
|
147 "FX 4 (Atmosphere)",
|
|
148 "FX 5 (Brightness)",
|
|
149 "FX 6 (Goblins)",
|
|
150 "FX 7 (Echoes)",
|
|
151 "FX 8 (Sci-fi)",
|
|
152 /*Ethnic*/
|
|
153 "Sitar",
|
|
154 "Banjo",
|
|
155 "Shamisen",
|
|
156 "Koto",
|
|
157 "Kalimba",
|
|
158 "Bagpipe",
|
|
159 "Fiddle",
|
|
160 "Shanai",
|
|
161 /*Percussive*/
|
|
162 "Tinkle bell",
|
|
163 "Agogo",
|
|
164 "Steel drums",
|
|
165 "Woodblock",
|
|
166 "Taiko drum",
|
|
167 "Melodic tom",
|
|
168 "Synth drum",
|
|
169 "Reverse cymbal",
|
|
170 /*FX*/
|
|
171 "Guitar fret noise",
|
|
172 "Breath noise",
|
|
173 "Seashore",
|
|
174 "Bird tweet",
|
|
175 "Telephone ring",
|
|
176 "Helicopter",
|
|
177 "Applause",
|
|
178 "Gunshot",
|
|
179 };
|
|
180
|
|
181 static const char *szGMDrums[128]={
|
|
182 "???", /* C0 */
|
|
183 "???",
|
|
184 "???",
|
|
185 "???",
|
|
186 "???",
|
|
187 "???",
|
|
188 "???",
|
|
189 "???",
|
|
190 "???",
|
|
191 "???",
|
|
192 "???",
|
|
193 "???",
|
|
194 "???", /* C1 */
|
|
195 "???",
|
|
196 "???",
|
|
197 "???",
|
|
198 "???",
|
|
199 "???",
|
|
200 "???",
|
|
201 "???",
|
|
202 "???",
|
|
203 "???",
|
|
204 "???",
|
|
205 "???",
|
|
206 "???", /* C2 */
|
|
207 "???",
|
|
208 "???",
|
|
209 "High Q",
|
|
210 "Slap",
|
|
211 "???",
|
|
212 "???",
|
|
213 "Sticks",
|
|
214 "Square click",
|
|
215 "???",
|
|
216 "???",
|
|
217 "Acoustic Kick Drum",
|
|
218 "Electric Kick Drum", /* C3=36 */
|
|
219 "Side Stick",
|
|
220 "Acoustic Snare Drum",
|
|
221 "Hand Clap",
|
|
222 "Electric Snare Drum ", /*(crisp, electronic)", */
|
|
223 "Low Floor Tom",
|
|
224 "Closed Hi Hat",
|
|
225 "High Floor Tom",
|
|
226 "Opening Hi Hat", /* pedal hh */
|
|
227 "Low Tom",
|
|
228 "Open Hi Hat",
|
|
229 "Low Mid Tom",
|
|
230 "High Mid Tom", /* c4 */
|
|
231 "Crash Cymbal 1",
|
|
232 "High Tom",
|
|
233 "Ride Cymbal 1",
|
|
234 "Chinese Boom",
|
|
235 "Ride Bell",
|
|
236 "Tamborine",
|
|
237 "Splash cymbal",
|
|
238 "Cowbell",
|
|
239 "Crash Cymbal (2)",
|
|
240 "Vibra Slap",
|
|
241 "Ride Cymbal (2)",
|
|
242 "Hi bongo", /* c5 */
|
|
243 "Lo bongo",
|
|
244 "Mute High Conga",
|
|
245 "Open High Conga",
|
|
246 "Low Conga",
|
|
247 "High Timbale",
|
|
248 "Low Timbale",
|
|
249 "High Agogo",
|
|
250 "Low Agogo",
|
|
251 "Cabasa",
|
|
252 "Maracas",
|
|
253 "Short Hi Whistle",
|
|
254 "Long Low Whistle", /*c6 */
|
|
255 "Short Guiro",
|
|
256 "Long Guiro",
|
|
257 "Claves",
|
|
258 "High Woodblock",
|
|
259 "Low Woodblock",
|
|
260 "Mute Cuica",
|
|
261 "Open Cuica",
|
|
262 "Mute Triangle",
|
|
263 "Open Triangle",
|
|
264 "Shaker",
|
|
265 "Jingle Bell",
|
|
266 "Tring", /* C7 */
|
|
267 "Castinets",
|
|
268 "Mute Sudro",
|
|
269 "Open Sudro",
|
|
270 };
|
|
271
|
|
272 static const char *szCCList[] = {
|
|
273 "Bank Select",
|
|
274 "Modulation",
|
|
275 "Breath Control",
|
|
276 "Undefined 3",
|
|
277 "Foot",
|
|
278 "Portamento Time",
|
|
279 "Date Entry",
|
|
280 "Volume ",
|
|
281 "Balance",
|
|
282 "Undefined 9",
|
|
283 "Pan",
|
|
284 "Expression",
|
|
285 "Effort Control 1",
|
|
286 "Effort Control 2",
|
|
287 "Undefined 14",
|
|
288 "Undefined 15",
|
|
289 "General Purpose 1",
|
|
290 "General Purpose 2",
|
|
291 "General Purpose 3",
|
|
292 "General Purpose 4",
|
|
293 /* 20-31 are undefined */
|
|
294 "Undefined 20",
|
|
295 "Undefined 21",
|
|
296 "Undefined 22",
|
|
297 "Undefined 23",
|
|
298 "Undefined 24",
|
|
299 "Undefined 25",
|
|
300 "Undefined 26",
|
|
301 "Undefined 27",
|
|
302 "Undefined 28",
|
|
303 "Undefined 29",
|
|
304 "Undefined 30",
|
|
305 "Undefined 31",
|
|
306 /* LSB for control changes 0-31 32-63 */
|
|
307 "lsb-32", "lsb-33", "lsb-34", "lsb-35", "lsb-36", "lsb-37", "lsb-38",
|
|
308 "lsb-39", "lsb-40", "lsb-41", "lsb-42", "lsb-43", "lsb-44", "lsb-45",
|
|
309 "lsb-46", "lsb-47", "lsb-48", "lsb-49", "lsb-50", "lsb-51", "lsb-52",
|
|
310 "lsb-53", "lsb-54", "lsb-55", "lsb-56", "lsb-57", "lsb-58", "lsb-59",
|
|
311 "lsb-60", "lsb-61", "lsb-62", "lsb-63",
|
|
312
|
|
313 "Sustain Pedal",
|
|
314 "Portamento",
|
|
315 "Pedal Sustenuto",
|
|
316 "Pedal Soft",
|
|
317 "Legato Foot Switch",
|
|
318 "Hold 2",
|
|
319 "Sound Variation",
|
|
320 "Harm Content",
|
|
321 "Release Time",
|
|
322 "Attack Time",
|
|
323 "Brightness",
|
|
324 "Reverb",
|
|
325 "Delay",
|
|
326 "Pitch Transpose",
|
|
327 "Flange",
|
|
328 "Special FX",
|
|
329 "General Purpose 5",
|
|
330 "General Purpose 6",
|
|
331 "General Purpose 7",
|
|
332 "General Purpose 8",
|
|
333 "Portamento Control",
|
|
334 /* 85-90 are undefined */
|
|
335 "Undefined 85",
|
|
336 "Undefined 86",
|
|
337 "Undefined 87",
|
|
338 "Undefined 88",
|
|
339 "Undefined 89",
|
|
340 "Undefined 90",
|
|
341 "FX Depth",
|
|
342 "Tremelo Depth",
|
|
343 "Chorus Depth",
|
|
344 "Celesta Depth",
|
|
345 "Phaser Depth",
|
|
346 "Data Inc",
|
|
347 "Data Dec",
|
|
348 "Non Reg Param LSB",
|
|
349 "Non Ref Param MSB",
|
|
350 "Reg Param LSB",
|
|
351 "Reg Param MSB",
|
|
352 /* 102-119 are undefined */
|
|
353 "Undefined 102",
|
|
354 "Undefined 103",
|
|
355 "Undefined 104",
|
|
356 "Undefined 105",
|
|
357 "Undefined 106",
|
|
358 "Undefined 107",
|
|
359 "Undefined 108",
|
|
360 "Undefined 109",
|
|
361 "Undefined 110",
|
|
362 "Undefined 111",
|
|
363 "Undefined 112",
|
|
364 "Undefined 113",
|
|
365 "Undefined 114",
|
|
366 "Undefined 115",
|
|
367 "Undefined 116",
|
|
368 "Undefined 117",
|
|
369 "Undefined 118",
|
|
370 "Undefined 119",
|
|
371 "All Sound Off",
|
|
372 "Reset All Controllers",
|
|
373 "Local Control",
|
|
374 "All Notes Off",
|
|
375 "Omni Mode Off",
|
|
376 "Omni Mode On",
|
|
377 "Mono Mode On",
|
|
378 "Poly Mode On",
|
|
379 };
|
|
380
|
|
381 static const char *szNoteName[] = {
|
|
382 "C ",
|
|
383 "Db",
|
|
384 "D ",
|
|
385 "Eb",
|
|
386 "E ",
|
|
387 "F ",
|
|
388 "Gb",
|
|
389 "G ",
|
|
390 "Ab",
|
|
391 "A ",
|
|
392 "Bb",
|
|
393 "B ",
|
|
394 };
|
|
395
|
|
396 static float fFreqlist[] = {
|
|
397 261.63f,
|
|
398 277.18f,
|
|
399 293.66f,
|
|
400 311.13f,
|
|
401 329.63f,
|
|
402 349.23f,
|
|
403 369.99f,
|
|
404 392.00f,
|
|
405 415.30f,
|
|
406 440.00f,
|
|
407 466.16f,
|
|
408 493.88f,
|
|
409 };
|
|
410
|
|
411 /*
|
|
412 ** Name resolving functions
|
|
413 */
|
|
414 BOOL muGetInstrumentName(char *pName, int iInstr)
|
|
415 {
|
|
416 if (iInstr < 0 || iInstr > 127)
|
|
417 return FALSE;
|
|
418 strcpy(pName, szPatchList[iInstr]);
|
|
419 return TRUE;
|
|
420 }
|
|
421
|
|
422 BOOL muGetDrumName(char *pName, int iInstr)
|
|
423 {
|
|
424 if (iInstr < 0 || iInstr > 127)
|
|
425 return FALSE;
|
|
426 strcpy(pName, szGMDrums[iInstr]);
|
|
427 return TRUE;
|
|
428 }
|
|
429
|
|
430 BOOL muGetMIDIMsgName(char *pName, tMIDI_MSG iMsg)
|
|
431 {
|
|
432 switch(iMsg)
|
|
433 {
|
|
434 case msgNoteOff:
|
|
435 strcpy(pName, "Note off");
|
|
436 break;
|
|
437
|
|
438 case msgNoteOn:
|
|
439 strcpy(pName, "Note on");
|
|
440 break;
|
|
441
|
|
442 case msgNoteKeyPressure:
|
|
443 strcpy(pName, "Note key pressure");
|
|
444 break;
|
|
445
|
|
446 case msgSetParameter:
|
|
447 strcpy(pName, "Set parameter");
|
|
448 break;
|
|
449
|
|
450 case msgSetProgram:
|
|
451 strcpy(pName, "Set program");
|
|
452 break;
|
|
453
|
|
454 case msgChangePressure:
|
|
455 strcpy(pName, "Change pressure");
|
|
456 break;
|
|
457
|
|
458 case msgSetPitchWheel:
|
|
459 strcpy(pName, "Set pitch wheel");
|
|
460 break;
|
|
461
|
|
462 case msgMetaEvent:
|
|
463 strcpy(pName, "Meta event");
|
|
464 break;
|
|
465
|
|
466 case msgSysEx1:
|
|
467 strcpy(pName, "SysEx1");
|
|
468 break;
|
|
469
|
|
470 case msgSysEx2:
|
|
471 strcpy(pName, "SysEx2");
|
|
472 break;
|
|
473
|
|
474 default:
|
|
475 return FALSE;
|
|
476 }
|
|
477 return TRUE;
|
|
478 }
|
|
479
|
|
480 BOOL muGetControlName(char *pName, tMIDI_CC iCC)
|
|
481 {
|
|
482 if (iCC < 0 || iCC > 127)
|
|
483 return FALSE;
|
|
484 strcpy(pName, szCCList[iCC]);
|
|
485 return TRUE;
|
|
486 }
|
|
487
|
|
488 BOOL muGetKeySigName(char *pName, tMIDI_KEYSIG iKey)
|
|
489 {
|
|
490 static const char *iKeysList[2][8] = {
|
|
491 /*#*/{"C ", "G ", "D ", "A ", "E ", "B ", "F#", "C#", },
|
|
492 /*b*/{"C ", "F ", "Bb", "Eb", "Ab", "Db", "Gb", "Cb", },
|
|
493 };
|
|
494
|
|
495 int iRootNum = (iKey&7);
|
|
496 int iFlats = (iKey&keyMaskNeg);
|
|
497 int iMin = (iKey&keyMaskMin);
|
|
498
|
|
499 strcpy(pName,iKeysList[iFlats?1:0][iRootNum]);
|
|
500 strcat(pName,iMin?" Min":" Maj");
|
|
501 return TRUE;
|
|
502 }
|
|
503
|
|
504 BOOL muGetTextName(char *pName, tMIDI_TEXT iEvent)
|
|
505 {
|
|
506 if (iEvent<1 || iEvent>7) return FALSE;
|
|
507 return muGetMetaName(pName, (tMIDI_META)iEvent);
|
|
508 }
|
|
509
|
|
510 BOOL muGetMetaName(char *pName, tMIDI_META iEvent)
|
|
511 {
|
|
512 switch(iEvent)
|
|
513 {
|
|
514 case metaSequenceNumber: strcpy(pName, "Sequence Number"); break;
|
|
515 case metaTextEvent: strcpy(pName, "Text Event"); break;
|
|
516 case metaCopyright: strcpy(pName, "Copyright"); break;
|
|
517 case metaTrackName: strcpy(pName, "Track Name"); break;
|
|
518 case metaInstrument: strcpy(pName, "Instrument"); break;
|
|
519 case metaLyric: strcpy(pName, "Lyric"); break;
|
|
520 case metaMarker: strcpy(pName, "Marker"); break;
|
|
521 case metaCuePoint: strcpy(pName, "Cue Point"); break;
|
|
522 case metaMIDIPort: strcpy(pName, "MIDI Port"); break;
|
|
523 case metaEndSequence: strcpy(pName, "End Sequence"); break;
|
|
524 case metaSetTempo: strcpy(pName, "Set Tempo"); break;
|
|
525 case metaSMPTEOffset: strcpy(pName, "SMPTE Offset"); break;
|
|
526 case metaTimeSig: strcpy(pName, "Time Sig"); break;
|
|
527 case metaKeySig: strcpy(pName, "Key Sig"); break;
|
|
528 case metaSequencerSpecific: strcpy(pName, "Sequencer Specific"); break;
|
|
529 default: return FALSE;
|
|
530 }
|
|
531 return TRUE;
|
|
532
|
|
533 }
|
|
534
|
|
535
|
|
536 /*
|
|
537 ** Conversion Functions
|
|
538 */
|
|
539 int muGetNoteFromName(const char *pName)
|
|
540 {
|
|
541 int note_map[] = {9, 11, 0, 2, 4, 5, 7};
|
|
542 char *p, cpy[16];
|
|
543 int note=0;
|
|
544
|
|
545 strncpy(cpy, pName, 15);
|
|
546 cpy[15] = '\0';
|
|
547 p = cpy;
|
|
548
|
|
549 while(!isalpha(*p) && *p)
|
|
550 p++;
|
|
551
|
|
552 if (*p)
|
|
553 {
|
|
554 note = toupper(*p)-'A';
|
|
555 if (note >= 0 && note <= 7)
|
|
556 {
|
|
557 note = note_map[note];
|
|
558 p++;
|
|
559 if (*p == 'b')
|
|
560 note--, p++;
|
|
561 else if (*p == '#')
|
|
562 note++, p++;
|
|
563
|
|
564 note += atoi(p)*12+MIDI_NOTE_C0;
|
|
565 }
|
|
566 }
|
|
567
|
|
568 return note;
|
|
569 }
|
|
570
|
|
571 char *muGetNameFromNote(char *pStr, int iNote)
|
|
572 {
|
|
573 if (!pStr) return NULL;
|
|
574
|
|
575 if (iNote<0 || iNote>127)
|
|
576 strcpy(pStr, "ERR");
|
|
577 else
|
|
578 sprintf(pStr, "%s%d", szNoteName[iNote%12], ((iNote-MIDI_NOTE_C0)/12));
|
|
579
|
|
580 return pStr;
|
|
581 }
|
|
582
|
|
583 float muGetFreqFromNote(int iNote)
|
|
584 {
|
|
585 int oct = iNote/12-5;
|
|
586 float freq;
|
|
587
|
|
588 if (iNote<0 || iNote>127) return 0;
|
|
589
|
|
590 freq = fFreqlist[iNote%12];
|
|
591
|
|
592 while(oct > 0)
|
|
593 freq *= 2.0f, oct--;
|
|
594
|
|
595 while(oct < 0)
|
|
596 freq /= 2.0f, oct++;
|
|
597
|
|
598 return freq;
|
|
599 }
|
|
600
|
|
601 int muGetNoteFromFreq(float fFreq)
|
|
602 {
|
|
603 /* This is for completeness, I'm not sure of how often it
|
|
604 ** will get used. Therefore, the code is un-optimised :)
|
|
605 */
|
|
606 int iNote, iBestNote=0;
|
|
607 float fDiff=20000, f;
|
|
608
|
|
609 for(iNote=0;iNote<127;++iNote)
|
|
610 {
|
|
611 f = muGetFreqFromNote(iNote);
|
|
612 f -= fFreq; if (f<0) f=-f;
|
|
613 if (f < fDiff)
|
|
614 {
|
|
615 fDiff = f;
|
|
616 iBestNote = iNote;
|
|
617 }
|
|
618 }
|
|
619
|
|
620 return iBestNote;
|
|
621 }
|
|
622
|
|
623
|
|
624 int muGuessChord(const int *pNoteStatus, const int channel, const int lowRange, const int highRange) {
|
|
625 int octave[24];
|
|
626 int i;
|
|
627 int lowestNote=999;
|
|
628 int startNote = 999;
|
|
629 int chordRoot = 0;
|
|
630 int chordType = 0;
|
|
631 int chordAdditions = 0;
|
|
632
|
|
633 for(i=0;i<24;++i) {
|
|
634 octave[i] = 0;
|
|
635 }
|
|
636
|
|
637 for(i=lowRange;i<=highRange;++i) {
|
|
638 if (pNoteStatus[channel*128 + i]) {
|
|
639 if (i<lowestNote) {
|
|
640 lowestNote = i;
|
|
641 }
|
|
642 ++octave[i%12];
|
|
643 ++octave[i%12+12];
|
|
644 if ((i%12) < startNote) {
|
|
645 startNote = i%12;
|
|
646 }
|
|
647 }
|
|
648 }
|
|
649
|
|
650 if (lowestNote == 999) {
|
|
651 return -1;
|
|
652 }
|
|
653
|
|
654 /* Bring it into line with the 0-11 range */
|
|
655 lowestNote %= 12;
|
|
656
|
|
657 /* Majors */
|
|
658 if (octave[startNote+3] && octave[startNote+8]) {
|
|
659 chordRoot = startNote+8;
|
|
660 chordType = CHORD_TYPE_MAJOR;
|
|
661 } else if (octave[startNote+5] && octave[startNote+9]) {
|
|
662 chordRoot = startNote+5;
|
|
663 chordType = CHORD_TYPE_MAJOR;
|
|
664 } else if (octave[startNote+4] && octave[startNote+7]) {
|
|
665 chordRoot = startNote;
|
|
666 chordType = CHORD_TYPE_MAJOR;
|
|
667
|
|
668 /* Minor */
|
|
669 } else if (octave[startNote+4] && octave[startNote+9]) {
|
|
670 chordRoot = startNote+9;
|
|
671 chordType = CHORD_TYPE_MINOR;
|
|
672 } else if (octave[startNote+5] && octave[startNote+8]) {
|
|
673 chordRoot = startNote+5;
|
|
674 chordType = CHORD_TYPE_MINOR;
|
|
675 } else if (octave[startNote+3] && octave[startNote+7]) {
|
|
676 chordRoot = startNote;
|
|
677 chordType = CHORD_TYPE_MINOR;
|
|
678
|
|
679 /* Diminished */
|
|
680 } else if (octave[startNote+3] && octave[startNote+6]) {
|
|
681 chordRoot = lowestNote;
|
|
682 chordType = CHORD_TYPE_DIM;
|
|
683 } else if (octave[startNote+6] && octave[startNote+9]) {
|
|
684 chordRoot = lowestNote;
|
|
685 chordType = CHORD_TYPE_DIM;
|
|
686
|
|
687 /* Augmented */
|
|
688 } else if (octave[startNote+4] && octave[startNote+8]) {
|
|
689 chordRoot = lowestNote;
|
|
690 chordType = CHORD_TYPE_AUG;
|
|
691 }
|
|
692
|
|
693 if (octave[chordRoot + 10]) {
|
|
694 chordAdditions |= CHORD_ADD_7TH;
|
|
695 }
|
|
696 if (octave[chordRoot + 11]) {
|
|
697 chordAdditions |= CHORD_ADD_MAJ7TH;
|
|
698 }
|
|
699 if (octave[chordRoot + 2]) {
|
|
700 chordAdditions |= CHORD_ADD_9TH;
|
|
701 }
|
|
702
|
|
703 chordRoot %= 12;
|
|
704
|
|
705 if (chordType == 0) {
|
|
706 return -1;
|
|
707 }
|
|
708
|
|
709 return chordRoot | chordType | chordAdditions | (lowestNote<<16);
|
|
710 }
|
|
711
|
|
712 char *muGetChordName(char *str, int chord) {
|
|
713 int root = chord & CHORD_ROOT_MASK;
|
|
714 int bass = (chord & CHORD_BASS_MASK) >> 16;
|
|
715
|
|
716 if (root < 0 || root > 11) {
|
|
717 root = 0;
|
|
718 }
|
|
719
|
|
720 if (bass < 0 || bass > 11) {
|
|
721 bass = 0;
|
|
722 }
|
|
723
|
|
724 strcpy(str, szNoteName[root]);
|
|
725
|
|
726 switch(chord & CHORD_TYPE_MASK) {
|
|
727 case CHORD_TYPE_MAJOR:
|
|
728 break;
|
|
729 case CHORD_TYPE_MINOR:
|
|
730 strcat(str, "m");
|
|
731 break;
|
|
732 case CHORD_TYPE_AUG:
|
|
733 strcat(str, " aug");
|
|
734 break;
|
|
735 case CHORD_TYPE_DIM:
|
|
736 strcat(str, " dim");
|
|
737 break;
|
|
738 }
|
|
739
|
|
740 if (chord & CHORD_ADD_7TH) {
|
|
741 strcat(str, "+7");
|
|
742 }
|
|
743 if (chord & CHORD_ADD_9TH) {
|
|
744 strcat(str, "+9");
|
|
745 }
|
|
746 if (chord & CHORD_ADD_MAJ7TH) {
|
|
747 strcat(str, "+7M");
|
|
748 }
|
|
749
|
|
750 if (bass != root) {
|
|
751 strcat(str, "/");
|
|
752 strcat(str, szNoteName[bass]);
|
|
753 }
|
|
754
|
|
755 return str;
|
|
756 }
|
|
757
|