-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha_midi.c
1412 lines (1129 loc) · 36.3 KB
/
a_midi.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
/*
Copyright (C) 1994-1995 Apogee Software, Ltd.
Copyright (C) 2023 Frenkel Smeijers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**********************************************************************
module: MIDI.C
author: James R. Dose
date: May 25, 1994
Midi song file playback routines.
(c) Copyright 1994 James R. Dose. All Rights Reserved.
**********************************************************************/
#include <dos.h>
#include <time.h>
#include "id_heads.h"
#include "dmx.h"
#include "a_inter.h"
#include "a_midi.h"
#include "a_music.h"
#include "a_taskmn.h"
#define RELATIVE_BEAT( measure, beat, tick ) \
( ( tick ) + ( ( beat ) << 9 ) + ( ( measure ) << 16 ) )
//Bobby Prince thinks this may be 100
//#define GENMIDI_DefaultVolume 100
#define GENMIDI_DefaultVolume 90
#define MAX_FORMAT 1
#define NUM_MIDI_CHANNELS 16
#define TIME_PRECISION 16
#define MIDI_HEADER_SIGNATURE 0x6468544d // "MThd"
#define MIDI_TRACK_SIGNATURE 0x6b72544d // "MTrk"
#define MIDI_VOLUME 7
#define MIDI_RPN_MSB 100
#define MIDI_RPN_LSB 101
#define MIDI_DATAENTRY_MSB 6
#define MIDI_DATAENTRY_LSB 38
#define MIDI_PITCHBEND_MSB 0
#define MIDI_PITCHBEND_LSB 0
#define MIDI_RUNNING_STATUS 0x80
#define MIDI_NOTE_OFF 0x8
#define MIDI_NOTE_ON 0x9
#define MIDI_POLY_AFTER_TCH 0xA
#define MIDI_CONTROL_CHANGE 0xB
#define MIDI_PROGRAM_CHANGE 0xC
#define MIDI_AFTER_TOUCH 0xD
#define MIDI_PITCH_BEND 0xE
#define MIDI_SPECIAL 0xF
#define MIDI_SYSEX 0xF0
#define MIDI_SYSEX_CONTINUE 0xF7
#define MIDI_META_EVENT 0xFF
#define MIDI_END_OF_TRACK 0x2F
#define MIDI_TEMPO_CHANGE 0x51
#define MIDI_TIME_SIGNATURE 0x58
#define MIDI_RESET_ALL_CONTROLLERS 0x79
#define MIDI_ALL_NOTES_OFF 0x7b
#define MIDI_MONO_MODE_ON 0x7E
#define GET_NEXT_EVENT( track, data ) \
( data ) = *( track )->pos; \
( track )->pos += 1
#define GET_MIDI_CHANNEL( event ) ( ( event ) & 0xf )
#define GET_MIDI_COMMAND( event ) ( ( event ) >> 4 )
#define EMIDI_INFINITE -1
#define EMIDI_END_LOOP_VALUE 127
#define EMIDI_ALL_CARDS 127
#define EMIDI_INCLUDE_TRACK 110
#define EMIDI_EXCLUDE_TRACK 111
#define EMIDI_PROGRAM_CHANGE 112
#define EMIDI_VOLUME_CHANGE 113
#define EMIDI_CONTEXT_START 114
#define EMIDI_CONTEXT_END 115
#define EMIDI_LOOP_START 116
#define EMIDI_LOOP_END 117
#define EMIDI_SONG_LOOP_START 118
#define EMIDI_SONG_LOOP_END 119
#define EMIDI_GeneralMIDI 0
#define EMIDI_SoundCanvas 1
#define EMIDI_AWE32 2
#define EMIDI_WaveBlaster 3
#define EMIDI_SoundBlaster 4
#define EMIDI_ProAudio 5
#define EMIDI_SoundMan16 6
#define EMIDI_Adlib 7
#define EMIDI_Ultrasound 9
#define EMIDI_AffectsCurrentCard( c, type ) \
( ( ( c ) == EMIDI_ALL_CARDS ) || ( ( c ) == ( type ) ) )
#define EMIDI_NUM_CONTEXTS 7
typedef struct
{
unsigned char *pos;
unsigned char *loopstart;
short loopcount;
short RunningStatus;
unsigned time;
long FPSecondsPerTick;
short tick;
short beat;
short measure;
short BeatsPerMeasure;
short TicksPerBeat;
short TimeBase;
long delay;
short active;
} songcontext;
typedef struct
{
unsigned char *start;
unsigned char *pos;
long delay;
boolean active;
short RunningStatus;
short currentcontext;
songcontext context[ EMIDI_NUM_CONTEXTS ];
boolean EMIDI_IncludeTrack;
boolean EMIDI_ProgramChange;
boolean EMIDI_VolumeChange;
} track;
static long _MIDI_ReadNumber( void *from, size_t size );
static long _MIDI_ReadDelta( track *ptr );
static void _MIDI_ResetTracks( void );
static void _MIDI_AdvanceTick( void );
static void _MIDI_MetaEvent( track *Track );
static void _MIDI_SysEx( track *Track );
static boolean _MIDI_InterpretControllerInfo(track *Track, boolean TimeSet, int32_t channel, int32_t c1, int32_t c2);
static void _MIDI_SendControlChange( int channel, int c1, int c2 );
static void _MIDI_SetChannelVolume( int channel, int volume );
static void _MIDI_SendChannelVolumes( void );
static void _MIDI_InitEMIDI( void );
static void MIDI_SetTempo(int32_t tempo);
static const int _MIDI_CommandLengths[ NUM_MIDI_CHANNELS ] =
{
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 2, 0
};
static track *_MIDI_TrackPtr = NULL;
static int _MIDI_TrackMemSize;
static int _MIDI_NumTracks;
static boolean _MIDI_SongActive = false;
static boolean _MIDI_SongLoaded = false;
static boolean _MIDI_Loop = false;
static task *_MIDI_PlayRoutine = NULL;
static int _MIDI_Division;
static int _MIDI_Tick = 0;
static int _MIDI_Beat = 1;
static int _MIDI_Measure = 1;
static unsigned _MIDI_Time;
static int _MIDI_BeatsPerMeasure;
static int _MIDI_TicksPerBeat;
static int _MIDI_TimeBase;
static long _MIDI_FPSecondsPerTick;
static unsigned _MIDI_TotalTime;
static int _MIDI_TotalTicks;
static int _MIDI_TotalBeats;
static int _MIDI_TotalMeasures;
static unsigned long _MIDI_PositionInTicks;
static int _MIDI_Context;
static int _MIDI_ActiveTracks;
#define MIDI_MaxVolume 255
static int _MIDI_TotalVolume = MIDI_MaxVolume;
static int _MIDI_ChannelVolume[ NUM_MIDI_CHANNELS ];
static midifuncs *_MIDI_Funcs = NULL;
static boolean Reset = false;
static int MIDI_Tempo = 120;
/*---------------------------------------------------------------------
Function: _MIDI_ReadNumber
Reads a variable length number from a MIDI track.
---------------------------------------------------------------------*/
static long _MIDI_ReadNumber
(
void *from,
size_t size
)
{
unsigned char *FromPtr;
long value;
if ( size > 4 )
{
size = 4;
}
FromPtr = ( unsigned char * )from;
value = 0;
while( size-- )
{
value <<= 8;
value += *FromPtr++;
}
return( value );
}
/*---------------------------------------------------------------------
Function: _MIDI_ReadDelta
Reads a variable length encoded delta delay time from the MIDI data.
---------------------------------------------------------------------*/
static long _MIDI_ReadDelta
(
track *ptr
)
{
long value;
unsigned char c;
GET_NEXT_EVENT( ptr, value );
if ( value & 0x80 )
{
value &= 0x7f;
do
{
GET_NEXT_EVENT( ptr, c );
value = ( value << 7 ) + ( c & 0x7f );
}
while ( c & 0x80 );
}
return( value );
}
/*---------------------------------------------------------------------
Function: _MIDI_ResetTracks
Sets the track pointers to the beginning of the song.
---------------------------------------------------------------------*/
static void _MIDI_ResetTracks
(
void
)
{
int i;
track *ptr;
_MIDI_Tick = 0;
_MIDI_Beat = 1;
_MIDI_Measure = 1;
_MIDI_Time = 0;
_MIDI_BeatsPerMeasure = 4;
_MIDI_TicksPerBeat = _MIDI_Division;
_MIDI_TimeBase = 4;
_MIDI_PositionInTicks = 0;
_MIDI_ActiveTracks = 0;
_MIDI_Context = 0;
ptr = _MIDI_TrackPtr;
for( i = 0; i < _MIDI_NumTracks; i++ )
{
ptr->pos = ptr->start;
ptr->delay = _MIDI_ReadDelta( ptr );
ptr->active = ptr->EMIDI_IncludeTrack;
ptr->RunningStatus = 0;
ptr->currentcontext = 0;
ptr->context[ 0 ].loopstart = ptr->start;
ptr->context[ 0 ].loopcount = 0;
if ( ptr->active )
{
_MIDI_ActiveTracks++;
}
ptr++;
}
}
/*---------------------------------------------------------------------
Function: _MIDI_AdvanceTick
Increment tick counters.
---------------------------------------------------------------------*/
static void _MIDI_AdvanceTick
(
void
)
{
_MIDI_PositionInTicks++;
_MIDI_Time += _MIDI_FPSecondsPerTick;
_MIDI_Tick++;
while( _MIDI_Tick > _MIDI_TicksPerBeat )
{
_MIDI_Tick -= _MIDI_TicksPerBeat;
_MIDI_Beat++;
}
while( _MIDI_Beat > _MIDI_BeatsPerMeasure )
{
_MIDI_Beat -= _MIDI_BeatsPerMeasure;
_MIDI_Measure++;
}
}
/*---------------------------------------------------------------------
Function: _MIDI_SysEx
Interpret SysEx Event.
---------------------------------------------------------------------*/
static void _MIDI_SysEx
(
track *Track
)
{
int length;
length = _MIDI_ReadDelta( Track );
Track->pos += length;
}
/*---------------------------------------------------------------------
Function: _MIDI_MetaEvent
Interpret Meta Event.
---------------------------------------------------------------------*/
static void _MIDI_MetaEvent
(
track *Track
)
{
int command;
int length;
int denominator;
int tempo;
GET_NEXT_EVENT( Track, command );
GET_NEXT_EVENT( Track, length );
switch( command )
{
case MIDI_END_OF_TRACK :
Track->active = false;
_MIDI_ActiveTracks--;
break;
case MIDI_TEMPO_CHANGE :
tempo = 60000000L / _MIDI_ReadNumber( Track->pos, 3 );
MIDI_SetTempo( tempo );
break;
case MIDI_TIME_SIGNATURE :
if ( ( _MIDI_Tick > 0 ) || ( _MIDI_Beat > 1 ) )
{
_MIDI_Measure++;
}
_MIDI_Tick = 0;
_MIDI_Beat = 1;
_MIDI_BeatsPerMeasure = (int)*Track->pos;
denominator = (int)*( Track->pos + 1 );
_MIDI_TimeBase = 1;
while( denominator > 0 )
{
_MIDI_TimeBase += _MIDI_TimeBase;
denominator--;
}
_MIDI_TicksPerBeat = ( _MIDI_Division * 4 ) / _MIDI_TimeBase;
break;
}
Track->pos += length;
}
/*---------------------------------------------------------------------
Function: _MIDI_InterpretControllerInfo
Interprets the MIDI controller info.
---------------------------------------------------------------------*/
static boolean _MIDI_InterpretControllerInfo(track *Track, boolean TimeSet, int32_t channel, int32_t c1, int32_t c2)
{
track *trackptr;
int tracknum;
int loopcount;
switch( c1 )
{
case MIDI_MONO_MODE_ON :
Track->pos++;
break;
case MIDI_VOLUME :
if ( !Track->EMIDI_VolumeChange )
{
_MIDI_SetChannelVolume( channel, c2 );
}
break;
case EMIDI_INCLUDE_TRACK :
case EMIDI_EXCLUDE_TRACK :
break;
case EMIDI_PROGRAM_CHANGE :
if ( Track->EMIDI_ProgramChange )
{
_MIDI_Funcs->ProgramChange(channel, c2 & 0x7f);
}
break;
case EMIDI_VOLUME_CHANGE :
if ( Track->EMIDI_VolumeChange )
{
_MIDI_SetChannelVolume( channel, c2 );
}
break;
case EMIDI_CONTEXT_START :
break;
case EMIDI_CONTEXT_END :
if ( ( Track->currentcontext == _MIDI_Context ) ||
( _MIDI_Context < 0 ) ||
( Track->context[ _MIDI_Context ].pos == NULL ) )
{
break;
}
Track->currentcontext = _MIDI_Context;
Track->context[ 0 ].loopstart = Track->context[ _MIDI_Context ].loopstart;
Track->context[ 0 ].loopcount = Track->context[ _MIDI_Context ].loopcount;
Track->pos = Track->context[ _MIDI_Context ].pos;
Track->RunningStatus = Track->context[ _MIDI_Context ].RunningStatus;
if ( TimeSet )
{
break;
}
_MIDI_Time = Track->context[ _MIDI_Context ].time;
_MIDI_FPSecondsPerTick = Track->context[ _MIDI_Context ].FPSecondsPerTick;
_MIDI_Tick = Track->context[ _MIDI_Context ].tick;
_MIDI_Beat = Track->context[ _MIDI_Context ].beat;
_MIDI_Measure = Track->context[ _MIDI_Context ].measure;
_MIDI_BeatsPerMeasure = Track->context[ _MIDI_Context ].BeatsPerMeasure;
_MIDI_TicksPerBeat = Track->context[ _MIDI_Context ].TicksPerBeat;
_MIDI_TimeBase = Track->context[ _MIDI_Context ].TimeBase;
TimeSet = true;
break;
case EMIDI_LOOP_START :
case EMIDI_SONG_LOOP_START :
if ( c2 == 0 )
{
loopcount = EMIDI_INFINITE;
}
else
{
loopcount = c2;
}
if ( c1 == EMIDI_SONG_LOOP_START )
{
trackptr = _MIDI_TrackPtr;
tracknum = _MIDI_NumTracks;
}
else
{
trackptr = Track;
tracknum = 1;
}
while( tracknum > 0 )
{
trackptr->context[ 0 ].loopcount = loopcount;
trackptr->context[ 0 ].pos = trackptr->pos;
trackptr->context[ 0 ].loopstart = trackptr->pos;
trackptr->context[ 0 ].RunningStatus = trackptr->RunningStatus;
trackptr->context[ 0 ].active = trackptr->active;
trackptr->context[ 0 ].delay = trackptr->delay;
trackptr->context[ 0 ].time = _MIDI_Time;
trackptr->context[ 0 ].FPSecondsPerTick = _MIDI_FPSecondsPerTick;
trackptr->context[ 0 ].tick = _MIDI_Tick;
trackptr->context[ 0 ].beat = _MIDI_Beat;
trackptr->context[ 0 ].measure = _MIDI_Measure;
trackptr->context[ 0 ].BeatsPerMeasure = _MIDI_BeatsPerMeasure;
trackptr->context[ 0 ].TicksPerBeat = _MIDI_TicksPerBeat;
trackptr->context[ 0 ].TimeBase = _MIDI_TimeBase;
trackptr++;
tracknum--;
}
break;
case EMIDI_LOOP_END :
case EMIDI_SONG_LOOP_END :
if ( ( c2 != EMIDI_END_LOOP_VALUE ) ||
( Track->context[ 0 ].loopstart == NULL ) ||
( Track->context[ 0 ].loopcount == 0 ) )
{
break;
}
if ( c1 == EMIDI_SONG_LOOP_END )
{
trackptr = _MIDI_TrackPtr;
tracknum = _MIDI_NumTracks;
_MIDI_ActiveTracks = 0;
}
else
{
trackptr = Track;
tracknum = 1;
_MIDI_ActiveTracks--;
}
while( tracknum > 0 )
{
if ( trackptr->context[ 0 ].loopcount != EMIDI_INFINITE )
{
trackptr->context[ 0 ].loopcount--;
}
trackptr->pos = trackptr->context[ 0 ].loopstart;
trackptr->RunningStatus = trackptr->context[ 0 ].RunningStatus;
trackptr->delay = trackptr->context[ 0 ].delay;
trackptr->active = trackptr->context[ 0 ].active;
if ( trackptr->active )
{
_MIDI_ActiveTracks++;
}
if ( !TimeSet )
{
_MIDI_Time = trackptr->context[ 0 ].time;
_MIDI_FPSecondsPerTick = trackptr->context[ 0 ].FPSecondsPerTick;
_MIDI_Tick = trackptr->context[ 0 ].tick;
_MIDI_Beat = trackptr->context[ 0 ].beat;
_MIDI_Measure = trackptr->context[ 0 ].measure;
_MIDI_BeatsPerMeasure = trackptr->context[ 0 ].BeatsPerMeasure;
_MIDI_TicksPerBeat = trackptr->context[ 0 ].TicksPerBeat;
_MIDI_TimeBase = trackptr->context[ 0 ].TimeBase;
TimeSet = true;
}
trackptr++;
tracknum--;
}
break;
default :
if ( _MIDI_Funcs->ControlChange )
{
_MIDI_Funcs->ControlChange( channel, c1, c2 );
}
}
return TimeSet;
}
/*---------------------------------------------------------------------
Function: _MIDI_ServiceRoutine
Task that interperates the MIDI data.
---------------------------------------------------------------------*/
static void _MIDI_ServiceRoutine
(
task *Task
)
{
int event;
int channel;
int command;
track *Track;
int tracknum;
int c1;
int c2;
boolean TimeSet = false;
UNUSED(Task);
if ( !_MIDI_SongActive )
{
return;
}
Track = _MIDI_TrackPtr;
tracknum = 0;
while( tracknum < _MIDI_NumTracks )
{
while ( ( Track->active ) && ( Track->delay == 0 ) )
{
GET_NEXT_EVENT( Track, event );
if ( GET_MIDI_COMMAND( event ) == MIDI_SPECIAL )
{
switch( event )
{
case MIDI_SYSEX :
case MIDI_SYSEX_CONTINUE :
_MIDI_SysEx( Track );
break;
case MIDI_META_EVENT :
_MIDI_MetaEvent( Track );
break;
}
if ( Track->active )
{
Track->delay = _MIDI_ReadDelta( Track );
}
continue;
}
if ( event & MIDI_RUNNING_STATUS )
{
Track->RunningStatus = event;
}
else
{
event = Track->RunningStatus;
Track->pos--;
}
channel = GET_MIDI_CHANNEL( event );
command = GET_MIDI_COMMAND( event );
if ( _MIDI_CommandLengths[ command ] > 0 )
{
GET_NEXT_EVENT( Track, c1 );
if ( _MIDI_CommandLengths[ command ] > 1 )
{
GET_NEXT_EVENT( Track, c2 );
}
}
switch ( command )
{
case MIDI_NOTE_OFF :
if ( _MIDI_Funcs->NoteOff )
{
_MIDI_Funcs->NoteOff( channel, c1, c2 );
}
break;
case MIDI_NOTE_ON :
if ( _MIDI_Funcs->NoteOn )
{
_MIDI_Funcs->NoteOn( channel, c1, c2 );
}
break;
case MIDI_POLY_AFTER_TCH :
if ( _MIDI_Funcs->PolyAftertouch )
{
_MIDI_Funcs->PolyAftertouch( channel, c1, c2 );
}
break;
case MIDI_CONTROL_CHANGE :
TimeSet = _MIDI_InterpretControllerInfo(Track, TimeSet, channel, c1, c2);
break;
case MIDI_PROGRAM_CHANGE :
if ( ( _MIDI_Funcs->ProgramChange ) &&
( !Track->EMIDI_ProgramChange ) )
{
_MIDI_Funcs->ProgramChange(channel, c1 & 0x7f);
}
break;
case MIDI_AFTER_TOUCH :
if ( _MIDI_Funcs->ChannelAftertouch )
{
_MIDI_Funcs->ChannelAftertouch( channel, c1 );
}
break;
case MIDI_PITCH_BEND :
if ( _MIDI_Funcs->PitchBend )
{
_MIDI_Funcs->PitchBend( channel, c1, c2 );
}
break;
default :
break;
}
Track->delay = _MIDI_ReadDelta( Track );
}
Track->delay--;
Track++;
tracknum++;
if ( _MIDI_ActiveTracks == 0 )
{
_MIDI_ResetTracks();
if ( _MIDI_Loop )
{
tracknum = 0;
Track = _MIDI_TrackPtr;
}
else
{
_MIDI_SongActive = false;
break;
}
}
}
_MIDI_AdvanceTick();
}
/*---------------------------------------------------------------------
Function: _MIDI_SendControlChange
Sends a control change to the proper device
---------------------------------------------------------------------*/
static void _MIDI_SendControlChange(int channel, int c1, int c2)
{
if ( _MIDI_Funcs == NULL )
return;
if ( _MIDI_Funcs->ControlChange == NULL )
return;
_MIDI_Funcs->ControlChange( channel, c1, c2 );
}
/*---------------------------------------------------------------------
Function: MIDI_AllNotesOff
Sends all notes off commands on all midi channels.
---------------------------------------------------------------------*/
static void MIDI_AllNotesOff(void)
{
int channel;
for( channel = 0; channel < NUM_MIDI_CHANNELS; channel++ )
{
_MIDI_SendControlChange( channel, 0x40, 0 );
_MIDI_SendControlChange( channel, MIDI_ALL_NOTES_OFF, 0 );
_MIDI_SendControlChange( channel, 0x78, 0 );
}
}
/*---------------------------------------------------------------------
Function: _MIDI_SetChannelVolume
Sets the volume of the specified midi channel.
---------------------------------------------------------------------*/
static void _MIDI_SetChannelVolume
(
int channel,
int volume
)
{
_MIDI_ChannelVolume[ channel ] = volume;
if ( _MIDI_Funcs == NULL )
{
return;
}
if ( _MIDI_Funcs->ControlChange == NULL )
{
return;
}
// For user volume
volume *= 256;
if ( _MIDI_Funcs->SetVolume == NULL )
{
volume *= _MIDI_TotalVolume;
volume /= MIDI_MaxVolume;
}
// For user volume
volume >>= 8;
_MIDI_Funcs->ControlChange( channel, MIDI_VOLUME, volume );
}
/*---------------------------------------------------------------------
Function: _MIDI_SendChannelVolumes
Sets the volume on all the midi channels.
---------------------------------------------------------------------*/
static void _MIDI_SendChannelVolumes
(
void
)
{
int channel;
for( channel = 0; channel < NUM_MIDI_CHANNELS; channel++ )
{
_MIDI_SetChannelVolume( channel, _MIDI_ChannelVolume[ channel ] );
}
}
/*---------------------------------------------------------------------
Function: MIDI_Reset
Resets the MIDI device to General Midi defaults.
---------------------------------------------------------------------*/
static void MIDI_Reset(void)
{
int channel;
long time;
unsigned flags;
MIDI_AllNotesOff();
flags = DisableInterrupts();
_enable();
time = clock() + CLOCKS_PER_SEC/24;
while(clock() < time)
;
RestoreInterrupts( flags );
for( channel = 0; channel < NUM_MIDI_CHANNELS; channel++ )
{
_MIDI_SendControlChange( channel, MIDI_RESET_ALL_CONTROLLERS, 0 );
_MIDI_SendControlChange( channel, MIDI_RPN_MSB, MIDI_PITCHBEND_MSB );
_MIDI_SendControlChange( channel, MIDI_RPN_LSB, MIDI_PITCHBEND_LSB );
_MIDI_SendControlChange( channel, MIDI_DATAENTRY_MSB, 2 ); /* Pitch Bend Sensitivity MSB */
_MIDI_SendControlChange( channel, MIDI_DATAENTRY_LSB, 0 ); /* Pitch Bend Sensitivity LSB */
_MIDI_ChannelVolume[ channel ] = GENMIDI_DefaultVolume;
}
_MIDI_SendChannelVolumes();
Reset = true;
}
/*---------------------------------------------------------------------
Function: MIDI_SetVolume
Sets the total volume of the music.
---------------------------------------------------------------------*/
void MIDI_SetVolume(int32_t volume)
{
if (_MIDI_Funcs == NULL)
return;
if (volume < 0)
volume = 0;
if (volume > MIDI_MaxVolume)
volume = MIDI_MaxVolume;
_MIDI_TotalVolume = volume;
if (_MIDI_Funcs->SetVolume)
_MIDI_Funcs->SetVolume(volume);
else
_MIDI_SendChannelVolumes();
}
/*---------------------------------------------------------------------
Function: MIDI_ContinueSong
Continues playback of a paused song.
---------------------------------------------------------------------*/
void MIDI_ContinueSong
(
void
)
{
if ( _MIDI_SongLoaded )
{
_MIDI_SongActive = true;
}
}
/*---------------------------------------------------------------------
Function: MIDI_PauseSong
Pauses playback of the current song.
---------------------------------------------------------------------*/
void MIDI_PauseSong
(
void
)
{
if ( _MIDI_SongLoaded )
{
_MIDI_SongActive = false;
MIDI_AllNotesOff();
}
}
/*---------------------------------------------------------------------
Function: MIDI_SetMidiFuncs
Selects the routines that send the MIDI data to the music device.
---------------------------------------------------------------------*/
void MIDI_SetMidiFuncs
(
midifuncs *funcs
)
{
_MIDI_Funcs = funcs;
}
/*---------------------------------------------------------------------
Function: MIDI_StopSong
Stops playback of the currently playing song.