-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathallegro.cpp
executable file
·3576 lines (3187 loc) · 112 KB
/
allegro.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
// Allegro: music representation system, with
// extensible in-memory sequence structure
// upward compatible with MIDI
// implementations in C++ and Serpent
// external, text-based representation
// compatible with Aura
//
/* CHANGE LOG:
04 apr 03 -- fixed bug in add_track that caused infinite loop
*/
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "memory.h"
#include <iostream>
#include <fstream>
#include <inttypes.h>
using namespace std;
#include "allegro.h"
#include "algrd_internal.h"
#include "algsmfrd_internal.h"
// #include "trace.h" -- only needed for debugging
#include "math.h"
#define STREQL(x, y) (strcmp(x, y) == 0)
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define ROUND(x) ((int) ((x) + 0.5))
// 4311 is type cast ponter to long warning
// 4996 is warning against strcpy
// 4267 is size_t to long warning
#pragma warning(disable: 4311 4996 4267)
Alg_atoms symbol_table;
Serial_read_buffer Alg_track::ser_read_buf; // declare the static variables
Serial_write_buffer Alg_track::ser_write_buf;
bool within(double d1, double d2, double epsilon)
{
d1 -= d2;
return d1 < epsilon && d1 > -epsilon;
}
char *heapify(const char *s)
{
char *h = new char[strlen(s) + 1];
strcpy(h, s);
return h;
}
void Alg_atoms::expand()
{
maxlen = (maxlen + 5); // extra growth for small sizes
maxlen += (maxlen >> 2); // add 25%
Alg_attribute *new_atoms = new Alg_attribute[maxlen];
// now do copy
memcpy(new_atoms, atoms, len * sizeof(Alg_attribute));
if (atoms) delete[] atoms;
atoms = new_atoms;
}
// insert_new -- insert an attribute name and type
//
// attributes are stored as a string consisting of the type
// (a char) followed by the attribute name. This makes it
// easy to retrieve the type or the name or both.
//
Alg_attribute Alg_atoms::insert_new(const char *name, char attr_type)
{
if (len == maxlen) expand();
char *h = new char[strlen(name) + 2];
strcpy(h + 1, name);
*h = attr_type;
atoms[len++] = h;
return h;
}
Alg_attribute Alg_atoms::insert_attribute(Alg_attribute attr)
{
// should use hash algorithm
for (int i = 0; i < len; i++) {
if (STREQL(attr, atoms[i])) {
return atoms[i];
}
}
return insert_new(attr + 1, attr[0]);
}
Alg_attribute Alg_atoms::insert_string(const char *name)
{
char attr_type = name[strlen(name) - 1];
for (int i = 0; i < len; i++) {
if (attr_type == atoms[i][0] &&
STREQL(name, atoms[i] + 1)) {
return atoms[i];
}
}
return insert_new(name, attr_type);
}
void Alg_parameter::copy(Alg_parameter_ptr parm)
{
*this = *parm; // copy all fields
// if the value is a string, copy the string
if (attr_type() == 's') {
s = heapify(s);
}
}
void Alg_parameter::show()
{
switch (attr[0]) {
case 'r':
printf("%s:%g", attr_name(), r);
break;
case 's':
printf("%s:%s", attr_name(), s);
break;
case 'i':
printf("%s:%lld", attr_name(), i);
break;
case 'l':
printf("%s:%s", attr_name(), (l ? "t" : "f"));
break;
case 'a':
printf("%s:%s", attr_name(), a);
break;
}
}
Alg_parameter::~Alg_parameter()
{
if (attr_type() == 's' && s) {
delete[] s;
}
}
void Alg_parameters::insert_real(Alg_parameters **list, const char *name,
double r)
{
Alg_parameters_ptr a = new Alg_parameters(*list);
*list = a;
a->parm.set_attr(symbol_table.insert_string(name));
a->parm.r = r;
assert(a->parm.attr_type() == 'r');
}
void Alg_parameters::insert_string(Alg_parameters **list, const char *name,
const char *s)
{
Alg_parameters_ptr a = new Alg_parameters(*list);
*list = a;
a->parm.set_attr(symbol_table.insert_string(name));
// string is deleted when parameter is deleted
a->parm.s = heapify(s);
assert(a->parm.attr_type() == 's');
}
void Alg_parameters::insert_int64(Alg_parameters **list, const char *name,
int64 i)
{
Alg_parameters_ptr a = new Alg_parameters(*list);
*list = a;
a->parm.set_attr(symbol_table.insert_string(name));
a->parm.i = i;
assert(a->parm.attr_type() == 'i');
}
void Alg_parameters::insert_logical(Alg_parameters **list, const char *name,
bool l)
{
Alg_parameters_ptr a = new Alg_parameters(*list);
*list = a;
a->parm.set_attr(symbol_table.insert_string(name));
a->parm.l = l;
assert(a->parm.attr_type() == 'l');
}
void Alg_parameters::insert_atom(Alg_parameters **list, const char *name,
const char *s)
{
Alg_parameters_ptr a = new Alg_parameters(*list);
*list = a;
a->parm.set_attr(symbol_table.insert_string(name));
a->parm.a = symbol_table.insert_string(s);
assert(a->parm.attr_type() == 'a');
}
Alg_parameters *Alg_parameters::remove_key(Alg_parameters **list,
const char *name)
{
while (*list) {
if (STREQL((*list)->parm.attr_name(), name)) {
Alg_parameters_ptr p = *list;
*list = p->next;
p->next = NULL;
return p; // caller should free this pointer
}
list = &((*list)->next);
}
return NULL;
}
Alg_parameter_ptr Alg_parameters::find(Alg_attribute attr)
{
assert(attr);
Alg_parameters_ptr temp = this;
while (temp) {
if (temp->parm.attr == attr) {
return &(temp->parm);
}
}
return NULL;
}
int Alg_event::get_type_code()
{
if (!is_note()) {
const char* attr = get_attribute();
if (STREQL(attr, "gater")) // volume change
return ALG_GATE;
if (STREQL(attr, "bendr")) // pitch bend
return ALG_BEND;
if (strncmp(attr, "control", 7) == 0) // control change
// note that midi control changes have attributes of the form
// "control<n>" where n is the decimal number (as a character string)
// of the midi controller, e.g. control2 is the breath controller.
// We don't check for decimal numbers in the range 0-127, so any
// attribute that begins with "control" is an ALG_CONTROL:
return ALG_CONTROL;
if (STREQL(attr, "programi")) // program change
return ALG_PROGRAM;
if (STREQL(attr, "pressurer")) // pressure change
return ALG_PRESSURE;
if (STREQL(attr, "keysigi")) // key signature
return ALG_KEYSIG;
if (STREQL(attr, "timesig_numi")) // time signature numerator
return ALG_TIMESIG_NUM;
if (STREQL(attr, "timesig_deni")) // time signature denominator
return ALG_TIMESIG_DEN;
return ALG_OTHER;
}
return ALG_NOTE; // it is a note
}
void Alg_event::set_parameter(Alg_parameter_ptr new_parameter)
{
Alg_parameter_ptr parm;
if (is_note()) {
Alg_note_ptr note = (Alg_note_ptr) this;
parm = note->parameters->find(new_parameter->attr);
if (!parm) {
note->parameters = new Alg_parameters(note->parameters);
parm = &(note->parameters->parm);
}
} else { // update
Alg_update_ptr update = (Alg_update_ptr) this;
parm = &(update->parameter);
}
parm->copy(new_parameter); // copy entire parameter
}
void Alg_event::set_string_value(const char *a, const char *value)
{
assert(a); // must be non-null
Alg_attribute attr = symbol_table.insert_string(a);
assert(attr[0] == 's');
Alg_parameter parm;
parm.set_attr(attr);
parm.s = value;
set_parameter(&parm);
parm.s = NULL; // do this to prevent string from being freed
}
void Alg_event::set_real_value(const char *a, double value)
{
assert(a); // must be non-null
// attr is like a, but it has the type code prefixed for
// fast lookup, and it is a unique string in symbol_table
// e.g. a="attackr" -> attr="rattackr"
Alg_attribute attr = symbol_table.insert_string(a);
assert(attr[0] == 'r');
Alg_parameter parm;
parm.set_attr(attr);
parm.r = value;
set_parameter(&parm);
// since type is 'r' we don't have to NULL the string
}
void Alg_event::set_logical_value(const char *a, bool value)
{
assert(a); // must be non-null
Alg_attribute attr = symbol_table.insert_string(a);
assert(attr[0] == 'l');
Alg_parameter parm;
parm.set_attr(attr);
parm.l = value;
set_parameter(&parm);
// since type is 'l' we don't have to NULL the string
}
void Alg_event::set_int64_value(const char *a, int64 value)
{
assert(a); // must be non-null
Alg_attribute attr = symbol_table.insert_string(a);
assert(attr[0] == 'i');
Alg_parameter parm;
parm.set_attr(attr);
parm.i = value;
set_parameter(&parm);
// since tpye is 'i' we don't have to NULL the string
}
void Alg_event::set_atom_value(const char *a, const char *value)
{
assert(a); // must be non-null
Alg_attribute attr = symbol_table.insert_string(a);
assert(attr[0] == 'a');
Alg_parameter parm;
parm.set_attr(attr);
parm.a = value;
set_parameter(&parm);
/* since type is 'a' we don't have to null the string */
}
float Alg_event::get_pitch()
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
return note->pitch;
}
float Alg_event::get_loud()
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
return note->loud;
}
double Alg_event::get_start_time()
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
return note->time;
}
double Alg_event::get_end_time()
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
return note->time + note->dur;
}
double Alg_event::get_duration()
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
return note->dur;
}
void Alg_event::set_pitch(float p)
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
note->pitch = p;
}
void Alg_event::set_loud(float l)
{
assert(is_note());
Alg_note *note = (Alg_note *) this;
note->loud = l;
}
void Alg_event::set_duration(double d)
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
note->dur = d;
}
bool Alg_event::has_attribute(const char *a)
{
assert(is_note());
assert(a); // must be non-null
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
Alg_parameter_ptr parm = note->parameters->find(attr);
return parm != NULL;
}
char Alg_event::get_attribute_type(const char *a)
{
assert(is_note());
assert(a);
return a[strlen(a) - 1];
}
const char *Alg_event::get_string_value(const char *a, const char *value)
{
assert(is_note());
assert(a); // must be non-null
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
assert(a[0] == 's'); // must be of type string
Alg_parameter_ptr parm = note->parameters->find(attr);
if (parm) return parm->s;
return value;
}
double Alg_event::get_real_value(const char *a, double value)
{
assert(is_note());
assert(a);
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
assert(a[0] == 'r'); // must be of type real
Alg_parameter_ptr parm = note->parameters->find(attr);
if (parm) return parm->r;
return value;
}
bool Alg_event::get_logical_value(const char *a, bool value)
{
assert(is_note());
assert(a);
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
assert(a[0] == 'l'); // must be of type logical
Alg_parameter_ptr parm = note->parameters->find(attr);
if (parm) return parm->l;
return value;
}
int64 Alg_event::get_int64_value(const char *a, int64 value)
{
assert(is_note());
assert(a);
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
assert(a[0] == 'i'); // must be of type integer
Alg_parameter_ptr parm = note->parameters->find(attr);
if (parm) return parm->i;
return value;
}
const char *Alg_event::get_atom_value(const char *a, const char *value)
{
assert(is_note());
assert(a);
Alg_note* note = (Alg_note *) this;
Alg_attribute attr = symbol_table.insert_string(a);
assert(a[0] == 'a'); // must be of type atom
Alg_parameter_ptr parm = note->parameters->find(attr);
if (parm) return parm->a;
// if default is a string, convert to an atom (unique
// string in symbol table) and return it
return (value == NULL ? NULL :
symbol_table.insert_string(value));
}
void Alg_event::delete_attribute(const char *a)
{
assert(is_note());
Alg_note* note = (Alg_note *) this;
Alg_parameters::remove_key(&(note->parameters), a);
}
const char *Alg_event::get_attribute()
// Note: this returns a string, not an Alg_attribute
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
return update->parameter.attr_name();
}
char Alg_event::get_update_type()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
return update->parameter.attr_type();
}
const char *Alg_event::get_string_value()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
assert(get_update_type() == 's');
return update->parameter.s;
}
double Alg_event::get_real_value()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
assert(get_update_type() == 'r');
return update->parameter.r;
}
bool Alg_event::get_logical_value()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
assert(get_update_type() == 'l');
return update->parameter.l;
}
int64 Alg_event::get_int64_value()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
assert(get_update_type() == 'i');
return update->parameter.i;
}
const char *Alg_event::get_atom_value()
{
assert(is_update());
Alg_update* update = (Alg_update *) this;
assert(get_update_type() == 'a');
return update->parameter.a;
}
bool Alg_event::overlap(double t, double len, bool all)
{
// event starts within region
if (time >= t && time <= t + len - ALG_EPS)
return true;
if (all && is_note()) {
double dur = ((Alg_note_ptr) this)->dur;
// note overlaps with region
if (time < t && time + dur - ALG_EPS > t)
return true;
}
// does not overlap
return false;
}
Alg_note::Alg_note(Alg_note_ptr note)
{
*this = *note; // copy all fields
// parameters is now a shared pointer. We need to copy the
// parameters
Alg_parameters_ptr next_param_ptr = parameters;
while (next_param_ptr) {
Alg_parameters_ptr new_params = new Alg_parameters(next_param_ptr->next);
new_params->parm.copy(&(next_param_ptr->parm)); // copy the attribute and value
next_param_ptr = new_params->next;
}
}
Alg_note::~Alg_note()
{
while (parameters) {
Alg_parameters_ptr to_delete = parameters;
parameters = parameters->next;
delete to_delete;
}
}
void Alg_note::show()
{
printf("Alg_note: time %g, chan %d, dur %g, key %d, "
"pitch %g, loud %g, attributes ",
time, chan, dur, key, pitch, loud);
Alg_parameters_ptr parms = parameters;
while (parms) {
parms->parm.show();
printf(" ");
parms = parms->next;
}
printf("\n");
}
Alg_update::Alg_update(Alg_update_ptr update)
{
*this = *update; // copy all fields
// parameter requires careful copy to possibly duplicate string value:
this->parameter.copy(&(update->parameter));
}
void Alg_update::show()
{
printf("Alg_update: ");
parameter.show();
printf("\n");
}
void Alg_events::expand()
{
maxlen = (maxlen + 5); // extra growth for small sizes
maxlen += (maxlen >> 2); // add 25%
Alg_event_ptr *new_events = new Alg_event_ptr[maxlen];
// now do copy
memcpy(new_events, events, len * sizeof(Alg_event_ptr));
if (events) delete[] events;
events = new_events;
}
void Alg_events::insert(Alg_event_ptr event)
{
if (maxlen <= len) {
expand();
}
// Note: if the new event is the last one, the assignment
// events[i] = event; (below) will never execute, so just
// in case, we do the assignment here. events[len] will
// be replaced during the memmove() operation below if
// this is not the last event.
events[len] = event;
len++;
// find insertion point: (this could be a binary search)
for (int i = 0; i < len; i++) {
if (events[i]->time > event->time) {
// insert event at i
memmove(&events[i + 1], &events[i],
sizeof(Alg_event_ptr) * (len - i - 1));
events[i] = event;
return;
}
}
}
Alg_event_ptr Alg_events::uninsert(int index)
{
assert(0 <= index && index < len);
Alg_event_ptr event = events[index];
//printf("memmove: %x from %x (%d)\n", events + index, events + index + 1,
// sizeof(Alg_event_ptr) * (len - index - 1));
memmove(events + index, events + index + 1,
sizeof(Alg_event_ptr) * (len - index - 1));
len--;
return event;
}
void Alg_events::append(Alg_event_ptr event)
{
if (maxlen <= len) {
expand();
}
events[len++] = event;
// keep track of last note_off time
if (event->is_note()) {
Alg_note_ptr note = (Alg_note_ptr) event;
double note_off = note->time + note->dur;
if (note_off > last_note_off)
last_note_off = note_off;
}
}
Alg_events::~Alg_events()
{
assert(!in_use);
// individual events are not deleted, only the array
if (events) {
delete[] events;
}
}
Alg_event_list::Alg_event_list(Alg_track *owner)
{
events_owner = owner;
sequence_number = owner->sequence_number;
beat_dur = 0.0; real_dur = 0.0; type = 'e';
}
Alg_event_ptr &Alg_event_list::operator [](int i)
{
assert(i >= 0 && i < len);
return events[i];
}
Alg_event_list::~Alg_event_list()
{
// note that the events contained in the list are not destroyed
}
void Alg_event_list::set_start_time(Alg_event *event, double t)
{
// For Alg_event_list, find the owner and do the update there
// For Alg_track, change the time and move the event to the right place
// For Alg_seq, find the track and do the update there
int index, i;
Alg_track_ptr track_ptr;
if (type == 'e') { // this is an Alg_event_list
// make sure the owner has not changed its event set
assert(events_owner &&
sequence_number == events_owner->sequence_number);
// do the update on the owner
events_owner->set_start_time(event, t);
return;
} else if (type == 't') { // this is an Alg_track
// find the event in the track
track_ptr = (Alg_track_ptr) this;
// this should be a binary search since events are in time order
// probably there should be member function to do the search
for (index = 0; index < length(); index++) {
if ((*track_ptr)[index] == event) goto found_event;
}
} else { // type == 's', an Alg_seq
Alg_seq_ptr seq = (Alg_seq_ptr) this;
for (i = 0; i < seq->tracks(); i++) {
track_ptr = seq->track(i);
// if you implemented binary search, you could call it
// instead of this loop too.
for (index = 0; index < track_ptr->length(); index++) {
if ((*track_ptr)[index] == event) goto found_event;
}
}
}
assert(false); // event not found seq or track!
found_event:
// at this point, track[index] == event
// we could be clever and figure out exactly what notes to move
// but it is simpler to just remove the event and reinsert it:
track_ptr->uninsert(index);
event->time = t;
track_ptr->insert(event);
}
void Alg_beats::expand()
{
maxlen = (maxlen + 5); // extra growth for small sizes
maxlen += (maxlen >> 2); // add 25%
Alg_beat_ptr new_beats = new Alg_beat[maxlen];
// now do copy
memcpy(new_beats, beats, len * sizeof(Alg_beat));
if (beats) delete[] beats;
beats = new_beats;
}
void Alg_beats::insert(int i, Alg_beat_ptr beat)
{
assert(i >= 0 && i <= len);
if (maxlen <= len) {
expand();
}
memmove(&beats[i + 1], &beats[i], sizeof(Alg_beat) * (len - i));
memcpy(&beats[i], beat, sizeof(Alg_beat));
len++;
}
Alg_time_map::Alg_time_map(Alg_time_map *map)
{
refcount = 0;
assert(map->beats[0].beat == 0 && map->beats[0].time == 0);
assert(map->beats.len > 0);
// new_beats[0] = map->beats[0];
// this is commented because
// both new_beats[0] and map->beats[0] should be (0, 0)
for (int i = 1; i < map->beats.len; i++) {
beats.insert(i, &map->beats[i]);
}
last_tempo = map->last_tempo;
last_tempo_flag = map->last_tempo_flag;
}
void Alg_time_map::show()
{
printf("Alg_time_map: ");
for (int i = 0; i < beats.len; i++) {
Alg_beat &b = beats[i];
printf("(%g, %g) ", b.time, b.beat);
}
printf("last tempo: %g\n", last_tempo);
}
int Alg_time_map::locate_time(double time)
{
int i = 0;
while ((i < beats.len) && (time > beats[i].time)) {
i++;
}
return i;
}
int Alg_time_map::locate_beat(double beat)
{
int i = 0;
while ((i < beats.len) && (beat > beats[i].beat)) {
i++;
}
return i;
}
double Alg_time_map::beat_to_time(double beat)
{
Alg_beat_ptr mbi;
Alg_beat_ptr mbi1;
if (beat <= 0) {
return beat;
}
int i = locate_beat(beat);
// case 1: beat is between two time/beat pairs
if (0 < i && i < beats.len) {
mbi = &beats[i - 1];
mbi1 = &beats[i];
// case 2: beat is beyond last time/beat pair
} else if (i == beats.len) {
if (last_tempo_flag) {
return beats[i - 1].time +
(beat - beats[i - 1].beat) / last_tempo;
} else if (i == 1) {
return beat * 60.0 / ALG_DEFAULT_BPM;
// so we use that as default allegro tempo too
} else {
mbi = &beats[i - 2];
mbi1 = &beats[i - 1];
}
// case 3: beat is at time 0
} else /* if (i == 0) */ {
return beats[0].time;
}
// whether we extrapolate or interpolate, the math is the same
double time_dif = mbi1->time - mbi->time;
double beat_dif = mbi1->beat - mbi->beat;
return mbi->time + (beat - mbi->beat) * time_dif / beat_dif;
}
double Alg_time_map::time_to_beat(double time)
{
Alg_beat_ptr mbi;
Alg_beat_ptr mbi1;
if (time <= 0.0) return time;
int i = locate_time(time);
if (i == beats.len) {
if (last_tempo_flag) {
return beats[i - 1].beat +
(time - beats[i - 1].time) * last_tempo;
} else if (i == 1) {
return time * (ALG_DEFAULT_BPM / 60.0);
} else {
mbi = &beats[i - 2];
mbi1 = &beats[i - 1];
}
} else {
mbi = &beats[i - 1];
mbi1 = & beats[i];
}
double time_dif = mbi1->time - mbi->time;
double beat_dif = mbi1->beat - mbi->beat;
return mbi->beat + (time - mbi->time) * beat_dif / time_dif;
}
void Alg_time_map::insert_beat(double time, double beat)
{
int i = locate_time(time); // i is insertion point
if (i < beats.len && within(beats[i].time, time, 0.000001)) {
// replace beat if time is already in the map
beats[i].beat = beat;
} else {
Alg_beat point;
point.beat = beat;
point.time = time;
beats.insert(i, &point);
}
// beats[i] contains new beat
// make sure we didn't generate a zero tempo.
// if so, space beats by one microbeat as necessary
int j = i;
if (j == 0) j = 1; // do not adjust beats[0]
while (j < beats.len &&
beats[j - 1].beat + 0.000001 >= beats[j].beat) {
beats[j].beat = beats[j - 1].beat + 0.000001;
j++;
}
}
bool Alg_time_map::insert_tempo(double tempo, double beat)
{
tempo = tempo / 60.0; // convert to beats per second
// change the tempo at the given beat until the next beat event
if (beat < 0) return false;
double time = beat_to_time(beat);
int i = locate_time(time);
if (i >= beats.len || !within(beats[i].time, time, 0.000001)) {
insert_beat(time, beat);
}
// now i is index of beat where tempo will change
if (i == beats.len - 1) {
last_tempo = tempo;
// printf("last_tempo to %g\n", last_tempo);
last_tempo_flag = true;
} else { // adjust all future beats
// compute the difference in beats
double diff = beats[i + 1].beat - beats[i].beat;
// convert beat difference to seconds at new tempo
diff = diff / tempo;
// figure out old time difference:
double old_diff = beats[i + 1].time - time;
// compute difference too
diff = diff - old_diff;
// apply new_diff to score and beats
i++;
while (i < beats.len) {
beats[i].time = beats[i].time + diff;
i++;
}
}
return true;
}
double Alg_time_map::get_tempo(double beat)
{
Alg_beat_ptr mbi;
Alg_beat_ptr mbi1;
// if beat < 0, there is probably an error; return something nice anyway
if (beat < 0) return ALG_DEFAULT_BPM / 60.0;
int i = locate_beat(beat);
// this code is similar to beat_to_time() so far, but we want to get
// beyond beat if possible because we want the tempo FOLLOWING beat
// (Consider the case beat == 0.0)
if (i < beats.len && beat >= beats[i].beat) i++;
// case 1: beat is between two time/beat pairs
if (i < beats.len) {
mbi = &beats[i - 1];
mbi1 = &beats[i];
// case 2: beat is beyond last time/beat pair
} else /* if (i == beats.len) */ {
if (last_tempo_flag) {
return last_tempo;
} else if (i == 1) {
return ALG_DEFAULT_BPM / 60.0;
} else {
mbi = &beats[i - 2];
mbi1 = &beats[i - 1];
}
}
double time_dif = mbi1->time - mbi->time;