forked from redcode/SpecEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectSound.asm
1258 lines (1001 loc) · 47.5 KB
/
DirectSound.asm
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
;###########################################################################
; DirectSound API interface to emulate AY-3-8912 sound device
;###########################################################################
InBufferBlock PROTO :DWORD
;====================================================
; The following macro is based on
; this Win32 header file macro
;
; #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
; ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
; ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
;
; This presumes all params passed in are in bytes
;====================================================
mmioFOURCC MACRO ch0, ch1, ch2, ch3
mov al, ch3
shl eax, 8
mov al, ch2
shl eax, 8
mov al, ch1
shl eax, 8
mov al, ch0
ENDM
AUDIOFREQ equ 44100
.data?
align 4
DSBUFFERSIZE DWORD ? ; (AUDIOFREQ/FPS)*8*8 ; must be a multiple of 4
; StereoOutputMode equates:
OUTPUT_NORMAL equ 0
OUTPUT_ABC equ 1
OUTPUT_ACB equ 2
OUTPUT_ECHO equ 3
OUTPUT_REVERB equ 4
OUTPUT_PLUS3 equ 5
.data
align 16
PSGDivisor equ 1 ; default = 2
PSGVolumes dd 0 ; vol 0
dd 108 / PSGDivisor ; vol 1
dd 159 / PSGDivisor ; vol 2
dd 223 / PSGDivisor ; vol 3
dd 335 / PSGDivisor ; vol 4
dd 511 / PSGDivisor ; vol 5
dd 703 / PSGDivisor ; vol 6
dd 1119 / PSGDivisor ; vol 7
dd 1343 / PSGDivisor ; vol 8
dd 2143 / PSGDivisor ; vol 9
dd 2943 / PSGDivisor ; vol 10
dd 3679 / PSGDivisor ; vol 11
dd 4655 / PSGDivisor ; vol 12
dd 5759 / PSGDivisor ; vol 13
dd 6911 / PSGDivisor ; vol 14
dd 8191 / PSGDivisor ; vol 15
PSG_CENTRALISE_RANGE equ (8191 / PSGDivisor) / 2
.data?
align 16
lpds LPDIRECTSOUND ?
SecondaryBuffer LPDIRECTSOUNDBUFFER ?
DriveStepBuffer LPDIRECTSOUNDBUFFER ?
dsbd DSBUFFERDESC <?>
WaveFormat WAVEFORMATEX <?>
align 16
audio_ptr_1 DWORD ?
audio_ptr_2 DWORD ?
audio_length_1 DWORD ?
audio_length_2 DWORD ?
CurrentPlayCursor DWORD ?
CurrentWriteCursor DWORD ?
SamplesPerFrame DWORD ?
SampleBytesPerFrame DWORD ?
lpAYBuffer DWORD ?
DSBuff_WritePosn DWORD ?
DSBuff_WaitPosn DWORD ?
AY_BufferedBytes DWORD ?
lpAYBuffer_Posn DWORD ?
SamplesBeforeStreaming DWORD ?
SamplesToBuffer DWORD ? ; number of samples to buffer before streaming to DirectSound = (SAMPLESPERFRAME * FrameSkipCounter)
SampleBytesToBuffer DWORD ?
StereoWide1PtrS DWORD ?
StereoWide1PtrM DWORD ?
StereoWide2PtrS DWORD ?
StereoWide2PtrM DWORD ?
StereoWide1BufferS WORD 768*8 DUP (?)
StereoWide1BufferM WORD 768*8 DUP (?)
StereoEndBuffer1 WORD ?
StereoWide2BufferS WORD 768*8 DUP (?)
StereoWide2BufferM WORD 768*8 DUP (?)
StereoEndBuffer2 WORD ?
; equates for Sound_Effect
EFFECT_NONE equ 0
EFFECT_ECHO equ 1
EFFECT_REVERB equ 2
Sound_Effect BYTE ?
SoundPlaying BYTE ?
DSoundStarted BYTE ?
Emulate_AY BYTE ?
process_priority_set BYTE ?
.code
;#################################################################################
InitAudio:
.if lpAYBuffer != NULL
call ClearSoundBuffers
mov SoundPlaying, 0
mov eax, lpAYBuffer
mov lpAYBuffer_Posn, eax ; our current write posn in our private buffer
mov RandomSeed, 1
mov [StereoWide1PtrS], offset StereoWide1BufferS
mov [StereoWide1PtrM], offset StereoWide1BufferM
mov [StereoWide2PtrS], offset StereoWide2BufferS
mov [StereoWide2PtrM], offset StereoWide2BufferM
mov eax, AUDIOFREQ ; / Frames Per Second
invoke Div2Int, eax, MACHINE.FramesPerSecond
mov SamplesPerFrame, eax
; SampleBytesPerFrame = SamplesPerFrame * 4
shl eax, 2
mov SampleBytesPerFrame,eax
mov eax, SamplesPerFrame
movzx ecx, FrameSkipCounter
mul ecx
mov SamplesToBuffer, eax ; = (SamplesPerFrame * FrameSkipCounter)
mov SamplesBeforeStreaming, eax ; number of samples to take before streaming to DirectSound
shl eax, 2
mov SampleBytesToBuffer,eax ; = (SamplesToBuffer * 4)
mov eax, SampleBytesToBuffer
mov DSBuff_WaitPosn, eax ; = 1 * SampleBytesToBuffer
shl eax, 1
mov DSBuff_WritePosn, eax ; = 2 * SampleBytesToBuffer
.endif
ret
ReinitAudio:
call ShutdownDirectSound
call StartupDirectSound
call InitAudio
ret
;#################################################################################
ClearSoundBuffers:
.if DSoundStarted == TRUE
DSBINVOKE mLock, SecondaryBuffer, NULL, NULL, addr audio_ptr_1, addr audio_length_1, addr audio_ptr_2, addr audio_length_2, DSBLOCK_ENTIREBUFFER
.if eax == DSERR_BUFFERLOST
call RestoreSoundBuffer
jmp ClearSoundBuffers
.endif
.if eax == DS_OK
push edi
xor eax, eax
mov edi, audio_ptr_1
mov ecx, audio_length_1
shr ecx, 2
rep stosd
mov ecx, audio_length_1
and ecx, 3
rep stosb
mov edi, audio_ptr_2
mov ecx, audio_length_2
shr ecx, 2
rep stosd
mov ecx, audio_length_2
and ecx, 3
rep stosb
pop edi
DSBINVOKE Unlock, SecondaryBuffer, audio_ptr_1, audio_length_1, audio_ptr_2, audio_length_2
.if eax != DS_OK
FATAL "DirectSound Unlock failed !"
.endif
.endif
.endif
ret
;#################################################################################
BEEPERFADE MACRO
.if BeepHold > 0
dec BeepHold
.else
mov BeepHold, 12
mov dx, BeepVal
mov ax, BeepCentreVal
sub ax, dx
sar ax, 2
add dx, ax
mov BeepVal, dx
.endif
ENDM
align 16
Create_Audio_Sample:
cmp lpAYBuffer, NULL
je SampleBeepExit ; exit if we have no private audio buffer
.if BoostLoadingNoise ; transferring loading data direct to Speccy?
mov BeeperSubCount, 0
mov BeeperSubTotal, 0
.if EarBit == 0
mov ax, -30000
.else
mov ax, 30000
.endif
mov bx, ax
jmp No_Audio_Effects
.endif
.if BoostSavingNoise ; transferring ROM SAVE data direct to Speccy?
mov BeeperSubCount, 0
mov BeeperSubTotal, 0
.if MICVal == 0 ; use MIC to save direct to Speccy, EAR sends loading tones
mov ax, -30000
.else
mov ax, 30000
.endif
mov bx, ax
jmp No_Audio_Effects
.endif
mov Total_ChanA, 0
mov Total_ChanB, 0
mov Total_ChanC, 0
mov SampleCounter, 0
; BEEPERFADE
mov dx, BeepVal
.if BeeperSubCount > 1
mov eax, BeeperSubTotal
mov edx, eax
shr edx, 16
div BeeperSubCount
mov dx, ax
.endif
mov BeeperSubCount, 0
mov BeeperSubTotal, 0
; for real tape mode, tape signals are heard from the line-in socket
; otherwise, if a virtual tape is playing, take virtual tape signal into output stream
.if RealTapeMode == FALSE
.if TapePlaying
add dx, EarVal
.endif
.endif
.if uSpeech_Enabled
push edx
invoke uSpeech_GetSample
pop edx
mov ax, uSpeech_Output
.if Sound_Effect != EFFECT_NONE
shr ax, 2
.endif
add dx, ax
.endif
.if SpecDrum_Enabled
add dx, SpecDrum_Output
.endif
.if Covox_Enabled && (HardwareMode == HW_PENTAGON128)
add dx, Covox_Output
.endif
.if SPGfx.TVNoiseCounter > 0
xor dx, dx
call TVNoiseBit
.if !ZERO?
mov dx, word ptr [PSGVolumes+15*4] ; read lower word of (dword) volume 15 entry
.endif
mov [FinalChanA], dx
mov [FinalChanB], dx
mov [FinalChanC], dx
xor dx, dx
.endif
; process stereomodes ---------------
movzx eax, StereoOutputMode
and eax, 7
jmp [AudioOutProcs+eax*4]
.data
align 16
AudioOutProcs dd OUT_NORMAL, OUT_ABC, OUT_ACB, OUT_PLUS3
dd OUT_NORMAL, OUT_NORMAL, OUT_NORMAL, OUT_NORMAL
.code
; ax = left channel, bx = right channel
;--------------------------------------------------------------------------------
align 16
OUT_NORMAL: mov ax, [FinalChanA]
add ax, [FinalChanB]
add ax, [FinalChanC]
add ax, dx ; ax = left-channel output
mov bx, ax ; bx = right-channel output
jmp AudioOut_Done
;--------------------------------------------------------------------------------
align 16
OUT_ABC: mov ax, [FinalChanB]
add ax, dx
mov bx, ax
add ax, [FinalChanA]
add bx, [FinalChanC]
jmp AudioOut_Done
;--------------------------------------------------------------------------------
align 16
OUT_ACB: mov ax, [FinalChanC]
add ax, dx
mov bx, ax
add ax, [FinalChanA]
add bx, [FinalChanB]
jmp AudioOut_Done
;--------------------------------------------------------------------------------
align 16
OUT_PLUS3: xor eax, eax
add ax, [FinalChanA]
.if ax > 2000
sub ax, 2000
.endif
add ax, [FinalChanB]
.if ax > 2000
sub ax, 2000
.endif
add ax, [FinalChanC]
.if ax > 2000
sub ax, 2000
.endif
sal ax, 1
add ax, dx ; ax = left channel output
.if ax > 32000
mov ax, 32000
.endif
mov bx,ax ; bx = right channel output
; end process stereomodes ---------------
AudioOut_Done:
.if Sound_Effect == EFFECT_ECHO
mov edi,[StereoWide1PtrS]
mov [edi],ax ; store left channel
add edi,2
ifc edi eq offset StereoEndBuffer1 then mov edi, offset StereoWide1BufferS
mov [StereoWide1PtrS],edi
mov edi,[StereoWide1PtrM]
mov cx,[edi] ; cx = old left channel
add edi,2
ifc edi eq offset StereoEndBuffer1 then mov edi, offset StereoWide1BufferS
mov [StereoWide1PtrM],edi
mov edi,[StereoWide2PtrS]
mov [edi],bx ; store right channel
add edi,2
ifc edi eq offset StereoEndBuffer2 then mov edi, offset StereoWide2BufferS
mov [StereoWide2PtrS],edi
mov edi,[StereoWide2PtrM]
mov dx,[edi] ; dx = old right channel
add edi,2
ifc edi eq offset StereoEndBuffer2 then mov edi, offset StereoWide2BufferS
mov [StereoWide2PtrM],edi
sar ax, 1
sar bx, 1
sar cx, 2
sar dx, 2
add ax, dx
add bx, cx
.elseif Sound_Effect == EFFECT_REVERB
mov edi,[StereoWide1PtrM]
mov cx,[edi] ; cx = old left channel
add edi,2
ifc edi eq offset StereoEndBuffer1 then mov edi, offset StereoWide1BufferS
mov [StereoWide1PtrM],edi
mov edi,[StereoWide2PtrM]
mov dx,[edi] ; dx = old right channel
add edi,2
ifc edi eq offset StereoEndBuffer2 then mov edi, offset StereoWide2BufferS
mov [StereoWide2PtrM],edi
sar ax, 1
sar bx, 1
sar cx, 1
sar dx, 1
add ax, dx
add bx, cx
mov edi,[StereoWide1PtrS]
mov [edi],ax ; store left channel
add edi,2
ifc edi eq offset StereoEndBuffer1 then mov edi, offset StereoWide1BufferS
mov [StereoWide1PtrS],edi
mov edi,[StereoWide2PtrS]
mov [edi],bx ; store right channel
add edi,2
ifc edi eq offset StereoEndBuffer2 then mov edi, offset StereoWide2BufferS
mov [StereoWide2PtrS],edi
.endif
No_Audio_Effects:
mov edi, lpAYBuffer_Posn ; our current buffer posn
add lpAYBuffer_Posn, 4 ; update our new current buffer posn
mov [edi], ax ; store left channel sample
mov [edi+2], bx ; store right channel sample
dec SamplesBeforeStreaming
je StreamAudioData
SampleBeepExit: ret
StreamAudioData:
call StreamAudio ; stream audio data to DirectSound buffer
mov eax, SamplesToBuffer
mov SamplesBeforeStreaming, eax
ret
;#################################################################################
align 16
Sample_AY proc
.if HighQualityAY && (FULLSPEEDMODE == NULL)
.if Emulate_AY
movzx eax, AYTimer
shr eax, 4 ; /16
.if !ZERO?
SETLOOP eax
call UpdateAYState_HQ
ifc SampleCounter lt 4 then call GetAYSample
ENDLOOP
.endif
.endif
and AYTimer, 15
mov al, MACHINE.AUDIOPERIOD.CurrentCyclesPerSample
.if SampleTimer >= al
.if Emulate_AY
.while SampleCounter < 4
call GetAYSample
.endw
mov eax, Total_ChanA
mov ebx, Total_ChanB
mov ecx, Total_ChanC
; take the average over 4 samples
sar eax, 2
sar ebx, 2
sar ecx, 2
; centralise output around zero point
sub eax, PSG_CENTRALISE_RANGE
sub ebx, PSG_CENTRALISE_RANGE
sub ecx, PSG_CENTRALISE_RANGE
mov FinalChanA, ax
mov FinalChanB, bx
mov FinalChanC, cx
.endif
call Create_Audio_Sample
mov al, MACHINE.AUDIOPERIOD.CurrentCyclesPerSample
sub SampleTimer, al
; adjust CyclesPerSample for the next sample period
mov cl, MACHINE.AUDIOPERIOD.CyclesPerSample
mov al, MACHINE.AUDIOPERIOD.SampleLoopCount
add al, 1
.if al == MACHINE.AUDIOPERIOD.SampleLoopAdjustRate
add cl, MACHINE.AUDIOPERIOD.SampleLoopAdjustValue
xor al, al
.endif
mov MACHINE.AUDIOPERIOD.SampleLoopCount, al
mov MACHINE.AUDIOPERIOD.CurrentCyclesPerSample, cl
.endif
ret
.else
mov al, MACHINE.AUDIOPERIOD.CurrentCyclesPerSample
.if SampleTimer >= al
.if Emulate_AY
movzx eax, AYTimer
shr eax, 4 ; /16
.if !ZERO?
SETLOOP eax
call UpdateAYState_HQ
ENDLOOP
.endif
call GetAYSample
.endif
and AYTimer, 15
mov eax, Total_ChanA
mov ebx, Total_ChanB
mov ecx, Total_ChanC
; centralise output around zero point
sub eax, PSG_CENTRALISE_RANGE
sub ebx, PSG_CENTRALISE_RANGE
sub ecx, PSG_CENTRALISE_RANGE
mov FinalChanA, ax
mov FinalChanB, bx
mov FinalChanC, cx
call Create_Audio_Sample
mov al, MACHINE.AUDIOPERIOD.CurrentCyclesPerSample
sub SampleTimer, al
; adjust CyclesPerSample for the next sample period
mov cl, MACHINE.AUDIOPERIOD.CyclesPerSample
mov al, MACHINE.AUDIOPERIOD.SampleLoopCount
add al, 1
.if al == MACHINE.AUDIOPERIOD.SampleLoopAdjustRate
add cl, 1
xor al, al
.endif
mov MACHINE.AUDIOPERIOD.SampleLoopCount, al
mov MACHINE.AUDIOPERIOD.CurrentCyclesPerSample, cl
.endif
.endif
ret
Sample_AY endp
align 16
UpdateAYState_HQ: lea esi, AYBase
xor byte ptr [esi+AY_EnvelopeClock], 1
je HandleChannels_HQ ;HandleWhiteNoise_HQ
; we only handle the envelope clock if a channel is using it
; a write to reg 13 will reset the envelope counter to zero anyway
test dword ptr [esi+AY_R8], 101010h
je HandleWhiteNoise_HQ
mov ax, word ptr [esi+AY_EnvCounter]
add ax, 1
.if ax >= [esi+AY_EnvPeriod]
movzx ebx, byte ptr [esi+AY_R13]
mov word ptr [esi+AY_EnvCounter], 0
call [ExecEnvVectors+ebx*4]
xor eax, eax ; reset envelope counter
.endif
mov [esi+AY_EnvCounter], ax
HandleWhiteNoise_HQ: mov al, [esi+AY_WhiteNoiseCounter]
add al, 1
.if al >= [esi+AY_InternalR6]
mov al, [esi+AY_R6]
cmp al, 1
adc al, 0
mov [esi+AY_InternalR6], al
; create new white noise output value
shr dword ptr [esi+AY_RandomSeed], 1
setc byte ptr [esi+AY_NoiseOutput]
.if CARRY?
xor dword ptr [esi+AY_RandomSeed], 24000h shr 1
.endif
xor eax, eax ; reset white noise counter
.endif
mov [esi+AY_WhiteNoiseCounter], al
HandleChannels_HQ: xor di, di
mov dx, 1
mov ax, [esi+AY_ChACounter]
mov bx, [esi+AY_ChBCounter]
mov cx, [esi+AY_ChCCounter]
add ax, dx
add bx, dx
add cx, dx
cmp [esi+AY_ToneA], ax
setbe dl
cmovbe ax, di
xor byte ptr [esi+AY_ChAOutput], dl
mov word ptr [esi+AY_ChACounter], ax
cmp [esi+AY_ToneB], bx
setbe dl
cmovbe bx, di
xor byte ptr [esi+AY_ChBOutput], dl
mov word ptr [esi+AY_ChBCounter], bx
cmp [esi+AY_ToneC], cx
setbe dl
cmovbe cx, di
xor byte ptr [esi+AY_ChCOutput], dl
mov word ptr [esi+AY_ChCCounter], cx
ret
;#################################################################################
.data
align 4
ExecEnvVectors dd ExecEnv0, ExecEnv1, ExecEnv2, ExecEnv3, ExecEnv4, ExecEnv5, ExecEnv6, ExecEnv7
dd ExecEnv8, ExecEnv9, ExecEnv10, ExecEnv11, ExecEnv12, ExecEnv13, ExecEnv14, ExecEnv15
.code
DoDECAY MACRO
dec dword ptr [esi+AY_EnvVolume]
ENDM
DoATTACK MACRO
inc dword ptr [esi+AY_EnvVolume]
ENDM
align 16
ExecEnv1:
ExecEnv2:
ExecEnv3:
ExecEnv0: cmp byte ptr [esi+AY_EnvMode], env_OFF
je @F
cmp dword ptr [esi+AY_EnvVolume], 0
je Set_OFF
DoDECAY
@@: ret
align 16
ExecEnv5:
ExecEnv6:
ExecEnv7:
ExecEnv4: cmp byte ptr [esi+AY_EnvMode], env_OFF
je @F
cmp dword ptr [esi+AY_EnvVolume], 15
je Set_OFF
DoATTACK
@@: ret
align 16
ExecEnv8: cmp dword ptr [esi+AY_EnvVolume], 0
je Set_DECAY
DoDECAY
ret
align 16
ExecEnv9: cmp byte ptr [esi+AY_EnvMode], env_OFF
je @F
cmp dword ptr [esi+AY_EnvVolume], 0
je Set_OFF
DoDECAY
@@: ret
align 16
ExecEnv10: .if byte ptr [esi+AY_EnvMode] == env_DECAY
cmp dword ptr [esi+AY_EnvVolume], 0
je Set_ATTACK
DoDECAY
ret
.else
cmp dword ptr [esi+AY_EnvVolume], 15
je Set_DECAY
DoATTACK
ret
.endif
ret
align 16
ExecEnv11: cmp byte ptr [esi+AY_EnvMode], env_HOLD
je @F
cmp dword ptr [esi+AY_EnvVolume], 0
je Set_HOLD
DoDECAY
@@: ret
align 16
ExecEnv12: cmp dword ptr [esi+AY_EnvVolume], 15
je Set_ATTACK
DoATTACK
ret
align 16
ExecEnv13: cmp byte ptr [esi+AY_EnvMode], env_HOLD
je @F
cmp dword ptr [esi+AY_EnvVolume], 15
je Set_HOLD
DoATTACK
@@: ret
align 16
ExecEnv14: .if byte ptr [esi+AY_EnvMode] == env_ATTACK
cmp dword ptr [esi+AY_EnvVolume], 15
je Set_DECAY
DoATTACK
ret
.else
cmp dword ptr [esi+AY_EnvVolume], 0
je Set_ATTACK
DoDECAY
ret
.endif
ret
align 16
ExecEnv15: cmp byte ptr [esi+AY_EnvMode], env_OFF
je @F
cmp dword ptr [esi+AY_EnvVolume], 15
je Set_OFF
DoATTACK
@@: ret
Set_OFF: mov byte ptr [esi+AY_EnvMode], env_OFF
mov dword ptr [esi+AY_EnvVolume], 0
ret
Set_ATTACK: mov byte ptr [esi+AY_EnvMode], env_ATTACK
mov dword ptr [esi+AY_EnvVolume], 0
ret
Set_DECAY: mov byte ptr [esi+AY_EnvMode], env_DECAY
mov dword ptr [esi+AY_EnvVolume], 15
ret
Set_HOLD: mov byte ptr [esi+AY_EnvMode], env_HOLD
mov dword ptr [esi+AY_EnvVolume], 15
ret
;#################################################################################
align 16
GetAYSample:
lea esi, AYBase
inc dword ptr [esi+AY_SampleCounter]
mov dh, [esi+AY_R7]
mov bh, [esi+AY_NoiseOutput]
mov al, [esi+AY_ChAOutput]
mov cl, [esi+AY_R8]
call MixOutput
add [esi+AY_Total_ChanA], ecx
mov [esi+AY_FinalChanA], cx
shr dh, 1
mov al, [esi+AY_ChBOutput]
mov cl, [esi+AY_R9]
call MixOutput
add [esi+AY_Total_ChanB], ecx
mov [esi+AY_FinalChanB], cx
shr dh, 1
mov al, [esi+AY_ChCOutput]
mov cl, [esi+AY_R10]
call MixOutput
add [esi+AY_Total_ChanC], ecx
mov [esi+AY_FinalChanC], cx
ret
align 16
MixOutput: xor edi, edi
mov dl, dh
mov ch, dh
and dl, 1
shr ch, 3
or al, dl ; (Tone OR ToneEnable)
or ch, bh ; (Noise OR NoiseEnable)
and al, ch ; al = 0 OR 1 for this channel's final output
shr al, 1
sbb edi, edi ; edi = 0 for no output, -1 for output
test cl, 16
cmovnz ecx, [esi+AY_EnvVolume]
and ecx, 15
mov ecx, [PSGVolumes+ecx*4]
and ecx, edi
ret
;#################################################################################
align 16
StreamAudio:
.if lpAYBuffer == NULL ; exit if we have no private buffer
ret
.endif
; retrieve the number of bytes of audio data we have buffered
mov eax,lpAYBuffer_Posn
sub eax,lpAYBuffer ; bytes of audio data = (current buffer posn - base buffer posn)
mov AY_BufferedBytes,eax ; AY_BufferedBytes = number of bytes of audio data we have
; now we lock the DS buffer and stream our audio data
LockBeepBuffer:
DSBINVOKE mLock, SecondaryBuffer, DSBuff_WritePosn, AY_BufferedBytes, \
addr audio_ptr_1, addr audio_length_1, \
addr audio_ptr_2, addr audio_length_2, \
NULL
.if eax == DSERR_BUFFERLOST
call RestoreSoundBuffer
jmp LockBeepBuffer
.endif
.if eax == DS_OK
mov esi, lpAYBuffer
mov edi, audio_ptr_1
mov ecx, audio_length_1
shr ecx, 2
rep movsd
mov ecx, audio_length_1
and ecx, 3
rep movsb
mov esi, lpAYBuffer
add esi, audio_length_1
mov edi, audio_ptr_2
mov ecx, audio_length_2
shr ecx, 2
rep movsd
mov ecx, audio_length_2
and ecx, 3
rep movsb
DSBINVOKE Unlock, SecondaryBuffer, audio_ptr_1, audio_length_1, \
audio_ptr_2, audio_length_2
.if eax != DS_OK
FATAL "DirectSound Unlock failed !"
.endif
.if SoundPlaying == 0
DSBINVOKE SetCurrentPosition, SecondaryBuffer, 0
DSBINVOKE Play, SecondaryBuffer, 0, 0, DSBPLAY_LOOPING
mov SoundPlaying, 1
.endif
.endif
mov eax, lpAYBuffer
mov lpAYBuffer_Posn, eax ; reset our current buffer posn to the base of the buffer
; DSBuff_WritePosn = (DSBuff_WritePosn + AY_BufferedBytes) MOD DSBUFFERSIZE
mov eax, DSBuff_WritePosn
add eax, AY_BufferedBytes
.if eax >= DSBUFFERSIZE
sub eax, DSBUFFERSIZE
.endif
mov DSBuff_WritePosn, eax
;-----------------------------------------
; stream audio to wave file if required
.if AudioFH != 0
.if AudioFileLength < (200*(1024*1024)) ; limit to approx 200MB
invoke WriteFile, AudioFH, lpAYBuffer, AY_BufferedBytes, addr BytesSaved, NULL
mov eax, AY_BufferedBytes
add AudioFileLength, eax
.endif
.endif
;-----------------------------------------
mov process_priority_set, FALSE
.if (TapePlaying == TRUE) && (FastTapeLoading == TRUE)
jmp BWP_Cont ; skip DSound buffer syncing
.endif
.if FULLSPEEDMODE == NULL
jmp WaitForPosn_1
WaitForPosn: .if process_priority_set == FALSE
invoke SetPriorityClass, $fnc (GetCurrentProcess), HIGH_PRIORITY_CLASS
mov process_priority_set, TRUE
.endif
invoke Sleep, 1
WaitForPosn_1: DSBINVOKE GetCurrentPosition, SecondaryBuffer, addr CurrentPlayCursor, addr CurrentWriteCursor
.if eax != DS_OK
FATAL "DirectSound GetCurrentPosition() failed!"
.endif
; loop back until (CurrentWriteCursor >= DSBuff_WaitPosn) && (CurrentWriteCursor < DSBuff_WaitPosn + (SAMPLEBYTESPERFRAME-1))
; are we in the buffer block we're waiting for?
invoke InBufferBlock, DSBuff_WaitPosn
cmp eax, TRUE
je BWP_Cont ; exit if we are
; else are we in the preceding buffer block still?
mov ecx, DSBuff_WaitPosn
sub ecx, SampleBytesToBuffer
.if CARRY?
add ecx, DSBUFFERSIZE
.endif
invoke InBufferBlock, ecx
cmp eax, TRUE
je WaitForPosn ; loop with sleep if we're in this buffer block
.endif
;-----------------------------------------
BWP_Cont: .if process_priority_set == TRUE
invoke SetPriorityClass, $fnc (GetCurrentProcess), NORMAL_PRIORITY_CLASS
mov process_priority_set, FALSE
.endif
; DSBuff_WaitPosn = (DSBuff_WaitPosn + AY_BufferedBytes) MOD DSBUFFERSIZE
mov eax, DSBuff_WaitPosn
add eax, AY_BufferedBytes
.if eax >= DSBUFFERSIZE
sub eax, DSBUFFERSIZE
.endif
mov DSBuff_WaitPosn, eax
BWP_Exit: ret
align 16
InBufferBlock proc uses ebx ecx,
DSBlockOffset: DWORD
mov eax, CurrentWriteCursor ; eax = current writecursor position
mov ebx, DSBlockOffset ; ebx = block start position
mov ecx, ebx
add ecx, SampleBytesToBuffer ; add (SAMPLEBYTESPERFRAME - 1)
dec ecx ; ecx = block end position
.if (eax >= ebx) && (eax <= ecx)