-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSPKR.IN_
1222 lines (974 loc) · 40.3 KB
/
SPKR.IN_
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
;ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
;ÛÛ ÛÛ
;ÛÛ SPKR.INC ÛÛ
;ÛÛ ÛÛ
;ÛÛ IBM Audio Interface Library -- XMIDI interpreter for IBM PC 1-voice ÛÛ
;ÛÛ and Tandy 3-voice internal speakers ÛÛ
;ÛÛ ÛÛ
;ÛÛ Version 1.00 of 10-Nov-91: Initial version for AIL V2.0 release ÛÛ
;ÛÛ 1.01 of 29-Feb-92: Linear pitch/modulation options added ÛÛ
;ÛÛ PC speaker clicks reduced ÛÛ
;ÛÛ 1.02 of 21-Apr-92: Tandy modulation bug fixed ÛÛ
;ÛÛ ÛÛ
;ÛÛ 8086 ASM source compatible with Turbo Assembler v2.0 or later ÛÛ
;ÛÛ Author: John Miles ÛÛ
;ÛÛ ÛÛ
;ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
;ÛÛ ÛÛ
;ÛÛ Copyright (C) 1991, 1992 Miles Design, Inc. ÛÛ
;ÛÛ ÛÛ
;ÛÛ Miles Design, Inc. ÛÛ
;ÛÛ 10926 Jollyville #308 ÛÛ
;ÛÛ Austin, TX 78759 ÛÛ
;ÛÛ (512) 345-2642 / FAX (512) 338-9630 / BBS (512) 454-9990 ÛÛ
;ÛÛ ÛÛ
;ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
;
;Driver-specific configuration equates
;
IFDEF IBMPC
MAX_REC_CHAN equ 2 ;Max channel recognized by synths
MAX_TRUE_CHAN equ 2 ;Max channel available for locking
NUM_VOICES equ 1 ;# of physical voices available
NUM_SLOTS equ 2 ;# of virtual voices available
ELSEIFDEF TANDY
MAX_REC_CHAN equ 4
MAX_TRUE_CHAN equ 4
NUM_VOICES equ 3
NUM_SLOTS equ 4
ELSEIFDEF CMS
MAX_REC_CHAN equ 10
MAX_TRUE_CHAN equ 9
NUM_VOICES equ 12
NUM_SLOTS equ 12
soundPort equ 0220h
ENDIF
MIN_TRUE_CHAN equ 2 ;Min channel # (1-based)
DEF_SYNTH_VOL equ 100 ;Init vol=100%
DEF_TC_SIZE equ 0 ;No timbres used
MAX_TIMBS equ 0
VEL_SENS equ 1 ;Velocity sensitivity disabled if 0
VEL_TRUE equ 0 ;Full velocity sensitivity range if 1
;(set to 0 to reduce playback noise)
DEF_PITCH_RANGE equ 12 ;Default pitch wheel range (semitones)
LINEAR_PITCH equ 1 ;0 for semitonal pitch wheel response
EMULATE_MOD equ 0 ;(Active only w/LINEAR_PITCH = 1)
MOD_RATE equ 20 ; FM simulated at 20 Hz
MOD_DEPTH equ 5 ; 5 pitch wheel units/modulation unit
;
;Driver Description Table (DDT)
;Returned by describe_driver() proc
;
DDT LABEL WORD
min_API_version dw 200 ;Minimum API version required = 2.00
drvr_type dw 3 ;Type 3: XMIDI emulation
data_suffix db 4 dup (0) ;No Global Timbre Library used
device_name_o dw OFFSET devnames ;Pointer to list of supported devices
device_name_s dw ?
default_IO dw -1 ;Factory default I/O parameters
default_IRQ dw -1
default_DMA dw -1
default_DRQ dw -1
service_rate dw QUANT_RATE ;Request QUANT_RATE calls/second
display_size dw 0 ;No display
devnames LABEL BYTE
IFDEF IBMPC
db 'IBM PC or compatible internal speaker',0
ELSEIFDEF TANDY
db 'Tandy 3-voice internal sound',0
ELSEIFDEF CMS
db 'Creative Music System (CMS) / Game Blaster',0
ENDIF
db 0 ;0 to end list of device names
;
;Default setup values & internal constants
;
IFDEF IBMPC
value_l db 136,181,78,64,65,205,196,61,67,124,42,214
db 136,181,255,209,32,167,226,30,206,190,242,138
db 68,65,127,232,144,99,99,143,231,95,1,189
db 162,160,191,244,72,177,49,199,112,47,254,224
db 209,208,222,251,36,88,152,227,57,151,255,111
db 232,104,239,125,17,172,76,241,156,75,255,183
db 116,52,247,190,136,86,38,248,206,165,127,91
db 58,26,251,223,196,171,147,124,103,82,63,45
value_h db 142,134,253,240,226,213,201,190,179,169,160,150
db 142,134,126,119,113,106,100,95,89,84,79,75
db 71,67,63,59,56,53,50,47,44,42,40,37
db 35,33,31,29,28,26,25,23,22,21,19,18
db 17,16,15,14,14,13,12,11,11,10,9,9
db 8,8,7,7,7,6,6,5,5,5,4,4
db 4,4,3,3,3,3,3,2,2,2,2,2
db 2,2,1,1,1,1,1,1,1,1,1,1
ELSEIFDEF TANDY
value_l db 7,7,9,14,6,0,12,10,10,8,0,9
db 7,7,9,14,6,0,12,10,10,8,0,9
db 7,7,9,14,6,0,12,10,10,8,0,9
db 7,7,9,14,6,0,12,10,10,12,15,5
db 11,3,12,7,3,0,14,13,13,14,15,2
db 5,9,14,3,9,0,7,14,6,15,7,1
db 10,4,15,9,4,0,11,7,3,15,11,8
db 5,2,15,12,10,8,5,3,1,15,13,12
value_h db 53,50,47,44,42,40,37,35,33,63,60,56
db 53,50,47,44,42,40,37,35,33,63,60,56
db 53,50,47,44,42,40,37,35,33,63,60,56
db 53,50,47,44,42,40,37,35,33,31,29,28
db 26,25,23,22,21,20,18,17,16,15,14,14
db 13,12,11,11,10,10,9,8,8,7,7,7
db 6,6,5,5,5,5,4,4,4,3,3,3
db 3,3,2,2,2,2,2,2,2,1,1,1
atten db 15,14,14,14,14,14,14,14,13,13,13,13,13,13,13,13
db 12,12,12,12,12,12,12,12,11,11,11,11,11,11,11,11
db 10,10,10,10,10,10,10,10,9,9,9,9,9,9,9,9
db 8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,7
db 6,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5
db 4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3
db 2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
atten_reg db 090h,0b0h,0d0h
part_reg db 080h,0a0h,0c0h
ELSEIFDEF CMS
; Logic channel - first chip/second chip
ChanReg db 000,001,002,003,004,005,000,001,002,003,004,005
; Set octave command
OctavReg db 10h,10h,11h,11h,12h,12h,10h,10h,11h,11h,12h,12h
; Freq
value_l db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
db 003,031,058,083,107,130,151,172,191,209,226,242
; Octave
value_h db 000,000,000,000,000,000,000,000,000,000,000,000
db 001,001,001,001,001,001,001,001,001,001,001,001
db 002,002,002,002,002,002,002,002,002,002,002,002
db 003,003,003,003,003,003,003,003,003,003,003,003
db 004,004,004,004,004,004,004,004,004,004,004,004
db 005,005,005,005,005,005,005,005,005,005,005,005
db 006,006,006,006,006,006,006,006,006,006,006,006
db 007,007,007,007,007,007,007,007,007,007,007,007
; Volume
atten db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2
db 3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4
db 5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6
db 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8
db 9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10
db 11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12
db 13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,15
ENDIF
vel_graph db 82,85,88,91,94,97,100,103,106,109,112,115,118,121,124,127
;
;Misc. data
;
S_status db NUM_SLOTS dup (?) ;2=key on, 1=key off, 0=slot free
S_voice db NUM_SLOTS dup (?) ;Voice # or -1 assigned to slot
S_channel db NUM_SLOTS dup (?) ;MIDI channel owning slot
S_note db NUM_SLOTS dup (?) ;MIDI note # for slot's voice
S_keynum db NUM_SLOTS dup (?) ;MIDI key # before RBS translation
S_transpose db NUM_SLOTS dup (?) ;MIDI note transposition for slot
S_velocity db NUM_SLOTS dup (?) ;keyon velocity for note
S_sustain db NUM_SLOTS dup (?) ;note sustained if nonzero
S_update db NUM_SLOTS dup (?) ;bit mask for register updates
S_BLOCK db NUM_SLOTS dup (?) ;KON/BLOCK values
S_atten db NUM_SLOTS dup (?) ;Tandy attenuation register setup
S_p_val dw NUM_SLOTS dup (?)
S_mod_value dw NUM_SLOTS dup (?) ;instantaneous pitch for mod emulation
FREE equ 0 ;S_status[] phase equates
KEYON equ 1
KEYOFF equ 2
U_ALL_REGS equ 01000001b ;Bit mask equates for S_update
U_KSLTL equ 01000000b
U_FREQ equ 00000001b
MIDI_vol db NUM_CHANS dup (?) ;volume
MIDI_pitch_l db NUM_CHANS dup (?) ;pitchwheel LSB
MIDI_pitch_h db NUM_CHANS dup (?) ;pitchwheel MSB
MIDI_express db NUM_CHANS dup (?) ;expression
MIDI_sus db NUM_CHANS dup (?) ;HOLD1 pedal
MIDI_mod db NUM_CHANS dup (?) ;modulation
MIDI_vprot db NUM_CHANS dup (?) ;voice protection
MIDI_voices db NUM_CHANS dup (?) ;# of voices assigned to channel
V_channel db NUM_VOICES dup (?) ;voice assigned to MIDI channel n or -1
S_V_priority dw NUM_SLOTS dup (?) ;adjusted voice priorities
MOD_accum dw ? ;DDA accumulator for modulation timing
MOD_sign dw ? ;sign (+/-) for modulation
last_val_l db ?
last_val_h db ?
IFDEF CMS
CmsOctaveStore db 6 dup (0)
ChanEnableReg db 2 dup (0)
ENDIF
INCLUDE cms.inc ;define driver procedure call numbers
;****************************************************************************
;* *
;* I/O routines *
;* *
;****************************************************************************
;****************************************************************************
detect_device PROC H,IO_ADDR,IRQ,DMA,DRQ
USES ds,si,di ;Attempt to detect hardware
pushf
cli
cld
IFDEF IBMPC
mov ax,1 ;IBM speaker always present
ELSEIFDEF TANDY
mov ax,0f000h ;search PC BIOS for string 'Tandy'
mov es,ax
mov di,0
mov cx,65535
mov al,'T'
__search_T: repne scasb
jne __not_found
cmp WORD PTR es:[di],'na'
jne __search_T
cmp WORD PTR es:[di+2],'yd'
jne __search_T
jmp __exit ;found, assume Tandy sound available
__not_found: mov ax,0
ELSEIFDEF CMS
mov ax,1 ; Temp
ENDIF
__exit: POP_F
ret
ENDP
;****************************************************************************
reset_synth PROC ;Init hardware regs
USES ds,si,di
pushf
cli
IFDEF IBMPC
in al,61h
and al,11111100b
out 61h,al ;turn the speaker off
ELSEIFDEF TANDY
mov al,9fh ;turn off any active sounds
out 0c0h,al
mov al,0bfh
out 0c0h,al
mov al,0dfh
out 0c0h,al
mov al,68h ;enable internal speaker
out 61h,al
ELSEIFDEF CMS
call cmsReset
;mov ChanEnableReg,0
ENDIF
POP_F
ret
ENDP
;****************************************************************************
;* *
;* Timbre cache management / related API calls *
;* *
;****************************************************************************
;****************************************************************************
protect_timbre PROC H,Bank:BYTE,Num:BYTE
ret
ENDP
;****************************************************************************
unprotect_timbre PROC H,Bank:BYTE,Num:BYTE
ret
ENDP
;****************************************************************************
timbre_status PROC H,Bank:BYTE,Num:BYTE
USES ds,si,di ;Return 0 if timbre not resident
pushf
cli
mov ax,1 ;(no timbres needed)
POP_F
ret
ENDP
;****************************************************************************
get_cache_size PROC H
USES ds,si,di
pushf
cli
mov ax,DEF_TC_SIZE
POP_F
ret
ENDP
;****************************************************************************
define_cache PROC H,Addr:FAR PTR,Size
ret
ENDP
;****************************************************************************
get_request PROC H,Sequence
USES ds,si,di
pushf
cli
mov ax,-1
POP_F
ret
ENDP
;****************************************************************************
install_timbre PROC H,Bank:BYTE,Num:BYTE,Addr:FAR PTR
ret
ENDP
;****************************************************************************
;* *
;* MIDI interpreter and related procedures *
;* *
;****************************************************************************
IF EMULATE_MOD
serve_synth PROC ;Update simulated modulation
USES ds,si,di ;Called once per quantization interval
add MOD_accum,MOD_RATE
cmp MOD_accum,QUANT_RATE
jae __do_M_period
ret
__do_M_period: sub MOD_accum,QUANT_RATE
xor MOD_sign,1
mov si,0 ;mark appropriate voice parameters
__flag_slot: cmp S_status[si],FREE ;as "changed"
je __flag_next
mov bl,S_channel[si]
mov bh,0
mov al,MOD_DEPTH
mul MIDI_mod[bx]
cmp MOD_sign,0
je __store
neg ax
__store: mov bx,si
mov S_mod_value[si][bx],ax
or S_update[si],U_FREQ
call update_voice C,si ;update the hardware registers
__flag_next: inc si
cmp si,NUM_SLOTS
jne __flag_slot
ret
ENDP
ENDIF
;****************************************************************************
init_synth PROC ;Init MIDI synthesizer emulation
USES ds,si,di
pushf
cli
cld
mov di,0
__init_chans: mov MIDI_voices[di],0
inc di
cmp di,NUM_CHANS
jne __init_chans
mov di,0
__init_slots: mov S_status[di],FREE
mov bx,di
mov S_mod_value[di][bx],0
inc di
cmp di,NUM_SLOTS
jne __init_slots
mov di,0
__init_voices: mov V_channel[di],-1
inc di
cmp di,NUM_VOICES
jne __init_voices
mov MOD_accum,0
mov MOD_sign,0
mov last_val_l,-1
mov last_val_h,-1
POP_F
ret
ENDP
;****************************************************************************
assign_voice PROC Slot ;Allocate hardware voice to slot
USES ds,si,di
mov bx,-1 ;try to find an unassigned voice
__search_free: inc bx
cmp bx,NUM_VOICES
je __seize_voice ;(# of active slots > # of voices)
cmp V_channel[bx],-1
jne __search_free
mov si,[Slot] ;found free voice, assign it to slot
mov S_voice[si],bl
mov di,bx
mov bl,S_channel[si]
inc MIDI_voices[bx]
mov V_channel[di],bl
mov S_update[si],U_ALL_REGS
call update_voice C,si ;update the hardware
ret
__seize_voice: call update_priority ;assign voice based on priority search
ret
ENDP
;****************************************************************************
release_voice PROC Slot ;Release slot's voice
USES ds,si,di
mov si,[Slot]
cmp S_voice[si],-1
je __exit
and S_BLOCK[si],11011111b
or S_update[si],U_FREQ ;force KON = 0...
call update_voice C,si ;...silence any note...
mov bh,0 ;...and deallocate the voice
mov bl,S_channel[si]
dec MIDI_voices[bx]
mov bl,S_voice[si]
mov V_channel[bx],-1
mov S_voice[si],-1
__exit: ret
ENDP
;****************************************************************************
VAL_SHIFT MACRO
IFDEF TANDY
mov cl,ah
mov ch,0
shl cx,1
shl cx,1
shl cx,1
shl cx,1
or cl,al
mov ax,cx
ENDIF
ENDM
RECOMBINE MACRO
IFDEF TANDY
mov cx,ax
shr cx,1
shr cx,1
shr cx,1
shr cx,1
mov ah,cl
and al,0fh
ENDIF
ENDM
update_voice PROC Slot ;Update hardware regs for slot
USES ds,si,di
LOCAL voice,vol:BYTE,lvol,rvol:BYTE
LOCAL f_num
LOCAL val_l:BYTE,val_h:BYTE
LOCAL pitch,notenum,mid_val
LOCAL MOD_value
NOJUMPS
mov si,[Slot] ;update only requested parameters for
;speed
mov bl,S_voice[si]
cmp bl,-1
je __exit ;no hardware voice assigned, exit
mov bh,0
mov ah,0
mov voice,bx ;voice #
test S_update[si],U_KSLTL
jnz __go_KSLTL
__KSLTL_done: test S_update[si],U_FREQ
jnz __go_FREQ ;(update FREQ's KeyOn bit last)
__FREQ_done:
__exit: ret
__go_KSLTL: jmp __KSLTL
__go_FREQ: jmp __FREQ
;----------------------------------------------------------------------------
__KSLTL: IFDEF IBMPC
ELSE ;IFDEF TANDY
mov di,WORD PTR S_channel[si]
and di,0fh ;DI = MIDI channel
mov al,MIDI_vol[di]
mul MIDI_express[di]
shl ax,1 ;(AX*2)/256 = AX/128 ÷ AX/127
mov al,ah
cmp al,1
sbb al,-1 ;(error = 1/127 units; round up if !0)
mul S_velocity[si]
shl ax,1
mov al,ah
cmp al,1
sbb al,-1 ;(error = 1/127 units; round up if !0)
mov ah,0
mov bx,ax ;BX=composite (vol+expression) volume
mov al,atten[bx]
mov S_atten[si],al
or S_update[si],U_FREQ ;signal need to update keyon volume
ENDIF
and S_update[si],NOT U_KSLTL
jmp __KSLTL_done
;----------------------------------------------------------------------------
__FREQ: test S_BLOCK[si],00100000b
jnz __key_on ;KON bit = 1; turn note on
IFDEF IBMPC
in al,61h ;turn IBM speaker off
and al,11111100b
out 61h,al
ELSEIFDEF TANDY
mov bx,voice
mov al,atten_reg[bx]
or al,0fh ;set maximum Tandy attenuation
out 0c0h,al
ELSEIFDEF CMS
call CmsSound C,voice,0,0,0,0
ENDIF
jmp __end_freq
__key_on: mov bl,S_channel[si] ;get pitch bend word value
mov bh,0
mov al,MIDI_pitch_h[bx]
mov ah,0
mov cx,7
shl ax,cl
or al,MIDI_pitch_l[bx]
mov pitch,ax ;store it
mov bl,S_note[si] ;get key # 12-108
mov bh,0
mov al,S_transpose[si]
cbw
add bx,ax
sub bx,24 ;normalize to 0-95
__norm_1: add bx,12
cmp bx,0
jl __norm_1
add bx,12
__norm_2: sub bx,12
cmp bx,95
jg __norm_2
mov notenum,bx ;store it
IF LINEAR_PITCH
push si
push di
shl si,1
mov ax,S_mod_value[si]
mov MOD_value,ax
mov bx,notenum ;get upper and lower note value bounds
sub bx,DEF_PITCH_RANGE
jg __lin_1
mov bx,0
__lin_1: mov al,value_l[bx]
mov ah,value_h[bx]
VAL_SHIFT
mov si,ax ;SI=low value
mov bx,notenum
add bx,DEF_PITCH_RANGE
cmp bx,95
jb __lin_2
mov bx,95
__lin_2: mov al,value_l[bx]
mov ah,value_h[bx]
VAL_SHIFT
mov di,ax ;DI=high value
mov bx,notenum
mov al,value_l[bx]
mov ah,value_h[bx]
VAL_SHIFT
mov dx,ax ;DX=0 value
mov mid_val,dx
mov ax,pitch
sub ax,2000h ;E000=min 0=none 1FFF=max
add ax,MOD_value
cmp ax,0
jl __neg
sub di,dx
mov dx,di ;positive bend: DX = maximum
jmp __set
__neg: sub dx,si ;negative bend: DX = maximum
__set: imul dx
mov cx,2000h
idiv cx
add ax,mid_val
RECOMBINE
mov val_l,al
mov val_h,ah
pop di
pop si
ELSE
mov ax,pitch
sub ax,1fffh
mov cx,5 ;divide by 0x20, preserving sign
sar ax,cl ;(range now +0x100 to -0x0FF)
mov cl,DEF_PITCH_RANGE
mov ch,0 ;normally 12 (+0xc00 to -0xBF4)
imul cx
add ah,BYTE PTR notenum ;add computed note offset * 256
add ax,8 ;add 1/32 to round to 1/16
mov cx,4 ;derive true note #
sar ax,cl ;(expressed in 1/16 halftones)
sub ax,(12*16)
__norm_3: add ax,(12*16)
cmp ax,0
jl __norm_3
add ax,(12*16)
__norm_4: sub ax,(12*16)
cmp ax,(96*16)-1
jg __norm_4
shr ax,1
shr ax,1
shr ax,1
shr ax,1
mov bx,ax
mov al,value_l[bx]
mov val_l,al
mov al,value_h[bx]
mov val_h,al
ENDIF ;LINEAR_PITCH
IFDEF IBMPC
mov bl,val_l ;avoid redundant timer resets to
mov bh,val_h ;reduce clicks
cmp bl,last_val_l
jne __set_freq
cmp bh,last_val_h
je __on
__set_freq: mov al,0b6h ;program speaker frequency on Timer 2
out 43h,al
jmp $+2
mov al,bl
out 42h,al
jmp $+2
mov al,bh
out 42h,al
jmp $+2
mov last_val_l,bl
mov last_val_h,bh
__on: in al,61h
or al,00000011b
out 61h,al ;turn the PC speaker on
ELSEIFDEF TANDY
mov di,voice ;set Tandy freq, keyon level
mov al,part_reg[di]
or al,val_l
out 0c0h,al
jmp $+2
mov al,val_h
out 0c0h,al
jmp $+2
mov al,atten_reg[di]
or al,S_atten[si]
out 0c0h,al
ELSEIFDEF CMS
xor ah,ah
mov al,val_l ; ax = frequence
xor cl,cl
mov cl,val_h ; cx = octave
xor dh,dh
mov dl,S_atten[si] ; dx = amplitude
;mov dx,15
call CmsSound C,voice,ax,cx,dx,dx
ENDIF ;TANDY
__end_freq: and S_update[si],NOT U_FREQ
jmp __FREQ_done
JUMPS
ENDP
;****************************************************************************
update_priority PROC ;Maintain synthesizer voice priority
LOCAL vcount,low_p,high_p
USES ds,si,di
mov dx,0 ;zero active slot count
mov si,-1
__get_priority: inc si ;build adjusted priority table and
cmp si,NUM_SLOTS ;reallocate voices if necessary
je __chk_demand
cmp S_status[si],FREE
je __get_priority
inc dx ;slot active, bump count
mov bx,si
mov di,WORD PTR S_channel[si]
and di,0fh ;DI = slot's MIDI channel
mov ax,0ffffh
cmp MIDI_vprot[di],64 ;priority = max if voice protection on
jge __adj_priority
mov ax,S_p_val[bx][si]
__adj_priority: mov cl,MIDI_voices[di] ;AX = slot's base priority
mov ch,0
sub ax,cx ;priority -= # of voices in channel
jnc __set_priority
mov ax,0
__set_priority: mov S_V_priority[bx][si],ax
jmp __get_priority
__chk_demand: cmp dx,NUM_VOICES ;more active slots than voices?
jbe __exit ;no, exit
mov vcount,dx
__sort_p_list: mov ax,0 ;set AX = unvoiced highest priority
mov dx,-1 ;set DX = voiced lowest priority
mov si,-1
__for_slot: inc si
cmp si,NUM_SLOTS
je __reassign
cmp S_status[si],FREE
je __for_slot
mov bx,si
mov di,S_V_priority[bx][si]
cmp S_voice[si],-1
jne __chk_low
cmp di,ax
jb __for_slot
mov ax,di
mov high_p,si ;highest-priority unvoiced slot index
jmp __for_slot
__chk_low: cmp di,dx
ja __for_slot
mov dx,di
mov low_p,si ;lowest-priority voiced slot index
jmp __for_slot
__reassign: cmp ax,dx ;highest unvoiced < lowest voiced?
jb __exit ;yes, we're done
mov si,low_p
mov bh,0
mov bl,S_voice[si]
push bx
call release_voice C,si
mov S_status[si],FREE ;release any slots deallocated...
pop bx
mov si,high_p
mov S_voice[si],bl
mov di,bx
mov bl,S_channel[si]
inc MIDI_voices[bx]
mov V_channel[di],bl
mov S_update[si],U_ALL_REGS
call update_voice C,si ;update the hardware
dec vcount
jnz __sort_p_list ;keep sorting until priorities valid
__exit: ret
ENDP
;****************************************************************************
slot_phase PROC Slot ;Set up slot parameters
USES ds,si,di
mov si,[Slot]
mov bx,si
shl bx,1
mov S_BLOCK[si],00100000b ;set KON, clear BLOCK mask
mov S_p_val[bx],32767 ;instrument priority = average
mov S_update[si],U_ALL_REGS ;flag all registers "dirty"
ret
ENDP
;****************************************************************************
note_off PROC Chan:BYTE,Note:BYTE
USES ds,si,di ;Turn MIDI note off
mov si,-1 ;find all slots in which note is
__next_slot: mov al,[Note] ;playing
mov bl,[Chan]
__find_note: inc si
cmp si,NUM_SLOTS
je __exit
cmp S_status[si],KEYON
jne __find_note
cmp S_keynum[si],al
jne __find_note
cmp S_channel[si],bl
jne __find_note
mov bh,0
cmp MIDI_sus[bx],64
jge __sustained
call release_voice C,si ;release the slot's voice
mov S_status[si],FREE
jmp __next_slot
__sustained: mov S_sustain[si],1
jmp __next_slot
__exit: ret
ENDP
;****************************************************************************
note_on PROC Chan,Note,Velocity ;Turn MIDI note on
USES ds,si,di
mov di,[Chan]
mov si,0
__find_slot: cmp S_status[si],FREE
je __slot_found
inc si ;find a free virtual voice slot
cmp si,NUM_SLOTS
jne __find_slot
jmp __exit ;exit if no virtual voice available
__slot_found: mov ax,[Chan] ;establish MIDI channel
mov S_channel[si],al
mov dx,[Note]
mov S_keynum[si],dl ;save MIDI key #
mov S_note[si],dl
mov S_transpose[si],0 ;(no transposition)
IF VEL_SENS
mov ax,[Velocity] ;establish note velocity
IF NOT VEL_TRUE
shr al,1
shr al,1
shr al,1
lea bx,vel_graph ;scale back velocity sensitivity to
xlat cs:[bx] ;reduce perceived playback noise
ENDIF
ELSE
mov al,127 ;default velocity = 127
ENDIF
mov S_velocity[si],al
mov S_status[si],KEYON ;flag note "on" in slot
mov S_sustain[si],0 ;init sustained flag
call slot_phase C,si ;set up BNK timbre in slot
mov S_voice[si],-1
call assign_voice C,si ;assign hardware voice to slot