-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathConvert-pitch.cpp
1486 lines (1284 loc) · 41.8 KB
/
Convert-pitch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sat Aug 8 12:24:49 PDT 2015
// Last Modified: Wed Aug 19 00:06:39 PDT 2015
// Filename: Convert-pitch.cpp
// URL: https://github.com/craigsapp/humlib/blob/master/src/Convert-pitch.cpp
// Syntax: C++11; humlib
// vim: syntax=cpp ts=3 noexpandtab nowrap
//
// Description: Conversions related to pitch.
//
#include "Convert.h"
#include "HumRegex.h"
#include <cctype>
#include <cmath>
#include <vector>
using namespace std;
namespace hum {
// START_MERGE
//////////////////////////////
//
// Convert::kernToScientificPitch -- Convert a **kern pitch to
// ScientificPitch notation, which is the diatonic letter name,
// followed by a possible accidental, then an optional separator
// string, and finally the octave number. A string representing a
// chord can be given to this function, and the output will return
// a list of the pitches in the chord, separated by a space.
// default value: flat = "b"
// default value: sharp = "#"
// default value: separator = ""
//
string Convert::kernToScientificPitch(const string& kerndata,
string flat, string sharp, string separator) {
vector<string> subtokens = Convert::splitString(kerndata);
string output;
for (int i=0; i<(int)subtokens.size(); i++) {
char diatonic = Convert::kernToDiatonicUC(subtokens[i]);
int accidental = Convert::kernToAccidentalCount(subtokens[i]);
int octave = Convert::kernToOctaveNumber(subtokens[i]);
if ((i > 0) && (i < (int)subtokens.size()-1)) {
output += " ";
}
output += diatonic;
for (int j=0; j<abs(accidental); j++) {
output += (accidental < 0 ? flat : sharp);
}
output += separator;
output += to_string(octave);
}
return output;
}
//////////////////////////////
//
// Convert::kernToDiatonicPC -- Convert a kern token into a diatonic
// note pitch-class where 0="C", 1="D", ..., 6="B". -1000 is returned
// if the note is rest, and -2000 if there is no pitch information in the
// input string. Only the first subtoken in the string is considered.
//
int Convert::kernToDiatonicPC(const string& kerndata) {
for (int i=0; i<(int)kerndata.size(); i++) {
if (kerndata[i] == ' ') {
break;
}
if (kerndata[i] == 'r') {
return -1000;
}
switch (kerndata[i]) {
case 'A': case 'a': return 5;
case 'B': case 'b': return 6;
case 'C': case 'c': return 0;
case 'D': case 'd': return 1;
case 'E': case 'e': return 2;
case 'F': case 'f': return 3;
case 'G': case 'g': return 4;
}
}
return -2000;
}
//////////////////////////////
//
// Convert::kernToDiatonicUC -- Convert a kern token into a diatonic
// note pitch-class. "R" is returned if the note is rest, and
// "X" is returned if there is no pitch name in the string.
// Only the first subtoken in the string is considered.
//
char Convert::kernToDiatonicUC(const string& kerndata) {
for (int i=0; i<(int)kerndata.size(); i++) {
if (kerndata[i] == ' ') {
break;
}
if (kerndata[i] == 'r') {
return 'R';
}
if (('A' <= kerndata[i]) && (kerndata[i] <= 'G')) {
return kerndata[i];
}
if (('a' <= kerndata[i]) && (kerndata[i] <= 'g')) {
return toupper(kerndata[i]);
}
}
return 'X';
}
//////////////////////////////
//
// Convert::kernToDiatonicLC -- Similar to kernToDiatonicUC, but
// the returned pitch name is lower case.
//
char Convert::kernToDiatonicLC(const string& kerndata) {
return tolower(Convert::kernToDiatonicUC(kerndata));
}
//////////////////////////////
//
// Convert::kernToAccidentalCount -- Convert a kern token into a count
// of accidentals in the first subtoken. Sharps are assigned to the
// value +1 and flats to -1. So a double sharp is +2 and a double
// flat is -2. Only the first subtoken in the string is considered.
// Cases such as "#-" should not exist, but in this case the return
// value will be 0.
//
int Convert::kernToAccidentalCount(const string& kerndata) {
int output = 0;
for (int i=0; i<(int)kerndata.size(); i++) {
if (kerndata[i] == ' ') {
break;
}
if (kerndata[i] == '-') {
output--;
}
if (kerndata[i] == '#') {
output++;
}
}
return output;
}
//////////////////////////////
//
// Convert::kernToOctaveNumber -- Convert a kern token into an octave number.
// Middle C is the start of the 4th octave. -1000 is returned if there
// is not pitch in the string. Only the first subtoken in the string is
// considered.
//
int Convert::kernToOctaveNumber(const string& kerndata) {
int uc = 0;
int lc = 0;
if (kerndata == ".") {
return -1000;
}
for (int i=0; i<(int)kerndata.size(); i++) {
if (kerndata[i] == ' ') {
break;
}
if (kerndata[i] == 'r') {
return -1000;
}
uc += ('A' <= kerndata[i]) && (kerndata[i] <= 'G') ? 1 : 0;
lc += ('a' <= kerndata[i]) && (kerndata[i] <= 'g') ? 1 : 0;
}
if ((uc > 0) && (lc > 0)) {
// invalid pitch description
return -1000;
}
if (uc > 0) {
return 4 - uc;
} else if (lc > 0) {
return 3 + lc;
} else {
return -1000;
}
}
//////////////////////////////
//
// Convert::kernToBase40PC -- Convert **kern pitch to a base-40 pitch class.
// Will ignore subsequent pitches in a chord.
//
int Convert::kernToBase40PC(const string& kerndata) {
int diatonic = Convert::kernToDiatonicPC(kerndata);
if (diatonic < 0) {
return diatonic;
}
int accid = Convert::kernToAccidentalCount(kerndata);
int output = -1000;
switch (diatonic) {
case 0: output = 0; break;
case 1: output = 6; break;
case 2: output = 12; break;
case 3: output = 17; break;
case 4: output = 23; break;
case 5: output = 29; break;
case 6: output = 35; break;
}
output += accid;
return output + 2; // +2 to make c-flat-flat bottom of octave.
}
//////////////////////////////
//
// Convert::kernToBase40 -- Convert **kern pitch to a base-40 integer.
// Will ignore subsequent pitches in a chord.
//
int Convert::kernToBase40(const string& kerndata) {
string trimmed = Convert::trimWhiteSpace(kerndata);
int pc = Convert::kernToBase40PC(trimmed);
if (pc < 0) {
return pc;
}
int octave = Convert::kernToOctaveNumber(trimmed);
return pc + 40 * octave;
}
//////////////////////////////
//
// Convert::kernToBase12PC -- Convert **kern pitch to a base-12 pitch-class.
// C=0, C#/D-flat=1, D=2, etc. Will return -1 instead of 11 for C-, and
// will return 12 instead of 0 for B#.
//
int Convert::kernToBase12PC(const string& kerndata) {
int diatonic = Convert::kernToDiatonicPC(kerndata);
if (diatonic < 0) {
return diatonic;
}
int accid = Convert::kernToAccidentalCount(kerndata);
int output = -1000;
switch (diatonic) {
case 0: output = 0; break;
case 1: output = 2; break;
case 2: output = 4; break;
case 3: output = 5; break;
case 4: output = 7; break;
case 5: output = 9; break;
case 6: output = 11; break;
}
output += accid;
return output;
}
//////////////////////////////
//
// Convert::kernToBase12 -- Convert **kern pitch to a base-12 integer.
// (middle C = 48).
//
int Convert::kernToBase12(const string& kerndata) {
int pc = Convert::kernToBase12PC(kerndata);
int octave = Convert::kernToOctaveNumber(kerndata);
return pc + 12 * octave;
}
//////////////////////////////
//
// Convert::base40ToKern -- Convert Base-40 integer pitches into
// **kern pitch representation.
//
string Convert::base40ToKern(int b40) {
int octave = b40 / 40;
int accidental = Convert::base40ToAccidental(b40);
int diatonic = Convert::base40ToDiatonic(b40) % 7;
char base = 'a';
switch (diatonic) {
case 0: base = 'c'; break;
case 1: base = 'd'; break;
case 2: base = 'e'; break;
case 3: base = 'f'; break;
case 4: base = 'g'; break;
case 5: base = 'a'; break;
case 6: base = 'b'; break;
}
if (octave < 4) {
base = toupper(base);
}
int repeat = 0;
if (octave > 4) {
repeat = octave - 4;
} else if (octave < 3) {
repeat = 3 - octave;
}
if (repeat > 12) {
cerr << "Error: unreasonable octave value: " << octave << " for " << b40 << endl;
exit(1);
}
string output;
output += base;
for (int i=0; i<repeat; i++) {
output += base;
}
if (accidental == 0) {
return output;
}
if (accidental > 0) {
for (int i=0; i<accidental; i++) {
output += '#';
}
} else if (accidental < 0) {
for (int i=0; i<-accidental; i++) {
output += '-';
}
}
return output;
}
//////////////////////////////
//
// Convert::base40ToDiatonic -- find the diatonic pitch of the
// given base-40 pitch. Output pitch classes: 0=C, 1=D, 2=E,
// 3=F, 4=G, 5=A, 6=B. To this the diatonic octave is added.
// To get only the diatonic pitch class, mod by 7: (% 7).
// Base-40 pitches are not allowed, and the algorithm will have
// to be adjusted to allow them. Currently any negative base-40
// value is presumed to be a rest and not processed.
//
int Convert::base40ToDiatonic(int b40) {
int chroma = b40 % 40;
int octaveoffset = (b40 / 40) * 7;
if (b40 < 0) {
return -1; // rest;
}
switch (chroma) {
case 0: case 1: case 2: case 3: case 4: // C-- to C##
return 0 + octaveoffset;
case 6: case 7: case 8: case 9: case 10: // D-- to D##
return 1 + octaveoffset;
case 12: case 13: case 14: case 15: case 16: // E-- to E##
return 2 + octaveoffset;
case 17: case 18: case 19: case 20: case 21: // F-- to F##
return 3 + octaveoffset;
case 23: case 24: case 25: case 26: case 27: // G-- to G##
return 4 + octaveoffset;
case 29: case 30: case 31: case 32: case 33: // A-- to A##
return 5 + octaveoffset;
case 35: case 36: case 37: case 38: case 39: // B-- to B##
return 6 + octaveoffset;
}
// found an empty slot, so return rest:
return -1;
}
//////////////////////////////
//
// Convert::base40ToMidiNoteNumber --
//
int Convert::base40ToMidiNoteNumber(int b40) {
// +1 since middle-C octave is 5 in MIDI:
int octave = b40 / 40 + 1;
int accidental = Convert::base40ToAccidental(b40);
int diatonicpc = Convert::base40ToDiatonic(b40) % 7;
switch (diatonicpc) {
case 0: return octave * 12 + 0 + accidental;
case 1: return octave * 12 + 2 + accidental;
case 2: return octave * 12 + 4 + accidental;
case 3: return octave * 12 + 5 + accidental;
case 4: return octave * 12 + 7 + accidental;
case 5: return octave * 12 + 9 + accidental;
case 6: return octave * 12 + 11 + accidental;
default: return -1000; // can't deal with negative pitches
}
}
//////////////////////////////
//
// Convert::base40ToAccidental -- +1 = 1 sharp, +2 = double sharp, 0 = natural
// -1 = 1 flat, -2 = double flat
//
int Convert::base40ToAccidental(int b40) {
if (b40 < 0) {
// not considering low pitches. If so then the mod operator
// below whould need fixing.
return 0;
}
switch (b40 % 40) {
case 0: return -2; // C-double-flat
case 1: return -1; // C-flat
case 2: return 0; // C
case 3: return 1; // C-sharp
case 4: return 2; // C-double-sharp
case 5: return 1000;
case 6: return -2;
case 7: return -1;
case 8: return 0; // D
case 9: return 1;
case 10: return 2;
case 11: return 1000;
case 12: return -2;
case 13: return -1;
case 14: return 0; // E
case 15: return 1;
case 16: return 2;
case 17: return -2;
case 18: return -1;
case 19: return 0; // F
case 20: return 1;
case 21: return 2;
case 22: return 1000;
case 23: return -2;
case 24: return -1;
case 25: return 0; // G
case 26: return 1;
case 27: return 2;
case 28: return 1000;
case 29: return -2;
case 30: return -1;
case 31: return 0; // A
case 32: return 1;
case 33: return 2;
case 34: return 1000;
case 35: return -2;
case 36: return -1;
case 37: return 0; // B
case 38: return 1;
case 39: return 2;
}
return 0;
}
///////////////////////////////
//
// Convert::kernToMidiNoteNumber -- Convert **kern to MIDI note number
// (middle C = 60). Middle C is assigned to octave 5 rather than
// octave 4 for the kernToBase12() function.
//
int Convert::kernToMidiNoteNumber(const string& kerndata) {
int pc = Convert::kernToBase12PC(kerndata);
int octave = Convert::kernToOctaveNumber(kerndata);
return pc + 12 * (octave + 1);
}
//////////////////////////////
//
// Convert::kernToBase7 -- Convert **kern pitch to a base-7 integer.
// This is a diatonic pitch class with C=0, D=1, ..., B=6.
//
int Convert::kernToBase7(const string& kerndata) {
int diatonic = Convert::kernToDiatonicPC(kerndata);
if (diatonic < 0) {
return diatonic;
}
int octave = Convert::kernToOctaveNumber(kerndata);
return diatonic + 7 * octave;;
}
//////////////////////////////
//
// Convert::pitchToWbh -- Convert a given diatonic pitch class and
// accidental adjustment into an integer. The diatonic pitch class
// is C=0, D=1, E=2, F=3, G=4, A=5, B=6. "acc" is the accidental
// count: -2=double flat, -1=double flat, 0 natural, +1=sharp, etc.
// "octave" is the octave number, with middle-C being the start of
// octave 4. // "maxacc" is the maximum accidental which defines
// the base:
// maxacc = 2 -> Base-40.
// maxacc = n -> Base (n*2+1)*7 + 5.
//
int Convert::pitchToWbh(int dpc, int acc, int octave, int maxacc) {
if (dpc > 6) {
// allow for pitch-classes expressed as ASCII characters:
dpc = tolower(dpc) - 'a' + 5;
dpc = dpc % 7;
}
int output = -1000;
switch (dpc) {
case 0: output = maxacc; break;
case 1: output = 3 * maxacc + 2; break;
case 2: output = 5 * maxacc + 4; break;
case 3: output = 7 * maxacc + 5; break;
case 4: output = 9 * maxacc + 7; break;
case 5: output = 11 * maxacc + 9; break;
case 6: output = 13 * maxacc + 11; break;
}
if (output < 0) {
return output;
}
return (output + acc) + (7 * (maxacc * 2 + 1) + 5) * octave;
}
//////////////////////////////
//
// Convert::wbhToPitch -- Convert an integer-based pitch into
// a diatonic pitch class, accidental alteration and octave number
// The output diatonic pitch classes are 0=C, 1=D, 2=E, 3=F, 4=G, 5=A, 6=B.
// "acc" is the accidental count: -2=double flat, -1=double flat,
// 0 natural, +1=sharp, etc.
// "octave" is the octave number, with middle-C being the start of
// octave 4.
// "maxacc" is the maximum accidental which defines
// the base:
// maxacc = 2 -> Base-40.
// maxacc = n -> Base (n*2+1)*7 + 5.
// This valus must match the the analogous value used in PitchToWbh().
//
void Convert::wbhToPitch(int& dpc, int& acc, int& octave, int maxacc,
int wbh) {
int cwidth = maxacc * 2 + 1;
int base = 7 * cwidth + 5;
octave = wbh / base;
int pc = wbh % base;
// test for C diatonic pitch:
int pctest = cwidth;
if (pc < pctest) {
dpc = 0;
acc = pc - pctest + maxacc + 1;
return;
}
// test for D diatonic pitch
pctest += 1 + cwidth;
if (pc < pctest) {
dpc = 1;
acc = pc - pctest + maxacc + 1;
return;
}
// test for E diatonic pitch
pctest += 1 + cwidth;
if (pc < pctest) {
dpc = 2;
acc = pc - pctest + maxacc + 1;
return;
}
// test for F diatonic pitch
pctest += cwidth;
if (pc < pctest) {
dpc = 3;
acc = pc - pctest + maxacc + 1;
return;
}
// test for G diatonic pitch
pctest += 1 + cwidth;
if (pc < pctest) {
dpc = 4;
acc = pc - pctest + maxacc + 1;
return;
}
// test for A diatonic pitch
pctest += 1 + cwidth;
if (pc < pctest) {
dpc = 5;
acc = pc - pctest + maxacc + 1;
return;
}
// test for B diatonic pitch
pctest += 1 + cwidth;
if (pc < pctest) {
dpc = 6;
acc = pc - pctest + maxacc + 1;
return;
}
// if acc in any of the above tests is +3/-3, then there was an
// accidental overflow (overflow of the accidental).
}
//////////////////////////////
//
// Convert::kernClefToBaseline -- returns the diatonic pitch
// of the bottom line on the staff.
//
int Convert::kernClefToBaseline(HTp input) {
return kernClefToBaseline((string)*input);
}
int Convert::kernClefToBaseline(const string& input) {
string clefname;
if (input.compare(0, 5, "*clef") == 0) {
clefname = input.substr(5);
} else if (input.compare(0, 4, "clef") == 0) {
clefname = input.substr(4);
} else {
cerr << "Error in Convert::kernClefToBaseline: " << input << endl;
return -1000;
}
if (clefname == "G2") { // treble clef
return Convert::kernToBase7("e");
} else if (clefname == "F4") { // bass clef
return Convert::kernToBase7("GG");
} else if (clefname == "C3") { // alto clef
return Convert::kernToBase7("F");
} else if (clefname == "C4") { // tenor clef
return Convert::kernToBase7("D");
} else if (clefname == "Gv2") { // vocal tenor clef
return Convert::kernToBase7("E");
// rest of C clef possibilities:
} else if (clefname == "C1") { // soprano clef
return Convert::kernToBase7("c");
} else if (clefname == "C2") { // mezzo-soprano clef
return Convert::kernToBase7("A");
} else if (clefname == "C5") { // baritone clef
return Convert::kernToBase7("BB");
// rest of G clef possibilities:
} else if (clefname == "G1") { // French-violin clef
return Convert::kernToBase7("g");
} else if (clefname == "G3") {
return Convert::kernToBase7("c");
} else if (clefname == "G4") {
return Convert::kernToBase7("A");
} else if (clefname == "G5") {
return Convert::kernToBase7("F");
// rest of F clef possibilities:
} else if (clefname == "F1") {
return Convert::kernToBase7("F");
} else if (clefname == "F2") {
return Convert::kernToBase7("D");
} else if (clefname == "F3") {
return Convert::kernToBase7("BB");
} else if (clefname == "F5") {
return Convert::kernToBase7("EE");
// rest of G clef down an octave possibilities:
} else if (clefname == "Gv1") {
return Convert::kernToBase7("G");
} else if (clefname == "Gv3") {
return Convert::kernToBase7("C");
} else if (clefname == "Gv4") {
return Convert::kernToBase7("AA");
} else if (clefname == "Gv5") {
return Convert::kernToBase7("FF");
// F clef down an octave possibilities:
} else if (clefname == "Fv1") {
return Convert::kernToBase7("FF");
} else if (clefname == "Fv2") {
return Convert::kernToBase7("DD");
} else if (clefname == "Fv3") {
return Convert::kernToBase7("BBB");
} else if (clefname == "Fv4") {
return Convert::kernToBase7("GGG");
} else if (clefname == "Fv5") {
return Convert::kernToBase7("EEE");
// C clef down an octave possibilities:
} else if (clefname == "Cv1") {
return Convert::kernToBase7("C");
} else if (clefname == "Cv2") {
return Convert::kernToBase7("AA");
} else if (clefname == "Cv3") {
return Convert::kernToBase7("FF");
} else if (clefname == "Cv4") {
return Convert::kernToBase7("DD");
} else if (clefname == "Cv5") {
return Convert::kernToBase7("BBB");
// G clef up an octave possibilities:
} else if (clefname == "G^1") {
return Convert::kernToBase7("gg");
} else if (clefname == "G^2") {
return Convert::kernToBase7("ee");
} else if (clefname == "G^3") {
return Convert::kernToBase7("cc");
} else if (clefname == "G^4") {
return Convert::kernToBase7("a");
} else if (clefname == "G^5") {
return Convert::kernToBase7("f");
// F clef up an octave possibilities:
} else if (clefname == "F^1") {
return Convert::kernToBase7("f");
} else if (clefname == "F^2") {
return Convert::kernToBase7("d");
} else if (clefname == "F^3") {
return Convert::kernToBase7("B");
} else if (clefname == "F^4") {
return Convert::kernToBase7("G");
} else if (clefname == "F^5") {
return Convert::kernToBase7("E");
// C clef up an octave possibilities:
} else if (clefname == "C^1") {
return Convert::kernToBase7("cc");
} else if (clefname == "C^2") {
return Convert::kernToBase7("a");
} else if (clefname == "C^3") {
return Convert::kernToBase7("f");
} else if (clefname == "C^4") {
return Convert::kernToBase7("d");
} else if (clefname == "C^5") {
return Convert::kernToBase7("B");
// there are also two octaves down (*clefGvv2) and two octaves up (*clefG^^2)
} else {
// but just use treble clef if don't know what the clef it by this point
return Convert::kernToBase7("e");
}
}
//////////////////////////////
//
// Convert::base40ToTrans -- convert a base-40 interval into
// a trans program's diatonic/chromatic alteration marker
//
string Convert::base40ToTrans(int base40) {
int sign = 1;
int chroma;
int octave;
if (base40 < 0) {
sign = -1;
chroma = -base40 % 40;
octave = -base40 / 40;
} else {
sign = +1;
chroma = base40 % 40;
octave = base40 / 40;
}
int cval = 0;
int dval = 0;
switch (chroma * sign) {
case 0: dval=0; cval=0; break; // C -> C
case 1: dval=0; cval=1; break; // C -> C#
case 2: dval=0; cval=2; break; // C -> C##
case 4: dval=1; cval=0; break; // C -> D--
case 5: dval=1; cval=1; break; // C -> D-
case 6: dval=1; cval=2; break; // C -> D
case 7: dval=1; cval=3; break; // C -> D#
case 8: dval=1; cval=4; break; // C -> D##
case 10: dval=2; cval=2; break; // C -> E--
case 11: dval=2; cval=3; break; // C -> E-
case 12: dval=2; cval=4; break; // C -> E
case 13: dval=2; cval=5; break; // C -> E#
case 14: dval=2; cval=6; break; // C -> E##
case 15: dval=3; cval=3; break; // C -> F--
case 16: dval=3; cval=4; break; // C -> F-
case 17: dval=3; cval=5; break; // C -> F
case 18: dval=3; cval=6; break; // C -> F#
case 19: dval=3; cval=7; break; // C -> F##
case 21: dval=4; cval=5; break; // C -> G--
case 22: dval=4; cval=6; break; // C -> G-
case 23: dval=4; cval=7; break; // C -> G
case 24: dval=4; cval=8; break; // C -> G#
case 25: dval=4; cval=9; break; // C -> G##
case 27: dval=5; cval=7; break; // C -> A--
case 28: dval=5; cval=8; break; // C -> A-
case 29: dval=5; cval=9; break; // C -> A
case 30: dval=5; cval=10; break; // C -> A#
case 31: dval=5; cval=11; break; // C -> A##
case 33: dval=6; cval=9; break; // C -> B--
case 34: dval=6; cval=10; break; // C -> B-
case 35: dval=6; cval=11; break; // C -> B
case 36: dval=6; cval=12; break; // C -> B#
case 37: dval=6; cval=13; break; // C -> B##
case 38: dval=7; cval=10; break; // C -> c--
case 39: dval=7; cval=11; break; // C -> c-
case -1: dval=-0; cval=-1; break; // c -> c-
case -2: dval=-0; cval=-2; break; // c -> c--
case -3: dval=-1; cval=1; break; // c -> B##
case -4: dval=-1; cval=-0; break; // c -> B#
case -5: dval=-1; cval=-1; break; // c -> B
case -6: dval=-1; cval=-2; break; // c -> B-
case -7: dval=-1; cval=-3; break; // c -> B--
case -9: dval=-2; cval=-1; break; // c -> A##
case -10: dval=-2; cval=-2; break; // c -> A#
case -11: dval=-2; cval=-3; break; // c -> A
case -12: dval=-2; cval=-4; break; // c -> A-
case -13: dval=-2; cval=-5; break; // c -> A-
case -15: dval=-3; cval=-3; break; // c -> G##
case -16: dval=-3; cval=-4; break; // c -> G#
case -17: dval=-3; cval=-5; break; // c -> G
case -18: dval=-3; cval=-6; break; // c -> G-
case -19: dval=-3; cval=-7; break; // c -> G--
case -21: dval=-4; cval=-5; break; // c -> F##
case -22: dval=-4; cval=-6; break; // c -> F#
case -23: dval=-4; cval=-7; break; // c -> F
case -24: dval=-4; cval=-8; break; // c -> F-
case -25: dval=-4; cval=-9; break; // c -> F--
case -26: dval=-5; cval=-6; break; // c -> E##
case -27: dval=-5; cval=-7; break; // c -> E#
case -28: dval=-5; cval=-8; break; // c -> E
case -29: dval=-5; cval=-9; break; // c -> E-
case -30: dval=-5; cval=-10; break; // c -> E--
case -32: dval=-6; cval=-8; break; // c -> D##
case -33: dval=-6; cval=-9; break; // c -> D#
case -34: dval=-6; cval=-10; break; // c -> D
case -35: dval=-6; cval=-11; break; // c -> D-
case -36: dval=-6; cval=-12; break; // c -> D--
case -38: dval=-7; cval=-10; break; // c -> C##
case -39: dval=-7; cval=-11; break; // c -> C#
default:
dval=0; cval=0;
}
if (octave > 0) {
dval = dval + sign * octave * 7;
cval = cval + sign * octave * 12;
}
string output = "d";
output += to_string(dval);
output += "c";
output += to_string(cval);
return output;
}
//////////////////////////////
//
// Convert::base40ToIntervalAbbr --
//
string Convert::base40ToIntervalAbbr(int base40interval) {
if (base40interval < -1000) {
return "r";
}
string output;
if (base40interval < 0) {
output = "-";
base40interval = -base40interval;
}
// Add chromatic prefix
switch (base40interval % 40) {
case 0: output += "p" ; break; // C
case 1: output += "a" ; break; // C#
case 2: output += "aa" ; break; // C##
case 3: output += "X" ; break; // X
case 4: output += "d" ; break; // D--
case 5: output += "m" ; break; // D-
case 6: output += "M" ; break; // D
case 7: output += "a" ; break; // D#
case 8: output += "aa" ; break; // D##
case 9: output += "X" ; break; // X
case 10: output += "d" ; break; // E--
case 11: output += "m" ; break; // E-
case 12: output += "M" ; break; // E
case 13: output += "a" ; break; // E#
case 14: output += "aa" ; break; // E##
case 15: output += "dd" ; break; // F--
case 16: output += "d" ; break; // F-
case 17: output += "p" ; break; // F
case 18: output += "a" ; break; // F#
case 19: output += "aa" ; break; // F##
case 20: output += "X" ; break; // X
case 21: output += "dd" ; break; // G--
case 22: output += "d" ; break; // G-
case 23: output += "p" ; break; // G
case 24: output += "a" ; break; // G#
case 25: output += "aa" ; break; // G##
case 26: output += "X" ; break; // X
case 27: output += "d" ; break; // A--
case 28: output += "m" ; break; // A-
case 29: output += "M" ; break; // A
case 30: output += "a" ; break; // A#
case 31: output += "aa" ; break; // A##
case 32: output += "X" ; break; // X
case 33: output += "d" ; break; // B--
case 34: output += "m" ; break; // B-
case 35: output += "M" ; break; // B
case 36: output += "a" ; break; // B#
case 37: output += "aa" ; break; // B##
case 38: output += "dd" ; break; // C--
case 39: output += "d" ; break; // C-
}
// Add base-7 number
char buffer2[32] = {0};
int diatonic = Convert::base40IntervalToDiatonic(base40interval)+1;
snprintf(buffer2, 32, "%d", diatonic);
output += buffer2;
return output;
}
//////////////////////////////
//
// Convert::base40IntervalToDiatonic -- convert a base40 interval
// into a diatonic interval (excluding the chromatic alteration)
//
int Convert::base40IntervalToDiatonic(int base40interval) {
int sign = 1;
if (base40interval < 0) {
sign = -1;
base40interval = -base40interval;
}
int octave = base40interval / 40;
base40interval = base40interval % 40;
int diatonic = 0;
switch (base40interval) {
case 0: diatonic = 0; break; // C
case 1: diatonic = 0; break; // C#
case 2: diatonic = 0; break; // C##
case 3: diatonic = 1000; break; // blank
case 4: diatonic = 1; break; // D--
case 5: diatonic = 1; break; // D-
case 6: diatonic = 1; break; // D
case 7: diatonic = 1; break; // D#
case 8: diatonic = 1; break; // D##
case 9: diatonic = 1000; break; // blank
case 10: diatonic = 2; break; // E--
case 11: diatonic = 2; break; // E-
case 12: diatonic = 2; break; // E
case 13: diatonic = 2; break; // E#
case 14: diatonic = 2; break; // E##
case 15: diatonic = 3; break; // F--
case 16: diatonic = 3; break; // F-
case 17: diatonic = 3; break; // F
case 18: diatonic = 3; break; // F#
case 19: diatonic = 3; break; // F##
case 20: diatonic = 1000; break; // blank
case 21: diatonic = 4; break; // G--
case 22: diatonic = 4; break; // G-
case 23: diatonic = 4; break; // G
case 24: diatonic = 4; break; // G#
case 25: diatonic = 4; break; // G##