-
Notifications
You must be signed in to change notification settings - Fork 65
/
mlimbs.c
1350 lines (1202 loc) · 52.2 KB
/
mlimbs.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) 2015-2018 Night Dive Studios, LLC.
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
* $Source: r:/prj/cit/src/RCS/mlimbs.c $
* $Revision: 1.32 $
* $Author: dc $
* $Date: 1994/11/23 00:13:15 $
*/
#include <string.h>
#include "mlimbs.h"
#define CHANNEL_MAP
#define LOCK_ALL_CHANNELS
//uchar mlimbs_on = FALSE;
//char mlimbs_status = 0; // could make this one bitfield of status, on/off, enable/not, so on
int mlimbs_timer_id; // what our timer handle is
static uchar *mlimbs_theme = NULL; // data about the current theme
volatile int mlimbs_master_slot = -1;
static int mlimbs_cur_theme_id = -1;
volatile struct mlimbs_piece_info xseq_info[MAX_SEQUENCES]; // Sequence specific information
volatile struct mlimbs_request_info current_request[MLIMBS_MAX_SEQUENCES - 1]; // Request information
volatile struct mlimbs_channel_info channel_info[MLIMBS_MAX_CHANNELS]; // MIDI channel information
volatile struct mlimbs_playing_info userID[MLIMBS_MAX_SEQUENCES - 1]; // Sequence instance specific information
volatile uchar mlimbs_update_requests = FALSE;
volatile uchar num_XMIDI_sequences = 0;
volatile uchar num_master_measures;
volatile uchar voices_used = 0;
volatile uchar max_voices = 0;
volatile void (*mlimbs_AI)(void) = NULL;
volatile ulong mlimbs_counter = 0;
volatile long mlimbs_error;
volatile uchar mlimbs_semaphore = FALSE;
int master_volume = 100;
char curr_play_list[10];
char loop_list[10];
char cpl_num;
// gruesome hacks to try and get this working for now
int mlimbs_priority[MLIMBS_MAX_SEQUENCES];
// convienience psuedo-function defines
#define _uiD_seq(i) ((SEQUENCE *)snd_get_sequence(userID[i].seq_id))
#define _mlimbs_top \
if (mlimbs_status == 0) \
return -1
// LONG mlimbs_timbre_callback(MDI_DRIVER *mdi, LONG bank, LONG patch);
/////////////////////////////////////////////////////////////////
// mlimbs_init (void)
//
// purpose:
// This routine initializes the MLIMBS system. This also
// must be used if you want to switch the SndMIDIDevice that
// MLIMBS uses. This must be called before using mlimbs.
//
// inputs:
// now uses the default global midi device always
int mlimbs_init(void) {
int i;
// if (!music_card) return -1;
if (mlimbs_status != 0)
return 1;
for (i = 0; i < 10; i++) {
curr_play_list[i] = -1;
loop_list[i] = -1;
}
cpl_num = 0;
/* What is max_voices???
// Determine maximum number of voices
#ifdef AIL_2
case SOUNDBLASTER:
case SOUNDBLASTERPRO:
case ADLIB:
max_voices = 9;
break;
case SOUNDBLASTERPRO2:
max_voices = 18;
case MT32:
// Switch max_voices to 32 when Roland specific dat files are
// available, since voices usage should be tracked using partials on a Roland.
// max_voices = 32; // Note that this here is actually the # of partials available.
max_voices = 9; // For now, since we're using only SB/ADLIB dat files, set voice
limit to 9. master_volume = 90; // Reduces distortion in Roland MT-32's break; default: max_voices = 9; break;
}
#else
max_voices=18;
#endif
snd_set_midi_sequences(MLIMBS_MAX_SEQUENCES);
#ifdef CALLBACK_ON
// Install mlimbs_callback, which will be called by the master sequence twice per loop.
// each one has a different value, one means half done, get ready, the other means do the switch
seq_miditrig=mlimbs_callback;
AIL_register_timbre_callback((MDI_DRIVER *)snd_midi,mlimbs_timbre_callback);
// seq_finish=mlimbs_seq_done_call;
// Since we cannot stop and start XMIDI sequences from within a MIDI callback, use
// a timer, running at 100 hz to start and stop XMIDI sequences. The mlimbs_callback
// simply sets a flag, letting the timer know when to update sequence status.
if ((mlimbs_timer_id = tm_add_process(mlimbs_timer_callback, 0, (TMD_FREQ/MLIMBS_TIMER_FREQUENCY))) == -1)
goto die;
#endif
#ifdef LOCK_ALL_CHANNELS
// Lock all MIDI channels on the current mlimbs_device, and
// initialize the channel_info[] array.
for (i = 0; i < MLIMBS_MAX_CHANNELS; i++)
{
channel_info[i].mchannel = AIL_lock_channel((MDI_DRIVER *)snd_midi);
channel_info[i].usernum = -1; // This indicates what sequence is using this channel. -1 is stale
handle. channel_info[i].status = MLIMBS_STOPPED; // Current status of the channel.
}
#endif
*/
mlimbs_status = 1;
return 1;
die:
mlimbs_shutdown();
return -1;
}
/////////////////////////////////////////////////////////////////
// mlimbs_shutdown
//
// purpose:
// This shuts down the mlimbs system. After calling this,
// call mlimbs_init again to restart it.
//
/////////////////////////////////////////////////////////////////
void mlimbs_shutdown(void) {
if (mlimbs_status == 0)
return;
/* later, man
mlimbs_purge_theme();
#ifdef CALLBACK_ON
seq_miditrig=NULL;
// seq_finish=NULL;
AIL_register_timbre_callback((MDI_DRIVER *)snd_midi,NULL);
tm_remove_process(mlimbs_timer_id);
#endif
#ifdef LOCK_ALL_CHANNELS
{ // Release all the locked MIDI channels, and clear the channel_info[] array
int i;
for (i = 0; i < MLIMBS_MAX_CHANNELS; i++)
{
if (channel_info[i].mchannel >= 0) AIL_release_channel((MDI_DRIVER
*)snd_midi,channel_info[i].mchannel); channel_info[i].mchannel = -1; channel_info[i].usernum = -1;
channel_info[i].status = MLIMBS_STOPPED;
}
}
#endif
*/
mlimbs_status = 0;
}
#ifdef NOT_YET //
/////////////////////////////////////////////////////////////////
// int mlimbs_load_theme
//
// purpose:
// Load theme will load an XMIDI file into theme, stopping
// playback and purging the previous XMIDI file from theme.
// It will also allocate state tables if needed and preload all
// timbres used by any sequence in the XMIDI file.
//
// inputs:
// char *xname Filename for the XMIDI theme file.
// char *xinfo Filename for the file to be used in filling out xseq_info[].
// char *GTL_filename Global Timbre Library filename, for preloading all timbres.
// return:
// 1 if successful
// -1 if unsuccessful
// Also set mlimbs_error to the following values if unsuccessful:
// -2 if it failed to load the XMIDI file
// -3 if it failed to load the data file.
/////////////////////////////////////////////////////////////////
int mlimbs_load_theme(char *xname, char *xinfo, int thmid) {
int i;
FILE *fil;
// secret_sprint((ss_temp,"try to load themed %s (%d) (%d)\n",xname,thmid,mlimbs_status));
_mlimbs_top;
mlimbs_purge_theme(); // Purge any previously loaded mlimbs theme
if ((mlimbs_theme = snd_load_raw(xname, NULL)) == NULL)
return -2;
if ((fil = fopen(xinfo, "rb")) == NULL)
return -3;
mlimbs_cur_theme_id = thmid;
fread(&num_XMIDI_sequences, sizeof(uchar), 1,
fil); // Number of sequences in this XMIDI file, NOT COUNTING THE MASTER SEQUENCE
fread(&num_master_measures, sizeof(uchar), 1,
fil); // Number of measures in the master sequence - i.e. # of measures per loop
num_XMIDI_sequences = min(num_XMIDI_sequences, MAX_SEQUENCES);
for (i = 0; i < num_XMIDI_sequences; i++) {
fread(&(xseq_info[i].max_voices), sizeof(uchar), 1, fil); // Maximum # of simultaneous voices
fread(&(xseq_info[i].avg_voices), sizeof(uchar), 1, fil); // Normal # of simultaneous voices
fread(&(xseq_info[i].channel_map), sizeof(ushort), 1, fil); // Bitmap of channel usage
fread(&(xseq_info[i].num_measures), sizeof(uchar), 1, fil); // # of measures
xseq_info[i].num_measures = max(
1,
xseq_info[i].num_measures); // Don't allow 0 measure chunks (prevents divide by 0 in mlimbs_timer_callback)
fread(&(xseq_info[i].priority), sizeof(int), 1, fil); // Default priority
fread(xseq_info[i].channel_voices, sizeof(uchar), 7, fil); // Now read in channel voice usage
}
fclose(fil);
// secret_sprint((ss_temp,"load themed %s (%d) a-ok\n",xname,thmid));
return 1;
}
/////////////////////////////////////////////////////////////////
// int mlimbs_start_theme
//
// purpose:
// This begins playback of the theme by playing the
// master_sequence (sequence 0). NOTE: This doesn't clear
// the current_request array and doesn't reset mlimbs_counter.
//
// inputs:
//
// return:
// 1 if successful
// -1 if not
// Sets mlimbs_error to the following error codes if unsuccessful.
// -1 if no mlimbs device present
// -2 if it failed to load the master track.
// -3 if it failed to start the master track.
//
/////////////////////////////////////////////////////////////////
int mlimbs_start_theme(void) {
if ((mlimbs_status == 0) || (mlimbs_theme == NULL))
return -1; // no can do
if (mlimbs_master_slot >= 0)
return 1; // already can done
if ((mlimbs_master_slot = snd_sequence_play(mrefBuild(mlimbs_cur_theme_id, CALLBACK_SEQ_NUM), mlimbs_theme,
CALLBACK_SEQ_NUM, NULL)) ==
SND_PERROR) { // better make sure this is looping, eh...
return -2;
}
AIL_set_sequence_loop_count(snd_sequence_ptr_from_id(mlimbs_master_slot), 0);
#ifdef CALLBACK_ON
// Start the mlimbs timer callback which will now start and stop
// various tracks according to how the current_request structures are set
tm_activate_process(mlimbs_timer_id);
#endif
// secret_sprint((ss_temp,"start theme %d callback %d\n",mlimbs_cur_theme_id,mlimbs_timer_id));
return 1;
}
// i hate these, thank you
void _mlimbs_clear_req(int i) {
current_request[i].pieceID = -1;
current_request[i].rel_vol = DEFAULT_REL_VOL;
current_request[i].ramp_time = DEFAULT_RAMP_TIME;
current_request[i].ramp = 0;
current_request[i].priority = 0;
current_request[i].loops = -1;
current_request[i].pan = -1;
current_request[i].channel_prioritize = FALSE;
current_request[i].crossfade = 0;
}
void _mlimbs_clear_uid(int i) {
int j;
userID[i].pieceID = -1;
userID[i].current_channel_map = 0;
userID[i].seq_id = -1;
userID[i].rel_vol = DEFAULT_REL_VOL;
userID[i].channel_prioritize = FALSE;
userID[i].crossfade_status = 11;
for (j = 0; j < 7; j++)
userID[i].sequence_channel_status[j] = SEQUENCE_CHANNEL_UNUSED;
}
/////////////////////////////////////////////////////////////////
// void mlimbs_stop_theme
//
// purpose:
// This routine stops all mlimbs XMIDI playback. After
// calling this routine, a call to mlimbs_start_theme is
// needed to restart playback. Note that this doesn't
// clear the current_request[] array and doesn't reset the
// mlimbs_counter.
//
//
// inputs:
//
//
/////////////////////////////////////////////////////////////////
void mlimbs_stop_theme(void) {
int i;
if (mlimbs_status == 0)
return;
/* Stop all sequences */
#ifdef CALLBACK_ON
tm_deactivate_process(mlimbs_timer_id);
#endif
mlimbs_update_requests = FALSE;
for (i = 0; i < MLIMBS_MAX_SEQUENCES - 1; i++) {
mlimbs_punt_piece(i);
_mlimbs_clear_req(i);
}
mlimbs_master_slot = -1;
voices_used = 0;
// secret_sprint((ss_temp,"stop theme %d callback %d\n",mlimbs_cur_theme_id,mlimbs_timer_id));
}
/////////////////////////////////////////////////////////////////
// void mlimbs_purge_theme
//
// purpose:
// This routine, stops all XMIDI sequence playback, unloads
// the XMIDI sequence from theme, and clears the xseq_info[]
// array. Note that it also reinitializes the current_request[]
// and xseq_info[] arrays.
//
// inputs:
//
//
/////////////////////////////////////////////////////////////////
void mlimbs_purge_theme(void) {
int i;
mlimbs_update_requests = FALSE;
/* Clear the current_request[] and arrays */
for (i = 0; i < MLIMBS_MAX_SEQUENCES - 1; i++) {
_mlimbs_clear_req(i);
_mlimbs_clear_uid(i);
}
memset(xseq_info, 0, sizeof(xseq_info)); // make sure this is full size
for (i = 0; i < MAX_SEQUENCES; i++)
xseq_info[i].num_measures = 1;
mlimbs_master_slot = -1;
if (mlimbs_status == 0)
return;
#ifdef CALLBACK_ON
tm_deactivate_process(mlimbs_timer_id);
#endif
snd_kill_all_sequences();
if (mlimbs_theme != NULL)
Free(mlimbs_theme);
mlimbs_theme = NULL;
mlimbs_update_requests = FALSE;
#ifdef LOCK_ALL_CHANNELS
for (i = 0; i < MLIMBS_MAX_CHANNELS; i++) {
channel_info[i].usernum = -1;
channel_info[i].status = MLIMBS_STOPPED;
}
#endif
num_XMIDI_sequences = 0;
num_master_measures = 1;
voices_used = 0;
mlimbs_counter = 0;
// secret_sprint((ss_temp,"purge theme %d callback %d\n",mlimbs_cur_theme_id,mlimbs_timer_id));
}
/////////////////////////////////////////////////////////////////
//
// mlimbs_mute_sequence_channel
//
// purpose: This will free a physical channel being used by a chunk's
// sequence channel and will set the sequence channel's status to
// SEQUENCE_CHANNEL_MUTED, or SEQUENCE_CHANNEL_PENDING. This routine is
// used when a chunk must give up a channel to a higher priority chunk,
// or when a chunk must be muted.
//
// inputs:
// int usernum // The sequence instance to give up a channel.
// int x // Which channel to relenquish.
// uchar mute // If TRUE, mute the channel. If FALSE merely,
// // relenquish the channel, and set the sequence channel's
// // status to SEQUENCE_CHANNEL_PENDING
/////////////////////////////////////////////////////////////////
void mlimbs_mute_sequence_channel(int usernum, int x, uchar mute) {
int val, seq_ch;
int phys_ch;
if (usernum < 0)
return;
if (userID[usernum].pieceID < 0)
return;
// secret_sprint((ss_temp,"want to mute %d's %d, %d\n",usernum,x,mute));
if (mute == TRUE) {
switch (userID[usernum].sequence_channel_status[x - 10]) {
case SEQUENCE_CHANNEL_PENDING:
userID[usernum].sequence_channel_status[x - 10] = SEQUENCE_CHANNEL_MUTED;
case SEQUENCE_CHANNEL_MUTED:
case SEQUENCE_CHANNEL_UNUSED:
return;
default:
break;
}
} else if (userID[usernum].sequence_channel_status[x - 10] < 0)
return;
phys_ch = userID[usernum].sequence_channel_status[x - 10];
if (channel_info[phys_ch].status == MLIMBS_PLAYING_PIECE) { /* Remap the channel if it is playing in a sequence */
// secret_sprint((ss_temp,"is playing, remapping it %d from %d\n",phys_ch,x));
seq_ch = x;
if (seq_ch >= 11) { /* First check if the XMIDI bank select controller was set*/
/* If it's < 0,this means the controller wasn't initialized. Thus, we need to initialize it
to 0 in order for the stop/resume trick to work */
val = AIL_controller_value(_uiD_seq(usernum), seq_ch, XMIDI_BANK_SELECT);
#ifdef SND_CHANGE
// hey, this isnt supported in AIL3
if ((val < 0) || (val > 127))
AIL_set_controller_value(_uiD_seq(usernum), seq_ch, XMIDI_BANK_SELECT, 0);
#endif
if ((val < 0) || (val > 127)) {
AIL_send_channel_voice_message(NULL, _uiD_seq(usernum), MIDI_CONTROL_CHANGE | (seq_ch - 1),
XMIDI_BANK_SELECT, 0);
}
AIL_stop_sequence(_uiD_seq(usernum));
AIL_map_sequence_channel(_uiD_seq(usernum), seq_ch, seq_ch);
AIL_resume_sequence(_uiD_seq(usernum));
channel_info[phys_ch].status = MLIMBS_STOPPED;
}
}
/* Indicate that this physical channel is free */
channel_info[phys_ch].usernum = -1;
channel_info[phys_ch].sequence_channel = -1;
voices_used -= xseq_info[userID[usernum].pieceID].channel_voices[x - 10];
userID[usernum].sequence_channel_status[x - 10] =
(mute == TRUE) ? SEQUENCE_CHANNEL_MUTED : SEQUENCE_CHANNEL_PENDING;
userID[usernum].current_channel_map &= ~(0x0001 << phys_ch);
// secret_sprint((ss_temp,"is now free\n"));
}
//////////////////////////////////////////////////////////////////
// mlimbs_unmute_sequence_channel
//
// This routine attempts to map a sequence channel of a given
// chunk to a physical channel. It searches for physical channels
// not currently used by other chunks.
//
// NOTE: mlimbs_unmute_sequence_channel will not attempt to free
// channels or voices.
//
//////////////////////////////////////////////////////////////////
int mlimbs_unmute_sequence_channel(int usernum, int x) {
int i;
if (usernum < 0)
return 1;
if (userID[usernum].pieceID < 0)
return 1;
// secret_sprint((ss_temp,"unmuting %d of %d\n",usernum,x));
switch (userID[usernum].sequence_channel_status[x - 10]) {
case SEQUENCE_CHANNEL_MUTED:
userID[usernum].sequence_channel_status[x - 10] = SEQUENCE_CHANNEL_PENDING;
case SEQUENCE_CHANNEL_PENDING:
break;
case SEQUENCE_CHANNEL_UNUSED:
default:
return 1;
}
/* First, make sure there are enough voices */
if (xseq_info[userID[usernum].pieceID].channel_voices[x - 10] + voices_used > max_voices)
return -1;
/* Now, look for unused channels */
for (i = 0; i < MLIMBS_MAX_CHANNELS; i++) {
if (channel_info[i].status == MLIMBS_STOPPED) {
// secret_sprint((ss_temp,"undoing %d (%d)\n",i,channel_info[i].mchannel));
AIL_stop_sequence(_uiD_seq(usernum));
AIL_map_sequence_channel(_uiD_seq(usernum), x, channel_info[i].mchannel);
AIL_resume_sequence(_uiD_seq(usernum));
channel_info[i].status = MLIMBS_PLAYING_PIECE;
channel_info[i].usernum = usernum;
channel_info[i].sequence_channel = x;
voices_used += xseq_info[userID[usernum].pieceID].channel_voices[x - 10];
userID[usernum].sequence_channel_status[x - 10] = i;
userID[usernum].current_channel_map |= (0x0001 << i);
return 1;
}
}
// secret_sprint((ss_temp,"we lost, no joy\n"));
return -1;
}
/////////////////////////////////////////////////////////////////
// mlimbs_channel_prioritize
//
// This routine attempts to acquire free channels and voices
// from lower priority chunks for a given chunk.
// For channels used by lower priority chunks, this routine simply
// frees them. Call mlimbs_assign_channels to give them to a
// chunk. The minimum number of voices required is the number of
// voices the chunk plays on channel 10. If not enough voices
// can be freed, then nothing is punted, and the chunk is not played.
// If channel_prioritize is set to FALSE, then mlimbs_channel_prioritize
// will attempt to acquire all necessary free channels and voices,else
// fail.
//
/////////////////////////////////////////////////////////////////
int mlimbs_channel_prioritize(int priority, int pieceID, int voices_needed, uchar crossfade, uchar channel_prioritize) {
int i, j;
int channels_needed, num_free_channels;
int seq_punted;
int voices_punted;
int punt_list[MLIMBS_MAX_SEQUENCES];
int min;
int min_voices_needed;
if (pieceID >= num_XMIDI_sequences)
return -1;
channels_needed = num_free_channels = seq_punted = 0;
voices_punted = 0;
/* the punt_list is a list of userID[] entries */
for (i = 0; i < MLIMBS_MAX_SEQUENCES; i++)
punt_list[i] = -1;
/* First, count free physical_channels */
for (i = 0; i < MLIMBS_MAX_CHANNELS; i++)
if (channel_info[i].usernum == -1)
num_free_channels++;
/* Count # of channels needed */
if (crossfade != TRUE || channel_prioritize == FALSE) {
for (i = 11, channels_needed = 0; i < 16; i++)
if ((xseq_info[pieceID].channel_map >> i) & (0x0001))
channels_needed++;
} else
channels_needed = 0; // If we're crossfading, mlimbs_channel_prioritize
// doesn't need to get channels for this piece, right now.
// Minimum # of voices needed to play = # of voices on channel 10 since MIDI on
// channel 10 is never remapped to a nonphysical channel
min_voices_needed = xseq_info[pieceID].channel_voices[0] - (max_voices - voices_used);
// If we're crossfading, we only needed the minimum # of voices.
if (crossfade == TRUE && channel_prioritize == TRUE)
voices_needed = min_voices_needed;
if (voices_needed <= 0)
if (channels_needed <= num_free_channels)
return 1;
/*************************************/
/* Find all the channels we can punt */
/* and voices we can free. */
/*************************************/
for (i = 0; i < MLIMBS_MAX_SEQUENCES - 1; i++) {
if (userID[i].pieceID >= 0) {
// index mlimbs_priority[] with i+1 because the master track is
// already in slot 0, while userID[0] corresponds to slot 1.
if (mlimbs_priority[i + 1] > priority)
continue;
punt_list[seq_punted] = i;
seq_punted++;
// For channel_prioritized chunks, we can steal individual channels and voices. For non
// channel prioritized chunks, you have to punt the entire piece.
if (userID[i].channel_prioritize == TRUE) {
for (j = 16; j > 10; j--) {
/* If this sequence channel is currently using a physical channel...*/
if (userID[i].sequence_channel_status[j - 10] >= 0) {
num_free_channels++;
voices_punted += xseq_info[userID[i].pieceID].channel_voices[j - 10];
}
}
voices_punted += xseq_info[userID[i].pieceID].channel_voices[0];
} else {
#ifdef USE_MAX_VOICES
voices_punted += xseq_info[userID[i].pieceID].max_voices;
#else
voices_punted += xseq_info[userID[i].pieceID].avg_voices;
#endif
for (j = 0; j < MLIMBS_MAX_CHANNELS; j++)
if ((userID[i].current_channel_map >> j) & (0x0001))
num_free_channels++;
}
}
}
// If there aren't enough voices or channels, then
// do not punt anything.
if (channel_prioritize == TRUE) {
if ((voices_punted < min_voices_needed) || (num_free_channels == 0))
return -1;
} else {
if ((voices_punted < voices_needed) || (num_free_channels < channels_needed))
return -1;
}
// Now, punt as many voices and channels that we can
voices_punted = 0;
while ((voices_punted < voices_needed) || (num_free_channels < channels_needed)) {
// Each time through, find the lowest priority thing in the punt list
min = -1;
for (i = 0; i < seq_punted; i++) {
if (punt_list[i] >= 0) {
if (min < 0)
min = i;
if (mlimbs_priority[punt_list[i] + 1] < mlimbs_priority[punt_list[min] + 1])
min = i;
}
}
if (min < 0) {
if (channel_prioritize == TRUE) {
if (voices_punted < min_voices_needed)
return -1;
else
return 1; // Its o.k. not to have all necessary channels
} else
return -1;
}
// For channel_prioritized chunks, we can steal individual channels and voices. For non
// channel prioritized chunks, you have to punt the entire piece.
if (userID[punt_list[min]].channel_prioritize == TRUE) {
for (j = 16; j > 10; j--) {
if (userID[punt_list[min]].sequence_channel_status[j - 10] >= 0) {
num_free_channels++;
voices_punted += xseq_info[userID[punt_list[min]].pieceID].channel_voices[j - 10];
mlimbs_mute_sequence_channel(punt_list[min], j, FALSE);
}
if ((voices_punted >= voices_needed) && (num_free_channels >= channels_needed))
break;
}
// If punting channels 11-16 wasn't enough, punt entire
// piece, thus freeing channel 10 voices as well
if (j == 10) {
mlimbs_punt_piece(punt_list[min]);
voices_punted += xseq_info[userID[punt_list[min]].pieceID].channel_voices[0];
} else {
// If we got enough voices and channels, only punt entire
// piece if nothing is playing on channel 10
for (j = 16; j > 10; j--) {
if (userID[punt_list[min]].sequence_channel_status[j - 10] >= 0)
break;
}
if ((j == 10) && (xseq_info[userID[punt_list[min]].pieceID].channel_voices[0] <= 0)) {
mlimbs_punt_piece(punt_list[min]);
voices_punted += xseq_info[userID[punt_list[min]].pieceID].channel_voices[0];
}
}
}
else {
for (j = 0; j < MLIMBS_MAX_CHANNELS; j++)
if ((userID[punt_list[min]].current_channel_map >> j) & (0x0001))
num_free_channels++;
#ifdef USE_MAX_VOICES
voices_punted += xseq_info[userID[punt_list[min]].pieceID].max_voices;
#else
voices_punted += xseq_info[userID[punt_list[min]].pieceID].avg_voices;
#endif
mlimbs_punt_piece(punt_list[min]);
}
punt_list[min] = -1;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// int mlimbs_assign_channels
//
// purpose:
// This routine will mark the userID[].sequence_channel_status[] array
// according to what sequence channels need to use. It then maps as
// many sequence channels as it can to physical channels, in order,
// from 11 to 16, unless the crossfade argument is TRUE.
//
// inputs:
// int usernum
// bool crossfade // If TRUE, all the sequence channels used by
// // the sequence are set to SEQUENCE_CHANNEL_MUTED so crossfade
// // code can unmute channels as the sequence is crossfaded in.
//
/////////////////////////////////////////////////////////////////
int mlimbs_assign_channels(int usernum, uchar crossfade) {
uint j;
ushort c_map;
if (usernum >= (MLIMBS_MAX_SEQUENCES - 1))
return -1;
c_map = xseq_info[userID[usernum].pieceID].channel_map;
if (c_map == 0)
return 1; // If no channels are requested from dynamic channel allocation, return.
for (j = 11; j < 17; j++) {
if ((c_map >> (j - 1)) & 0x0001) { // Mark the sequence channel as waiting for a physical channel
// unless the sequence is to be crossfaded in, in which
// case, mute all channels.
if (crossfade == FALSE) {
userID[usernum].sequence_channel_status[j - 10] = SEQUENCE_CHANNEL_PENDING;
if (mlimbs_unmute_sequence_channel(usernum, j) < 0) {
if (userID[usernum].channel_prioritize == FALSE)
return -1;
}
} else
userID[usernum].sequence_channel_status[j - 10] = SEQUENCE_CHANNEL_MUTED;
} else
userID[usernum].sequence_channel_status[j - 10] = SEQUENCE_CHANNEL_UNUSED;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// int mlimbs_play_piece
//
// purpose:
// This routine will begin playback of a single 'snippet' in
// the theme.
//
// inputs:
// int pieceID Index into the xseq_info array.
// int priority Priority of the request. If < 0, then
// use the default priority (in xseq_info[]).
// int loops Number of times to play the piece
// int rel_vol Relative volume to start the piece at. This will
// be set to 0 for pieces that will ramp up.
// uchar channel_prioritize - if TRUE, then its ok to play the
// chunk even if not enough channels are available,
// If FALSE, then only play chunk if all channels can
// be played.
// uchar crossfade
//
// return:
// -1 if failed
// usernum - an integer index into the userID[] array
/////////////////////////////////////////////////////////////////
int mlimbs_play_piece(int pieceID, int priority, int loops, int rel_vol, uchar channel_prioritize, uchar crossfade) {
int i, slot, usernum, voices_needed, voices_available;
if (pieceID >= num_XMIDI_sequences)
return -1;
if (loops == 0)
return 1;
// secret_sprint((ss_temp,"play piece %d %d %d %d %d %d\n",pieceID,
// priority,loops,rel_vol,channel_prioritize,crossfade));
// Check for duplicate piece IDs?
if (cpl_num < 10) {
for (i = 0; i < cpl_num; i++)
if (curr_play_list[i] == pieceID) {
// secret_sprint((ss_temp,"cur play list %d already has %d\n",i,pieceID));
return 1;
}
} else
Warning(("BADNESS! cpl_num = %d!\n"));
if (priority < 0)
priority = xseq_info[pieceID].priority;
voices_needed = 0;
voices_available = max_voices - voices_used;
// First, free as many voices and channels as possible.
if (channel_prioritize) { // Only need to count # of voices needed if we're not crossfading. since channel 10
// voices
// are used by mlimbs_channel_prioritize as the # of voices needed if we are crossfading
if (crossfade != TRUE) {
for (i = 0, voices_needed = 0; i < 7; i++)
voices_needed += xseq_info[pieceID].channel_voices[i];
voices_needed -= voices_available;
}
if (mlimbs_channel_prioritize(priority, pieceID, voices_needed, crossfade, TRUE) < 0)
return -1;
} else {
#ifdef USE_MAX_VOICES
voices_needed = -(voices_available - xseq_info[pieceID].max_voices);
#else
voices_needed = -(voices_available - xseq_info[pieceID].avg_voices);
#endif
if (mlimbs_channel_prioritize(priority, pieceID, voices_needed, crossfade, FALSE) < 0)
return -1;
}
/***********************************/
/* Now, attempt to load the piece. */
/***********************************/
slot = snd_sequence_play(mrefBuild(mlimbs_cur_theme_id, pieceID + 1), mlimbs_theme, pieceID + 1, NULL);
usernum = slot - 1;
// secret_sprint((ss_temp,"play piece got slot %d for %d\n",slot,pieceID));
if (slot >= 0) {
userID[usernum].pieceID = pieceID; // What is going on here?
userID[usernum].seq_id = slot;
userID[usernum].channel_prioritize = channel_prioritize;
userID[usernum].crossfade_status = 11;
// Note: tallying of voice usage is now done in mlimbs_assign channels. Only add channel 10 here
voices_used += xseq_info[pieceID].channel_voices[0];
if (cpl_num < 10)
curr_play_list[cpl_num++] = pieceID;
if (mlimbs_assign_channels(usernum, crossfade) == -1) { /* Try and assign the channels and voices freed above */
mlimbs_punt_piece(usernum);
return -1;
}
// Now set the initial volume
AIL_set_sequence_volume(_uiD_seq(usernum), (unsigned)rel_vol * master_volume / 100, 0);
return (usernum); // Return entry of userID[]
} else
return -1;
}
// CHANGE - now resets pieceID as well (inside clearUid)
// we can clearly change it back if we want to
// void mlimbs_punt_piece
// This routine will stop playback of a single 'snippet' in the theme.
// inputs: usernum Index into the userID array.
void mlimbs_punt_piece(int usernum) {
int i, slot, ch;
if (userID[usernum].pieceID < 0 || userID[usernum].pieceID >= num_XMIDI_sequences)
return;
slot = usernum + 1;
// secret_sprint((ss_temp,"looking to punt slot %d (unum %d)\n",slot,usernum));
if (slot >= 0) {
if (cpl_num < 10) {
for (i = 0; i < cpl_num; i++) {
if (curr_play_list[i] == userID[usernum].pieceID) {
if (i != (cpl_num - 1)) {
curr_play_list[i] = curr_play_list[cpl_num - 1];
loop_list[i] = loop_list[cpl_num - 1];
}
cpl_num--;
}
}
}
// secret_sprint((ss_temp,"end seq it is\n",slot,usernum));
snd_end_sequence(slot);
for (i = 11; i < 17; i++) {
ch = userID[usernum].sequence_channel_status[i - 10];
if (ch >= 0) {
voices_used -= xseq_info[userID[usernum].pieceID].channel_voices[i - 10];
channel_info[ch].usernum = -1;
channel_info[ch].sequence_channel = -1;
if (channel_info[ch].status == MLIMBS_PLAYING_PIECE)
channel_info[ch].status = MLIMBS_STOPPED;
}
userID[usernum].sequence_channel_status[i - 10] = SEQUENCE_CHANNEL_UNUSED;
}
voices_used -= xseq_info[userID[usernum].pieceID].channel_voices[0];
_mlimbs_clear_uid(usernum);
}
}
////////////////////////////////////////////////////////////////
// mlimbs_get_crossfade_status
//
// purpose:
// Given a chunkID, it returns its current crossfade_status.
// Whether it is getting crossfaded in or out depends on
// the crossfade field in the current_request structure. It
// basically searches the array of userID[]'s for a userID that
// is currently playing the given chunk.
//
// inputs:
// int pieceID - id of the chunk to get the status of
// outputs:
// >= 0 - the correct crossfade_status
// 0 - not crossfading)
// 10-16 - next channel to be crossfaded
//
// < 0 - the chunk is not currently playing.
//
///////////////////////////////////////////////////////////////
schar mlimbs_get_crossfade_status(int pieceID) {
int i;
for (i = 0; i < MLIMBS_MAX_SEQUENCES - 1; i++)
if (userID[i].pieceID == pieceID)
return userID[i].crossfade_status;
return -1;
}
////////////////////////////////////////////////////////////////
// mlimbs_callback
//
// purpose:
// This is the routine that gets called by an XMIDI controller
// 119. All the mlimbs_callback does is call the mlimbs_AI, and
// then set mlimbs_update_requests to TRUE, which tells the mlimbs_timer_callback
// to actual execute its code, when next it is called.
//
////////////////////////////////////////////////////////////////
#pragma disable_message(202)
void cdecl mlimbs_callback(snd_midi_parms *mprm, unsigned trigger_value) {
if (trigger_value) {
mlimbs_update_requests = TRUE;
} else {
if (mlimbs_AI != NULL) // This routine accesses the music AI which should set the structures in
{
mlimbs_AI(); // current_request[] to tell mlimbs which pieces should be playing.
/*KLC
{
extern schar curr_crossfade;
extern int new_theme;
extern uchar decon_count , decon_time ;
extern uchar in_deconst , old_deconst ;
secret_sprint((ss_temp, "note in %d old %d, count %d time %d, cf %d, nt %d\n",
in_deconst, old_deconst, decon_count, decon_time, curr_crossfade, new_theme));
}
*/
}
}
}
#pragma enable_message(202)
#ifdef COW
// the key, here, is we need to update all sequences which are done
// ie. clear out their state and such....
void cdecl mlimbs_seq_done_call(snd_midi_parms *seq) {}
#endif
//////////////////////////////////////////////////////////////////////////////
// mlimbs_reassign_channels(void)
//
// This routine assigns currently free channels to chunks that are playing,
// and are lacking channels.
//
/////////////////////////////////////////////////////////////////////////////
void mlimbs_reassign_channels(void) {