-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAY.pas
1987 lines (1760 loc) · 47.1 KB
/
AY.pas
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
{
This is part of Vortex Tracker II project
(c)2000-2009 S.V.Bulba
Author: Sergey Bulba, [email protected]
Support page: http://bulba.untergrund.net/
Version 2.0 and later
(c)2017-2021 Ivan Pirog, [email protected]
https://github.com/ivanpirog/vortextracker
}
unit AY;
interface
uses Windows, trfuncs, WaveOutAPI, ayumi;
type
//Available soundchips
ChTypes = (No_Chip, AY_Chip, YM_Chip);
TPlayModes = (PMPlayModule, PMPlayPattern, PMPlayLine);
const
{//Two amplitude tables of sound chips (c)Hacker KAY
Amplitudes_AY: array[0..15] of Word =
(0, 836, 1212, 1773, 2619, 3875, 5397, 8823, 10392, 16706, 23339,
29292, 36969, 46421, 55195, 65535);
Amplitudes_YM: array[0..31] of Word =
(0, 0, $F8, $1C2, $29E, $33A, $3F2, $4D7, $610, $77F, $90A, $A42,
$C3B, $EC2, $1137, $13A7, $1750, $1BF9, $20DF, $2596, $2C9D, $3579,
$3E55, $4768, $54FF, $6624, $773B, $883F, $A1DA, $C0FC, $E094, $FFFF); }
// Two amplitude tables of sound chips (c) Introspec
Amplitudes_AY: array[0..15] of Word =
(
$0000, $028F, $03B3, $0564, $07DC, $0BA9, $1083, $1B7C, $2068, $347A, $4ACE,
$5F72, $7E16, $A2A4, $CE3A, $FFFF
);
Amplitudes_YM: array[0..31] of Word =
(
$0000, $0000, $0131, $01FA, $02CE, $0393, $045A, $0520, $063D, $079A, $08FA,
$0A57, $0C6D, $0EEF, $116C, $13E9, $17AF, $1C70, $2137, $2603, $2D3A, $3628,
$3F13, $47F6, $556F, $6682, $77A6, $88D0, $A29A, $C20C, $E142, $FFFF
);
AY_Freq_Def = 1750000; //1773400;
Interrupt_Freq_Def = 48828; //50000;
NumberOfChannels_Def = 2;
SampleRate_Def = 44100;
SampleBit_Def = 16;
Index_AL_Def = 255;
Index_AR_Def = 13;
Index_BL_Def = 170;
Index_BR_Def = 170;
Index_CL_Def = 13;
Index_CR_Def = 255;
StdChannelsAllocationDef = 1;
Filt_NKoefs = 32; //powers of 2
type
PRegisters = ^TRegisters;
TRegisters = packed record
case Boolean of
True: (Index: array[0..13] of byte);
False: (TonA, TonB, TonC: word;
Noise, Mixer: byte;
AmplitudeA, AmplitudeB, AmplitudeC: byte;
Envelope: word;
EnvType: byte);
end;
TSoundChip = object
AYRegisters: TRegisters;
First_Period: boolean;
Ampl: integer;
Ton_Counter_A, Ton_Counter_B, Ton_Counter_C, Noise_Counter: packed record
case integer of
0: (Lo: word;
Hi: word);
1: (Re: longword);
end;
Envelope_Counter: packed record
case integer of
0: (Lo: dword;
Hi: dword);
1: (Re: int64);
end;
Ton_A, Ton_B, Ton_C: integer;
Noise: packed record
case boolean of
True: (Seed: longword);
False: (Low: word;
Val: dword);
end;
Case_EnvType: procedure of object;
Ton_EnA, Ton_EnB, Ton_EnC, Noise_EnA, Noise_EnB, Noise_EnC: boolean;
Envelope_EnA, Envelope_EnB, Envelope_EnC: boolean;
procedure Case_EnvType_0_3__9;
procedure Case_EnvType_4_7__15;
procedure Case_EnvType_8;
procedure Case_EnvType_10;
procedure Case_EnvType_11;
procedure Case_EnvType_12;
procedure Case_EnvType_13;
procedure Case_EnvType_14;
procedure Synthesizer_Logic_Q;
procedure Synthesizer_Logic_P;
procedure SetMixerRegister(Value: byte);
procedure SetEnvelopeRegister(Value: byte);
procedure SetAmplA(Value: byte);
procedure SetAmplB(Value: byte);
procedure SetAmplC(Value: byte);
procedure Synthesizer_Mixer_Q;
procedure Synthesizer_Mixer_Q_Mono;
end;
TFilt_K = array of integer;
var
Filt_M: integer = Filt_NKoefs;
IsFilt: boolean = True;
Filt_K, Filt_XL, Filt_XR: TFilt_K;
Filt_I: integer;
PlayMode: TPlayModes;
DCType, DCCutOff: Integer;
NumberOfSoundChips: integer = MaxNumberOfSoundChips;
//Sound chip parameters
SoundChip: array[1..MaxNumberOfSoundChips] of TSoundChip;
AyumiChip1, AyumiChip2: TAyumi;
//Parameters for all sound chips
Index_AL, Index_AR, Index_BL, Index_BR, Index_CL, Index_CR: byte;
Emulating_Chip: ChTypes;
AY_Freq: integer;
Level_AR, Level_AL, Level_BR, Level_BL, Level_CR, Level_CL: array[0..31] of Integer;
LevelL, LevelR, Left_Chan, Right_Chan: integer;
Tick_Counter: byte;
Tik: packed record
case Integer of
0: (Lo: Word;
Hi: word);
1: (Re: dword);
end;
Delay_in_tiks: dword;
Current_Tik: longword;
Number_Of_Tiks: packed record
case boolean of
false: (lo: longword;
hi: longword);
true: (re: int64);
end;
IntFlag: boolean;
AY_Tiks_In_Interrupt: longword;
Synthesizer: procedure(Buf: pointer);
StdChannelsAllocation: integer;
Real_End: array[1..MaxNumberOfSoundChips] of boolean;
Real_End_All, LoopAllowed: boolean;
(*type
TFilt_K = array of integer;
var
Filt_M:integer = Filt_NKoefs;
IsFilt:boolean = True;
Filt_K,Filt_XL,Filt_XR:TFilt_K;
Filt_I:integer;
PlayMode:TPlayModes;
Index_AL,Index_AR,Index_BL,Index_BR,Index_CL,Index_CR:byte;
Emulating_Chip:ChTypes;
AY_Freq:integer;
First_Period:boolean;
Ampl:integer;
Ton_Counter_A,
Ton_Counter_B,
Ton_Counter_C,
Noise_Counter:packed record
case integer of
0:(Lo:word;
Hi:word);
1:(Re:longword);
end;
Envelope_Counter:packed record
case integer of
0:(Lo:dword;
Hi:dword);
1:(Re:int64);
end;
Ton_A,Ton_B,Ton_C:integer;
Noise:packed record
case boolean of
True: (Seed:longword);
False:(Low:word;
Val:dword);
end;
Level_AR,Level_AL,
Level_BR,Level_BL,
Level_CR,Level_CL:array[0..31]of Integer;
Left_Chan,Right_Chan:integer;
Tick_Counter:byte;
Tik:packed record
Case Integer of
0:(Lo:Word;
Hi:word);
1:(Re:dword);
end;
Delay_in_tiks:dword;
Case_EnvType:procedure;
Ton_EnA,Ton_EnB,
Ton_EnC,Noise_EnA,
Noise_EnB,Noise_EnC:boolean;
Envelope_EnA,Envelope_EnB,Envelope_EnC:boolean;
Current_Tik:longword;*)
RenderEngine: Integer = 2;
(* Number_Of_Tiks:packed record
case boolean of
false:(lo:longword;
hi:longword);
true: (re:int64);
end;
IntFlag:boolean;
AY_Tiks_In_Interrupt:longword;
Synthesizer:procedure(Buf:pointer);
Real_End,LoopAllowed:boolean;
StdChannelsAllocation:integer;*)
procedure SetAyumiRegisters(Ayumi: TAyumi; r:PRegisters);
procedure Synthesizer_Ayumi(Buf: pointer);
procedure Synthesizer_Stereo16(Buf: pointer);
procedure Synthesizer_Stereo16_P(Buf: pointer);
procedure Synthesizer_Stereo8(Buf: pointer);
procedure Synthesizer_Stereo8_P(Buf: pointer);
procedure Synthesizer_Mono16(Buf: pointer);
procedure Synthesizer_Mono16_P(Buf: pointer);
procedure Synthesizer_Mono8(Buf: pointer);
procedure Synthesizer_Mono8_P(Buf: pointer);
procedure UpdatePanoram;
procedure MakeBuffer(Buf: pointer);
(*procedure SetEnvelopeRegister(Value:byte);
procedure SetMixerRegister(Value:byte);
procedure SetAmplA(Value:byte);
procedure SetAmplB(Value:byte);
procedure SetAmplC(Value:byte);*)
procedure SetDefault(samrate, nchan, sambit: integer);
procedure Calculate_Level_Tables;
function ToggleChanMode: string;
function SetStdChannelsAllocation(CA: integer): string;
procedure SetIntFreq(f: integer);
procedure SetSampleRate(f: integer);
procedure SetBuffers(len, num: integer);
procedure SetBitRate(SB: integer);
procedure SetNChans(St: integer);
procedure Set_Engine(EngineIndex: Integer);
procedure SetAYFreq(f: integer);
procedure SetFilter(Filt: boolean; M: integer);
procedure CalcFiltKoefs;
implementation
uses Childwin, Main;
{$J+} { Assignable Typed Constant }
type
T24Bit = record
b1, b2, b3: Byte
end;
TS16 = packed array[0..0] of record
Left: smallint;
Right: smallint;
end;
PS16 = ^TS16;
TS8 = packed array[0..0] of record
Left: byte;
Right: byte;
end;
PS8 = ^TS8;
TS24 = packed array[0..0] of record
Left: T24Bit;
Right: T24Bit;
end;
PS24 = ^TS24;
TS32 = packed array[0..0] of record
Left: Integer;
Right: Integer;
end;
PS32 = ^TS32;
TM16 = packed array[0..0] of smallint;
PM16 = ^TM16;
TM8 = packed array[0..0] of byte;
PM8 = ^TM8;
TM24 = packed array[0..0] of T24Bit;
PM24 = ^TM24;
TM32 = packed array[0..0] of Integer;
PM32 = ^TM32;
procedure TSoundChip.Case_EnvType_0_3__9;
begin
if First_Period then
begin
dec(Ampl);
if Ampl = 0 then First_Period := False
end
end;
procedure TSoundChip.Case_EnvType_4_7__15;
begin
if First_Period then
begin
Inc(Ampl);
if Ampl = 32 then
begin
First_Period := False;
Ampl := 0
end
end
end;
procedure TSoundChip.Case_EnvType_8;
begin
Ampl := (Ampl - 1) and 31
end;
procedure TSoundChip.Case_EnvType_10;
begin
if First_Period then
begin
dec(Ampl);
if Ampl < 0 then
begin
First_Period := False;
Ampl := 0
end
end
else
begin
inc(Ampl);
if Ampl = 32 then
begin
First_Period := True;
Ampl := 31
end
end
end;
procedure TSoundChip.Case_EnvType_11;
begin
if First_Period then
begin
dec(Ampl);
if Ampl < 0 then
begin
First_Period := False;
Ampl := 31
end
end
end;
procedure TSoundChip.Case_EnvType_12;
begin
Ampl := (Ampl + 1) and 31
end;
procedure TSoundChip.Case_EnvType_13;
begin
if First_Period then
begin
inc(Ampl);
if Ampl = 32 then
begin
First_Period := False;
Ampl := 31
end
end
end;
procedure TSoundChip.Case_EnvType_14;
begin
if not First_Period then
begin
dec(Ampl);
if Ampl < 0 then
begin
First_Period := True;
Ampl := 0
end
end
else
begin
inc(Ampl);
if Ampl = 32 then
begin
First_Period := False;
Ampl := 31
end
end
end;
function NoiseGenerator(Seed: integer): integer;
asm
shld edx,eax,16
shld ecx,eax,19
xor ecx,edx
and ecx,1
add eax,eax
and eax,$1ffff
inc eax
xor eax,ecx
end;
procedure TSoundChip.Synthesizer_Logic_Q;
begin
inc(Ton_Counter_A.Hi);
if Ton_Counter_A.Hi >= AYRegisters.TonA then
begin
Ton_Counter_A.Hi := 0;
Ton_A := Ton_A xor 1
end;
inc(Ton_Counter_B.Hi);
if Ton_Counter_B.Hi >= AYRegisters.TonB then
begin
Ton_Counter_B.Hi := 0;
Ton_B := Ton_B xor 1
end;
inc(Ton_Counter_C.Hi);
if Ton_Counter_C.Hi >= AYRegisters.TonC then
begin
Ton_Counter_C.Hi := 0;
Ton_C := Ton_C xor 1
end;
inc(Noise_Counter.Hi);
if (Noise_Counter.Hi and 1 = 0) and
(Noise_Counter.Hi >= AYRegisters.Noise shl 1) then
begin
Noise_Counter.Hi := 0;
Noise.Seed := NoiseGenerator(Noise.Seed);
end;
if Envelope_Counter.Hi = 0 then Case_EnvType;
inc(Envelope_Counter.Hi);
if Envelope_Counter.Hi >= AYRegisters.Envelope then
Envelope_Counter.Hi := 0
end;
procedure TSoundChip.Synthesizer_Logic_P;
var
k: word;
k2: longword;
begin
inc(Ton_Counter_A.Re, Delay_In_Tiks);
k := AYRegisters.TonA; if k = 0 then inc(k);
if k <= Ton_Counter_A.Hi then
begin
Ton_A := Ton_A xor ((Ton_Counter_A.Hi div k) and 1);
Ton_Counter_A.Hi := Ton_Counter_A.Hi mod k
end;
inc(Ton_Counter_B.Re, Delay_In_Tiks);
k := AYRegisters.TonB; if k = 0 then inc(k);
if k <= Ton_Counter_B.Hi then
begin
Ton_B := Ton_B xor ((Ton_Counter_B.Hi div k) and 1);
Ton_Counter_B.Hi := Ton_Counter_B.Hi mod k
end;
inc(Ton_Counter_C.Re, Delay_In_Tiks);
k := AYRegisters.TonC; if k = 0 then inc(k);
if k <= Ton_Counter_C.Hi then
begin
Ton_C := Ton_C xor ((Ton_Counter_C.Hi div k) and 1);
Ton_Counter_C.Hi := Ton_Counter_C.Hi mod k
end;
inc(Noise_Counter.Re, Delay_In_Tiks);
k := AYRegisters.Noise; if k = 0 then inc(k);
k := k shl 1;
if Noise_Counter.Hi >= k then
begin
Noise_Counter.Hi := Noise_Counter.Hi mod k;
Noise.Seed := NoiseGenerator(Noise.Seed);
end;
k2 := AYRegisters.Envelope; if k2 = 0 then inc(k2);
if Envelope_Counter.Hi = 0 then inc(Envelope_Counter.Hi, k2);
while (Envelope_Counter.Hi >= k2) do
begin
dec(Envelope_Counter.Hi, k2);
Case_EnvType
end;
inc(Envelope_Counter.Re, int64(Delay_In_Tiks) shl 16)
end;
procedure TSoundChip.SetMixerRegister(Value: byte);
begin
AYRegisters.Mixer := Value;
Ton_EnA := (Value and 1) = 0;
Noise_EnA := (Value and 8) = 0;
Ton_EnB := (Value and 2) = 0;
Noise_EnB := (Value and 16) = 0;
Ton_EnC := (Value and 4) = 0;
Noise_EnC := (Value and 32) = 0
end;
procedure TSoundChip.SetEnvelopeRegister(Value: byte);
begin
Envelope_Counter.Hi := 0;
First_Period := True;
if (Value and 4) = 0 then
ampl := 32
else
ampl := -1;
AYRegisters.EnvType := Value;
case Value of
0..3, 9: Case_EnvType := Case_EnvType_0_3__9;
4..7, 15: Case_EnvType := Case_EnvType_4_7__15;
8: Case_EnvType := Case_EnvType_8;
10: Case_EnvType := Case_EnvType_10;
11: Case_EnvType := Case_EnvType_11;
12: Case_EnvType := Case_EnvType_12;
13: Case_EnvType := Case_EnvType_13;
14: Case_EnvType := Case_EnvType_14;
end;
end;
procedure TSoundChip.SetAmplA(Value: byte);
begin
AYRegisters.AmplitudeA := Value;
Envelope_EnA := (Value and 16) = 0;
end;
procedure TSoundChip.SetAmplB(Value: byte);
begin
AYRegisters.AmplitudeB := Value;
Envelope_EnB := (Value and 16) = 0;
end;
procedure TSoundChip.SetAmplC(Value: byte);
begin
AYRegisters.AmplitudeC := Value;
Envelope_EnC := (Value and 16) = 0;
end;
//sorry for assembler, I can't make effective qword procedure on pascal...
function ApplyFilter(Lev: integer; var Filt_X: TFilt_K): integer;
asm
push ebx
push esi
push edi
add esp,-8
mov ecx,Filt_M
mov edi,Filt_K
lea esi,edi+ecx*4
mov ebx,[edx]
mov ecx,Filt_I
mov [ebx+ecx*4],eax
imul dword ptr [edi]
mov [esp],eax
mov [esp+4],edx
@lp: dec ecx
jns @gz
mov ecx,Filt_M
@gz: mov eax,[ebx+ecx*4]
add edi,4
imul dword ptr [edi]
add [esp],eax
adc [esp+4],edx
cmp edi,esi
jnz @lp
mov Filt_I,ecx
pop eax
pop edx
pop edi
pop esi
pop ebx
test edx,edx
jns @nm
add eax,0FFFFFFh
adc edx,0
@nm: shrd eax,edx,24
end;
procedure TSoundChip.Synthesizer_Mixer_Q;
var
LevL, LevR, k: integer;
begin
LevL := 0;
LevR := LevL;
k := 1;
if Ton_EnA then k := Ton_A;
if Noise_EnA then k := k and Noise.Val;
if k <> 0 then
begin
if Envelope_EnA then
begin
inc(LevL, Level_AL[AYRegisters.AmplitudeA * 2 + 1]);
inc(LevR, Level_AR[AYRegisters.AmplitudeA * 2 + 1])
end
else
begin
inc(LevL, Level_AL[Ampl]);
inc(LevR, Level_AR[Ampl])
end
end;
k := 1;
if Ton_EnB then k := Ton_B;
if Noise_EnB then k := k and Noise.Val;
if k <> 0 then
if Envelope_EnB then
begin
inc(LevL, Level_BL[AYRegisters.AmplitudeB * 2 + 1]);
inc(LevR, Level_BR[AYRegisters.AmplitudeB * 2 + 1])
end
else
begin
inc(LevL, Level_BL[Ampl]);
inc(LevR, Level_BR[Ampl])
end;
k := 1;
if Ton_EnC then k := Ton_C;
if Noise_EnC then k := k and Noise.Val;
if k <> 0 then
if Envelope_EnC then
begin
inc(LevL, Level_CL[AYRegisters.AmplitudeC * 2 + 1]);
inc(LevR, Level_CR[AYRegisters.AmplitudeC * 2 + 1])
end
else
begin
inc(LevL, Level_CL[Ampl]);
inc(LevR, Level_CR[Ampl])
end;
inc(LevelL, LevL);
inc(LevelR, LevR)
end;
function packPosPatLine(PositionNumber, PatternNumber, LineNumber: Integer): Integer;
begin
Result := (PositionNumber and $1FF) +
(PatternNumber shl 9) +
(LineNumber shl 17);
end;
procedure FillPlayGrid;
var k: Cardinal;
begin
k := NOfTicks div PlayGridLen;
MkVisPos := NOfTicks - (PlayGridLen * k);
with PlVars[1] do
PlayingGrid[MkVisPos].M1 := packPosPatLine(CurrentPosition, CurrentPattern, CurrentLine - 1);
if NumberOfSoundChips > 1 then
with PlVars[2] do
PlayingGrid[MkVisPos].M2 := packPosPatLine(CurrentPosition, CurrentPattern, CurrentLine - 1);
end;
procedure SetAyumiRegisters(Ayumi: TAyumi; r:PRegisters);
begin
Ayumi.SetTone(0, (r.Index[1] shl 8) or r.Index[0]);
Ayumi.SetTone(1, (r.Index[3] shl 8) or r.Index[2]);
Ayumi.SetTone(2, (r.Index[5] shl 8) or r.Index[4]);
Ayumi.SetNoise(r.Index[6]);
Ayumi.SetMixer(0, r.Index[7] and 1, (r.Index[7] shr 3) and 1, r.Index[8] shr 4);
Ayumi.SetMixer(1, (r.Index[7] shr 1) and 1, (r.Index[7] shr 4) and 1, (r.Index[9] shr 4) and 1);
Ayumi.SetMixer(2, (r.Index[7] shr 2) and 1, (r.Index[7] shr 5) and 1, (r.Index[10] shr 4) and 1);
Ayumi.SetVolume(0, r.Index[8] and $0f);
Ayumi.SetVolume(1, r.Index[9] and $0f);
Ayumi.SetVolume(2, r.Index[10] and $0f);
Ayumi.SetEnvelope((r.Index[12] shl 8) or r.Index[11]);
if (r.Index[13] <> 255) then
ayumi.setEnvelopeShape(r.Index[13]);
r.Index[13] := 255;
end;
procedure Synthesizer_Stereo16;
var
Tmp: integer;
begin
repeat
Tmp := 0; LevelL := Tmp; LevelR := Tmp;
for Tmp := 1 to NumberOfSoundChips do
begin
SoundChip[Tmp].Synthesizer_Logic_Q;
SoundChip[Tmp].Synthesizer_Mixer_Q;
end;
if IsFilt then
begin
Tmp := Filt_I;
LevelL := ApplyFilter(LevelL, Filt_XL);
Filt_I := Tmp;
LevelR := ApplyFilter(LevelR, Filt_XR)
end;
inc(Left_Chan, LevelL);
inc(Right_Chan, LevelR);
Inc(Current_Tik);
Inc(Tick_Counter);
if Tick_Counter >= Tik.Hi then
begin
Inc(Tik.Re, Delay_In_Tiks);
Dec(Tik.Hi, Tick_Counter);
Tmp := Left_Chan div Tick_Counter;
if Tmp > 32767 then
Tmp := 32767
else if Tmp < -32768 then
Tmp := -32768;
PS16(Buf)^[BuffLen].Left := Tmp;
Tmp := Right_Chan div Tick_Counter;
if Tmp > 32767 then
Tmp := 32767
else if Tmp < -32768 then
Tmp := -32768;
PS16(Buf)^[BuffLen].Right := Tmp;
FillPlayGrid;
Inc(BuffLen);
Inc(NOfTicks);
Tmp := 0;
Left_Chan := Tmp;
Right_Chan := Tmp;
Tick_Counter := Tmp;
if BuffLen = BufferLength then
begin
if Current_Tik < Number_Of_Tiks.Hi then
IntFlag := True;
exit
end
end
until Current_Tik >= Number_Of_Tiks.Hi;
Tmp := 0;
Number_Of_Tiks.hi := Tmp;
Current_Tik := Tmp
end;
procedure Synthesizer_Stereo8;
var
Tmp: integer;
begin
repeat
Tmp := 0; LevelL := Tmp; LevelR := Tmp;
for Tmp := 1 to NumberOfSoundChips do
begin
SoundChip[Tmp].Synthesizer_Logic_Q;
SoundChip[Tmp].Synthesizer_Mixer_Q;
end;
if IsFilt then
begin
Tmp := Filt_I;
LevelL := ApplyFilter(LevelL, Filt_XL);
Filt_I := Tmp;
LevelR := ApplyFilter(LevelR, Filt_XR)
end;
inc(Left_Chan, LevelL);
inc(Right_Chan, LevelR);
Inc(Current_Tik);
Inc(Tick_Counter);
if Tick_Counter >= Tik.Hi then
begin
Inc(Tik.Re, Delay_In_Tiks);
Dec(Tik.Hi, Tick_Counter);
Tmp := Left_Chan div Tick_Counter;
if Tmp > 127 then
Tmp := 127
else if Tmp < -128 then
Tmp := -128;
PS8(Buf)^[BuffLen].Left := 128 + Tmp;
Tmp := Right_Chan div Tick_Counter;
if Tmp > 127 then
Tmp := 127
else if Tmp < -128 then
Tmp := -128;
PS8(Buf)^[BuffLen].Right := 128 + Tmp;
FillPlayGrid;
Inc(BuffLen);
Inc(NOfTicks);
Tmp := 0;
Left_Chan := Tmp;
Right_Chan := Tmp;
Tick_Counter := Tmp;
if BuffLen = BufferLength then
begin
if Current_Tik < Number_Of_Tiks.Hi then
IntFlag := True;
exit
end
end
until Current_Tik >= Number_Of_Tiks.Hi;
Tmp := 0;
Number_Of_Tiks.hi := Tmp;
Current_Tik := Tmp
end;
procedure TSoundChip.Synthesizer_Mixer_Q_Mono;
var
Lev, k: integer;
begin
Lev := 0;
k := 1;
if Ton_EnA then k := Ton_A;
if Noise_EnA then k := k and Noise.Val;
if k <> 0 then
if Envelope_EnA then
inc(Lev, Level_AL[AYRegisters.AmplitudeA * 2 + 1])
else
inc(Lev, Level_AL[Ampl]);
k := 1;
if Ton_EnB then k := Ton_B;
if Noise_EnB then k := k and Noise.Val;
if k <> 0 then
if Envelope_EnB then
inc(Lev, Level_BL[AYRegisters.AmplitudeB * 2 + 1])
else
inc(Lev, Level_BL[Ampl]);
k := 1;
if Ton_EnC then k := Ton_C;
if Noise_EnC then k := k and Noise.Val;
if k <> 0 then
if Envelope_EnC then
inc(Lev, Level_CL[AYRegisters.AmplitudeC * 2 + 1])
else
inc(Lev, Level_CL[Ampl]);
inc(LevelL, Lev)
end;
procedure Synthesizer_Ayumi;
const
Volume: Integer = 0;
LevelRatio: Double = 0;
MaxPeak = 1.6;
MinPeak = -1.6;
var
r: PRegisters;
Chip, k: Integer;
Left, Right: Double;
begin
for Chip := 1 to NumberOfSoundChips do begin
r := @SoundChip[Chip].AYRegisters;
if Chip = 1 then
SetAyumiRegisters(AyumiChip1, r)
else
SetAyumiRegisters(AyumiChip2, r);
end;
repeat
Inc(Current_Tik);
Inc(Tick_Counter);
if Tick_Counter >= Tik.Hi then begin
Inc(Tik.Re, Delay_In_Tiks);
Dec(Tik.Hi, Tick_Counter);
AyumiChip1.Process;
AyumiChip1.RemoveDC;
Left := AyumiChip1.left;
Right := AyumiChip1.right;
if NumberOfSoundChips = 2 then begin
AyumiChip2.Process;
AyumiChip2.RemoveDC;
Left := Left + AyumiChip2.left;
Right := Right + AyumiChip2.right;
end;
if MainForm.GlobalVolume <> Volume then begin
Volume := MainForm.GlobalVolume;
LevelRatio := exp(Volume * ln(2) / MainForm.GlobalVolumeMax) - 1;
end;
Left := Left * LevelRatio;
Right := Right * LevelRatio;
if Left > MaxPeak then Left := MaxPeak;
if Left < MinPeak then Left := MinPeak;
if Right > MaxPeak then Right := MaxPeak;
if Right < MinPeak then Right := MinPeak;
case NumberOfChannels of
// MONO
1: begin
case SampleBit of
16: begin
PM16(Buf)^[BuffLen] := Round(((Left + Right) / 2) * $4FFF);
end;
24: begin
k := Round(((Left + Right) / 2) * $4FFFFF);
PM24(Buf)^[BuffLen].b1 := k and $FF;
PM24(Buf)^[BuffLen].b2 := k shr 8;
PM24(Buf)^[BuffLen].b3 := k shr 16;
end;
32: begin
PM32(Buf)^[BuffLen] := Round(((Left + Right) / 2) * $4FFFFFFF);
end;
end;
end;
// STEREO
2: begin
case SampleBit of
16: begin
PS16(Buf)^[BuffLen].Left := Round(Left * $4FFF);
PS16(Buf)^[BuffLen].Right := Round(Right * $4FFF);
end;
24: begin
k := Round(Left * $4FFFFF);
PS24(Buf)^[BuffLen].Left.b1 := k and $FF;
PS24(Buf)^[BuffLen].Left.b2 := k shr 8;
PS24(Buf)^[BuffLen].Left.b3 := k shr 16;
k := Round(Right * $4FFFFF);
PS24(Buf)^[BuffLen].Right.b1 := k and $FF;
PS24(Buf)^[BuffLen].Right.b2 := k shr 8;
PS24(Buf)^[BuffLen].Right.b3 := k shr 16;
end;
32: begin
PS32(Buf)^[BuffLen].Left := Round(Left * $4FFFFFFF);
PS32(Buf)^[BuffLen].Right := Round(Right * $4FFFFFFF);
end;
end;
end;
end;
FillPlayGrid;
Inc(BuffLen);
Inc(NOfTicks);
Tick_Counter := 0;
if BuffLen = BufferLength then
begin
if Current_Tik < Number_Of_Tiks.Hi then
IntFlag := True;
Exit;
end;
end;
until Current_Tik >= Number_Of_Tiks.Hi;
Number_Of_Tiks.hi := 0;
Current_Tik := 0;
end;
procedure Synthesizer_Mono16;
var
Tmp: integer;
begin
repeat
LevelL := 0;
for Tmp := 1 to NumberOfSoundChips do
begin
SoundChip[Tmp].Synthesizer_Logic_Q;
SoundChip[Tmp].Synthesizer_Mixer_Q_Mono;
end;
if IsFilt then
LevelL := ApplyFilter(LevelL, Filt_XL);