-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathabcmatch.c
1561 lines (1406 loc) · 41.3 KB
/
abcmatch.c
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
/* abcmatch.c
This contains the main program and core functions of the program to
compare abc files.
The abc file may either be a single tune or a compilation
of tunes. The program searches for specific bars in the
file and returns its positions in the file. There are
various matching criteria ranging from exact to approximate
match. The program assumes there is a file called match.abc
containing a description of the bars to be matched. Though
you can run abcmatch as a stand alone program, it was
really designed to run with a graphical user interface such
as runabc.tcl (version 1.59 or higher).
Limitations:
tied notes longer than 8 quarter notes are ignored.
Road map to the code:
Matching:
The templates derived from match.abc are stored in the
global arrays tpmidipitch, tpnotelength, tpnotes, tpbars, and
tpbarlineptr.
If temporal quantization is selected, (i.e. qnt >0) then
the template is represented by mpitch_samples[] and msamples[].
The temporal quantization representation is created by the
function make_bar_image (). All the bars in the template
file match.abc are converted and stored in mpitch_samples
and msamples[] when match.abc is processed. However, the bars
in the tunes compared are converted only as needed.
match_notes() is called if there is no temporal quantization
(exact matching).
match_samples() is called if there is temporal quantization.
difference_midipitch() is called if we are doing contour
matching on the temporal quantized images. For exact matching
(i.e. qnt == 0), when contouring is specified (con ==1) the
differencing (and possibly quantization of the contour) is
performed in the function match_notes.
count_matched_tune_bars () is called if only want an overview
(brief ==1).
find_and_report_matching_bars() is called if we want the details
(brief !=1);
*/
#define VERSION "1.83 Feb 19 2024 abcmatch"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "abc.h"
#include "parseabc.h"
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define BAR -1000
#define RESTNOTE -2000
#define ANY -3000
enum ACTION
{
none,
cpitch_histogram,
cwpitch_histogram,
clength_histogram,
cpitch_histogram_table,
interval_pdf,
interval_pdf_table
} action;
int *checkmalloc (int);
extern int getarg (char *, int, char **);
void free_feature_representation ();
/* variables shared with abcparse.c and abcstore.c */
/* many of these variables are used in event_int which has
been moved to this file.
*/
extern int *pitch, *num, *denom, *pitchline;
extern char **atext, **words;
extern featuretype *feature;
extern int notes;
extern int note_unit_length;
extern int time_num, time_denom;
extern int sf, mi;
extern FILE *fp;
extern int xrefno;
extern int check, nowarn, noerror, verbose, maxnotes, xmatch, dotune;
extern int maxtexts, maxwords;
extern int voicesused;
/* midipitch: pitch in midi units of each note in the abc file. A pitch
of zero is reserved for rest notes. Bar lines are signaled with BAR.
If contour matching is used, then all the pitch differences are offsetted
by 256 to avoid the likelihood of a zero difference mistaken for a
rest.
notelength: represents the length of a note. A quarter note is 24 units
(assuming L:1/4 in abc file) and all other note sizes are proportional.
Thus a half note is 48 units, a sixteenth note is 6 units.
nnotes: the number of notes in the midipitch array.
nbars: the number of bar lines in the midipitch array.
barlineptr: indicates the index in midipitch, notelength of the start
of bar lines.
*/
/* data structure for input to matcher. */
int imidipitch[10000]; /* pitch-barline midi note representation of input tune */
int inotelength[10000]; /* notelength representation of input tune */
int innotes; /*number of notes in imidipitch,inotelength representation */
int inbars; /*number of bars in input tune */
int ibarlineptr[2000]; /*pointers to bar lines in imidipitch */
int itimesig_num, itimesig_denom;
int imaxnotes = 10000; /* maximum limits of this program */
int imaxbars = 2000;
int resolution = 12; /* default to 1/8 note resolution */
int anymode = 0; /* default to matching all bars */
int ignore_simple = 0; /* ignore simple bars */
int con = 0; /* default for no contour matching */
int qnt = 0; /* pitch difference quantization flag */
int brief = 0; /* set brief to 1 if brief mode */
int cthresh = 3; /* minimum number of common bars to report */
int fixednumberofnotes=0; /* for -fixed parameter */
int fileindex = -1;
int wphist, phist, lhist; /* flags for computing pitch or length histogram */
int pdftab; /* flag for computing pitch pdf for each tune */
int qntflag = 0; /* flag for turning on contour quantization */
int wpintv = 0; /* flag for computing interval pdf */
int wpipdf = 0; /* flag for computing interval pdf for each tune*/
int norhythm = 0; /* ignore note lengths */
int levdist = 0; /* levenshtein distance */
char *templatefile;
/* data structure for matcher (template) (usually match.abc)*/
int tpmidipitch[1000]; /*pitch-barline representation for template */
int tpnotelength[1000]; /*note lengths for template */
int tpnotes; /* number of notes in template */
int tpbars; /* number of bar lines in template */
int tpbarlineptr[300]; /* I don't expect 300 bar lines, but lets play safe */
int tptimesig_num, tptimesig_denom;
int tpmaxnotes = 1000; /* maximum limits of this program */
int tpmaxbars = 300;
unsigned char tpbarstatus[300];
int pitch_histogram[128];
int weighted_pitch_histogram[128];
int length_histogram[144];
int interval_histogram[128];
int lastpitch;
/* compute the midi offset for the key signature. Since
we do not have negative indices in arrays, sf2midishift[7]
corresponds to no flats/no sharps, ie. C major scale.
If sf is the number of sharps (when positive) and the number of
flats when negative, then sf2midishift[7+i] is the offset in
semitones from C. Thus G is 7, D is 2, Bb is 10 etc.
*/
int sf2midishift[] = { 11, 6, 1, 8, 3, 10, 5, 0, 7, 2, 9, 4, 11, 6, 1, };
/* when using a resolution greater than 0, time resolution
* is reduced by a certain factor. The resolution is reduced
* in the arrays ipitch_samples and mpitch_samples. We need
* more room in mpitch_samples for the template, since we
* use this template over and over. Therefore we compute it
* and store it for the entire template. On the other hand, the
* input tune is always changing, so we only store one bar at
* a time for the input tune.
*/
int ipitch_samples[400], isamples;
int mpitch_samples[4000], msamples[160]; /* maximum number of bars 160 */
char titlename[48];
char keysignature[16];
int tpxref = 0; /* template reference number */
int tp_fileindex = 0; /* file sequence number for tpxref */
void
make_note_representation (int *nnotes, int *nbars, int maxnotes, int maxbars,
int *timesig_num, int *timesig_denom,
int *barlineptr, int *notelength, int *midipitch)
/* converts between the feature,pitch,num,denom representation to the
midipitch,notelength,... representation. This simplification does
not preserve chords, decorations, grace notes etc.
*/
{
float fract;
int i;
int skip_rests, multiplier, inchord, ingrace;
int maxpitch=0; /* [SDG] 2020-06-03 */
*nnotes = 0;
*nbars = 0;
inchord = 0;
ingrace = 0;
skip_rests = 0;
*timesig_num = time_num;
*timesig_denom = time_denom;
multiplier = (int) 24.0;
barlineptr[*nbars] = 0;
for (i = 0; i < notes; i++)
{
switch (feature[i])
{
case NOTE:
if (inchord)
maxpitch = MAX (maxpitch, pitch[i]);
else if (ingrace)
break;
else
{
midipitch[*nnotes] = pitch[i];
fract = (float) num[i] / (float) denom[i];
notelength[*nnotes] = (int) (fract * multiplier + 0.01);
(*nnotes)++;
}
break;
case TNOTE:
midipitch[*nnotes] = pitch[i];
fract = (float) num[i] / (float) denom[i];
notelength[*nnotes] = (int) (fract * multiplier + 0.01);
(*nnotes)++;
skip_rests = 2;
break;
case REST:
if (skip_rests > 0)
{
skip_rests--;
break;
}
else /* for handling tied notes */
{
midipitch[*nnotes] = RESTNOTE;
fract = (float) num[i] / (float) denom[i];
notelength[*nnotes] = (int) (fract * multiplier + 0.01);
(*nnotes)++;
break;
}
case CHORDON:
inchord = 1;
maxpitch = 0;
break;
case CHORDOFF:
inchord = 0;
midipitch[*nnotes] = maxpitch;
fract = (float) num[i] / (float) denom[i];
notelength[*nnotes] = (int) (fract * multiplier + 0.01);
(*nnotes)++;
break;
case GRACEON:
ingrace = 1;
break;
case GRACEOFF:
ingrace = 0;
break;
case DOUBLE_BAR:
case SINGLE_BAR:
case REP_BAR:
case REP_BAR2:
case BAR_REP:
/* bar lines at the beginning of the music can cause the
bar numbering to be incorrect and loss of synchrony with
runabc. If no notes were encountered and nbars = 0,
then we are at the beginning and we wish to bypass
these bar line indications. Note bar numbering starts
from 0. [SS] 2013-11-17
*/
if (*nnotes > 0) { /* [SS] 2021-11-25 */
midipitch[*nnotes] = BAR;
notelength[*nnotes] = BAR;
(*nnotes)++;
if (*nbars < maxbars) (*nbars)++;
else printf("abcmatch too many bars\n");
}
barlineptr[*nbars] = *nnotes;
break;
case TIME:
*timesig_num = num[i];
*timesig_denom = denom[i];
if (*timesig_num == 2 && *timesig_denom ==2) {
*timesig_num = 4;
*timesig_denom = 4;
/* [SS] 2013-11-12 */
}
break;
default:
break;
}
if (*nnotes > imaxnotes)
{
printf ("ran out of space for midipitch for xref %d\n",xrefno);
exit (0);
}
if (*nbars > imaxbars)
{
printf ("ran out of space for barlineptr for xref %d\n",xrefno);
exit (0);
}
}
midipitch[*nnotes + 1] = BAR; /* in case a final bar line is missing */
/*printf("refno =%d %d notes %d bar lines %d/%d time-signature %d sharps\n"
,xrefno,(*nnotes),(*nbars),(*timesig_num),(*timesig_denom),sf);*/
#ifdef DEBUG
printf("nbars = %d\n",*nbars);
for (i=0;i<*nbars;i++) printf("barlineptr[%d] = %d\n",i,barlineptr[i]);
i = 0;
while (i < 50) {
for (j=i;j<i+10;j++) printf("%d ",midipitch[j]);
printf("\n");
i +=10;
}
printf("\n\n");
#endif
}
/* This function is not used yet. */
int
quantize5 (int pitch)
{
if (pitch < -4)
return -2;
if (pitch < -1)
return -1;
if (pitch > 4)
return 2;
if (pitch > 1)
return 1;
return 0;
}
int quantize7(int pitch)
{
if (pitch < -4)
return -3;
if (pitch < -2)
return -2;
if (pitch < 0)
return -1;
if (pitch > 4)
return 3;
if (pitch >2)
return 2;
if (pitch >0)
return 1;
return 0;
}
void
init_histograms ()
{
int i;
for (i = 0; i < 128; i++)
pitch_histogram[i] = 0;
for (i = 0; i < 128; i++)
weighted_pitch_histogram[i] = 0;
for (i = 0; i < 144; i++)
length_histogram[i] = 0;
for (i = 0; i < 128; i++)
interval_histogram[i] = 0;
lastpitch = 0;
}
void
compute_note_histograms ()
{
int i, index, delta;
for (i = 0; i < innotes; i++)
{
index = imidipitch[i];
if (index < 0)
continue; /* [SS] 2013-02-26 */
pitch_histogram[index]++;
weighted_pitch_histogram[index] += inotelength[i];
index = inotelength[i];
if (index > 143)
index = 143;
length_histogram[index]++;
if (lastpitch != 0) {delta = imidipitch[i] - lastpitch;
delta += 60;
if (delta > -1 && delta < 128) interval_histogram[delta]++;
}
lastpitch = imidipitch[i];
}
}
void
print_length_histogram ()
{
int i;
printf ("\n\nlength histogram\n");
for (i = 0; i < 144; i++)
if (length_histogram[i] > 0)
printf ("%d %d\n", i, length_histogram[i]);
}
void
print_pitch_histogram ()
{
int i;
printf ("pitch_histogram\n");
for (i = 0; i < 128; i++)
if (pitch_histogram[i] > 0)
printf ("%d %d\n", i, pitch_histogram[i]);
}
void
print_weighted_pitch_histogram ()
{
int i;
printf ("weighted_pitch_histogram\n");
for (i = 0; i < 128; i++)
if (weighted_pitch_histogram[i] > 0)
printf ("%d %d\n", i, weighted_pitch_histogram[i]);
}
void
print_interval_pdf()
{
int i;
printf ("interval_histogram\n");
for (i = 0; i < 128; i++)
if (interval_histogram[i] > 0)
printf ("%d %d\n", i-60, interval_histogram[i]);
}
void
make_and_print_pitch_pdf ()
{
float pdf[12], sum;
int i, j;
for (i = 0; i < 12; i++)
pdf[i] = 0.0;
sum = 0.0;
for (i = 1; i < 128; i++)
{
j = i % 12;
pdf[j] += (float) weighted_pitch_histogram[i];
sum += (float) weighted_pitch_histogram[i];
}
if (sum <= 0.000001)
sum = 1.0;
for (i = 0; i < 12; i++)
{
pdf[i] = pdf[i] / sum;
printf ("%d %5.3f\n", i, pdf[i]);
}
}
void
make_and_print_interval_pdf ()
{
float pdf[24], sum;
int i, j;
for (i = 0; i < 24; i++)
pdf[i] = 0.0;
sum = 0.0;
for (i = 48; i < 72; i++)
{
j = i-48;
pdf[j] = (float) interval_histogram[i];
sum += (float) interval_histogram[i];
}
if (sum <= 0.000001)
sum = 1.0;
for (i = 0; i < 24; i++)
{
pdf[i] = pdf[i] / sum;
printf ("%d %5.3f\n", i-12, pdf[i]);
}
}
void
difference_midipitch (int *pitch_samples, int nsamples)
{
int i;
int lastsample, newsample;
lastsample = -1;
for (i = 0; i < nsamples; i++)
{
newsample = pitch_samples[i];
if (newsample == RESTNOTE)
{
pitch_samples[i] = RESTNOTE;
continue;
}
else if (lastsample == -1)
{
pitch_samples[i] = ANY;
}
else
{
pitch_samples[i] = newsample - lastsample;
if (qntflag > 0)
pitch_samples[i] = quantize7 (pitch_samples[i]);
}
lastsample = newsample;
}
}
int
make_bar_image (int bar_number, int resolution,
int *barlineptr, int *notelength, int nnotes, int delta_pitch,
int *midipitch, int *pitch_samples)
{
/* the function returns the midipitch at regular time interval
for bar %d xref %d\n,bar_number,xrefnos
(set by resolution) for a particular bar. Thus if you have
the notes CDEF in a bar (L=1/8), then integrated_length
will contain 12,24,36,48. If resolution is set to 6, then
pitch_samples will return the pitch at time units 0, 6, 12,...
mainly CCDDEEFF or 60,60,62,62,64,64,65,65 in midipitch.
The pitch is shifted by delta_pitch to account for a different
key signature in the matching template.
input: midipitch[],notelength,resolution,delta_pitch,nnotes
bar_number
output: pitch_samples
the function returns the number of pitch_samples i creates
*/
int integrated_length[50]; /* maximum of 50 notes in a bar */
/* integrated_length is the number of time units in the bar after note i;
*/
int offset, lastnote, lastpulse, lastsample;
int i, j, t;
offset = barlineptr[bar_number];
/* double bar is always placed at the beginning of the tune */
i = 1;
integrated_length[0] = notelength[offset];
lastnote = 0;
while (notelength[i + offset] != BAR)
{
if (notelength[i + offset] > 288)
return -1; /* don't try to handle notes longer than 2 whole */
integrated_length[i] =
integrated_length[i - 1] + notelength[i + offset];
lastnote = i;
i++;
if (i + offset > nnotes)
{
/* printf("make_bar_image -- running past last note for bar %d xref %d\n",bar_number,xrefno); */
break;
}
if (i > 49)
{
printf ("make_bar_image -- bar %d has too many notes for xref %d\n",
bar_number, xrefno);
break;
}
}
lastpulse = integrated_length[lastnote];
i = 0;
j = 0;
t = 0;
while (t < lastpulse)
{
while (t >= integrated_length[i])
i++;
while (t < integrated_length[i])
{
if (midipitch[i + offset] == RESTNOTE)
pitch_samples[j] = RESTNOTE; /* REST don't transpose */
else
pitch_samples[j] = midipitch[i + offset] + delta_pitch;
j++;
t += resolution;
if (j >= 400)
{
printf
("make_bar_image -- pitch_sample is out of space for bar %d xrefno = %d\n",
bar_number, xrefno);
break;
}
}
}
lastsample = j;
t = 0;
return lastsample;
}
/* for debugging */
void
print_bars (int mbar_number, int ibar_number)
{
int i;
int ioffset, moffset;
ioffset = ibarlineptr[ibar_number];
moffset = tpbarlineptr[mbar_number];
i = 0;
while (tpmidipitch[i + moffset] != BAR)
{
printf ("%d %d\n", tpmidipitch[i + moffset], imidipitch[i + ioffset]);
i++;
}
printf ("\n");
}
#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
int levenshtein(int *s1, int *s2, int s1len, int s2len) {
int x, y, lastdiag, olddiag; /* [SS] 2015-10-08 removed unsigned */
int column[33];
for (y = 1; y <= s1len; y++)
column[y] = y;
for (x = 1; x <= s2len; x++) {
column[0] = x;
for (y = 1, lastdiag = x-1; y <= s1len; y++) {
olddiag = column[y];
column[y] = MIN3(column[y] + 1, column[y-1] + 1, lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1));
lastdiag = olddiag;
}
if (column[x] >= levdist) return levdist;
}
if (column[s1len] < levdist && s1len/(2*levdist)>= 1) return 0;
return(column[s1len]);
}
int perfect_match (int *s1, int *s2, int s1len) {
int i;
for (i=0;i<s1len;i++) {
if (s1[i] != s2[i]) return -1;
}
return 0;
}
/* absolute match - matches notes relative to key signature */
/* It is called if the resolution variable is set to 0. */
/* -------------------------------------------------------- */
int
match_notes (int mbar_number, int ibar_number, int delta_pitch)
{
int i, notes;
int ioffset, moffset;
int tplastnote,lastnote; /* for contour matching */
int deltapitch,deltapitchtp;
int string1[32],string2[32];
ioffset = ibarlineptr[ibar_number];
moffset = tpbarlineptr[mbar_number];
/*printf("ioffset = %d moffset = %d\n",ioffset,moffset);*/
i = 0;
notes = 0;
lastnote = 0;
tplastnote =0;
if (tpmidipitch[moffset] == BAR)
return -1; /* in case nothing in bar */
while (tpmidipitch[i + moffset] != BAR)
{
/*printf("%d %d\n",imidipitch[i+ioffset],tpmidipitch[i+moffset]);*/
if (imidipitch[i + ioffset] == RESTNOTE
&& tpmidipitch[i + moffset] == RESTNOTE)
{
i++;
continue;
} /* REST -- don't transpose */
if (imidipitch[i + ioffset] == -1 || tpmidipitch[i + moffset] == -1)
{
printf("unexpected negative pitch at %d or %d for xref %d\n",i+ioffset,i+moffset,xrefno);
i++;
continue;
} /* unknown contour note */
if (norhythm == 1) {
inotelength[i + ioffset] = 0;
tpnotelength[i + moffset] = 0;
}
if (con == 1) {
/* contour matching */
if (tplastnote !=0) {
deltapitchtp = tpmidipitch[i+moffset] - tplastnote;
deltapitch = imidipitch[i+ioffset] - lastnote;
tplastnote = tpmidipitch[i+moffset];
lastnote = imidipitch[i+ioffset];
if (qntflag > 0) {
deltapitch = quantize7 (deltapitch);
deltapitchtp = quantize7(deltapitchtp);
}
string1[notes] = 256*deltapitch + inotelength[i + ioffset];
string2[notes] = 256*deltapitchtp + tpnotelength[i + moffset];
if (notes < 32) notes++;
else printf("notes > 32\n");
/* printf("%d %d\n",deltapitch,deltapitchtp);*/
}
else { /* first note in bar - no lastnote */
tplastnote = tpmidipitch[i+moffset];
lastnote = imidipitch[i+ioffset];
}
}
else {
/* absolute matching (with transposition) */
/*printf("%d %d\n",imidipitch[i+ioffset],tpmidipitch[i+moffset]-delta_pitch);*/
string1[notes] = 256*imidipitch[i+ioffset] + inotelength[i + ioffset];
string2[notes] = 256*(tpmidipitch[i+moffset] - delta_pitch) + tpnotelength[i + moffset];
if (notes < 32) notes++;
else printf("notes > 32\n");
}
i++;
}
if (imidipitch[i + ioffset] != BAR)
return -1; /* in case template has fewer notes */
if (notes < 2)
return -1;
if (levdist == 0) return perfect_match(string1,string2,notes);
else return levenshtein(string1,string2,notes,notes);
}
/* absolute match - matches notes relative to key signature */
/* this matching algorithm ignores bar lines and tries to match
a fixed number of notes set by fnotes. It is unnecessary for
the time signatures in the template and tune to match. */
int
fixed_match_notes (int fnotes, int mbar_number, int ibar_number, int delta_pitch)
{
int i,j, notes;
int ioffset, moffset;
int tplastnote,lastnote; /* for contour matching */
int deltapitch,deltapitchtp;
int string1[32],string2[32];
ioffset = ibarlineptr[ibar_number];
moffset = tpbarlineptr[mbar_number];
/*printf("ioffset = %d moffset = %d\n",ioffset,moffset);*/
i = j = 0;
notes = 0;
lastnote = 0;
tplastnote =0;
while (notes < fnotes)
{
/*printf("%d %d\n",imidipitch[j+ioffset],tpmidipitch[i+moffset]);*/
if (imidipitch[j + ioffset] == RESTNOTE
|| imidipitch[j + ioffset] == BAR)
{
j++;
continue;
} /* pass over RESTS or BARS */
if (tpmidipitch[i + moffset] == RESTNOTE
|| tpmidipitch[i + moffset] == BAR)
{
i++;
continue;
} /* pass over RESTS or BARS */
if (imidipitch[j + ioffset] == -1 || tpmidipitch[i + moffset] == -1)
{
printf("unexpected negative pitch at %d or %d for xref %d\n",i+ioffset,i+moffset,xrefno);
i++;
j++;
continue;
} /* unknown contour note */
if (norhythm == 1) {
inotelength[j + ioffset] = 0;
tpnotelength[i + moffset] = 0;
}
if (con == 1) {
/* contour matching */
if (tplastnote !=0) {
deltapitchtp = tpmidipitch[i+moffset] - tplastnote;
deltapitch = imidipitch[j+ioffset] - lastnote;
tplastnote = tpmidipitch[i+moffset];
lastnote = imidipitch[j+ioffset];
if (qntflag > 0) {
deltapitch = quantize7 (deltapitch);
deltapitchtp = quantize7(deltapitchtp);
}
string1[notes] = 256*deltapitch + inotelength[j + ioffset];
string2[notes] = 256*deltapitchtp + tpnotelength[i + moffset];
if (notes < 32) notes++;
else printf("notes > 32\n");
/* printf("deltapitch %d %d\n",deltapitch,deltapitchtp);
printf("length %d %d\n",inotelength[j + ioffset],tpnotelength[i+moffset]);
*/
if (deltapitch != deltapitchtp)
return -1;
/* match succeeded */
/* printf("%d %d\n",deltapitch,deltapitchtp);*/
} else { /* first note in bar - no lastnote */
tplastnote = tpmidipitch[i+moffset];
lastnote = imidipitch[j+ioffset];
}
}
else {
/* absolute matching (with transposition) */
/*printf("%d %d\n",imidipitch[j+ioffset],tpmidipitch[i+moffset]-delta_pitch);
printf("%d %d\n",inotelength[j+ioffset],tpnotelength[i+moffset]);
*/
string1[notes] = 256*imidipitch[j+ioffset] + inotelength[j + ioffset];
string2[notes] = 256*(tpmidipitch[i+moffset] - delta_pitch) + tpnotelength[i + moffset];
if (notes < 32) notes++;
else printf("notes > 32\n");
}
i++;
j++;
}
if (notes < 2)
return -1;
/*printf("ioffset = %d moffset = %d\n",ioffset,moffset);*/
if (levdist == 0) return perfect_match(string1,string2,notes);
else return levenshtein(string1,string2,notes,notes);
}
/* for debugging */
void
print_bar_samples (int mmsamples, int *mmpitch_samples)
{
int i;
for (i = 0; i < mmsamples; i++)
printf ("%d %d\n", mmpitch_samples[i], ipitch_samples[i]);
}
int
match_samples (int mmsamples, int *mmpitch_samples)
{
int i;
int changes;
int last_sample;
int string1[32],string2[32];
int j;
if (mmsamples != isamples)
return -1;
changes = 0;
last_sample = ipitch_samples[0]; /* [SS] 2012-02-05 */
j = 0;
for (i = 0; i < mmsamples; i++)
{
if (ipitch_samples[i] == -1 || mmpitch_samples[i] == -1)
continue; /* unknown contour note (ie. 1st note) */
string1[j] = ipitch_samples[i];
string2[j] = mmpitch_samples[i];
j++;
if (last_sample != ipitch_samples[i])
{
last_sample = ipitch_samples[i];
changes++;
}
}
if (ignore_simple && changes < 3)
return -1;
if (levdist == 0) return perfect_match(string1,string2,j);
else return levenshtein(string1,string2,j,j);
}
int
match_any_bars (int tpbars, int barnum, int delta_key, int nmatches)
{
/* This function reports any bars in match template match a particular
* bar (barnum) in the tune. If a match is found then barnum is
* reported. It runs in one of two modes depending on the value
* of the variable called resolution.
* The template is not passed as an argument but is accessed from
* the global arrays, tpmidipitch[1000] and tpnotelength[1000].
*/
int kmatches;
int moffset, j, dif;
/* for every bar in match sample */
kmatches = nmatches;
moffset = 0;
if (resolution > 0)
{
isamples = make_bar_image (barnum, resolution,
ibarlineptr, inotelength, innotes, delta_key,
imidipitch,ipitch_samples);
if (isamples < 1)
return kmatches;
if (con == 1)
difference_midipitch (ipitch_samples, isamples);
for (j = 0; j < tpbars; j++)
{
dif = match_samples (msamples[j], mpitch_samples + moffset);
#if 0 /* debugging */
printf("bar %d\n",j);
print_bar_samples(msamples[j],mpitch_samples+moffset);
/*printf("dif = %d\n\n",dif);*/
#endif
moffset += msamples[j];
if (dif == 0)
{
if (tpxref > 0) tpbarstatus[j] = 1;
kmatches++;
if (kmatches == 1)
printf ("%d %d %d ", fileindex, xrefno, barnum);
/* subtract one from bar because first bar always seems to be 2 */
else
printf (" %d ", barnum );
break;
}
}
}
else /* exact match */
{
for (j = 0; j < tpbars; j++)
{
if (fixednumberofnotes)
dif = fixed_match_notes (fixednumberofnotes,j, barnum, delta_key);
else
dif = match_notes (j, barnum, delta_key);
if (dif == 0)
{
if (tpxref > 0) tpbarstatus[j] = 1;
kmatches++;
if (kmatches == 1)
printf ("%d %d %d ", fileindex, xrefno, barnum);
else
printf (" %d ", barnum );
break;
} /* dif condition */
} /*for loop */
} /* resolution condition */
return kmatches;
}
int
match_all_bars (int tpbars, int barnum, int delta_key, int nmatches)
{
/* This function tries to match all the bars in the match template
with the bars in the bars in the tune. All the template bars
must match in the same sequence in order to be reported.
It runs in one of two modes depending on the value of resolution.
*/
int kmatches;
int moffset, j, dif;
int matched_bars[20];
/* for every bar in match sample */
kmatches = 0;
moffset = 0;
if (resolution > 0)
for (j = 0; j < tpbars; j++)
{
isamples = make_bar_image (barnum + j, resolution,
ibarlineptr, inotelength, innotes,
delta_key, imidipitch ,ipitch_samples);
if (con == 1)
difference_midipitch (ipitch_samples, isamples);
dif = match_samples (msamples[j], mpitch_samples + moffset);
moffset += msamples[j];
if (dif != 0)
return nmatches;
matched_bars[kmatches] = barnum + j ;
kmatches++;
if (j > 15)
break;
}
else
for (j = 0; j < tpbars; j++)
{
if (fixednumberofnotes)
dif = fixed_match_notes (fixednumberofnotes,j, barnum + j, delta_key);
else
dif = match_notes (j, barnum + j, delta_key);
if (dif != 0)
return nmatches;
matched_bars[kmatches] = barnum + j ;