-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMS.C
730 lines (647 loc) · 19 KB
/
CMS.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
#ifdef CMS
//#define CMS_DEBUG // verbose to CMSLOG.TXT debug file
//#define DRUMS_ONLY // for percussion debug purpouses
#ifdef CMS_DEBUG
#include <stdio.h>
#include <stdarg.h>
#endif
#include "cms.h"
#ifdef CMS_DEBUG
void debug_log(const char *fmt, ...)
{
FILE *f_log;
va_list ap;
f_log = fopen("cmslog.log", "a");
va_start(ap, fmt);
vfprintf(f_log, fmt, ap);
va_end(ap);
fclose(f_log);
}
#endif
mid_channel cms_synth[MAX_CMS_CHANNELS]; // CMS synth
static unsigned short freqtable[128] = {
8, 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15,
16, 17, 18, 19, 21, 22, 23, 24, 26, 28, 29, 31,
33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62,
65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123,
131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976,
2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902,
8372, 8870, 9397, 9956,10548,11175,11840,12544};
static unsigned short pitchtable[256] = { /* pitch wheel */
29193U,29219U,29246U,29272U,29299U,29325U,29351U,29378U, /* -128 */
29405U,29431U,29458U,29484U,29511U,29538U,29564U,29591U, /* -120 */
29618U,29644U,29671U,29698U,29725U,29752U,29778U,29805U, /* -112 */
29832U,29859U,29886U,29913U,29940U,29967U,29994U,30021U, /* -104 */
30048U,30076U,30103U,30130U,30157U,30184U,30212U,30239U, /* -96 */
30266U,30293U,30321U,30348U,30376U,30403U,30430U,30458U, /* -88 */
30485U,30513U,30541U,30568U,30596U,30623U,30651U,30679U, /* -80 */
30706U,30734U,30762U,30790U,30817U,30845U,30873U,30901U, /* -72 */
30929U,30957U,30985U,31013U,31041U,31069U,31097U,31125U, /* -64 */
31153U,31181U,31209U,31237U,31266U,31294U,31322U,31350U, /* -56 */
31379U,31407U,31435U,31464U,31492U,31521U,31549U,31578U, /* -48 */
31606U,31635U,31663U,31692U,31720U,31749U,31778U,31806U, /* -40 */
31835U,31864U,31893U,31921U,31950U,31979U,32008U,32037U, /* -32 */
32066U,32095U,32124U,32153U,32182U,32211U,32240U,32269U, /* -24 */
32298U,32327U,32357U,32386U,32415U,32444U,32474U,32503U, /* -16 */
32532U,32562U,32591U,32620U,32650U,32679U,32709U,32738U, /* -8 */
32768U,32798U,32827U,32857U,32887U,32916U,32946U,32976U, /* 0 */
33005U,33035U,33065U,33095U,33125U,33155U,33185U,33215U, /* 8 */
33245U,33275U,33305U,33335U,33365U,33395U,33425U,33455U, /* 16 */
33486U,33516U,33546U,33576U,33607U,33637U,33667U,33698U, /* 24 */
33728U,33759U,33789U,33820U,33850U,33881U,33911U,33942U, /* 32 */
33973U,34003U,34034U,34065U,34095U,34126U,34157U,34188U, /* 40 */
34219U,34250U,34281U,34312U,34343U,34374U,34405U,34436U, /* 48 */
34467U,34498U,34529U,34560U,34591U,34623U,34654U,34685U, /* 56 */
34716U,34748U,34779U,34811U,34842U,34874U,34905U,34937U, /* 64 */
34968U,35000U,35031U,35063U,35095U,35126U,35158U,35190U, /* 72 */
35221U,35253U,35285U,35317U,35349U,35381U,35413U,35445U, /* 80 */
35477U,35509U,35541U,35573U,35605U,35637U,35669U,35702U, /* 88 */
35734U,35766U,35798U,35831U,35863U,35895U,35928U,35960U, /* 96 */
35993U,36025U,36058U,36090U,36123U,36155U,36188U,36221U, /* 104 */
36254U,36286U,36319U,36352U,36385U,36417U,36450U,36483U, /* 112 */
36516U,36549U,36582U,36615U,36648U,36681U,36715U,36748U}; /* 120 */
unsigned char CMSFreqMap[128] = {
0 , 3, 7, 11, 15, 19, 23, 27,
31 , 34, 38, 41, 45, 48, 51, 55,
58 , 61, 64, 66, 69, 72, 75, 77,
80 , 83, 86, 88, 91, 94, 96, 99,
102,104,107,109,112,114,116,119,
121,123,125,128,130,132,134,136,
138,141,143,145,147,149,151,153,
155,157,159,161,162,164,166,168,
170,172,174,175,177,179,181,182,
184,186,188,189,191,193,194,196,
197,199,200,202,203,205,206,208,
209,210,212,213,214,216,217,218,
219,221,222,223,225,226,227,228,
229,231,232,233,234,235,236,237,
239,240,241,242,243,244,245,246,
247,249,250,251,252,253,254,255
};
// CMS IO port address
unsigned short cmsPort;
// The 12 note-within-an-octave values for the SAA1099, starting at B
static unsigned char noteAdr[] = {5, 32, 60, 85, 110, 132, 153, 173, 192, 210, 227, 243};
// Volume
static unsigned char atten[128] = {
0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,
11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,
13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
};
// Logic channel - first chip/second chip
static unsigned char ChanReg[12] = {000,001,002,003,004,005,000,001,002,003,004,005};
// Set octave command
static unsigned char OctavReg[12] = {0x10,0x10,0x11,0x11,0x12,0x12,0x10,0x10,0x11,0x11,0x12,0x12};
unsigned char CmsOctaveStore[12];
unsigned char ChanEnableReg[2] = {0,0};
// Note priority
unsigned char NotePriority;
unsigned short channelpitch[16];
void __declspec( naked ) cmsWrite(void)
{
/*
parameters
dx = port base+offset
ah = register
al = data
*/
_asm
{
inc dx
xchg al,ah
out dx,al
dec dx
xchg al,ah
out dx,al
ret
}
}
void __declspec( naked ) cmsNull(unsigned short port)
{
/*
parameters
dx = port offset to null
*/
_asm
{
add dx,cmsPort // FIXME! CMSPortAddr
mov cx,20h
xor ax,ax
loop_nul: // null all 20 registers
call cmsWrite
inc ah
loop loop_nul
mov ax,1C02h // reset chip
call cmsWrite
mov ax,1C01h // enable this chip
call cmsWrite
ret
}
}
void cmsReset(unsigned short port)
{
unsigned char i;
cmsPort = port;
_asm
{
mov dx,0
call cmsNull
mov dx,2
call cmsNull
}
for (i=0;i<11;i++)CmsOctaveStore[i]=0;
ChanEnableReg[0]=0;
ChanEnableReg[1]=0;
for (i=0;i<MAX_CMS_CHANNELS;i++)
{
cms_synth[i].note=0;
cms_synth[i].priority=0;
cms_synth[i].ch = 0;
cms_synth[i].voice = 0;
cms_synth[i].velocity = 0;
}
for (i=0;i<16;i++)
{
channelpitch[i] = 8192;
}
NotePriority = 0;
}
static void CMS_SetRegister(unsigned short regAddr, unsigned char reg, unsigned char val)
{
outp(regAddr + 1, reg); /* Select the register */
outp(regAddr, val); /* Set the value of the register */
}
#if 0
void cmsDisableVoice(unsigned char voice)
{
if (voice > 5)
{
ChanEnableReg[1] &= ~(1 << ChanReg[voice]);
CMS_SetRegister(cmsPort + 2, 0x14, ChanEnableReg[1]);
}
else
{
ChanEnableReg[0] &= ~(1 << ChanReg[voice]);
CMS_SetRegister(cmsPort, 0x14, ChanEnableReg[0]);
}
}
void cmsSound(unsigned char voice,unsigned char freq,unsigned char octave,unsigned char amplitudeLeft,unsigned char amplitudeRight)
{
if (voice > 5)
{
if (ChanReg[voice]&0x1 == 0)
CmsOctaveStore[OctavReg[voice]-0x10+3] = (CmsOctaveStore[OctavReg[voice]-0x10+3] & 0xF0) | octave;
else
CmsOctaveStore[OctavReg[voice]-0x10+3] = ((CmsOctaveStore[OctavReg[voice]-0x10+3] & 0xF) << 4) | octave;
CMS_SetRegister(cmsPort + 2, OctavReg[voice], CmsOctaveStore[OctavReg[voice]-0x10+3]);
CMS_SetRegister(cmsPort + 2, ChanReg[voice], (amplitudeLeft << 4) | amplitudeRight);
CMS_SetRegister(cmsPort + 2, ChanReg[voice] | 0x8, freq);
ChanEnableReg[1] |= 1 << ChanReg[voice];
CMS_SetRegister(cmsPort + 2, 0x14, ChanEnableReg[1]);
}
else
{
if (ChanReg[voice]&0x1 == 0)
CmsOctaveStore[OctavReg[voice]-0x10] = (CmsOctaveStore[OctavReg[voice]-0x10] & 0xF0) | octave;
else
CmsOctaveStore[OctavReg[voice]-0x10] = ((CmsOctaveStore[OctavReg[voice]-0x10] & 0xF) << 4) | octave;
CMS_SetRegister(cmsPort, OctavReg[voice], CmsOctaveStore[OctavReg[voice]-0x10]);
CMS_SetRegister(cmsPort, ChanReg[voice], (amplitudeLeft << 4) | amplitudeRight);
CMS_SetRegister(cmsPort, ChanReg[voice] | 0x8, freq);
ChanEnableReg[0] |= 1 << ChanReg[voice];
CMS_SetRegister(cmsPort, 0x14, ChanEnableReg[0]);
}
}
#endif
void cmsDisableVoice(unsigned char voice)
{
_asm
{
xor bh,bh
mov bl,voice
mov dx,cmsPort // FIXME !!!
mov bl,ChanReg[bx] ; bl = true channel (0 - 5)
xor di,di
mov cl,voice
cmp cl,06h
jl skip_inc
inc di
add dx,2
skip_inc:
mov al,14h
inc dx
out dx,al
dec dx
mov al,ChanEnableReg[di]
mov ah,01h
mov cl,bl
shl ah,cl
not ah
and al,ah ; al = voice enable reg
out dx,al
mov ChanEnableReg[di],al
}
}
void cmsSetVolume(unsigned char voice,unsigned char amplitudeLeft,unsigned char amplitudeRight)
{
_asm
{
xor bh,bh
mov bl,voice
mov dx,cmsPort // FIXME !!!
cmp bl,06h ; check channel num > 5?
jl setVol ; yes - set port = port + 2
add dx,2
setVol:
mov bl,ChanReg[bx] ; bx = true channel (0 - 5)
mov al,byte ptr amplitudeLeft
mov ah,byte ptr amplitudeRight
mov cl,4
shl ah,cl
or al,ah
mov ah,bl
call cmsWrite
}
}
void cmsSound(unsigned char voice,unsigned char freq,unsigned char octave,unsigned char amplitudeLeft,unsigned char amplitudeRight)
{
_asm
{
xor bh,bh
mov bl,voice
mov dx,cmsPort // FIXME !!!
cmp bl,06h ; check channel num > 5?
jl setOctave ; yes - set port = port + 2
add dx,2
setOctave:
mov bl,ChanReg[bx] ; bx = true channel (0 - 5) //FIXME swap down
mov ah,OctavReg[bx] ; ah = Set octave command //FIXME swap up
;
; ah now = register
; 0,1,6,7=$10
; 2,3,8,9=$11
; 4,5,10,11=$12
;
; CMS octave regs are write only, so we have to track
; the values in adjoining voices manually
mov al,ah
xor ah,ah ; ax = set octave cmd (10h - 12h)
mov di,ax ; di = ax
sub di,010h ; di = octave cmd - 10h (0..2 index)
mov cl,voice
cmp cl,06h
jl skip_inc
add di,3
skip_inc:
mov ah,al ; set ah back to octave cmd
mov al,byte ptr CmsOctaveStore[di]
mov bh,octave
test bl,01h
jnz shiftOctave
and al,0F0h
jmp outOctave
shiftOctave:
and al,0Fh
mov cl,4
shl bh,cl
outOctave:
or al,bh
mov byte ptr CmsOctaveStore[di],al
call cmsWrite ; set octave to CMS
setAmp:
mov al,byte ptr amplitudeLeft
mov ah,byte ptr amplitudeRight
;and al,0Fh
mov cl,4
shl ah,cl
or al,ah
mov ah,bl
call cmsWrite
setFreq:
mov al,byte ptr freq
or ah,08h
call cmsWrite
voiceEnable:
mov al,14h
inc dx
out dx,al
dec dx
xor di,di
mov cl,voice
cmp cl,06h
jl skip_inc2
inc di
skip_inc2:
mov al,ChanEnableReg[di]
mov ah,01h
mov cl,bl
shl ah,cl
or al,ah
out dx,al
mov ChanEnableReg[di],al
}
}
// ****
// High-level CMS synth procedures
// ****
void cms_pitchwheel(unsigned short oplport, int channel, int pitchwheel)
{
unsigned char i;
unsigned short notefreq;
unsigned short period;
unsigned char mixer;
unsigned char note;
int pitch;
unsigned char octave;
channelpitch[channel] = pitchwheel;
for(i=0; i<MAX_CMS_CHANNELS; i++)
{
if ((cms_synth[i].ch==channel) && (cms_synth[i].note != 0))
{
note = cms_synth[i].note;
pitch = pitchwheel;
if (pitch != 0) {
if (pitch > 127) {
pitch = 127;
} else if (pitch < -128) {
pitch = -128;
}
}
notefreq = ((unsigned long)freqtable[note] * pitchtable[pitch + 128]) >> 15;
if ((notefreq > 31) && (notefreq < 7824))
{
octave=4;
while (notefreq<489)
{
notefreq=notefreq*2;
octave--;
}
while (notefreq>977)
{
notefreq=notefreq / 2;
octave++;
}
#ifndef DRUMS_ONLY
cmsSound(cms_synth[i].voice,CMSFreqMap[((notefreq-489)*128) / 489],octave,atten[cms_synth[i].velocity],atten[cms_synth[i].velocity]);
#endif
}
}
}
}
void cmsNoteOff(unsigned char channel, unsigned char note)
{
unsigned char i;
unsigned char voice;
if (channel == 9)
{
#ifdef CMS_DEBUG
debug_log("DRUM OFF note= %u\n",note);
#endif
CMS_SetRegister(cmsPort+2, 0x19, 0x0);
CMS_SetRegister(cmsPort+2, 0x15, 0x0); // noise ch 11
cmsDisableVoice(11);
}
else
{
voice = MAX_CMS_CHANNELS;
for(i=0; i<MAX_CMS_CHANNELS; i++)
{
if(cms_synth[i].note==note)
{
voice = i;
break;
}
}
// Note not found, ignore note off command
if(voice==MAX_CMS_CHANNELS)
{
#ifdef CMS_DEBUG
debug_log("not found Ch=%u,note=%u\n",channel & 0xFF,note & 0xFF);
#endif
return;
}
// decrease priority for all notes greater than current
for (i=0; i<MAX_CMS_CHANNELS; i++)
if (cms_synth[i].priority > cms_synth[voice].priority )
cms_synth[i].priority = cms_synth[i].priority - 1;
if (NotePriority != 0) NotePriority--;
cmsDisableVoice(voice);
cms_synth[voice].note = 0;
cms_synth[voice].priority = 0;
cms_synth[voice].ch = 0;
cms_synth[voice].voice = 0;
cms_synth[voice].velocity = 0;
}
}
void cmsNoteOn(unsigned char channel, unsigned char note, unsigned char velocity)
{
unsigned char octave;
unsigned char noteVal;
unsigned char i;
unsigned char voice;
unsigned char note_cms;
unsigned notefreq;
int pitch;
if (channel == 9)
{
if (velocity != 0)
{
#ifdef CMS_DEBUG
debug_log("DRUM ON note= %u\n",note);
#endif
CMS_SetRegister(cmsPort+2, 0x19, 0x84); // single dacay
switch (note) {
case 37: // Side Stick
CMS_SetRegister(cmsPort+2, 0x16, 0x00); // noise gen 1 31.3kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,2,atten[velocity],atten[velocity]);
break;
case 38: // Acoustic Snare
CMS_SetRegister(cmsPort+2, 0x16, 0x00); // noise gen 1 31.3kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,0,atten[velocity],atten[velocity]);
break;
case 39: // Hand Clap
CMS_SetRegister(cmsPort+2, 0x16, 0x10); // noise gen 1 15.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,3,atten[velocity],atten[velocity]);
break;
case 40: // Electric Snare
CMS_SetRegister(cmsPort+2, 0x16, 0x10); // noise gen 1 15.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,1,atten[velocity],atten[velocity]);
break;
case 42: // Closed Hi Hat
CMS_SetRegister(cmsPort+2, 0x16, 0x10); // noise gen 1 15.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,2,atten[velocity],atten[velocity]);
break;
case 44: // Pedal Hi-Hat
CMS_SetRegister(cmsPort+2, 0x16, 0x10); // noise gen 1 15.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,0,atten[velocity],atten[velocity]);
break;
case 52: // Chinese Cymbal
CMS_SetRegister(cmsPort+2, 0x16, 0x20); // noise gen 1 7.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,2,atten[velocity],atten[velocity]);
break;
case 55: // Splash Cymbal
CMS_SetRegister(cmsPort+2, 0x16, 0x20); // noise gen 1 7.6kHz
CMS_SetRegister(cmsPort+2, 0x15, 0x20); // noise ch 11
cmsSound(11,0,0,atten[velocity],atten[velocity]);
break;
case 71: // Short Whistle
case 72: // Long Whistle
cmsSound(11,0,6,atten[velocity],atten[velocity]);
break;
default:
cmsSound(11,0,1,atten[velocity],atten[velocity]);
}
}
else
{
CMS_SetRegister(cmsPort+2, 0x19, 0x0);
CMS_SetRegister(cmsPort+2, 0x15, 0x0); // noise ch 11
cmsDisableVoice(11);
}
}
else
if (velocity != 0)
{
/*
note_cms = note+1;
octave = (note_cms / 12) - 1; //Some fancy math to get the correct octave
noteVal = note_cms - ((octave + 1) * 12); //More fancy math to get the correct note
*/
NotePriority++;
voice = MAX_CMS_CHANNELS;
for(i=0; i<MAX_CMS_CHANNELS; i++)
{
if(cms_synth[i].note==0)
{
voice = i;
break;
}
}
// We run out of voices, find low priority voice
if(voice==MAX_CMS_CHANNELS)
{
unsigned char min_prior = cms_synth[0].priority;
#ifdef CMS_DEBUG
debug_log("out of voice. NotePriority=%u\n",NotePriority);
for (i=0; i<MAX_CMS_CHANNELS; i++)
debug_log("%u ",cms_synth[i].priority);
debug_log("\n");
#endif
// find note with min prioryty
voice = 0;
for (i=1; i<MAX_CMS_CHANNELS; i++)
if (cms_synth[i].priority < min_prior)
{
voice = i;
min_prior = cms_synth[i].priority;
}
// decrease all notes priority by one
for (i=0; i<MAX_CMS_CHANNELS; i++)
if (cms_synth[i].priority != 0)
cms_synth[i].priority = cms_synth[i].priority - 1;
// decrease current priority
if (NotePriority != 0) NotePriority--;
#ifdef CMS_DEBUG
debug_log("find low priority voice %u, NotePriority=%u\n",voice,NotePriority);
for (i=0; i<MAX_CMS_CHANNELS; i++)
debug_log("%u ",cms_synth[i].priority);
debug_log("\n");
#endif
}
pitch = channelpitch[channel];
if (pitch != 0) {
if (pitch > 127) {
pitch = 127;
} else if (pitch < -128) {
pitch = -128;
}
}
notefreq = ((unsigned long)freqtable[note] * pitchtable[pitch + 128]) >> 15;
if ((notefreq > 31) && (notefreq < 7824))
{
octave=4;
while (notefreq<489)
{
notefreq=notefreq*2;
octave--;
}
while (notefreq>977)
{
notefreq=notefreq / 2;
octave++;
}
#ifndef DRUMS_ONLY
cmsSound(voice,CMSFreqMap[((notefreq-489)*128) / 489],octave,atten[velocity],atten[velocity]);
#endif
cms_synth[voice].note = note;
cms_synth[voice].priority = NotePriority;
cms_synth[voice].velocity = velocity;
cms_synth[voice].ch = channel;
cms_synth[voice].voice = voice;
}
}
else
cmsNoteOff(channel,note);
}
void cmsTick(void)
{
#if 0
unsigned char i;
unsigned char noteVal;
for (i=0;i<MAX_CMS_CHANNELS;i++)
if (cms_synth[i].note != 0)
{
noteVal = cms_synth[i].volume-1;
if (noteVal != 0)
{
cmsSetVolume(i,atten[noteVal],atten[noteVal]);
cms_synth[i].volume = noteVal;
}
else
{
cmsDisableVoice(i);
cms_synth[i].volume = 0;
cms_synth[i].note = 0;
}
}
#endif
}
void cmsController(unsigned char channel, unsigned char id, unsigned char val)
{
unsigned char i;
if ((id==121) || (id==123)) // All Sound/Notes Off
{
for (i=0;i<MAX_CMS_CHANNELS;i++)
if (cms_synth[i].note != 0)
{
cmsDisableVoice(i);
cms_synth[i].note = 0;
cms_synth[i].priority = 0;
cms_synth[i].ch = 0;
cms_synth[i].voice = 0;
cms_synth[i].velocity = 0;
}
if (id==121)
{
for (i=0;i<16;i++)
channelpitch[i] = 8192;
}
}
}
#endif