Skip to content

Latest commit

 

History

History
4886 lines (4715 loc) · 132 KB

load_abc.cpp

File metadata and controls

4886 lines (4715 loc) · 132 KB
 
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
/*
MikMod Sound System
By Jake Stine of Divine Entertainment (1996-2000)
Support:
If you find problems with this code, send mail to:
air@divent.org
Distribution / Code rights:
Use this source code in any fashion you see fit. Giving me credit where
credit is due is optional, depending on your own levels of integrity and
honesty.
-----------------------------------------
Module: LOAD_ABC
ABC module loader.
by Peter Grootswagers (2006)
<email:pgrootswagers@planet.nl>
Portability:
All systems - all compilers (hopefully)
*/
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifndef _WIN32
Dec 4, 2019
Dec 4, 2019
34
35
#include <unistd.h> /* sleep() */
#endif
36
37
38
39
#include "stdafx.h"
#include "sndfile.h"
Nov 28, 2019
Nov 28, 2019
40
41
42
43
44
45
46
47
48
#ifndef MIDIFMT_SUPPORT
BOOL CSoundFile::TestABC(const BYTE *lpStream, DWORD dwMemLength) {
return FALSE;
}
BOOL CSoundFile::ReadABC(const BYTE *lpStream, DWORD dwMemLength) {
return FALSE;
}
#else
Oct 23, 2018
Oct 23, 2018
49
50
51
#include "load_pat.h"
Oct 23, 2018
Oct 23, 2018
52
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
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
#define putenv _putenv
#define strdup _strdup
#endif
#define MAXABCINCLUDES 8
#define MAXCHORDNAMES 80
#define ABC_ENV_DUMPTRACKS "MMABC_DUMPTRACKS"
#define ABC_ENV_NORANDOMPICK "MMABC_NO_RANDOM_PICK"
// gchords use tracks with vpos 1 thru 7
// drums use track with vpos 8
// voice chords use vpos 0 and vpos from 11 up
#define GCHORDBPOS 1
#define GCHORDFPOS 2
#define GCHORDCPOS 3
#define DRUMPOS 8
#define DRONEPOS1 9
#define DRONEPOS2 10
// in the patterns a whole note at unmodified tempo is 16 rows
#define ROWSPERNOTE 16
// a 1/64-th note played in triool equals a 1/96-th note, to be able
// to play them and also to play the 1/64-th we need a resolution of 192
// because 2/192 = 1/96 and 3/192 = 1/64
#define RESOLUTION 192
/**********************************************************************/
#pragma pack(1)
typedef enum {
note,
octave,
smpno,
volume,
effect,
effoper
} ABCEVENT_X_NOTE;
typedef enum {
none,
trill,
bow,
accent
} ABCEVENT_X_EFFECT;
typedef enum {
cmdflag,
command,
chordnum,
chordnote,
chordbase,
jumptype
} ABCEVENT_X_CMD;
typedef enum {
cmdsegno = '$',
cmdcapo = 'B',
cmdchord = 'C',
cmdfine = 'F',
cmdhide = 'H',
cmdjump = 'J',
cmdloop = 'L',
cmdcoda = 'O',
cmdpartbrk = 'P',
cmdsync = 'S',
cmdtempo = 'T',
cmdvariant = 'V',
cmdtocoda = 'X'
} ABCEVENT_CMD;
typedef enum {
jumpnormal,
jumpfade,
jumpdacapo,
jumpdcfade,
jumpdasegno,
jumpdsfade,
jumpfine,
jumptocoda,
jumpvariant,
jumpnot
} ABCEVENT_JUMPTYPE;
typedef struct _ABCEVENT
{
struct _ABCEVENT *next;
uint32_t tracktick;
union {
uint8_t par[6];
struct {
uint8_t flg;
uint8_t cmd;
uint32_t lpar; // for variant selections, bit pattern
};
};
uint8_t part;
uint8_t tiednote;
} ABCEVENT;
typedef struct _ABCTRACK
{
struct _ABCTRACK *next;
ABCEVENT *head;
ABCEVENT *tail;
ABCEVENT *capostart;
ABCEVENT *tienote;
int transpose;
int octave_shift;
uint32_t slidevoltime; // for crescendo and diminuendo
int slidevol; // -2:fade away, -1:diminuendo, 0:none, +1:crescendo
uint8_t vno; // 0 is track is free for use, from previous song in multi-songbook
uint8_t vpos; // 0 is main voice, other is subtrack for gchords, gchords or drumnotes
uint8_t tiedvpos;
uint8_t mute;
uint8_t chan; // 10 is percussion channel, any other is melodic channel
uint8_t volume;
uint8_t instr; // current instrument for this track
uint8_t legato;
char v[22]; // first twenty characters are significant
} ABCTRACK;
typedef struct _ABCMACRO
{
struct _ABCMACRO *next;
char *name;
char *subst;
char *n;
} ABCMACRO;
typedef struct _ABCHANDLE
{
ABCMACRO *macro;
ABCMACRO *umacro;
ABCTRACK *track;
long int pickrandom;
unsigned int len;
int speed;
char *line;
char *beatstring;
uint8_t beat[4]; // a:first note, b:strong notes, c:weak notes, n:strong note every n
char gchord[80]; // last setting for gchord
char drum[80]; // last setting for drum
char drumins[80]; // last setting for drum
char drumvol[80]; // last setting for drum
uint32_t barticks;
// parse variables, declared here to avoid parameter pollution
int abcchordvol, abcchordprog, abcbassvol, abcbassprog;
int ktrans;
int drumon, gchordon, droneon;
int dronegm, dronepitch[2], dronevol[2];
ABCTRACK *tp, *tpc, *tpr;
uint32_t tracktime;
} ABCHANDLE;
#pragma pack()
/**********************************************************************/
static int global_voiceno, global_octave_shift, global_tempo_factor, global_tempo_divider;
static char global_part;
static uint32_t global_songstart;
/* Named guitar chords */
static char chordname[MAXCHORDNAMES][8];
static int chordnotes[MAXCHORDNAMES][6];
static int chordlen[MAXCHORDNAMES];
static int chordsnamed = 0;
static const char *sig[] = {
" C D EF G A Bc d ef g a b", // 7 sharps C#
" C D EF G AB c d ef g ab ", // 6 sharps F#
" C DE F G AB c de f g ab ", // 5 sharps B
" C DE F GA B c de f ga b ", // 4 sharps E
" CD E F GA B cd e f ga b ", // 3 sharps A
" CD E FG A B cd e fg a b ", // 2 sharps D
" C D E FG A Bc d e fg a b", // 1 sharps G
" C D EF G A Bc d ef g a b", // 0 sharps C
" C D EF G AB c d ef g ab ", // 1 flats F
" C DE F G AB c de f g ab ", // 2 flats Bb
" C DE F GA B c de f ga b ", // 3 flats Eb
" CD E F GA B cd e f ga b ", // 4 flats Ab
" CD E FG A B cd e fg a b ", // 5 flats Db
"C D E FG A Bc d e fg a b ", // 6 flats Gb
"C D EF G A Bc d ef g a b ", // 7 flats Cb
// 0123456789012345678901234
};
static const char *keySigs[] = {
/* 0....:....1....:....2....:....3....:....4....:....5. */
"7 sharps: C# A#m G#Mix D#Dor E#Phr F#Lyd B#Loc ",
"6 sharps: F# D#m C#Mix G#Dor A#Phr BLyd E#Loc ",
"5 sharps: B G#m F#Mix C#Dor D#Phr ELyd A#Loc ",
"4 sharps: E C#m BMix F#Dor G#Phr ALyd D#Loc ",
"3 sharps: A F#m EMix BDor C#Phr DLyd G#Loc ",
"2 sharps: D Bm AMix EDor F#Phr GLyd C#Loc ",
"1 sharp : G Em DMix ADor BPhr CLyd F#Loc ",
"0 sharps: C Am GMix DDor EPhr FLyd BLoc ",
"1 flat : F Dm CMix GDor APhr BbLyd ELoc ",
"2 flats : Bb Gm FMix CDor DPhr EbLyd ALoc ",
"3 flats : Eb Cm BbMix FDor GPhr AbLyd DLoc ",
"4 flats : Ab Fm EbMix BbDor CPhr DbLyd GLoc ",
"5 flats : Db Bbm AbMix EbDor FPhr GbLyd CLoc ",
"6 flats : Gb Ebm DbMix AbDor BbPhr CbLyd FLoc ",
"7 flats : Cb Abm GbMix DbDor EbPhr FbLyd BbLoc ",
0
};
// local prototypes
static int abc_getnumber(const char *p, int *number);
static ABCTRACK *abc_locate_track(ABCHANDLE *h, const char *voice, int pos);
static void abc_add_event(ABCHANDLE *h, ABCTRACK *tp, ABCEVENT *e);
static void abc_add_setloop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime);
static void abc_add_setjumploop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, ABCEVENT_JUMPTYPE j);
static uint32_t abc_pattracktime(ABCHANDLE *h, uint32_t tracktime);
static int abc_patno(ABCHANDLE *h, uint32_t tracktime);
static int abc_isvalidchar(char c) {
return(isalpha(c) || isdigit(c) || isspace(c) || c == '%' || c == ':');
}
#if 0
static const char *abc_skipspace(const char *p)
{
while (*p && isspace(*p))
p++;
return p;
}
#endif
static void abc_extractkeyvalue(char *key, size_t key_max,
char *value, size_t value_max, const char *src)
{
while (*src && isspace(*src))
src++;
size_t key_size;
for (key_size = 0; key_size < key_max - 1 && *src;) {
if (*src == '=') {
src++;
break;
}
key[key_size++] = *src++;
}
while (key_size > 0 && isspace(key[key_size - 1]))
key_size--;
key[key_size] = '\0';
while (*src && isspace(*src))
src++;
size_t value_size;
for (value_size = 0; value_size < value_max - 1 && *src;)
value[value_size++] = *src++;
while (value_size > 0 && isspace(value[value_size - 1]))
value_size--;
value[value_size] = '\0';
key[key_max-1] = '\0';
value[value_max-1] = '\0';
}
static void abc_message(const char *s1, const char *s2)
{
char txt[256];
if( strlen(s1) + strlen(s2) > 255 ) return;
sprintf(txt, s1, s2);
fprintf(stderr, "load_abc > %s\n", txt);
}
static uint32_t modticks(uint32_t abcticks)
{
return abcticks / RESOLUTION;
}
static uint32_t abcticks(uint32_t modticks)
{
return modticks * RESOLUTION;
}
static uint32_t notelen_notediv_to_ticks(int speed, int len, int div)
{
uint32_t u;
if (div == 0) return 1;
u = (ROWSPERNOTE * RESOLUTION * speed * len * global_tempo_factor) / (div * global_tempo_divider);
return u;
}
static void abc_dumptracks(ABCHANDLE *h, const char *p)
{
ABCTRACK *t;
ABCEVENT *e;
int n,pat,row,tck;
char nn[3];
if( !h ) return;
for( t=h->track; t; t=t->next ) {
printf("track %d.%d chan=%d %s\n", (int)(t->vno), (int)(t->vpos),
(int)(t->chan), (char *)(t->v));
if( strcmp(p,"nonotes") )
n = 1;
else
n = 0;
for( e=t->head; e; e=e->next ) {
tck = modticks(e->tracktick);
row = tck / h->speed;
pat = row / 64;
tck = tck % h->speed;
row = row % 64;
nn[0] = ( e->tracktick % abcticks(h->speed * 64) ) ? ' ': '-';
if( e->flg == 1 ) {
printf(" %6d.%02d.%d%c%c %d.%d %s ",
pat, row, tck, nn[0], (int)(e->part), (int)(t->vno),
(int)(t->vpos), (char *)(t->v));
if( e->cmd == cmdchord ) {
nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[chordnote]];
nn[1] = "b # # # # # # # # # # #"[e->par[chordnote]];
nn[2] = '\0';
if( isspace(nn[1]) ) nn[1] = '\0';
printf("CMD %c: gchord %s%s",
(char)(e->cmd), nn, chordname[e->par[chordnum]]);
if( e->par[chordbase] != e->par[chordnote] ) {
nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[chordbase]];
nn[1] = "b # # # # # # # # # # #"[e->par[chordbase]];
nn[2] = '\0';
printf("/%s", nn);
}
printf("\n");
}
else
printf("CMD %c @%p 0x%08lX\n",
(char)(e->cmd), e,
(unsigned long)(e->lpar));
if( strcmp(p,"nonotes") )
n = 1;
else
n = 0;
}
else if( n ) {
printf(" %6d.%02d.%d%c%c %d.%d %s ", pat, row, tck, nn[0], e->part, t->vno, t->vpos, t->v);
if( e->par[note] ) {
nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[note]-23];
nn[1] = "b # # # # # # # # # # #"[e->par[note]-23];
nn[2] = '\0';
}
else strcpy(nn,"--");
printf("NOTE %s octave %d inst %s vol %03d\n",
nn, e->par[octave], pat_gm_name(pat_smptogm(e->par[smpno])),e->par[volume]);
if( strcmp(p,"all") )
n = 0;
}
}
}
}
Nov 22, 2019
Nov 22, 2019
406
#if defined(_WIN32) && defined(_mm_free)
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
#undef _mm_free
#endif
#define MMSTREAM FILE
#define _mm_fopen(name,mode) fopen(name,mode)
#define _mm_fgets(f,buf,sz) fgets(buf,sz,f)
#define _mm_fseek(f,pos,whence) fseek(f,pos,whence)
#define _mm_ftell(f) ftell(f)
#define _mm_read_UBYTES(buf,sz,f) fread(buf,sz,1,f)
#define _mm_read_SBYTES(buf,sz,f) fread(buf,sz,1,f)
#define _mm_feof(f) feof(f)
#define _mm_fclose(f) fclose(f)
#define DupStr(h,buf,sz) strdup(buf)
#define _mm_calloc(h,n,sz) calloc(n,sz)
#define _mm_recalloc(h,buf,sz,elsz) realloc(buf,sz)
#define _mm_free(h,p) free(p)
typedef struct {
char *mm;
int sz;
int pos;
} MMFILE;
static MMFILE *mmfopen(const char *name, const char *mode)
{
FILE *fp;
MMFILE *mmfile = NULL;
long len;
if( *mode != 'r' ) return NULL;
fp = fopen(name, mode);
if( !fp ) return NULL;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
if ( len > 0 )
mmfile = (MMFILE *)malloc(len+sizeof(MMFILE));
if( !mmfile || len <= 0 ) {
fclose(fp);
return NULL;
}
fseek(fp, 0, SEEK_SET);
fread(&mmfile[1],1,len,fp);
fclose(fp);
mmfile->mm = (char *)&mmfile[1];
mmfile->sz = len;
mmfile->pos = 0;
return mmfile;
}
static void mmfclose(MMFILE *mmfile)
{
free(mmfile);
}
static bool mmfeof(MMFILE *mmfile)
{
if( mmfile->pos < 0 ) return TRUE;
if( mmfile->pos < mmfile->sz ) return FALSE;
return TRUE;
}
static int mmfgetc(MMFILE *mmfile)
{
int b;
if( mmfeof(mmfile) ) return EOF;
b = mmfile->mm[mmfile->pos];
mmfile->pos++;
if( b=='\r' && !mmfeof(mmfile) && mmfile->mm[mmfile->pos] == '\n' ) {
b = '\n';
mmfile->pos++;
}
return b;
}
static void mmfgets(char buf[], unsigned int bufsz, MMFILE *mmfile)
{
int i,b;
for( i=0; i<(int)bufsz-1; i++ ) {
b = mmfgetc(mmfile);
if( b==EOF ) break;
buf[i] = b;
if( b == '\n' ) break;
}
buf[i] = '\0';
}
static long mmftell(MMFILE *mmfile)
{
return mmfile->pos;
}
static void mmfseek(MMFILE *mmfile, long p, int whence)
{
switch(whence) {
case SEEK_SET:
mmfile->pos = p;
break;
case SEEK_CUR:
mmfile->pos += p;
break;
case SEEK_END:
mmfile->pos = mmfile->sz + p;
break;
}
}
// =====================================================================================
static ABCEVENT *abc_new_event(ABCHANDLE *h, uint32_t abctick, const char data[])
// =====================================================================================
{
ABCEVENT *retval;
int i;
retval = (ABCEVENT *)_mm_calloc(h->trackhandle, 1,sizeof(ABCEVENT));
retval->next = NULL;
retval->tracktick = abctick;
for( i=0; i<6; i++ )
retval->par[i] = data[i];
retval->part = global_part;
retval->tiednote = 0;
return retval;
}
// =============================================================================
static ABCEVENT *abc_copy_event(ABCHANDLE *h, ABCEVENT *se)
// =============================================================================
{
ABCEVENT *e;
e = (ABCEVENT *)_mm_calloc(h->trackhandle, 1,sizeof(ABCEVENT));
e->next = NULL;
e->tracktick = se->tracktick;
e->flg = se->flg;
e->cmd = se->cmd;
e->lpar = se->lpar;
e->part = se->part;
return e;
}
// =============================================================================
static void abc_new_macro(ABCHANDLE *h, const char *m)
// =============================================================================
{
ABCMACRO *retval;
char key[256], value[256];
abc_extractkeyvalue(key, sizeof(key), value, sizeof(value), m);
retval = (ABCMACRO *)_mm_calloc(h->macrohandle, 1,sizeof(ABCMACRO));
retval->name = DupStr(h->macrohandle, key, strlen(key));
retval->n = strrchr(retval->name, 'n'); // for transposing macro's
retval->subst = DupStr(h->macrohandle, value, strlen(value));
retval->next = h->macro;
h->macro = retval;
}
// =============================================================================
static void abc_new_umacro(ABCHANDLE *h, const char *m)
// =============================================================================
{
ABCMACRO *retval, *mp;
char key[256], value[256];
abc_extractkeyvalue(key, sizeof(key), value, sizeof(value), m);
if( strlen(key) > 1 || strchr("~HIJKLMNOPQRSTUVWXY",toupper(key[0])) == 0 ) return;
while( char *q = strchr(key, '!') )
*q = '+'; // translate oldstyle to newstyle
if( !strcmp(key,"+nil+") ) { // delete a macro
mp = NULL;
for( retval=h->umacro; retval; retval = retval->next ) {
if( retval->name[0] == key[0] ) { // delete this one
if( mp ) mp->next = retval->next;
else h->umacro = retval->next;
_mm_free(h->macrohandle, retval);
return;
}
mp = retval;
}
return;
}
retval = (ABCMACRO *)_mm_calloc(h->macrohandle, 1,sizeof(ABCMACRO));
retval->name = DupStr(h->macrohandle, key, 1);
retval->subst = DupStr(h->macrohandle, value, strlen(value));
retval->n = 0;
retval->next = h->umacro; // by placing it up front we mask out the old macro until we +nil+ it
h->umacro = retval;
}
// =============================================================================
static ABCTRACK *abc_new_track(ABCHANDLE *h, const char *voice, int pos)
// =============================================================================
{
ABCTRACK *retval;
if( !pos ) global_voiceno++;
retval = (ABCTRACK *)_mm_calloc(h->trackhandle, 1,sizeof(ABCTRACK));
retval->next = NULL;
retval->vno = global_voiceno;
retval->vpos = pos;
retval->tiedvpos = pos;
retval->instr = 1;
strncpy(retval->v, voice, 20);
retval->v[20] = '\0';
retval->head = NULL;
retval->tail = NULL;
retval->capostart = NULL;
retval->tienote = NULL;
retval->mute = 0;
retval->chan = 0;
retval->transpose = 0;
retval->volume = h->track? h->track->volume: 120;
retval->slidevoltime = 0;
retval->slidevol = 0;
retval->legato = 0;
return retval;
}
static int abc_numtracks(ABCHANDLE *h)
{
int n;
ABCTRACK *t;
n=0;
for( t = h->track; t; t=t->next )
n++;
return n;
}
static int abc_interval(const char *s, const char *d)
{
const char *p;
int i,j,k;
int n,oct,m[2];
for( j=0; j<2; j++ ) {
if( j ) p = d;
else p = s;
switch(p[0]) {
case '^':
n = p[1];
i = 2;
break;
case '_':
n = p[1];
i = 2;
break;
case '=':
n = p[1];
i = 2;
break;
default:
n = p[0];
i = 1;
break;
}
for( k=0; k<25; k++ )
if( n == sig[7][k] )
break;
oct = 4; // ABC note pitch C is C4 and pitch c is C5
if( k > 12 ) {
oct++;
k -= 12;
}
while( p[i] == ',' || p[i] == '\'' ) {
if( p[i] == ',' )
oct--;
else
oct++;
i++;
}
m[j] = k + 12 * oct;
}
return m[0] - m[1];
}
static int abc_transpose(const char *v)
{
int i,j,t;
const char *m = "B", *mv = "";
t = 0;
global_octave_shift = 99;
for( ; *v && *v != ']'; v++ ) {
if( !strncasecmp(v,"t=",2) ) {
v+=2;
if( *v=='-' ) {
j = -1;
v++;
}
else j = 1;
v+=abc_getnumber(v,&i);
t += i * j;
global_octave_shift = 0;
}
if( !strncasecmp(v,"octave=",7) ) {
v+=7;
if( *v=='-' ) {
j = -1;
v++;
}
else j = 1;
v+=abc_getnumber(v,&i);
t += i * j * 12;
global_octave_shift = 0;
}
if( !strncasecmp(v,"transpose=",10) ) {
v+=10;
if( *v=='-' ) {
j = -1;
v++;
}
else j = 1;
v+=abc_getnumber(v,&i);
t += i * j;
global_octave_shift = 0;
}
if( !strncasecmp(v,"octave=",7) ) { // used in kv304*.abc
v+=7;
if( *v=='-' ) {
j = -1;
v++;
}
else j = 1;
v+=abc_getnumber(v,&i);
t += i * j * 12;
global_octave_shift = 0;
}
if( !strncasecmp(v,"m=",2) ) {
v += 2;
mv = v; // get the pitch for the middle staff line
while( *v && *v != ' ' && *v != ']' ) v++;
global_octave_shift = 0;
}
if( !strncasecmp(v,"middle=",7) ) {
v += 7;
mv = v; // get the pitch for the middle staff line
while( *v && *v != ' ' && *v != ']' ) v++;
global_octave_shift = 0;
}
if( !strncasecmp(v,"clef=",5) )
v += 5;
j = 1;
if( !strncasecmp(v,"treble",6) ) {
j = 0;
v += 6;
switch( *v ) {
case '1': v++; m = "d"; break;
case '2': v++;
default: m = "B"; break;
case '3': v++; m = "G"; break;
case '4': v++; m = "E"; break;
case '5': v++; m = "C"; break;
}
global_octave_shift = 0;
}
if( j && !strncasecmp(v,"bass",4) ) {
j = 0;
v += 4;
switch( *v ) {
case '1': v++; m = "C"; break;
case '2': v++; m = "A,"; break;
case '3': v++; m = "F,"; break;
case '4': v++;
default: m = "D,"; break;
case '5': v++; m = "B,,"; break;
}
if( global_octave_shift == 99 )
global_octave_shift = -2;
}
if( j && !strncasecmp(v,"tenor",5) ) {
j = 0;
v += 5;
switch( *v ) {
case '1': v++; m = "G"; break;
case '2': v++; m = "E"; break;
case '3': v++; m = "C"; break;
case '4': v++;
default: m = "A,"; break;
case '5': v++; m = "F,"; break;
}
if( global_octave_shift == 99 )
global_octave_shift = 1;
}
if( j && !strncasecmp(v,"alto",4) ) {
j = 0;
v += 4;
switch( *v ) {
case '1': v++; m = "G"; break;
case '2': v++; m = "E"; break;
case '3': v++;
default: m = "C"; break;
case '4': v++; m = "A,"; break;
case '5': v++; m = "F,"; break;
}
if( global_octave_shift == 99 )
global_octave_shift = 1;
}
if( j && strchr("+-",*v) && *v && v[1]=='8' ) {
switch(*v) {
case '+':
t += 12;
break;
case '-':
t -= 12;
break;
}
v += 2;
if( !strncasecmp(v,"va",2) ) v += 2;
global_octave_shift = 0;
j = 0;
}
if( j ) {
while( *v && *v != ' ' && *v != ']' ) v++;
}
}
if( strlen(mv) > 0 ) // someone set the middle note
t += abc_interval(mv, m);
if( global_octave_shift == 99 )
global_octave_shift = 0;
return t;
}
// =============================================================================
static ABCTRACK *abc_locate_track(ABCHANDLE *h, const char *voice, int pos)
// =============================================================================
{
ABCTRACK *tr, *prev, *trunused;
char vc[21];
int i, trans=0, voiceno=0, instrno = 1, channo = 0;
for( ; *voice == ' '; voice++ ) ; // skip leading spaces
for( i=0; i+1 < sizeof(vc) && *voice && *voice != ']' && *voice != '%' && !isspace(*voice); voice++ ) // can work with inline voice instructions
vc[i++] = *voice;
vc[i] = '\0';
prev = NULL;
trunused = NULL;
if( !pos ) trans = abc_transpose(voice);
for( tr=h->track; tr; tr=tr->next ) {
if( tr->vno == 0 ) {
if( !trunused ) trunused = tr; // must reuse mastertrack (h->track) as first
}
else {
if( !strncasecmp(tr->v, vc, 20) ) {
if( tr->vpos == pos )
return tr;
trans = tr->transpose;
global_octave_shift = tr->octave_shift;
voiceno = tr->vno;
instrno = tr->instr;
channo = tr->chan;
}
}
prev = tr;
}
if( trunused ) {
tr = trunused;
if( pos ) {
tr->vno = voiceno;
tr->instr = instrno;
tr->chan = channo;
}
else {
global_voiceno++;
tr->vno = global_voiceno;
tr->instr = 1;
tr->chan = 0;
}
tr->vpos = pos;
tr->tiedvpos = pos;
strncpy(tr->v, vc, 20);
tr->v[20] = '\0';
tr->mute = 0;
tr->transpose = trans;
tr->octave_shift = global_octave_shift;
tr->volume = h->track->volume;
tr->tienote = NULL;
tr->legato = 0;
return tr;
}
tr = abc_new_track(h, vc, pos);
if( pos ) {
tr->vno = voiceno;
tr->instr = instrno;
tr->chan = channo;
}
tr->transpose = trans;
tr->octave_shift = global_octave_shift;
if( prev ) prev->next = tr;
else h->track = tr;
return tr;
}
// =============================================================================
static ABCTRACK *abc_check_track(ABCHANDLE *h, ABCTRACK *tp)
// =============================================================================
{
if( !tp ) {
tp = abc_locate_track(h, "", 0); // must work for voiceless abc too...
tp->transpose = h->ktrans;
}
return tp;
}
static void abc_add_capo(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
{
ABCEVENT *e;
char d[6];
d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
d[cmdflag] = 1;
d[command] = cmdcapo;
e = abc_new_event(h, tracktime, d);
tp->capostart = e;
abc_add_event(h, tp, e); // do this last (recursion danger)
}
static void abc_add_segno(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
{
ABCEVENT *e;
char d[6];
d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
d[cmdflag] = 1;
d[command] = cmdsegno;
e = abc_new_event(h, tracktime, d);
abc_add_event(h, tp, e);
}
static void abc_add_coda(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
{
ABCEVENT *e;
char d[6];
d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
d[cmdflag] = 1;
d[command] = cmdcoda;
e = abc_new_event(h, tracktime, d);
abc_add_event(h, tp, e);
}
static void abc_add_fine(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
{
ABCEVENT *e;
char d[6];
d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
d[cmdflag] = 1;
d[command] = cmdfine;
e = abc_new_event(h, tracktime, d);
abc_add_event(h, tp, e);
}
static void abc_add_tocoda(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
{
ABCEVENT *e;
char d[6];
d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
d[cmdflag] = 1;
d[command] = cmdtocoda;
e = abc_new_event(h, tracktime, d);
abc_add_event(h, tp, e);
}
// first track is dirigent, remove all control events from other tracks
// to keep the information where the events should be relative to note events
// in the same tick the ticks are octated and four added for note events
// the control events that come before the note events get a decremented tick,
// those that come after get an incremented tick, for example:
// ctrl ctrl note ctrl ctrl note
// original: t t t t t+1 t+1
// recoded: 8t+1 8t+2 8t+4 8t+5 8t+11 8t+12
static void abc_remove_unnecessary_events(ABCHANDLE *h)
{
ABCTRACK *tp,*ptp;
ABCEVENT *ep, *el;
uint32_t ct, et;
int d;
ptp = NULL;
for( tp=h->track; tp; tp=tp->next ) {
el = NULL;
ep = tp->head;
ct = 0;
d = -3;
while( ep ) {
et = ep->tracktick;
ep->tracktick <<= 3;
ep->tracktick += 4;
if( ep->flg == 1 ) {
ep->tracktick += d;
d++;
if( d == 0 ) d = -1;
if( d == 4 ) d = 3;
if( tp!=h->track ) ep->cmd = cmdhide;
switch( ep->cmd ) {
case cmdhide:
case cmdsync:
if( el ) {
el->next = ep->next;
if( !el->next )
tp->tail = el;
_mm_free(h->trackhandle,ep);
ep = el->next;
}
else {
tp->head = ep->next;
if( !tp->head )
tp->tail = NULL;