-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathU_SMIX.PAS
1319 lines (1141 loc) · 43 KB
/
U_SMIX.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
{ SMIX is Copyright 1995 by Ethan Brodsky. All rights reserved. }
unit u_SMix; {Version 1.30}
{$X+} {$G+} {$R-}
interface
const
BlockLength = 512; {Size of digitized sound block }
LoadChunkSize = 2048; {Chunk size used for loading sounds from disk}
Voices = 6; {Number of available voices }
DefSamplingRate = 11025; {Sampling rate for output }
type
PSound = ^TSound;
TSound =
record
XMSHandle: word;
StartOfs: LongInt;
SoundSize: LongInt;
end;
procedure startupsmix;
procedure shutdownsmix;
procedure smixsetvolume(v:byte);
procedure smixplay(i2:byte;s:string);
procedure smixstop(i2:byte);
function smixplaying(i:byte):byte;
function GetSettings
(
var BaseIO : word;
var IRQ : byte;
var DMA : byte;
var DMA16 : byte
): boolean;
{Gets sound card settings from BLASTER environment variable }
{Parameters: }
{ BaseIO: Sound card base IO address }
{ IRQ: Sound card IRQ }
{ DMA: Sound card 8-bit DMA channel }
{ DMA16: Sound card 16-bit DMA channel (0 if none) }
{Returns: }
{ TRUE: Sound card settings found successfully }
{ FALSE: Sound card settings could not be found }
function InitSB(BaseIO: word; IRQ: byte; DMA, DMA16: byte): boolean;
{Initializes control parameters, resets DSP, and installs int. handler }
{ Parameters: (Can be found using GetSettings procedure in Detect) }
{ BaseIO: Sound card base IO address }
{ IRQ: Sound card IRQ setting }
{ DMA: Sound card 8-bit DMA channel }
{ DMA16: Sound card 16-bit DMA channel (0 if not supported) }
{ Returns: }
{ TRUE: Sound card successfully initialized (Maybe) }
{ FALSE: Sound card could not be initialized }
procedure ShutdownSB;
{Removes interrupt handler and resets DSP }
procedure SetSamplingRate(Rate: Word);
{Overrides default sampling rate set with DefSamplingRate constant }
{ Parameters: }
{ Rate: New sampling rate (will be rounded by sound card) }
{This function can either be called before calling InitMixing (after }
{calling InitSB) to change the sampling rate before playback begins or }
{called during playback to change the rate dynamically. The lowest }
{sampling rate that will work is roughly 5000 HZ. The highest sampling}
{rate that will work on all sound cards is 22050 HZ. If you only want }
{to support the SB16, then you can use rates all the way up to 48000 HZ}
procedure InitMixing;
{Allocates internal buffers and starts digitized sound output }
procedure ShutdownMixing;
{Deallocates internal buffers and stops digitized sound output }
function InitXMS: boolean;
{Attempts to intialize extended memory }
{ Returns: }
{ TRUE: Extended memory successfully initialized }
{ FALSE: Extended memory could not be initialized }
function GetFreeXMS: word;
{Returns amount of free XMS memory (In kilobytes) }
procedure InitSharing;
{Allocates an EMB that all sounds are stored in. This preserves EMB }
{handles, which are a scarce resource. Call this on initialization and}
{all sounds will automatically be stored in one EMB. Call LoadSound as}
{usual to allocate a sound, but FreeSound only deallocates the sound }
{data structure. Call ShutdownSharing before program termination to }
{free allocated extended memory. }
procedure ShutdownSharing;
{Shuts down EMB sharing and frees all allocated extended memory }
function OpenSoundResourceFile(FileName: string): boolean;
{Call this to open a resource file for loading sounds. After this has }
{been called, the Key parameter in the LoadSound function is used as a }
{resource key to locate the sound data in this file. }
{ Parameters: }
{ FileName: File name of resource file }
{ Returns: }
{ TRUE: Sound resource file exists }
{ FALSE: Error, sound resource file does not exist }
procedure CloseSoundResourceFile;
{Close sound resource file. If you have called this, the Key parameter}
{will act as a filename instead of a resource key. }
function LoadSound(var Sound: PSound; Key: string): boolean;
{Allocates an extended memory block and loads a sound from a file }
{ Parameters: }
{ Sound: Unallocated pointer to sound data structure }
{ Key: If a resource file has been opened then key is a resource }
{ identifier. Use the same ID as you used for SNDLIB. }
{ If a resource file has not been opened, then key is the }
{ filename to load the sound data from. }
{ Returns: }
{ TRUE: Sound loaded sucessfully }
{ FALSE: Error loading sound }
procedure FreeSound(var Sound: PSound);
{Deallocates extended memory and destroys sound data structure }
{ Parameters: }
{ Sound: Unallocated pointer to sound data structure }
function StartSound(Sound: PSound; Index: byte; Loop: boolean): boolean;
{Starts playing a sound }
{ Parameters: }
{ Sound: Pointer to sound data structure }
{ Index: A number to keep track of the sound with (Used to stop it)}
{ Loop: Indicates whether the sound should be continuously looped }
{ Returns: }
{ TRUE: Sound was played }
{ FALSE: There were no free channels and the sound was not played }
procedure StopSound(Index: byte);
{Stops playing sound }
{ Parameters: }
{ Index: Index of sound to stop (All with given index are stopped) }
function SoundPlaying(Index: byte): boolean;
{Checks if a sound is still playing }
{ Parameters: }
{ Index: Index used when the sound was started }
{ Returns: }
{ TRUE At least oen sound with the specified index is playing }
{ FALSE No sounds with the specified index are playing }
var
IntCount : LongInt; {Number of sound interrupts that have occured }
DSPVersion : word; {Contains the version of the installed DSP chip }
AutoInit : boolean; {Tells Auto-initialized DMA transfers are in use}
SixteenBit : boolean; {Tells whether 16-bit sound output is occuring }
VoiceCount : byte; {Number of voices currently in use }
SMIXsound : boolean; {Tells whether SMIX is currently operating }
smix, smixstarted, smixmixing: boolean; {whether smix is initialized for ACK}
smixpointer: array[1..Voices] of PSound;
soundloaded: array[1..Voices] of boolean;
smixnext:byte;
implementation
uses
DOS,
XMS;
const
BufferLength = BlockLength * 2;
var
ResetPort : word;
ReadPort : word;
WritePort : word;
PollPort : word;
AckPort : word;
PICRotatePort : word;
PICMaskPort : word;
DMAMaskPort : word;
DMAClrPtrPort : word;
DMAModePort : word;
DMABaseAddrPort : word;
DMACountPort : word;
DMAPagePort : word;
IRQStartMask : byte;
IRQStopMask : byte;
IRQIntVector : byte;
DMAStartMask : byte;
DMAStopMask : byte;
DMAMode : byte;
DMALength : word;
SMIXinitialized : boolean;
OldIntVector : pointer;
OldExitProc : pointer;
HandlerInstalled : boolean;
SamplingRate : word;
procedure startupsmix;
var BaseIO: word; IRQ, DMA, DMA16: byte;
begin
smixstarted:=true;
smix:=true;
if (GetSettings(BaseIO, IRQ, DMA, DMA16)) then
if not(InitSB(BaseIO, IRQ, DMA, 0)) then
begin; smix:=false; exit; end;
InitMixing;
if not(InitXMS) then
begin; smix:=false; exit; end;
{InitSharing;}
end;
procedure shutdownsmix;
begin
if smix=false then exit;
for smixnext:=1 to 4 do
smixstop(smixnext);
ShutdownMixing;
ShutdownSB;
{Shutdownsharing;}
end;
procedure smixsetvolume;
begin
Port[ResetPort-2]:=$22;
Port[ResetPort-1]:=(v or (v SHL 4));
end;
procedure smixplay(i2:byte;s:string);
var ignore,looping:boolean;
i:byte;
begin
i:=5-i2;
case i2 of
1:begin
looping:=false;
if smixmixing then
begin
i:=smixnext; inc(smixnext); if smixnext>Voices then smixnext:=4;
end;
end;
2:looping:=true;
3:looping:=true;
4:looping:=false;
else exit;
end;
if not smixstarted then startupsmix;
if not smix then exit;
if soundloaded[i] then
begin
if soundplaying(i) then StopSound(i);
freesound(smixpointer[i]);
end;
if LoadSound(smixpointer[i],s) then
begin
ignore:=StartSound(smixpointer[i], i, looping);
soundloaded[i]:=true;
end;
end;
procedure smixstop(i2:byte);
var i,ch1,ch2:byte;
begin
if i2=1 then begin;ch1:=4;ch2:=Voices;end
else begin;ch1:=5-i2;ch2:=5-i2;end;
for i:=ch1 to ch2 do
begin
if not smix then exit;
if soundloaded[i]=true then
begin
if soundplaying(i) then StopSound(i);
freesound(smixpointer[i]);
soundloaded[i]:=false;
end;
end;
end;
function smixplaying;
begin
if i=1 then
begin
smixplaying:=0;
for i:=4 to Voices do if soundloaded[i] then if soundplaying(i) then smixplaying:=1;
end else
begin
i:=5-i;
if soundloaded[i]=false then smixplaying:=0
else if soundplaying(i) then smixplaying:=1
else smixplaying:=0;
end;
end;
function UpcaseStr(Str: string): string;
var
i: byte;
Temp: string;
begin
Temp[0] := Str[0];
for i := 1 to Length(Str) do
Temp[i] := Upcase(Str[i]);
UpcaseStr := Temp;
end;
function GetSetting(Str: string; ID: char; Hex: boolean): word;
var
Temp : string;
Num : word;
Code : integer;
begin
Temp := Str;
if Pos(ID, Temp) <> 0
then
begin
Delete(Temp, 1, Pos(ID, Temp));
Delete(Temp, Pos(' ', Temp), 255);
if Hex then Insert('$', Temp, 1);
Val(Temp, Num, Code);
if Code = 0
then GetSetting := Num
else GetSetting := $FF;
end
else
GetSetting := $FF;
end;
function GetSettings
(
var BaseIO: word;
var IRQ: byte;
var DMA: byte;
var DMA16: byte
): boolean;
var
BLASTER: string;
begin
BLASTER := UpcaseStr(GetEnv('BLASTER'));
BaseIO := GetSetting(BLASTER, 'A', true); {Hex}
IRQ := GetSetting(BLASTER, 'I', false); {Decimal}
DMA := GetSetting(BLASTER, 'D', false); {Decimal}
DMA16 := GetSetting(BLASTER, 'H', false); {Decimal}
GetSettings := true;
if BLASTER = '' then GetSettings := false;
if BaseIO = $FF then GetSettings := false;
if IRQ = $FF then GetSettings := false;
if DMA = $FF then GetSettings := false;
{We can survive if there isn't a DMA16 channel}
end;
procedure WriteDSP(Value: byte);
begin
repeat until (Port[WritePort] and $80) = 0;
Port[WritePort] := Value;
end;
function ReadDSP: byte;
begin
repeat until (Port[PollPort] and $80) <> 0;
ReadDSP := Port[ReadPort];
end;
function ResetDSP: boolean;
var
i: byte;
begin
Port[ResetPort] := 1;
{Delay(1);} {One millisecond}
Port[ResetPort] := 0;
i := 100;
while (ReadDSP <> $AA) and (i > 0) do Dec(i);
if i > 0
then ResetDSP := true
else ResetDSP := false;
end;
procedure InstallHandler; forward;
procedure UninstallHandler; forward;
procedure MixExitProc; far; forward;
function InitSB(BaseIO: word; IRQ: byte; DMA, DMA16: byte): boolean;
begin
{Sound card IO ports}
ResetPort := BaseIO + $6;
ReadPort := BaseIO + $A;
WritePort := BaseIO + $C;
PollPort := BaseIO + $E;
{Reset DSP, get version, and pick output mode}
if not(ResetDSP)
then
begin
InitSB := false;
Exit;
end;
WriteDSP($E1); {Get DSP version number}
DSPVersion := ReadDSP shl 8; DSPVersion := DSPVersion + ReadDSP;
AutoInit := DSPVersion >= $0200;
SixteenBit := (DSPVersion >= $0400) and (DMA16 <> $FF) and (DMA16 > 3);
{Compute interrupt ports and parameters}
if IRQ <= 7
then
begin
IRQIntVector := $08+IRQ;
PICMaskPort := $21;
end
else
begin
IRQIntVector := $70+IRQ-8;
PICMaskPort := $A1;
end;
IRQStopMask := 1 shl (IRQ mod 8);
IRQStartMask := not(IRQStopMask);
{Compute DMA ports and parameters}
if SixteenBit
then {Sixteen bit}
begin
DMAMaskPort := $D4;
DMAClrPtrPort := $D8;
DMAModePort := $D6;
DMABaseAddrPort := $C0 + 4*(DMA16-4);
DMACountPort := $C2 + 4*(DMA16-4);
case DMA16
of
5: DMAPagePort := $8B;
6: DMAPagePort := $89;
7: DMAPagePort := $8A;
end;
DMAStopMask := DMA16-4 + $04; {000001xx}
DMAStartMask := DMA16-4 + $00; {000000xx}
DMAMode := DMA16-4 + $58; {010110xx}
AckPort := BaseIO + $F;
end
else {Eight bit}
begin
DMAMaskPort := $0A;
DMAClrPtrPort := $0C;
DMAModePort := $0B;
DMABaseAddrPort := $00 + 2*DMA;
DMACountPort := $01 + 2*DMA;
case DMA
of
0: DMAPagePort := $87;
1: DMAPagePort := $83;
2: DMAPagePort := $81;
3: DMAPagePort := $82;
end;
DMAStopMask := DMA + $04; {000001xx}
DMAStartMask := DMA + $00; {000000xx}
if AutoInit
then DMAMode := DMA + $58 {010110xx}
else DMAMode := DMA + $48; {010010xx}
AckPort := BaseIO + $E;
end;
if AutoInit
then DMALength := BufferLength
else DMALength := BlockLength;
InstallHandler;
SMIXinitialized := true;
SMIXsound := false;
SamplingRate := DefSamplingRate;
InitSB := true;
end;
procedure ShutdownSB;
begin
if HandlerInstalled
then UninstallHandler;
ResetDSP;
end;
function InitXMS: boolean;
begin
InitXMS := true;
if not(XMSInstalled)
then InitXMS := false
else XMSInit;
end;
function GetFreeXMS: word;
begin
GetFreeXMS := XMSGetFreeMem;
end;
{Voice control}
type
PVoice = ^TVoice;
TVoice =
record
Sound: PSound;
Index: byte;
CurPos: LongInt;
Loop: boolean;
end;
var
VoiceInUse: array[0..Voices-1] of boolean;
Voice: array[0..Voices-1] of TVoice;
CurBlock: byte;
{Sound buffer}
var
SoundBlock: array[1..BlockLength+1] of ShortInt;
{The length of XMS copies under HIMEM.SYS must be a mutiple }
{of two. If the sound data ends in mid-block, it may not be }
{possible to round up without corrupting memory. Therefore, }
{the copy buffer has been extended by one byte to eliminate }
{this problem. }
{Mixing buffers}
type
PMixingBlock = ^TMixingBlock;
TMixingBlock = array[1..BlockLength] of integer;
var
MixingBlock : TMixingBlock;
{Output buffers}
type {8-bit}
POut8Block = ^TOut8Block;
TOut8Block = array[1..BlockLength] of byte;
POut8Buffer = ^TOut8Buffer;
TOut8Buffer = array[1..2] of TOut8Block;
type {16-bit}
POut16Block = ^TOut16Block;
TOut16Block = array[1..BlockLength] of integer;
POut16Buffer = ^TOut16Buffer;
TOut16Buffer = array[1..2] of TOut16Block;
var
OutMemArea : pointer;
Out8Buffer : POut8Buffer;
Out16Buffer : POut16Buffer;
var
BlockPtr : array[1..2] of pointer;
CurBlockPtr : pointer;
var
{For auto-initialized transfers (Whole buffer)}
BufferAddr : LongInt;
BufferPage : byte;
BufferOfs : word;
{For single-cycle transfers (One block at a time)}
BlockAddr : array[1..2] of LongInt;
BlockPage : array[1..2] of byte;
BlockOfs : array[1..2] of word;
{Clipping for 8-bit output}
var
Clip8 : array[-128*Voices..128*Voices] of byte;
function TimeConstant(Rate: word): byte;
begin
TimeConstant := 256 - (1000000 div Rate);
end;
procedure InitSamplingRate(Rate: word);
begin
if SixteenBit
then
begin
WriteDSP($41); {Set digitized sound output sampling rate}
WriteDSP(Hi(Rate));
WriteDSP(Lo(Rate));
end
else
begin
WriteDSP($40); {Set digitized sound time constant }
WriteDSP(TimeConstant(Rate));
end;
end;
procedure SetSamplingRate(Rate: word);
begin
SamplingRate := Rate;
if (SMIXsound)
then
begin
if SixteenBit
then
begin
InitSamplingRate(SamplingRate);
WriteDSP($D6); {Continue 16-bit DMA mode digitized sound }
end
else
begin
WriteDSP($D0); {Pause 8-bit DMA mode digitized sound }
InitSamplingRate(SamplingRate);
WriteDSP($D4); {Continue 8-bit DMA mode digitized sound }
end;
end;
end;
procedure StartDAC;
begin
Port[DMAMaskPort] := DMAStopMask;
Port[DMAClrPtrPort] := $00;
Port[DMAModePort] := DMAMode;
Port[DMABaseAddrPort] := Lo(BufferOfs);
Port[DMABaseAddrPort] := Hi(BufferOfs);
Port[DMACountPort] := Lo(DMALength-1);
Port[DMACountPort] := Hi(DMALength-1);
Port[DMAPagePort] := BufferPage;
Port[DMAMaskPort] := DMAStartMask;
InitSamplingRate(SamplingRate);
if SixteenBit
then {Sixteen bit: SB16 and up (DSP 4.xx)}
begin
WriteDSP($B6); {16-bit DMA command: D/A, Auto-Init, FIFO}
WriteDSP($10); {16-bit DMA mode: Signed Mono }
WriteDSP(Lo(BlockLength - 1));
WriteDSP(Hi(BlockLength - 1));
end
else {Eight bit}
begin
WriteDSP($D1); {Turn on speaker }
if AutoInit
then {Eight bit auto-initialized: SBPro and up (DSP 2.00+)}
begin
WriteDSP($48); {Set DSP block transfer size }
WriteDSP(Lo(BlockLength - 1));
WriteDSP(Hi(BlockLength - 1));
WriteDSP($1C); {8-bit auto-init DMA mono sound output }
end
else {Eight bit single-cycle: Sound Blaster (DSP 1.xx+)}
begin
WriteDSP($14); {8-bit single-cycle DMA sound output }
WriteDSP(Lo(BlockLength - 1));
WriteDSP(Hi(BlockLength - 1));
end;
end;
SMIXsound := true;
end;
procedure StopDAC;
begin
SMIXsound := false;
if SixteenBit
then {Sixteen bit}
begin
WriteDSP($D5); {Pause 16-bit DMA sound I/O }
end
else {Eight bit}
begin
WriteDSP($D0); {Pause 8-bit DMA mode sound I/O }
WriteDSP($D3); {Turn off speaker }
end;
Port[DMAMaskPort] := DMAStopMask;
end;
{Setup for storing all sounds in one extended memory block (Saves handles)}
var
SharedEMB : boolean;
SharedHandle : word;
SharedSize : LongInt;
procedure InitSharing;
begin
SharedEMB := true;
SharedSize := 0;
XMSAllocate(SharedHandle, SharedSize);
end;
procedure ShutdownSharing;
begin
if SharedEMB then XMSFree(SharedHandle);
SharedEMB := false;
end;
{Setup for sound resource files}
var
ResourceFile : boolean;
ResourceFilename : string;
function FExist(FileName: string): boolean;
var
f: file;
begin
Assign(f, FileName);
{$I-}
Reset(f);
{$I+}
if (IOResult = 0)
then FExist := true
else FExist := false;
end;
function OpenSoundResourceFile(FileName: string): boolean;
begin
ResourceFile := true;
ResourceFilename := FileName;
OpenSoundResourceFile := FExist(FileName);
end;
procedure CloseSoundResourceFile;
begin
ResourceFile := false;
ResourceFilename := '';
end;
type
TKey = array[1..8] of char;
var
SoundFile : file;
SoundSize : LongInt;
function MatchingKeys(a, b: TKey): boolean;
var
i: integer;
begin
MatchingKeys := true;
for i := 1 to 8 do
if a <> b
then
MatchingKeys := false;
end;
procedure GetSoundFile(Key: string);
type
Resource =
record
Key: TKey;
Start: LongInt;
Size: LongInt;
end;
var
NumSounds: integer;
ResKey: TKey;
ResHeader: Resource;
Index: integer;
i: integer;
Found: boolean;
begin
if ResourceFile
then
begin
for i := 1 to 8 do
if i <= Length(Key)
then ResKey[i] := Key[i]
else ResKey[i] := #0;
Assign(SoundFile, ResourceFilename); Reset(SoundFile, 1);
BlockRead(SoundFile, NumSounds, SizeOf(NumSounds));
Found := false;
Index := 0;
while not(Found) and (Index < NumSounds) do
begin
Index := Index + 1;
BlockRead(SoundFile, ResHeader, SizeOf(ResHeader));
if MatchingKeys(ResHeader.Key, ResKey)
then
Found := true;
end;
if Found
then
begin
Seek(SoundFile, ResHeader.Start);
SoundSize := ResHeader.Size;
end
else
SoundSize := 0;
end
else
begin
Assign(SoundFile, Key); Reset(SoundFile, 1);
SoundSize := FileSize(SoundFile);
end;
end;
function Min(a, b: LongInt): LongInt;
begin
if a < b
then Min := a
else Min := b;
end;
{Loading and freeing sounds}
function LoadSound(var Sound: PSound; Key: string): boolean;
var
Size: LongInt;
InBuffer: array[1..LoadChunkSize] of byte;
Remaining: LongInt;
MoveParams: TMoveParams;
begin
LoadSound := false;
GetSoundFile(Key);
if (SoundSize = 0)
then exit;
New(Sound);
Sound^.SoundSize := SoundSize;
if not(SharedEMB)
then
begin
Sound^.StartOfs := 0;
if not(XMSAllocate(Sound^.XMSHandle, (SoundSize + 1023) div 1024))
then exit;
end
else
begin
Sound^.StartOfs := SharedSize;
Sound^.XMSHandle := SharedHandle;
SharedSize := SharedSize + SoundSize;
if not(XMSReallocate(SharedHandle, (SharedSize + 1023) div 1024))
then exit;
end;
MoveParams.SourceHandle := 0;
MoveParams.SourceOffset := LongInt(Addr(InBuffer));
MoveParams.DestHandle := Sound^.XMSHandle;
MoveParams.DestOffset := Sound^.StartOfs;
Remaining := Sound^.SoundSize;
repeat
MoveParams.Length := Min(Remaining, LoadChunkSize);
BlockRead(SoundFile, InBuffer, MoveParams.Length);
MoveParams.Length := ((MoveParams.Length+1) div 2) * 2;
{XMS copy lengths must be a multiple of two}
XMSMove(@MoveParams);
Inc(MoveParams.DestOffset, MoveParams.Length);
Dec(Remaining, MoveParams.Length);
until not(Remaining > 0);
Close(SoundFile);
LoadSound := true;
end;
procedure FreeSound(var Sound: PSound);
begin
if not(SharedEMB) then XMSFree(Sound^.XMSHandle);
Dispose(Sound); Sound := nil;
end;
{Voice maintainance}
procedure DeallocateVoice(VoiceNum: byte);
begin
VoiceInUse[VoiceNum] := false;
with Voice[VoiceNum] do
begin
Sound := nil;
Index := 0;
CurPos := 0;
Loop := false;
end;
end;
function StartSound(Sound: PSound; Index: byte; Loop: boolean): boolean;
var
i, Slot: byte;
begin
StartSound := false; {assume that we cannot play the sound}
Slot := $FF; i := 0;
repeat
if not(VoiceInUse[i])
then Slot := i;
Inc(i);
until ((Slot <> $FF) or (i=Voices));
if Slot <> $FF
then
begin
Inc(VoiceCount);
Voice[Slot].Sound := Sound;
Voice[Slot].Index := Index;
Voice[Slot].CurPos := 0;
Voice[Slot].Loop := Loop;
VoiceInUse[Slot] := true;
StartSound := true; {success}
end;
end;
procedure StopSound(Index: byte);
var
i: byte;
begin
for i := 0 to Voices-1 do
if (Voice[i].Sound <> nil) and (Voice[i].Index = Index)
then
begin
DeallocateVoice(i);
Dec(VoiceCount);
end;
end;
function SoundPlaying(Index: byte): boolean;
var
i: byte;
begin
SoundPlaying := False;
for i := 0 to Voices-1 do
if (Voice[i].Sound <> nil) and (Voice[i].Index = Index)
then SoundPlaying := True;
end;
procedure UpdateVoices;
var
VoiceNum: byte;
begin
for VoiceNum := 0 to Voices-1 do
begin
if VoiceInUse[VoiceNum]
then
if Voice[VoiceNum].CurPos >= Voice[VoiceNum].Sound^.SoundSize
then
begin
DeallocateVoice(VoiceNum);
Dec(VoiceCount);
end;
end;
end;
{Utility functions}
procedure SetCurBlock(BlockNum: byte);
begin
CurBlock := BlockNum;
CurBlockPtr := pointer(BlockPtr[BlockNum]);
end;
procedure ToggleBlock;
begin
if CurBlock = 1
then SetCurBlock(2)
else SetCurBlock(1);
end;
procedure SilenceBlock;
begin
FillChar(MixingBlock, BlockLength*2, 0); {FillChar uses REP STOSW}
end;
function GetLinearAddr(Ptr: pointer): LongInt;
begin
GetLinearAddr := LongInt(Seg(Ptr^))*16 + LongInt(Ofs(Ptr^));
end;
function NormalizePtr(p: pointer): pointer;
var
LinearAddr: LongInt;
begin
LinearAddr := GetLinearAddr(p);
NormalizePtr := Ptr(LinearAddr div 16, LinearAddr mod 16);
end;
procedure InitClip8;
var
i, Value: integer;
begin
for i := -128*Voices to 128*Voices do
begin
Value := i;
if (Value < -128) then Value := -128;
if (Value > +127) then Value := +127;
Clip8[i] := Value + 128;
end;
end;