Skip to content

Latest commit

 

History

History
1588 lines (1502 loc) · 40.5 KB

load_mid.cpp

File metadata and controls

1588 lines (1502 loc) · 40.5 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
34
35
36
37
/*
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_MID
MID module loader.
by Peter Grootswagers (2006)
<email:pgrootswagers@planet.nl>
Portability:
All systems - all compilers (hopefully)
*/
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifndef _WIN32
#include <unistd.h> // for sleep
#endif
#include "stdafx.h"
#include "sndfile.h"
Oct 23, 2018
Oct 23, 2018
38
39
40
#ifndef NO_MIDIFORMATS
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
#define PAN_LEFT 0x30
#define PAN_RIGHT 0xD0
#define MAX_POLYPHONY 16 // max notes in one midi channel
#define MAX_TRACKS (MAX_BASECHANNELS-6) // max mod tracks
#define WHEELSHIFT 10 // how many bits the 13bit midi wheel value must shift right
#include "load_pat.h"
#define ROWSPERNOTE 16
#define ENV_MMMID_SPEED "MMMID_SPEED"
#define ENV_MMMID_DEBUG "MMMID_DEBUG"
#define ENV_MMMID_VERBOSE "MMMID_VERBOSE"
/**********************************************************************/
typedef enum {
none,
wheeldown,
wheelup,
fxbrk,
tmpo,
fxsync,
modwheel,
mainvol,
prog
} MIDEVENT_X_EFFECT;
typedef struct _MIDEVENT
{
struct _MIDEVENT *next;
ULONG tracktick;
BYTE flg; // 1 = note present
BYTE note;
BYTE volume;
BYTE smpno;
BYTE fx;
BYTE fxparam;
} MIDEVENT;
typedef struct _MIDTRACK
{
struct _MIDTRACK *next;
MIDEVENT *head;
MIDEVENT *tail;
MIDEVENT *workevent; // keeps track of events in track
int balance; // last balance on this track
ULONG vtracktick; // tracktick of last note event (on or off)
BYTE chan;
BYTE vpos; // 0xff is track is free for use, otherwise it's the note playing on this track
BYTE volume; // last note volume on this track
BYTE instr; // current instrument for this track
} MIDTRACK;
#if defined(WIN32) && defined(_mm_free)
#undef _mm_free
#endif
Oct 23, 2018
Oct 23, 2018
98
#define MMSTREAM FILE
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
#define _mm_fseek(f,pos,whence) fseek(f,pos,whence)
#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 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;
unsigned int sz;
int pos;
} MMFILE;
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 long mmftell(MMFILE *mmfile)
{
return mmfile->pos;
}
static BYTE mmreadUBYTE(MMFILE *mmfile)
{
BYTE b;
b = (BYTE)mmfile->mm[mmfile->pos];
mmfile->pos++;
return b;
}
static void mmreadUBYTES(BYTE *buf, long sz, MMFILE *mmfile)
{
memcpy(buf, &mmfile->mm[mmfile->pos], sz);
mmfile->pos += sz;
}
static void mmreadSBYTES(char *buf, long sz, MMFILE *mmfile)
{
memcpy(buf, &mmfile->mm[mmfile->pos], sz);
mmfile->pos += sz;
}
/**********************************************************************/
typedef struct _MIDHANDLE
{
MMFILE *mmf;
MIDTRACK *track;
MIDTRACK *tp;
ULONG tracktime;
const char *debug;
const char *verbose;
int speed;
int midispeed;
int midiformat;
int resolution;
int miditracks;
int divider;
int tempo;
int percussion;
long deltatime;
} MIDHANDLE;
static void mid_dump_tracks(MIDHANDLE *h)
{
MIDTRACK *tr;
MIDEVENT *e;
int t;
printf("tracktime = %ld\n", (long)(h->tracktime));
printf("speed = %d\n", h->speed);
printf("midispeed = %d\n", h->midispeed);
printf("midiformat = %d\n", h->midiformat);
printf("resolution = %d\n", h->resolution);
printf("miditracks = %d\n", h->miditracks);
printf("divider = %d\n", h->divider);
printf("tempo = %d\n", h->tempo);
printf("percussion = %d\n", h->percussion);
printf("deltatime = %ld\n", h->deltatime);
t = 0;
for( tr=h->track; tr; tr = tr->next ) {
t++;
printf("TRACK %2d chan=%d note=0x%02x vol=%d pan=0x%02x instr=%d\n", t, tr->chan + 1, tr->vpos, tr->balance, tr->volume, tr->instr);
for( e=tr->head; e; e=e->next ) {
printf("%2d %6ld %s %3d %3d %3d ",
t, (long)(e->tracktick),
e->flg? "NOTE": "CTRL", e->note, e->volume, e->smpno);
switch( e->fx ) {
case fxbrk: printf("fxbrk\n");break;
case fxsync: printf("fxsync\n");break;
case prog: printf("prog %d\n", e->fxparam);break;
case mainvol: printf("mainvol %d\n", e->fxparam);break;
case modwheel: printf("modwheel %d\n", e->fxparam);break;
case wheeldown: printf("wheeldown %d\n", e->fxparam);break;
case wheelup: printf("wheelup %d\n", e->fxparam);break;
case tmpo: printf("tmpo %d\n", e->fxparam);break;
default: printf("\n");break;
}
}
}
}
static void mid_message(const char *s1, const char *s2)
{
char txt[256];
if( strlen(s1) + strlen(s2) > 255 ) return;
sprintf(txt, s1, s2);
fprintf(stderr, "load_mid > %s\n", txt);
}
static ULONG miditicks(MIDHANDLE *h, ULONG modtick)
{
return modtick * h->divider / ROWSPERNOTE / h->speed;
}
static ULONG modticks(MIDHANDLE *h, ULONG miditick)
{
return miditick * ROWSPERNOTE * h->speed / h->divider;
}
static void mid_adjust_for_optimal_tempo(MIDHANDLE *h, int maxtempo)
{
// the tempo is adjusted so that the maximum tempo is 255
// this way we have the biggest change that very short notes get played
// and we make sure the tempo doesn't become too large or too small
// if the piece in hand isn't so weird it changes tempo from 20 to 255, that is.
// tempo is only registered in first track (h->track) because it is a global event
MIDEVENT *e;
int d, t;
if( maxtempo < 1 ) return;
d = h->divider;
t = maxtempo;
h->divider = (t * d) / 255;
while( (h->midispeed = miditicks(h, h->speed)) < h->speed ) {
++t;
h->divider = (t * d) / 255;
}
if( h->verbose && t > maxtempo )
printf("Adjusted maximum tempo from %d to %d to get %d miditicks per patternrow\n",
maxtempo, 2 * maxtempo - t, h->midispeed);
if( h->track ) {
for( e=h->track->head; e; e=e->next ) {
if( e->fx == tmpo )
e->fxparam = (255 * e->fxparam ) / t;
}
}
}
// =====================================================================================
static MIDEVENT *mid_new_event(MIDHANDLE *h)
// =====================================================================================
{
MIDEVENT *retval;
retval = (MIDEVENT *)_mm_calloc(h->trackhandle, 1,sizeof(MIDEVENT));
retval->next = NULL;
retval->tracktick = h->tracktime;
retval->flg = 0;
retval->note = 0;
retval->volume = 0;
retval->smpno = 0;
retval->fx = none;
retval->fxparam = 0;
return retval;
}
// =====================================================================================
static MIDTRACK *mid_new_track(MIDHANDLE *h, int mch, int pos)
// =====================================================================================
{
MIDTRACK *retval;
retval = (MIDTRACK *)_mm_calloc(h->trackhandle, 1,sizeof(MIDTRACK));
retval->next = NULL;
retval->vpos = pos;
retval->instr = 1;
retval->chan = mch;
retval->head = NULL;
retval->tail = NULL;
retval->workevent = NULL;
retval->vtracktick = 0;
retval->volume = h->track? h->track->volume: 120;
retval->balance = 64;
return retval;
}
static int mid_numtracks(MIDHANDLE *h)
{
int n;
MIDTRACK *t;
n=0;
for( t = h->track; t; t=t->next )
n++;
return n;
}
// find out how many midichannel we have
static int mid_numchans(MIDHANDLE *h)
{
int i,c,n;
MIDTRACK *t;
c = 0;
for( t = h->track; t; t=t->next )
c |= (1<<t->chan);
n = 0;
for( i=0; i<16; i++ )
if( c & (1<<i) ) n++;
return n;
}
// find out which ordinal a midichannel has
static int mid_ordchan(MIDHANDLE *h, int mch)
{
int i,c,n;
MIDTRACK *t;
c = 0;
for( t = h->track; t; t=t->next )
c |= (1<<t->chan);
n = 0;
for( i=0; i<mch; i++ )
if( c & (1<<i) ) n++;
return n;
}
static void mid_rewind_tracks(MIDHANDLE *h)
{
MIDTRACK *tr;
h->tracktime = 0;
for( tr = h->track; tr; tr = tr->next ) {
tr->vpos = 0xff;
tr->workevent = tr->head;
tr->vtracktick = 0;
}
}
static void mid_update_track(MIDTRACK *tr)
{
MIDEVENT *e;
e = tr->workevent;
if( e->flg ) {
if( e->volume ) tr->vpos = e->note;
else tr->vpos = 0xff;
tr->volume = e->volume;
tr->vtracktick = e->tracktick;
}
if( e->fx == prog ) tr->instr = e->fxparam;
}
static void mid_sync_track(MIDTRACK *tr, ULONG tracktime)
{
MIDEVENT *e;
e = tr->workevent;
if( e && e->tracktick > tracktime ) e = tr->head; // start again....
for( ; e && e->tracktick <= tracktime; e=e->next ) {
tr->workevent = e;
mid_update_track(tr);
}
}
// =====================================================================================
static MIDTRACK *mid_find_track(MIDHANDLE *h, int mch, int pos)
// =====================================================================================
{
MIDTRACK *tr;
for( tr=h->track; tr; tr=tr->next ) {
mid_sync_track(tr, h->tracktime);
if( tr->chan == mch && tr->vpos == pos )
return tr;
}
return NULL;
}
// =====================================================================================
static MIDTRACK *mid_locate_track(MIDHANDLE *h, int mch, int pos)
// =====================================================================================
{
MIDTRACK *tr, *prev, *trunused;
MIDEVENT *e;
int instrno = 1;
int polyphony;
int vol = 0, bal = 0;
int numtracks;
ULONG tmin;
prev = NULL;
trunused = NULL;
polyphony = 0;
numtracks = 0;
tmin = h->midispeed; // minimal distance between note events in track
// look up track with desired channel and pos (note)
for( tr=h->track; tr; tr=tr->next ) {
mid_sync_track(tr, h->tracktime);
if( tr->chan == mch ) {
if( tr->vpos == pos )
return tr;
if( tr->vpos == 0xff ) {
// check if track with silence is quiet long enough
if( h->tracktime > tr->vtracktick + tmin ) trunused = tr;
}
else vol = tr->volume;
instrno = tr->instr;
bal = tr->balance;
polyphony++;
}
numtracks++;
prev = tr;
}
if( trunused ) {
trunused->vpos = pos;
return trunused;
}
if( polyphony > MAX_POLYPHONY || (polyphony > 0 && numtracks > MAX_TRACKS) ) { // do not use up too much channels
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
e = tr->workevent;
if( h->tracktime > e->tracktick + tmin ) {
tmin = h->tracktime - e->tracktick;
trunused = tr;
}
}
}
if( trunused ) {
trunused->vpos = pos;
return trunused;
}
}
if( numtracks > MAX_TRACKS ) { // we can not allocate new tracks
tmin = 0;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
e = tr->workevent;
if( h->tracktime >= e->tracktick + tmin ) {
tmin = h->tracktime - e->tracktick;
trunused = tr;
}
}
}
if( trunused ) {
trunused->vpos = pos;
return trunused;
}
tmin = 0;
for( tr=h->track; tr; tr=tr->next ) {
e = tr->workevent;
if( h->tracktime >= e->tracktick + tmin ) {
tmin = h->tracktime - e->tracktick;
trunused = tr;
}
}
if( trunused ) {
trunused->vpos = pos;
trunused->chan = mch;
return trunused;
}
}
tr = mid_new_track(h, mch, pos);
tr->instr = instrno;
tr->volume = vol;
tr->balance = bal;
if( prev ) prev->next = tr;
else h->track = tr;
return tr;
}
static void mid_add_event(MIDHANDLE *h, MIDTRACK *tp, MIDEVENT *e)
{
MIDEVENT *ew, *ep;
ep = NULL;
ew = tp->workevent;
if( ew && ew->tracktick > e->tracktick ) ew = tp->head; // start again from the beginning...
for( ; ew && ew->tracktick <= e->tracktick; ew = ew->next ) {
ep = ew;
tp->workevent = ew;
mid_update_track(tp);
}
if( ep ) {
ep->next = e;
e->next = ew;
}
else {
e->next = tp->head;
tp->head = e;
}
if( !e->next )
tp->tail = e;
tp->workevent = e;
mid_update_track(tp);
}
static void mid_add_tempo_event(MIDHANDLE *h, int tempo)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 0;
e->fx = tmpo;
e->fxparam = tempo;
mid_add_event(h, h->track, e);
}
static void mid_add_partbreak(MIDHANDLE *h)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 0;
e->fx = fxbrk;
mid_add_event(h, h->track, e);
}
static void mid_add_noteoff(MIDHANDLE *h, MIDTRACK *tp)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 1;
e->note = tp->vpos;
e->smpno = tp->instr;
mid_add_event(h, tp, e);
}
static void mid_add_noteon(MIDHANDLE *h, MIDTRACK *tp, int n, int vol)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 1;
e->note = n;
e->smpno = tp->instr;
e->volume = vol;
mid_add_event(h, tp, e);
}
static BYTE modtremolo(int midimod)
{
int m;
if( midimod == 0 ) return 0;
if( midimod > 63 ) {
m = (128 - midimod) / 4;
if( m==0 ) m = 1;
return m|0xf0; // find slide down
}
m = midimod / 4;
if( m==0 ) m = 1;
return (m<<4)|0x0f; // find slide up
}
// =====================================================================================
static void mid_mod_wheel(MIDHANDLE *h, int mch, int mod)
// =====================================================================================
{
MIDTRACK *tr;
MIDEVENT *e;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
mid_sync_track(tr, h->tracktime);
if( tr->vpos != 0xff ) { // only on tracks with notes on...
e = mid_new_event(h);
e->flg = 0;
e->fx = modwheel;
e->fxparam = modtremolo(mod);
mid_add_event(h, tr, e);
}
}
}
}
// =====================================================================================
static void mid_main_volume(MIDHANDLE *h, int mch, int vol)
// =====================================================================================
{
MIDTRACK *tr;
MIDEVENT *e;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
e = mid_new_event(h);
e->flg = 0;
e->fx = mainvol;
e->fxparam = vol;
mid_add_event(h, tr, e);
}
}
}
// transform 0..63..127 to left..center..right in 2n+1 areas
static int modpan(int midipan, int n)
{
int npan, area, x;
x = 2 * n + 1;
area = (midipan * x * (PAN_RIGHT - PAN_LEFT))>>7;
npan = (PAN_LEFT * x + area) / x;
return npan;
}
// =====================================================================================
static void mid_pan(MIDHANDLE *h, int mch, int pan)
// =====================================================================================
{
MIDTRACK *tr;
int hits;
hits = 0;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
hits++;
tr->balance = pan;
}
}
if( !hits ) {
tr = mid_locate_track(h, mch, 0xff);
tr->balance = pan;
}
}
// =====================================================================================
static void mid_add_program(MIDHANDLE *h, int mch, int pr)
// =====================================================================================
{
MIDTRACK *tr;
MIDEVENT *e;
int hits;
hits = 0;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
hits++;
e = mid_new_event(h);
e->flg = 0;
e->fx = prog;
e->fxparam = pat_gmtosmp(pr + 1);
mid_add_event(h, tr, e);
}
}
if( !hits ) {
tr = mid_locate_track(h, mch, 0xff);
e = mid_new_event(h);
e->flg = 0;
e->fx = prog;
e->fxparam = pat_gmtosmp(pr + 1);
mid_add_event(h, tr, e);
}
}
// =====================================================================================
static void mid_all_notes_off(MIDHANDLE *h, int mch)
// =====================================================================================
{
MIDTRACK *tr;
if( h->debug ) printf("%ld %d all notes off\n",(long)(h->tracktime), mch+1);
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch || mch == -1 ) {
mid_sync_track(tr, h->tracktime);
if( tr->vpos != 0xff )
mid_add_noteoff(h, tr);
}
}
}
static void mid_add_sync(MIDHANDLE *h, MIDTRACK *tp)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 0;
e->fx = fxsync;
mid_add_event(h, tp, e);
}
static BYTE mid_to_mod_wheel(unsigned int midwheel)
{
unsigned int i;
if( midwheel == 0 ) return 0;
i = midwheel >> WHEELSHIFT;
return i+1;
}
static void mid_add_wheel(MIDHANDLE *h, MIDTRACK *tp, int wheel)
{
MIDEVENT *e;
e = mid_new_event(h);
e->flg = 0;
if( wheel < 0 ) {
e->fx = wheeldown;
e->fxparam = mid_to_mod_wheel(-wheel);
}
else {
e->fx = wheelup;
e->fxparam = mid_to_mod_wheel(wheel);
}
mid_add_event(h, tp, e);
}
static void mid_add_pitchwheel(MIDHANDLE *h, int mch, int wheel)
{
MIDTRACK *tr;
int hits;
hits = 0;
for( tr=h->track; tr; tr=tr->next ) {
if( tr->chan == mch ) {
hits++;
mid_sync_track(tr, h->tracktime);
if( tr->vpos != 0xff ) // only on tracks with notes on...
mid_add_wheel(h, tr, wheel);
}
}
if( !hits ) { // special case in midiformat 1 events in first track...
tr = mid_locate_track(h, mch, 0xff);
mid_add_wheel(h, tr, wheel);
}
}
static uint32_t mid_read_long(MIDHANDLE *h)
{
BYTE buf[4];
mmreadUBYTES(buf, 4, h->mmf);
return (buf[0]<<24)|(buf[1]<<16)|(buf[2]<<8)|buf[3];
}
static short int mid_read_short(MIDHANDLE *h)
{
BYTE buf[2];
mmreadUBYTES(buf, 2, h->mmf);
return (buf[0]<<8)|buf[1];
}
static BYTE mid_read_byte(MIDHANDLE *h)
{
return mmreadUBYTE(h->mmf);
}
static int mid_read_delta(MIDHANDLE *h)
{
BYTE bits;
int i, d;
d = 0;
for( i=0; i<4; ) {
bits = mid_read_byte(h);
i++;
d = (d<<7)|(bits&0x7f);
if( !(bits & 0x80) )
break;
}
h->deltatime = d;
return i;
}
// =====================================================================================
BOOL CSoundFile::TestMID(const BYTE *lpStream, DWORD dwMemLength)
// =====================================================================================
{
char id[5];
MIDHANDLE h;
MMFILE mm;
mm.mm = (char *)lpStream;
mm.sz = dwMemLength;
h.mmf = &mm;
if (h.mmf->sz < 4) return FALSE;
mmfseek(h.mmf,0,SEEK_SET);
mmreadSBYTES(id, 4, h.mmf);
id[4] = '\0';
return !strcmp(id,"MThd") && mid_read_long(&h) == 6;
}
// =====================================================================================
static MIDHANDLE *MID_Init(void)
{
MIDHANDLE *retval;
retval = (MIDHANDLE *)calloc(1,sizeof(MIDHANDLE));
if( !retval ) return NULL;
retval->track = NULL;
retval->percussion = 0;
retval->debug = NULL;
return retval;
}
static void MID_CleanupTrack(MIDTRACK *tp)
{
MIDEVENT *ep, *en;
if( tp ) {
for( ep=tp->head; ep; ep = en ) {
en=ep->next;
free(ep);
}
tp->head = NULL;
}
}
// =====================================================================================
static void MID_CleanupTracks(MIDHANDLE *handle)
// =====================================================================================
{
MIDTRACK *tp, *tn;
if(handle) {
for( tp=handle->track; tp; tp = tn ) {
tn=tp->next;
MID_CleanupTrack(tp);
}
handle->track = NULL;
}
}
// =====================================================================================
static void MID_Cleanup(MIDHANDLE *handle)
// =====================================================================================
{
if(handle) {
MID_CleanupTracks(handle);
free(handle);
handle = 0;
}
}
static int mid_is_global_event(MIDEVENT *e)
{
return (e->fx == tmpo || e->fx == fxbrk);
}
static MIDEVENT *mid_next_global(MIDEVENT *e)
{
for( ; e && !mid_is_global_event(e); e=e->next ) ;
return e;
}
static MIDEVENT *mid_next_fx(MIDEVENT *e)
{
for( ; e && e->fx == none; e=e->next ) ;
return e;
}
static int mid_is_note_event(MIDEVENT *e)
{
#ifdef LOOPED_NOTES_OFF
return (e->flg == 0);
#else
if( e->flg == 0 ) return 0;
if( e->volume ) return 1;
return pat_smplooped(e->smpno); // let non looping samples die out...
#endif
}
static MIDEVENT *mid_next_note(MIDEVENT *e)
{
for( ; e && !mid_is_note_event(e); e=e->next ) ;
return e;
}
// =====================================================================================
static int MID_ReadPatterns(MODCOMMAND *pattern[], WORD psize[], MIDHANDLE *h, int numpat, int channels)
// =====================================================================================
{
int pat,row,i,ch;
BYTE n,ins,vol;
MIDTRACK *t;
MIDEVENT *e, *en, *ef, *el;
ULONG tt1, tt2;
MODCOMMAND *m;
int patbrk, tempo;
if( numpat > MAX_PATTERNS ) numpat = MAX_PATTERNS;
// initialize start points of event list in tracks
for( t = h->track; t; t = t->next ) t->workevent = t->head;
for( pat = 0; pat < numpat; pat++ ) {
pattern[pat] = CSoundFile::AllocatePattern(64, channels);
if( !pattern[pat] ) return 0;
psize[pat] = 64;
for( row = 0; row < 64; row++ ) {
tt1 = miditicks(h, (pat * 64 + row ) * h->speed);
tt2 = tt1 + h->midispeed;
ch = 0;
tempo = 0;
patbrk = 0;
if ( h->track )
for( e=mid_next_global(h->track->workevent); e && e->tracktick < tt2; e=mid_next_global(e->next) ) {
if( e && e->tracktick >= tt1 ) { // we have a controller event in this row
switch( e->fx ) {
case tmpo:
tempo = e->fxparam;
break;
case fxbrk:
patbrk = 1;
break;
}
}
}
for( t = h->track; t; t = t->next ) {
m = &pattern[pat][row * channels + ch];
m->param = 0;
m->command = CMD_NONE;
for( e=mid_next_fx(t->workevent); e && e->tracktick < tt2; e=mid_next_fx(e->next) ) {
if( e && e->tracktick >= tt1 ) { // we have a controller event in this row
switch( e->fx ) {
case modwheel:
m->param = e->fxparam;
m->command = CMD_VOLUMESLIDE;
break;
case wheelup:
m->param = e->fxparam|0x10;
m->command = CMD_XFINEPORTAUPDOWN;
break;
case wheeldown:
m->param = e->fxparam|0x20;
m->command = CMD_XFINEPORTAUPDOWN;
break;
}
}
}
for( e=mid_next_note(t->workevent); e && e->tracktick < tt1; e=mid_next_note(e->next) )
t->workevent = e;
i = 0;
ef = NULL;
en = e;
el = e;
for( ; e && e->tracktick < tt2; e=mid_next_note(e->next) ) { // we have a note event in this row
t->workevent = e;
i++;
if( e->volume ) {
if( !ef ) ef = e;
el = e;
}
}
if( i ) {
if( i == 1 || ef == el || !ef ) { // only one event in this row or a note on with some note off
if( ef ) e = ef;
else e = en;
el = t->workevent;
n = pat_modnote(e->note);
ins = e->smpno;
if( e->volume == 0 ) {
m->param = (BYTE)modticks(h, e->tracktick - tt1);
if( m->param ) { // note cut
m->command = CMD_S3MCMDEX;
m->param |= 0xC0;
}
else {
m->param = 0;
m->command = CMD_KEYOFF;
}
vol = 0;
}
else {
vol = e->volume/2;
if( el->volume == 0 ) {
m->param = (BYTE)modticks(h, el->tracktick - tt1);
if( m->param ) { // note cut
m->command = CMD_S3MCMDEX;
m->param |= 0xC0;
}
}
else {
m->param = (BYTE)modticks(h, e->tracktick - tt1);
if( m->param ) { // note delay
m->command = CMD_S3MCMDEX;
m->param |= 0xD0;
}
}
}
m->instr = ins;
m->note = n; // <- normal note
m->volcmd = VOLCMD_VOLUME;
m->vol = vol;
}
else {
// two notes in one row, use FINEPITCHSLIDE runonce effect
// start first note on first tick and framedly runonce on seconds note tick
// use volume and instrument of last note
n = pat_modnote(ef->note);
i = pat_modnote(el->note);
ins = el->smpno;
vol = el->volume/2;
if( vol > 64 ) vol = 64;
m->instr = ins;
m->note = n; // <- normal note
m->volcmd = VOLCMD_VOLUME;
m->vol = vol;
m->param = ((i > n)?i-n:n-i);
if( m->param < 16 ) {
if( m->param ) {
m->command = CMD_XFINEPORTAUPDOWN;
m->param |= (i > n)? 0x10: 0x20;
}
else { // retrigger same note...
m->command = CMD_RETRIG;
m->param = (BYTE)modticks(h, el->tracktick - tt1);
}
}
else
m->command = (i > n)? CMD_PORTAMENTOUP: CMD_PORTAMENTODOWN;
}
}
if( m->param == 0 && m->command == CMD_NONE ) {
if( tempo ) {
m->command = CMD_TEMPO;
m->param = tempo;
tempo = 0;
}
else {
if( patbrk ) {
m->command = CMD_PATTERNBREAK;
patbrk = 0;
}