forked from MobyGamer/XDC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSOUNDLIB.PAS
More file actions
2656 lines (2466 loc) · 68.7 KB
/
Copy pathSOUNDLIB.PAS
File metadata and controls
2656 lines (2466 loc) · 68.7 KB
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
{$IFNDEF VER60}
{$A+,B+,F-,G-,I-,O+,P+,Q-,R-,S-,T-,V-,X+,Y-}
{$ELSE}
{ Uh.... not enough money for BP7? }
{$A+,B+,F-,G-,I-,O+,R-,S-,V-,X+}
{$ENDIF}
(****************************************************************************
* Author : Stefan Goehler, Germany *
* Version : 1.2c *
* Task : Play and record with soundblaster and compatibles *
* You can use my units entirely in your own programs. Ripping any code off *
* is not allowed without my permission. If you wanna use this unit in *
* commercial products, I mean if you wanna get ANY money or other for *
* software based on one of my units, you'll have to contact me - we'll *
* find a way. *
******* *
* If there are any bugs, please email me! *
* If you don't know where the bug is, tell me what happened and also give *
* me your machine configuration. BTW, I am not responsible for any problems*
* with non- Creative soundcards! Even if there is written, the card is 100%*
* SB-compatible. If you have a problem on a non-SB, then you see that it *
* is not 100% compatible! Please don't bother me with such things then! *
* *
* my homepage: http://www.crossfire-designs.de *
* ^^^note that you can get there always the actual version of this unit *
* if you have additions, tips or sth. else, mail to stefan.goehler@gmx.de *
****** KNOWN PROBLEMS ******************************************************
* . The program is not able to play any compressed files. *
* . Soundlib will only play the first chunk of multi-chunked files *
****** HISTORY *************************************************************
* History *
* Version 1.0 : .first public available Version *
* (4th Sep. 1997) *
* Version 1.01: *fixed a bug with multifiles- LASTONE wasn't setted back *
* and the sb played not without reseta 16bit-file after a *
* 8bit file correctly *
* +added some additional in the readme *
* (6th Sep. 1997) *
* Version 1.1 -removed needless things *
* +added surround sound *
* *fixed a bug when playback runs, that you can't open a file*
* +added function getactpos- gives back the fileremain minus *
* the already played sounddata - very accurate *
* -removed LASTONE and used a better method *
* *fixed bug that the file won't play to the full end *
* (9th Sep. 1997) *
* Version 1.11 *fixed a bug which won't set the speaker on with older *
* version of soundblasters *
* *soundlib is now runable under TP6 without any changes *
* Version 1.12 *changed 'pitch' to 'treble' *
* *fixed a little bug that causes terrible sounds with some *
* files smaller than the doublebuffer *
* *changed line to linein because of conflicts with linedraw *
* Version 1.13 *changed wave-loader in that way it can also read wav-files*
* with additional chunks (like program, copyright...etc) *
* (31st March 1998) *
* Version 1.14 +new procedure getblasterstring - get name of the SB *
* *procedure errorproc is a variable - so if you have a gui *
* you are able to make a workaround for the textmode *
* error-messages *
* *voc-loader should now load nearly all vocs *
* +Protected mode support *
* *improved error-checking in procedure initblaster *
* *downsample-handling improved *
* *removed some detail bugs *
* (18th July 1998) *
* Version 1.15 +SB Pro 2.0 support added *
* *fixed small bug in dmagetpos *
* +made setagc public (why did I forget that???) *
* *fixed bug that the player doesn't play the last block *
* (9th November 1998) *
* Version 1.2 *fixed bug which caused nasty clicking, if wave file *
* contained additional data at the end *
* *fixed problem concerning the SBLive! SB16 emulator, which *
* caused the detection routine to say the soundcard is busy *
* *far better sounding stereo to mono downsampling *
* *SB Live! detection included (I hope only this one has *
* DSP version $40D), plus its support for 48 Khz *
* Version 1.2a *fixed playback problems under W2k sound emulators *
* Version 1.2b *8088 fixes, interrupt speedups by Trixter *
* Version 1.2c *Investigated early return from interrupts *
* Version 1.2d *String and other size optimizations *
****** FUTURE EXTENSIONS ***************************************************
* .ADPCM decoder *
* .variable samplerate converter *
****************************************************************************)
{Bugs:
- Probleme bei kleinen Files und downsampling
- bei downsamping mit SBpro ab 2. Block nur halbe samplingrate}
{$G-}
{{$DEFINE REENTRANT}
{
Define this if you need interrupts enabled while inside the interrupt
handler. This would be the case if your user-land interrupt handler code
(callproc) takes too long and/or you're on a slow 808x machine. Generally,
don't enable this unless you know what you're doing.
}
{{$DEFINE LONGINTR}
{
If defined, it is assumed that the interrupt handler will take longer
than normal. This flag will enable interrupts as soon as possible so
as to not bork other interrupts on the system. However, this means it
is remotely possible that we will get called before we're done running;
in that case, we need to enable the re-entrancy code, shown below:
}
{$IFDEF LONGINTR}
{$DEFINE REENTRANT}
{$ENDIF}
UNIT soundlib;
INTERFACE
USES
{$IFNDEF DPMI}
dos;
{$ELSE}
dos, winapi;
{$ENDIF}
TYPE
{$IFDEF DPMI}
tmemp = LONGINT;
{$ELSE}
tmemp = POINTER;
{$ENDIF}
tplay = RECORD
hispeed : BOOLEAN;{ SbPro internal usage }
blocksize : LONGINT;{ half size of the getted buffermemory }
p : tmemp; { pointer to the sounddata buffermemory }
activeblock : BYTE; { active bufferblock (because of doublebuffering) }
callproc : PROCEDURE;{ the procedure to call with the interrupt }
stopped : BOOLEAN; { the playback is stopped }
paused : BOOLEAN; { playback is paused }
data : ARRAY [0..19] OF
RECORD
frequency : LONGINT;{ samplingrate }
stereo : BOOLEAN;{ file is stereo }
sound16bit : BOOLEAN;{ file has 16 bit data }
format : WORD; { see format constants }
blockalign : WORD; { How we can seek in the file }
signed : BOOLEAN;{ sounddata is signed }
fileremain : LONGINT;{ remain of the file to play }
fdatasize : LONGINT;{ size of the real data in the file }
fsize : LONGINT;{ size of the file to play }
fdatastart : LONGINT;{ startposition of the sounddata in the file }
f : FILE; { file to read/write }
fclosed : BOOLEAN;{ shows if the file is open or not }
END;
END;
trec = RECORD
hispeed : BOOLEAN; { SbPro internal usage }
blocksize : LONGINT;{ half size of the getted buffermemory }
p : tmemp; { pointer to the sounddata buffermemory }
activeblock : BYTE; { active bufferblock (doublebuffering) }
callproc : PROCEDURE;{the procedure to call with the interrupt }
stopped : BOOLEAN;{ the recording is stopped }
paused : BOOLEAN;{ recording is paused }
frequency : LONGINT;{ samplingrate }
stereo : BOOLEAN;{ is recorded in stereo }
sound16bit : BOOLEAN;{ is recorded in 16 bit }
format : WORD; { see format constants }
signed : BOOLEAN;{ is sounddata signed }
fileremain : LONGINT;{ remain of the file to play }
fdatasize : LONGINT;
fsize : LONGINT;{ size of the file to play }
fdatastart : LONGINT;{ startposition of the sounddata in the file }
f : FILE; { file to read/write }
fclosed : BOOLEAN;{ shows if the file is open or not }
END;
CONST
sblive = $40D;
sb16 = $400;
sbpro20 = $301;
sbpro = $300;
sb21 = $210;
sb20 = $200;
sb10 = $100;
actfile : BYTE = 0;{ the momently used file }
{ Constants for the dma-port-addresses }
dma_lpage : ARRAY [0..7] OF
BYTE = ($87, $83, $81, $82, $88, $8B, $89, $8A);
dma_hpage : ARRAY [0..7] OF
WORD = ($487, $483, $481, $482, $00, $48B, $489, $48A);
dma_adr : ARRAY [0..7] OF
BYTE = ($00, $02, $04, $06, $C0, $C4, $C8, $CC);
dma_wc : ARRAY [0..7] OF
BYTE = ($01, $03, $05, $07, $C2, $C6, $CA, $CE);
snd_mono = 0;
snd_8bit = 0;
snd_nonsigned = 0;
snd_stereo = 1;
snd_16bit = 2;
snd_signed = 4;
off = FALSE;
on = TRUE;
{ Constants for volume settings }
master = 1;
voice = 2;
midi = 3;
cd = 4;
linein = 5;
mike = 6;
speaker = 7;
treble = 8;
bass = 9;
snd_left = 1;
snd_right = 2;
{ Constants for WAVe format types }
fmt_unknown = $0000;
fmt_pcm = $0001;
fmt_msadpcm = $0002;
fmt_imaadpcm = $0011;
buffersize : WORD = 16104; { Largest legal buffer value }
autorestore : BOOLEAN = TRUE;{ automatic restore mixersettings }
surround : BOOLEAN = FALSE;
sb_errorstr : ARRAY [0..8] OF
STRING[32] = ('No error',
'No soundcard found',
'Soundard not supported',
'Soundcard busy',
'File not found',
'Compressed files not supported',
'Format not supported',
'File handle not valid',
'Soundcard INT not responding');
sb_noerror = 0;
sb_nocardfound = 1;
sb_notsupported = 2;
sb_busy = 3;
sb_filenotfound = 4;
sb_nocompressed = 5;
sb_falseformat = 6;
sb_nohandles = 7;
sb_nointrespond = 8;
soundlib_vstr = 'SoundLib 1.2b';
soundlib_vnum = $012B;
VAR
oldexitproc : POINTER;
blaster : RECORD
irq : BYTE;
lodma, hidma : WORD;
address : WORD;
aweport : WORD; { not really used by unit }
version : WORD;
available : BOOLEAN;
{following will be filled with the maximum playback values}
minfreq : LONGINT;
monofreq,
stereofreq : LONGINT;
sound16bit : BOOLEAN;
stereo : BOOLEAN;
END;
play : tplay;
rec : trec;
dummy : WORD;
oldint : POINTER;
irqmsk, irqvec : BYTE;
dsbuf : POINTER; { Buffer for downsampling }
FUNCTION initblaster : BYTE;{call it _before_ any usage of the blaster}
FUNCTION getblasterstring : STRING;
PROCEDURE initrecord (p : tmemp;
buffersize, frequency : WORD;
flags : WORD);
PROCEDURE initplay (datanum : BYTE;
p : tmemp;
buffersize, frequency : WORD;
flags : WORD);
PROCEDURE exitplay;{stop playback - use it only if you haven't used open****}
PROCEDURE exitrecord;
PROCEDURE pauserecord (pause : BOOLEAN);
PROCEDURE pauseplay (pause : BOOLEAN);
PROCEDURE startplay (datanum : BYTE);{start playback if you used open****}
PROCEDURE stopplay; {stop playback if you used open****}
FUNCTION getactpos : LONGINT;
PROCEDURE silence (VAR p;
count : WORD);
PROCEDURE setplaypos (datanum : BYTE;
POS : LONGINT);{set fileposition of playback file - only if you used open****}
FUNCTION setagc (on : BOOLEAN) : BOOLEAN;
FUNCTION setvolume (what, left, right : BYTE) : BOOLEAN;
FUNCTION getvolume (what : BYTE;
VAR left, right : BYTE) : BOOLEAN;
FUNCTION setoutput (output : BYTE;
left, right : BOOLEAN) : BOOLEAN;
FUNCTION setinput (input, channel : BYTE;
left, right : BOOLEAN) : BOOLEAN;
FUNCTION getinput (input, channel : BYTE;
VAR left, right : BOOLEAN) : BOOLEAN;
FUNCTION setgain (out : BOOLEAN;
left, right : BYTE) : BOOLEAN;
FUNCTION getgain (out : BOOLEAN;
VAR left, right : BYTE) : BOOLEAN;
FUNCTION setmemsize (size : WORD) : BOOLEAN;
FUNCTION openvoc (datanum : BYTE;
filename : STRING) : BYTE;
FUNCTION openwav (datanum : BYTE;
filename : STRING) : BYTE;
FUNCTION opensound (datanum : BYTE;
s : STRING) : BYTE;{open wave or vocfile -the program detects that with file extension}
PROCEDURE closesound (datanum : BYTE);{close playback file and stop playback}
PROCEDURE savemixer;
PROCEDURE restoremixer;
PROCEDURE error (num : BYTE);
VAR
detectblaster : FUNCTION : BOOLEAN;
CONST
errorproc : PROCEDURE (num : BYTE) = error;
{Change this if you have i.e. a GUI}
IMPLEMENTATION
FUNCTION readmixer (reg : BYTE) : BYTE;
FORWARD;
PROCEDURE writemixer (reg, VAL : BYTE);
FORWARD;
PROCEDURE error (num : BYTE);
BEGIN
ASM
mov ax, 3;
INT 10h END;
WRITELN ('Error: ', sb_errorstr [num]);
HALT;
END;
FUNCTION getblasterstring : STRING;
BEGIN
WITH blaster DO
BEGIN
IF version >= sblive THEN
getblasterstring := 'Soundblaster Live!'
ELSE
IF version >= sb16 THEN
BEGIN
IF aweport = 0 THEN
getblasterstring := 'Soundblaster 16'
ELSE
getblasterstring := 'Soundblaster AWE32/64'
END
ELSE
IF version >= sbpro20 THEN
getblasterstring := 'Soundblaster Pro 2.0'
ELSE
IF version >= sbpro THEN
getblasterstring := 'Soundblaster Pro'
ELSE
IF version >= sb21 THEN
getblasterstring := 'Soundblaster 2.1'
ELSE
IF version >= sb20 THEN
getblasterstring := 'Soundblaster 2.0';
END;
END;
{procedure writedsp(v : byte);assembler;
asm
mov dx,blaster.address
add dx,0Ch
xor cx,cx
@lp:
in al,dx
dec cx
jz @err
cmp al,127
ja @lp
jmp @noerr
@err:
push sb_busy
call errorproc
@noerr:
mov al,v
out dx,al
end;}
FUNCTION writedsp (v : BYTE) : BOOLEAN;
assembler;
ASM
mov dx, blaster.address
add dx, 0Ch
XOR cx, cx
@lp :
IN al, dx
DEC cx
jz @err
test al, $80
jnz @lp
jmp @noerr
@err :
mov al, 0
jmp @END
@noerr :
mov al, v
out dx, al
mov al, 1
@END :
END;
FUNCTION readdsp : BYTE;
assembler;
ASM
mov dx, blaster.address
add dx, 0eh
XOR cx, cx
@lp :
IN al, dx
DEC cx
jz @err
test al, $80
jz @lp
jmp @noerr
@err :
mov al, 0
jmp @END
@noerr :
mov dx, blaster.address
add dx, 0Ah
IN al, dx
@END :
END;
VAR
endtime : LONGINT;
FUNCTION currenttime : LONGINT;
assembler;
ASM
{$IFDEF VER60}
mov ax, $0040;
mov es, ax
{$ELSE}
mov es, [seg0040]
{$ENDIF}
mov ax, es : [6Ch]
mov dx, es : [6eh]
END;
{function resetblaster : boolean;
var
b : byte;
begin
port[blaster.address+$6] := 1;
endtime := currenttime+3;
while endtime > currenttime do;
port[blaster.address+$6] := 0;
endtime := currenttime+2;
repeat
b := port[blaster.address+$E];
b := port[blaster.address+$A];
until (b = $AA)or(currenttime >= endtime);
resetblaster := b = $AA;
end;
procedure resetblaster_fast;
begin
port[blaster.address+$6] := 1;
port[blaster.address+$6] := 0;
end;}
FUNCTION resetblaster : BOOLEAN;
VAR
b : BYTE;
BEGIN
port [blaster.address + $6] := 1;
endtime := currenttime + 2;
WHILE (endtime > currenttime) DO
;
port [blaster.address + $6] := 0;
b := readdsp;
{ endtime := currenttime+3;
repeat
b := inp(blaster.address+$E);
b := inp(blaster.address+$A);
until (b = $AA)or(currenttime >= endtime);}
resetblaster := b = $AA;
END;
PROCEDURE setspeed (hz : BYTE);
assembler;
ASM
mov ax, 40h
push ax
call writedsp
mov al, hz
DEC al
push ax
call writedsp
END;
PROCEDURE setspeedSB16play (hz : WORD);
assembler;
ASM
mov ax, 42h
push ax
call writedsp
mov ax, hz
mov al, Ah
XOR Ah, Ah
push ax
call writedsp
mov ax, hz
XOR Ah, Ah
push ax
call writedsp
END;
PROCEDURE setspeedSB16record (hz : WORD);
assembler;
ASM
mov ax, 41h
push ax
call writedsp
mov ax, hz
mov al, Ah
XOR Ah, Ah
push ax
call writedsp
mov ax, hz
XOR Ah, Ah
push ax
call writedsp
END;
PROCEDURE setsamplingrate (hz : WORD);
BEGIN
IF blaster.version >= sb16 THEN
setspeedSB16play (hz)
ELSE
BEGIN
IF blaster.version >= sbpro THEN
setspeed (256 - (1000000 DIV (LONGINT (hz) * SUCC (BYTE (play.data [actfile] .stereo) ) ) ) )
ELSE
setspeed (256 - (1000000 DIV (LONGINT (hz) ) ) );
IF hz < 36000 THEN
BEGIN {Lowpass filter for better sound quality}
writemixer ($0E, readmixer ($0E) AND NOT 32);
IF hz < 18000 THEN
writemixer ($0C, readmixer ($0C) AND NOT 8) {3,2 khz}
ELSE
writemixer ($0C, readmixer ($0C) OR 8); {8,8 khz-filter}
END
ELSE
writemixer ($0E, readmixer ($0E) OR 32);
END;
END;
PROCEDURE setsamplingraterec (hz : WORD);
BEGIN
IF blaster.version >= sb16 THEN
setspeedSB16record (hz)
ELSE
setspeed (256 - (1000000 DIV (LONGINT (hz) * SUCC (BYTE (rec.stereo) ) ) ) );
END;
PROCEDURE pauserecord (pause : BOOLEAN);
{Pause the recording - if you play at the same resolution
(8 or 16 bit) as the recording, this procedure will pause
both recording and playing}
BEGIN
IF pause THEN
BEGIN
IF NOT rec.paused THEN
IF (rec.sound16bit) AND (blaster.version >= sb16) THEN
writedsp ($D5)
ELSE
writedsp ($D0);
END
ELSE
BEGIN
IF rec.paused THEN
IF (rec.sound16bit) AND (blaster.version >= sb16) THEN
writedsp ($D6)
ELSE
writedsp ($D4);
END;
rec.paused := pause;
END;
PROCEDURE pauseplay (pause : BOOLEAN);
{Pause the playback - if you record at the same resolution
(8 or 16 bit) as the playing, this procedure will pause
both recording and playing}
BEGIN
IF pause THEN
BEGIN
IF NOT play.paused THEN
IF (play.data [actfile] .sound16bit) AND (blaster.version >= sb16) THEN
writedsp ($D5)
ELSE
writedsp ($D0);
END
ELSE
BEGIN
IF play.paused THEN
IF (play.data [actfile] .sound16bit) AND (blaster.version >= sb16) THEN
writedsp ($D6)
ELSE
writedsp ($D4);
END;
play.paused := pause;
END;
VAR
settings1 : ARRAY [1..9] OF
RECORD
l, r : BYTE;
END;
settings2 : ARRAY [1..8] OF
BYTE;
PROCEDURE savemixer;
BEGIN
FOR dummy := 1 TO 9 DO
getvolume (dummy, settings1 [dummy] .l, settings1 [dummy] .r);
IF blaster.version >= sb16 THEN
FOR dummy := 1 TO 8 DO
settings2 [dummy] := readmixer ($3B + dummy)
ELSE
IF blaster.version >= sbpro THEN
BEGIN
settings2 [1] := readmixer ($0C);
settings2 [2] := readmixer ($0E);
END;
END;
PROCEDURE restoremixer;
BEGIN
FOR dummy := 1 TO 9 DO
setvolume (dummy, settings1 [dummy] .l, settings1 [dummy] .r);
IF blaster.version >= sb16 THEN
FOR dummy := 1 TO 8 DO
writemixer ($3B + dummy, settings2 [dummy])
ELSE
IF blaster.version >= sbpro THEN
BEGIN
writemixer ($0C, settings2 [1]);
writemixer ($0E, settings2 [2]);
END;
END;
PROCEDURE exitblaster;
far;
BEGIN
exitproc := oldexitproc;
IF blaster.available THEN
BEGIN
IF NOT play.data [actfile] .fclosed THEN
closesound (actfile);
IF NOT rec.stopped THEN
exitrecord;
SETINTVEC (irqvec + (blaster.irq AND 7), oldint);
IF blaster.irq < 8 THEN
port [$21] := port [$21] OR irqmsk
ELSE
port [$A1] := port [$A1] OR irqmsk;
writedsp ($D3);
IF blaster.irq < 8 THEN
port [$20] := $20
ELSE
port [$A0] := $20;
IF autorestore THEN
restoremixer;
{$IFNDEF DPMI}
FREEMEM (play.p, buffersize);
{$ELSE}
globaldosfree (play.p);
{$ENDIF}
FREEMEM (dsbuf, buffersize * 4);
END;
END;
PROCEDURE initdma (dma : BYTE;
p : tmemp;
size : WORD;
get, autoinit : BOOLEAN);
VAR
b : BYTE;
l : LONGINT;
segm, offs : WORD;
BEGIN
{$IFNDEF DPMI}
l := LONGINT (SEG (p^) ) SHL 4 + OFS (p^);
{$ELSE}
p := p AND $FFFF0000;
l := ( (p AND $FFFF0000) SHR 16) SHL 4 + p AND $FFFF;
{$ENDIF}
segm := l SHR 16;
IF dma > 3 THEN
offs := WORD ( (LONGINT (p) AND $FFFF0000) SHR 13 + (LONGINT (p) AND $FFFF) SHR 1)
ELSE
offs := l AND $FFFF;
IF get THEN
b := $44
ELSE
b := $48;
IF autoinit THEN
INC (b, $10);
IF dma > 3 THEN
BEGIN
port [$D4] := dma;
port [$D6] := b + dma - 4;
port [$D8] := 0;
END
ELSE
BEGIN
port [$0A] := dma + 4;
port [$0B] := b + dma;
port [$0C] := 0;
END;
port [dma_adr [dma] ] := LO (offs);
port [dma_adr [dma] ] := HI (offs);
port [dma_lpage [dma] ] := segm;
port [dma_wc [dma] ] := LO (size - 1);
port [dma_wc [dma] ] := HI (size - 1);
IF dma > 3 THEN
port [$D4] := dma - 4
ELSE
port [$0A] := dma;
END;
FUNCTION dmagetpos (dma : BYTE) : WORD;
VAR
w : WORD;
BEGIN
IF dma > 3 THEN
port [$D8] := 0
ELSE
port [$0C] := 0;
w := port [dma_wc [dma] ];
w := WORD (port [dma_wc [dma] ]) SHL 8 + w;
INC (w);
IF dma > 3 THEN
w := w SHL 1;
dmagetpos := w;
END;
FUNCTION getactpos : LONGINT;
VAR
dma : BYTE;
l, l2 : LONGINT;
BEGIN
IF (blaster.version >= sb16) AND (play.data [actfile] .sound16bit) THEN
dma := blaster.hidma
ELSE
dma := blaster.lodma;
l := (play.data [actfile] .fsize - play.data [actfile] .fileremain) +
(buffersize - dmagetpos (dma) ) - PRED (play.activeblock) * play.blocksize - buffersize;
l2 := play.data [actfile] .fsize - play.data [actfile] .fdatastart;
IF l > l2 THEN
l := l2;
getactpos := l;
END;
PROCEDURE playsb (p : tmemp;
dsize : WORD);
BEGIN
initdma (blaster.lodma, p, dsize, FALSE, FALSE);
writedsp ($14);
writedsp (LO (dsize - 1) );
writedsp (HI (dsize - 1) );
END;
PROCEDURE initrecord (p : tmemp;
buffersize, frequency : WORD;
flags : WORD);
VAR
size, dma : WORD;
BEGIN
IF NOT blaster.available THEN
EXIT;
rec.sound16bit := flags AND snd_16bit <> 0;
rec.stereo := flags AND snd_stereo <> 0;
rec.blocksize := buffersize SHR 1;
rec.frequency := frequency;
rec.p := p;
rec.activeblock := 2;{Block where the last data has been written to}
rec.stopped := FALSE;
rec.hispeed := FALSE;
size := play.blocksize;
setsamplingraterec (frequency);
IF blaster.version >= sb16 THEN
BEGIN
size := rec.blocksize;
IF rec.sound16bit THEN
BEGIN
size := size SHR 1;
dma := blaster.hidma;
END
ELSE
dma := blaster.lodma;
initdma (dma, rec.p, size SHL 1, TRUE, TRUE);
IF rec.sound16bit THEN
BEGIN
writedsp ($BE);
IF rec.stereo THEN
writedsp ($30)
ELSE
writedsp ($10);{signed!!!}
END
ELSE
BEGIN
writedsp ($CE);
IF rec.stereo THEN
writedsp ($20)
ELSE
writedsp ($00);
END;
writedsp (LO (size - 1) );
writedsp (HI (size - 1) );
END
ELSE
IF
( (blaster.version >= sb21) AND ( (rec.frequency > 13000) ) OR
( (blaster.version >= sbpro) AND ( (rec.frequency > 23000) OR (rec.stereo) ) ) ) THEN
BEGIN
{sb21 or higher and sr > 13000 or sbpro and sr > 23000}
writedsp ($D3);
rec.hispeed := TRUE;
IF (rec.stereo) AND (blaster.version >= sbpro) THEN{sbpro and stereo and sr >= 11025 and sr <= 22050}
writedsp ($A8);
initdma (blaster.lodma, rec.p, size SHL 1, TRUE, TRUE);
setsamplingrate (rec.frequency);
writedsp ($48);
writedsp (LO (size - 1) );
writedsp (HI (size - 1) );
writedsp ($98);
END
ELSE
BEGIN {sb20 or higher, mono and sr <=13000}
writedsp ($D3);
initdma (blaster.lodma, rec.p, size SHL 1, TRUE, TRUE);
setsamplingrate (rec.frequency);
writedsp ($48);
writedsp (LO (size - 1) );
writedsp (HI (size - 1) );
writedsp ($2C);
END;
END;
PROCEDURE downsample8 (p1, p2 : POINTER;
adder, size : WORD);
far;
assembler;
ASM
mov cx, size
OR cx, cx
jz @END
push ds
les di, p1
lds si, p2
mov bx, adder
@lp :
mov ax, es : [di]
mov ds : [si], ax
add di, bx
add si, 2
DEC cx
jnz @lp
pop ds
@END :
END;
PROCEDURE downsample8m (p1, p2 : POINTER;
adder, size : WORD);
far;
assembler;
ASM
mov cx, size
SHL cx, 1
jz @END
push ds
les di, p1
lds si, p2
mov bx, adder
SHR bx, 1
XOR dh, dh
@lp :
mov ax, es : [di]
mov dl, Ah
XOR Ah, Ah
add ax, dx
SHR ax, 1
mov ds : [si], al
add di, bx
INC si
DEC cx
jnz @lp
pop ds
@END :
END;
PROCEDURE downsample16 (p1, p2 : POINTER;
adder, size : WORD);
far;
assembler;
ASM
mov cx, size
OR cx, cx
jz @END
push ds
mov bx, adder
les di, p1
lds si, p2
@lp :
mov al, es : [di + 1]
mov Ah, es : [di + 3]
add al, $80
add Ah, $80
mov ds : [si], ax
add di, bx
add si, 2
DEC cx
jnz @lp
pop ds
@END :
END;
PROCEDURE downsample16m (p1, p2 : POINTER;
adder, size : WORD);
far;
assembler;
ASM
mov cx, size
SHL cx, 1
jz @END
push ds
mov bx, adder
SHR bx, 1
les di, p1
lds si, p2
@lp :
mov Ah, es : [di + 1]
mov dh, es : [di + 3]
sar ax, 1
sar ax, 1
sar ax, 1
sar ax, 1
sar ax, 1
sar ax, 1
sar ax, 1
sar ax, 1
sar dx, 1
sar dx, 1
sar dx, 1
sar dx, 1
sar dx, 1
sar dx, 1
sar dx, 1
sar dx, 1
add ax, dx
sar ax, 1
add al, $80
mov ds : [si], al
add di, bx