-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewsoundunit.pas
2078 lines (1737 loc) · 55.1 KB
/
newsoundunit.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
unit newsoundunit;
{.$DEFINE nosound}
interface
uses
MMSystem,
windows,
math,
sysutils,
//DSUtil,
DirectSound,
d3dx9,
typestuff,
MADXDllinterface,
DXErr9,
ActiveX;
const
DefSmpRt= 16000;
vDefsmprt:integer=Defsmprt;
MAX_PLAYING_BUFFERS=15;
MAX_BUFFER_AGE=15000;
type
TCombinedSoundBuffer = record
DSBuf,effbuf:IDirectSoundBuffer8;
DS3D,eff3d:IDirectSound3DBuffer8;
FX:IDirectSoundFXI3DL2Reverb;
FXdesc:DSEFFECTDESC;
FXhibaszar:cardinal;
id:integer;
typ:integer;
pos:TD3DXVector3;
tav:single;
state:byte;
played:cardinal;
epmd:single;
hangero:single;
end;
T3DSoundStream = record
DSbuf:IDirectSoundBuffer;
DS3D:IDirectSound3DBuffer8;
id:integer;
played:cardinal;
playing:boolean;
bufferbytes:cardinal;
lastwrite,lastpos:cardinal;
buffered:Tsmallintdynarr;
end;
TMemoryBuffer = record
format:TWaveFormatex;
caps:DSBCaps;
mindis:single;
data:pointer;
freq:cardinal;
end;
TDSCapture = class(Tobject)
protected
DScapture:IDirectSoundCapture;
buf:IDirectSoundCaptureBuffer;
public
captured:Tsmallintdynarr;
lastread:cardinal;
constructor Create;
destructor destroy;override;
procedure start;
procedure update;
procedure stop;
end;
//-----------------------------------------------------------------------------
// Name: class CWaveFile
// Desc: Encapsulates reading or writing sound data to or from a wave file
//-----------------------------------------------------------------------------
CWaveFile = class
public
m_pwfx: PWaveFormatEx; // Pointer to WAVEFORMATEX structure
m_hmmio: HMMIO; // MM I/O handle for the WAVE
m_ck: MMCKINFO; // Multimedia RIFF chunk
m_ckRiff: MMCKINFO; // Use in opening a WAVE file
m_dwSize: DWORD; // The size of the wave file
m_mmioinfoOut: MMIOINFO;
m_dwFlags: DWORD;
m_bIsReadingFromMemory: BOOL;
m_pbData: PByte;
m_pbDataCur: PByte;
m_ulDataSize: Cardinal;
m_pResourceBuffer: PChar;
protected
function ReadMMIO: HRESULT;
function WriteMMIO(pwfxDest: PWaveFormatEx): HRESULT;
public
constructor Create;
destructor Destroy; override;
function Open(strFileName: PChar; pwfx: PWaveFormatEx; dwFlags: DWORD): HRESULT;
function OpenFromMemory(pbData: PByte; ulDataSize: Cardinal; pwfx: PWaveFormatEx; dwFlags: DWORD): HRESULT;
function Close: HRESULT;
function Read(pBuffer: PByte; dwSizeToRead: DWORD; pdwSizeRead: PDWORD): HRESULT;
function Write(nSizeToWrite: LongWord; pbSrcData: PByte; out pnSizeWrote: LongWord): HRESULT;
function GetSize: DWORD;
function ResetFile: HRESULT;
property GetFormat: PWaveFormatEx read m_pwfx;
end;
function InitSound(hwindow:HWND):Hresult;
procedure PlaySound(mit:integer;loop:boolean;aid:integer;effects:boolean;hol:TD3DXVector3);
//1,1, ha nem érdekes... Vec3Zero ha nem érdekes
procedure SetSoundProperties(mit:integer; aid:integer;vol:longint;freq:single;effects:boolean;hol:TD3DXVector3);
procedure SetSoundVelocity(mit:integer; aid:integer;vel:TD3DXVector3);
procedure WriteToStreamBuffered(aid:integer;const mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0);
function WriteToStream(aid:integer;hely:TD3DXVector3;const mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0;channels:integer = 0):boolean;
procedure WriteToStreamSmallAmounts(aid:integer;var mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0;channels:integer = 0);
function StopStream(aid:integer):boolean;
procedure LoadStrm(fnev:string);
procedure PlayStrm(mit:integer;aid:integer;vol:integer=0;onlycreate:boolean=false);
procedure StopSound(mit:integer; aid:integer);
procedure LoadSound(fnev:string;haromd,freq,effects:boolean;mindistance:single);
procedure PlaceListener(vec:TD3DXvector3;szogx,szogy:single);
procedure StopAll;
procedure SetMainVolume(vol:single);
procedure CommitDeferredSoundStuff;
procedure CloseSound;
procedure zeneinit;
procedure zenefresh(volp:single);
procedure zenecleanup;
const
unitname='sound';
wfx:TWaveformatex=
(wFormatTag: WAVE_FORMAT_PCM; { format type }
nChannels: 1; { number of channels (i.e. mono, stereo, etc.) }
nSamplesPerSec: Defsmprt; { sample rate }
nAvgBytesPerSec: Defsmprt*2; { for buffer estimation }
nBlockAlign: 2; { block size of data }
wBitsPerSample: 16; { number of bits per sample of mono data }
cbSize: sizeof(TWaveformatex));
WAVEFILE_READ = 1;
WAVEFILE_WRITE = 2;
var
tempfreq:cardinal;
mp3filelist:array of string;
mp3strms:array [0..2] of array of string;
mp3strmp:array [0..2] of integer;
mp3strmp2:integer;
mp3strmpvalts:boolean;
mp3action,mp3ambient,mp3car:boolean;
mp3stationname:string;
mp3menu:string;
mp3pos:integer;
lastsoundaction:string;
bufPlaying:array of TCombinedSoundBuffer;
// bufplayingcount,playsoundcount,stopsoundcount,specialcreatecount:integer;
streams:array of T3DSoundStream;
zenebuffer:Tsmallintdynarr;
mainvolume:integer;
implementation
{const
testpreset:TDSFXWavesReverb=
( fInGain:0;
fReverbMix:-5;
fReverbTime:2000;
fHighFreqRTRatio: 0); }
const
BUFFERSTATUS_STOPPED=0;
BUFFERSTATUS_PLAYING=1;
BUFFERSTATUS_QFORPLAY=2;
//BUFFERSTATUS_QFORDELETE=3;
BUFFERSTATUS_QFORPLAYLOOPED=4;
BUFFERSTATUS_QFORSTOP=5;
var
DS:IDirectsound8;
DSBuf1: IDirectSoundBuffer;
bufLoaded, strmLoaded:array of TMemoryBuffer;
listener:IDirectSound3DListener;
listenerpos:TD3DXVector3;
effparam:TDSFXI3DL2REVERB=
( lRoom: 000; // [-10000, 0] default: -1000 mB
lRoomHF: 0000; // [-10000, 0] default: 0 mB
flRoomRolloffFactor: 0; // [0.0, 10.0] default: 0.0
flDecayTime: 0.5; // [0.1, 20.0] default: 1.49s
flDecayHFRatio: 0.83; // [0.1, 2.0] default: 0.83
lReflections: -1000; // [-10000, 1000] default: -2602 mB
flReflectionsDelay: 0.003; // [0.0, 0.3] default: 0.007 s
lReverb: -1000; // [-10000, 2000] default: 200 mB
flReverbDelay: 0.1; // [0.0, 0.1] default: 0.011 s
flDiffusion: 50; // [0.0, 100.0] default: 100.0 %
flDensity: 20; // [0.0, 100.0] default: 100.0 %
flHFReference: 5000; // [20.0, 20000.0] default: 5000.0 Hz)
);
//zene
zene2:Tmp3file=nil;
zene3:Tmp3stream=nil;
zenestrm:cardinal=0;
function korbekozott(a,b,x:cardinal):boolean;
begin
result:=((a<b) and ((a<x) and (b>x))) or
((a>b) and ((b>x) or (a<x)));
end;
procedure zeneinit;
var
rnd:integer;
tmp:string;
i:integer;
begin
if DS=nil then exit;
if (high(mp3filelist)<0) and (mp3menu='') then exit;
for i:=0 to high(mp3filelist) do
begin
rnd:=random(length(mp3filelist)-i)+i;
tmp:=mp3filelist[i];
mp3filelist[i]:= mp3filelist[rnd] ;
mp3filelist[rnd]:=tmp;
end;
mp3pos:=0;
if mp3menu='' then
zene2:= Tmp3file.create(mp3filelist[random(length(mp3filelist))])
else
zene2:= Tmp3file.create(mp3menu);
zenestrm:=gettickcount;
setlength(zenebuffer,0);
end;
var
zenethdvege:boolean=true;
procedure zenecleanup;
begin
if DS=nil then exit;
if (high(mp3filelist)<0) and (mp3menu='') then exit;
while not zenethdvege do;
stopstream(zenestrm);
setlength(zenebuffer,0);
if zene2<>nil then
zene2.free;
zene2:=nil;
if zene3<>nil then
zene3.free;
zene3:=nil;
end;
function zenefreshthd(nulla:Pointer):integer;
var
i:integer;
tmp:string;
rnd:integer;
tmpbuffer:Tsmallintdynarr;
lngt:integer;
begin
result:=1;
if DS=nil then exit;
if (high(mp3filelist)<0) and (mp3menu='')then
begin
if ((mp3strmp2=1) and mp3action) or
((mp3strmp2=0) and mp3ambient) or
((mp3strmp2=2) and mp3car) then
begin
if mp3strmpvalts or (zene3=nil) then
mp3stationname:='Tuning in...'
else
mp3stationname:=zene3.nev;
if zene3=nil then
begin
zene3:=Tmp3stream.create(mp3strms[mp3strmp2,mp3strmp[mp3strmp2]]);
zenestrm:=gettickcount;
end;
if mp3strmpvalts then
begin
setlength(zenebuffer,0);
if zene3<>nil then
zene3.destroy;
stopstream(zenestrm);
zene3:=Tmp3stream.create(mp3strms[mp3strmp2,mp3strmp[mp3strmp2]]);
zenestrm:=gettickcount;
mp3strmpvalts:=false;
end;
for i:=0 to 1 do
begin
setlength(tmpbuffer,0);
zene3.read(tmpbuffer);
lngt:=length(zenebuffer);
if length(tmpbuffer)>0 then
begin
setlength(zenebuffer,lngt+length(tmpbuffeR));
copymemory(@(zenebuffer[lngt]),@(tmpbuffer[0]),length(tmpbuffeR)*sizeof(smallint));
end;
if zene3.error>0 then
begin
mp3stationname:='Tune error '+inttostr(zene3.error);
mp3strmpvalts:=true;
if mp3strmp[mp3strmp2]<high(mp3strms[mp3strmp2]) then
inc(mp3strmp[mp3strmp2])
else
mp3strmp[mp3strmp2]:=0;
end;
end;
end
else
begin
stopstream(zenestrm);
setlength(zenebuffer,0);
end;
setlength(tmpbuffer,0);
end
else
begin
if zene2=nil then
zeneinit;
for i:=0 to 1 do
begin
setlength(tmpbuffer,0);
zene2.read(tmpbuffer);
lngt:=length(zenebuffer);
if length(tmpbuffer)>0 then
begin
setlength(zenebuffer,lngt+length(tmpbuffeR));
copymemory(@(zenebuffer[lngt]),@(tmpbuffer[0]),length(tmpbuffeR)*sizeof(smallint));
end;
end;
setlength(tmpbuffer,0);
if zene2.iseof then
begin
zene2.free;
setlength(zenebuffer,0);
if mp3menu='' then
begin
rnd:=random(length(mp3filelist));
tmp:=mp3filelist[mp3pos];
mp3filelist[mp3pos]:= mp3filelist[rnd];
mp3filelist[rnd]:=tmp;
if mp3pos<high(mp3filelist) then inc(mp3pos) else mp3pos:=0;
zene2:= Tmp3file.create(mp3filelist[mp3pos]);
end
else
zene2:= Tmp3file.create(mp3menu);
zenestrm:=gettickcount+cardinal(random(1000));
end;
end;
result:=0;
zenethdvege:=true;
end;
var
zenethdid:cardinal;
procedure zenefresh(volp:single);
var
mire:single;
vol:single;
begin
if DS=nil then exit;
if not zenethdvege then exit;
vol:=Math.Power(volp, 0.5);
if vol<=0 then mire:=0 else
mire:=(vol-1)*5000;
if zene2<>nil then
WritetostreamSmallAmounts(zenestrm,zenebuffer,zene2.samplerate,round(mire),2)
else
if zene3<>nil then
begin
if mp3strmp2=0 then mire:=mire-500;
WritetostreamSmallAmounts(zenestrm,zenebuffer,zene3.samplerate,round(mire),2);
end;
if length(zenebuffer)<22000 then
begin
zenethdvege:=false;
beginthread(nil,0,zenefreshthd,nil,0,zenethdid);
end;
end;
//SOUND INNENTÕL LEFELÉ!!!!!!!
function Loadbuf(var buf:Tmemorybuffer;flags:cardinal;fnev:string):Hresult;
var
mfile:Cwavefile;
pwfx:PWaveFormatex;
plb2: PChar;
dwWavDataRead: DWORD;
osszread:cardinal;
begin
Result:= E_FAIL;
if DS=nil then exit;
mFile:=Cwavefile.Create;
pwfx:=nil;
//getmem(pwfx,sizeof(Twaveformatex));
mfile.Open(Pchar(fnev),pwfx,WAVEFILE_READ);
mfile.ResetFile;
zeromemory(@buf.caps,sizeof(buf.caps));
buf.caps.dwSize:=sizeof(buf.caps);
//if (flags and DSBCAPS_CTRL3d)<>0 then flags:=(flags xor DSBCAPS_CTRL3d);
if (flags and DSBCAPS_CTRLFX)<>0 then
buf.caps.dwFlags:=DSBCAPS_CTRLVOLUME or flags
else
buf.caps.dwFlags:=DSBCAPS_CTRLVOLUME or DSBCAPS_STATIC or flags;
buf.caps.dwBufferBytes:=mfile.m_dwSize;
zeromemory(@buf.format,sizeof(buf.format));
copymemory(@buf,mfile.m_pwfx,sizeof(buf.format));
tempfreq:= buf.format.nSamplesPerSec;
if buf.format.nChannels>1 then
MessageBox(0,'nem mono a hang','cunt',0);
getmem(buf.data,buf.caps.dwBufferBytes);
plb2:=buf.data;
osszread:=0;
repeat
mfile.Read(pbyte(plb2),
mfile.m_dwSize-osszread,
@dwWavDataRead);
plb2:=plb2+dwWavdataread;
osszread:=osszread+dwWavDataRead;
until (0=dwWavDataRead) or (buf.caps.dwBufferBytes<=osszread);
mfile.Destroy;
Result:= S_OK;
end;
function InitSound(hwindow:HWND):Hresult;
var
adesc:_DSbufferdesc;
hib:HRESULT;
begin
result:=E_FAIL;
coinitialize(nil);
if failed(directsoundcreate8(nil,DS,nil)) then exit;
if failed(DS.SetCooperativeLevel(hwindow,DSSCL_PRIORITY)) then exit;
// if failed(DS.Initialize(nil)) then exit;
zeromemory(@adesc,sizeof(adesc));
adesc.dwSize:=sizeof(adesc);
adesc.dwFlags:=DSBCAPS_PRIMARYBUFFER or DSBCAPS_CTRL3D or DSBCAPS_CTRLVOLUME;
hib:=DS.CreateSoundBuffer(adesc,DSBuf1,nil);
if failed(hib)then exit;
DSBuf1.QueryInterface(IID_IDirectSound3DListener, listener);
DSBuf1.SetVolume(0+mainvolume);
Result:=S_OK;
end;
procedure LoadSound(fnev:string;haromd,freq,effects:boolean;mindistance:single);
var
flags:cardinal;
begin
if DS=nil then exit;
laststate:='Loading sound ' + fnev;
//effects:=false;
setlength(bufLoaded,length(bufLoaded)+1);
// zeromemory(@bufloaded[high(bufLoaded)],sizeof(TMemorybuffer));
flags:=0;
if haromd then flags:=flags or DSBCAPS_CTRL3D or DSBCAPS_MUTE3DATMAXDISTANCE;
if freq then flags:=flags or DSBCAPS_CTRLFREQUENCY;
//if effects then flags:=flags or DSBCAPS_CTRLFX;
loadbuf(bufloaded[high(bufLoaded)],flags,'data\snd\'+fnev+'.wav');
bufloaded[high(bufLoaded)].mindis:=mindistance;
bufloaded[high(bufLoaded)].freq:=tempfreq;
end;
procedure LoadStrm(fnev:string);
begin
if DS=nil then exit;
laststate:='Loading stream ' + fnev;
setlength(strmLoaded,length(strmLoaded)+1);
// zeromemory(@strmLoaded[high(strmLoaded)],sizeof(TMemorybuffer));
loadbuf(strmLoaded[high(strmLoaded)],DSBCAPS_CTRLVOLUME,'data\snd\rad\'+fnev+'.wav');
end;
procedure StopAll;
var
i:integer;
begin
if DS=nil then exit;
for i:=0 to high(bufPlaying) do
begin
if bufPlaying[i].DSBuf<>nil then bufPlaying[i].DSBuf.Stop;
if bufPlaying[i].effBuf<>nil then bufPlaying[i].effBuf.Stop;
end;
for i:=0 to high(streams) do
begin
if streams[i].DSBuf<>nil then streams[i].DSBuf.Stop;
end;
end;
procedure closesound;
var
i:integer;
begin
stopall;
for i:=0 to high(bufPlaying) do
begin
if bufPlaying[i].DS3D <>nil then bufPlaying[i].DS3D:=nil;
if bufPlaying[i].DSbuf<>nil then bufPlaying[i].DSbuf:=nil;
if bufPlaying[i].effbuf<>nil then bufPlaying[i].effbuf:=nil;
end;
for i:=0 to high(streams) do
begin
if streams[i].DS3D <>nil then streams[i].DS3D:=nil;
if streams[i].DSbuf<>nil then streams[i].DSbuf:=nil;
end;
{$IFNDEF kurvaDELPHIFOS}
for i:=0 to high(bufLoaded) do
begin
if bufLoaded[i].data<>nil then freemem(bufLoaded[i].data,bufloaded[i].caps.dwBufferBytes);
end;
setlength(bufplaying,0);
setlength(bufloaded ,0);
{$ENDIF}
//if zenefil<>nil then zenefil.Destroy;
//if zenebuf<>nil then zenebuf:=nil;
//ds:=nil;
end;
procedure PlaceListener(vec:TD3DXvector3;szogx,szogy:single);
var
vb,ve,vn:TD3DXvector3;
begin
if DS=nil then exit;
if listener=nil then exit;
listener.SetPosition(vec.x,vec.y,vec.z,DS3D_DEFERRED);
listenerpos:=vec;
ve:=D3DXVector3(sin(szogx)*cos(szogy),sin(szogy),cos(szogx)*cos(szogy));
vb:=D3DXVector3(ve.z,0,-ve.x);
d3dxvec3cross(vn,ve,vb);
d3dxvec3normalize(vb,vn);
d3dxvec3normalize(ve,ve);
listener.SetOrientation(ve.x,ve.y,ve.z,vb.x,vb.y,vb.z,DS3D_DEFERRED);
end;
procedure AddEffects(mi:integer);
var
hib:HRESULT;
caps:_DSBCaps;
begin
exit;
lastsoundaction:=lastsoundaction+'-and-AddEffects( typ:'+inttostr(bufplaying[mi].typ)+')';
with bufPlaying[mi] do
begin
if effbuf=nil then exit;
if FX<>nil then exit;
caps.dwSize:=sizeof(caps);
effbuf.getcaps(caps);
effbuf.stop;
if (caps.dwFlags and DSBCAPS_CTRLFX)=0 then exit;
FXdesc.dwSize:=(sizeof(FXdesc));
FXdesc.dwFlags:=0;
FXdesc.guidDSFXClass:=GUID_DSFX_STANDARD_I3DL2REVERB;
FXdesc.dwReserved1:=0;
FXdesc.dwReserved2:=0;
hib := effbuf.SetFX(1,@FXdesc,nil);
if FAILED(hib) or FAILED(FXhibaszar) then Exit;
if effbuf<>nil then
hib := effbuf.GetObjectInPath(FXdesc.guidDSFXClass, 0,IDirectSoundFXI3DL2Reverb, FX);
if FAILED(hib) then
begin FX:=nil; exit; end;
FX.SetQuality(DSFX_I3DL2REVERB_QUALITY_MIN);
FX.SetAllParameters(effparam);
end;
end;
//1, ha nem érdekel
procedure SetSoundProperties(mit:integer; aid:integer;vol:longint;freq:single;effects:boolean;hol:TD3DXVector3);
var
mi:integer;
i:integer;
begin
if DS=nil then exit;
// effects:=false;
constraintvec(hol);
lastsoundaction:='SetSoundProperties('+inttostr(mit)+','+inttostr(aid)+')';
mi:=-1;
for i:=0 to high(bufPlaying) do
if (bufPlaying[i].typ=mit) and (bufPlaying[i].id=aid) then
begin
mi:=i;
break;
end;
if mi=-1 then exit;
if (bufPlaying[mi].state=BUFFERSTATUS_STOPPED) or
(bufPlaying[mi].state=BUFFERSTATUS_QFORSTOP) then exit;
with bufPlaying[mi] do
begin
if DSBuf=nil then exit;
if freq<>1 then
DSbuf.SetFrequency(round(bufloaded[mit].freq * freq));
if vol<>1 then
DSbuf.SetVolume(vol+mainvolume);
if DS3d<>nil then
if (hol.y<>0) or (hol.x<>0) or (hol.z<>0) then
begin
DS3D.SetPosition(hol.x,hol.y,hol.z,DS3D_DEFERRED) ;
pos:=hol;
tav:=tavpointpointsq(pos,listenerpos)*hangero;
end;
if effbuf=nil then exit;
if freq<>1 then
effbuf.SetFrequency(round(bufloaded[mit].freq * freq));
if vol<>1 then
effbuf.SetVolume(vol+mainvolume);
if eff3d<>nil then
if (hol.y<>0) or (hol.x<>0) or (hol.z<>0) then
eff3D.SetPosition(hol.x,hol.y,hol.z,DS3D_DEFERRED)
end;
end;
procedure SetSoundVelocity(mit:integer; aid:integer;vel:TD3DXVector3);
var
mi:integer;
i:integer;
begin
if DS=nil then exit;
constraintvec(vel);
lastsoundaction:='SetSoundVelocity('+inttostr(mit)+','+inttostr(aid)+')';
mi:=-1;
for i:=0 to high(bufPlaying) do
if (bufPlaying[i].typ=mit) and (bufPlaying[i].id=aid) then
begin
mi:=i;
break;
end;
if mi=-1 then exit;
//if bufPlaying[mi].state=BUFFERSTATUS_QFORDELETE then exit;
with bufPlaying[mi] do
begin
if DSBuf=nil then exit;
if DS3d<>nil then
DS3D.SetVelocity(vel.x,vel.y,vel.z,DS3D_DEFERRED) ;
end;
end;
procedure SpecialDuplicate(mirol:TMemoryBuffer; var mire:IDirectSoundBuffer8);
var
caps:_DSBCaps;
desc:_DSBufferDesc;
format:TWaveformatex;
LockedBuffer1,LockedBuffer2:Pointer;
LBSize1,LBSize2:Dword;
hib:HRESULT;
ass:cardinal;
begin
if DS=nil then exit;
mire:=nil;
if mirol.data=nil then exit;
// inc(specialcreatecount);
caps:=mirol.caps;
format:=mirol.format;
zeromemory(@desc,sizeof(desc));
desc.dwSize:=sizeof(desc);
desc.dwFlags:=caps.dwFlags and (DSBCAPS_CTRLVOLUME or DSBCAPS_CTRL3D or DSBCAPS_CTRLFREQUENCY{ or DSBCAPS_CTRLFX});
desc.lpwfxFormat:=@format;
//desc.dwBufferBytes:=max(caps.dwBufferBytes,format.nAvgBytesPerSec*2);
desc.dwBufferBytes:=caps.dwBufferBytes;
hib:=DS.CreateSoundBuffer(desc,IDirectSoundBuffer(mire),nil);
if FAILED(hib) then exit;
if FAILED(mire.Lock(0, desc.dwBufferBytes,
@LockedBuffer2, @LBsize2, @lockedbuffer1, @LBsize1, DSBLOCK_ENTIREBUFFER)) then exit;
ass:=min(LBsize2,caps.dwBufferBytes);
copymemory(lockedbuffer2,mirol.data,ass);
{ if (LBsize2>ass) then
zeromemory(pointer(cardinal(lockedbuffer2)+ass),LBSize2-ass); }
mire.Unlock(LockedBuffer2, LBsize2, LockedBuffer1, LBSize1);
end;
//ha megtelt, true
function WriteToStream(aid:integer;hely:TD3DXVector3;const mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0;channels:integer = 0):boolean;
var
i:integer;
hol:integer;
desc:_DSBufferDesc;
writepos,playpos:cardinal;
writing:integer;
LB1,LB2:Psmallintarray;
lbs1,lbs2:cardinal;
wfx2:TWaveformatex;
begin
result:=false;
if DS=nil then exit;
if length(mit)=0 then exit;
hol:=-1;
for i:=0 to high(streams) do
if (streams[i].id=aid) then
hol:=i;
if hol<0 then
begin
setlength(streams,length(streams)+1);
with streams[high(streams)] do
begin
id:=aid;
playing:=false;
lastpos:=0;
lastwrite:=0;
zeromemory(@desc,sizeof(desc));
desc.dwSize:=sizeof(desc);
desc.dwFlags:=DSBCAPS_GLOBALFOCUS;
if (hely.y<>0) and (hely.x<>0) and (hely.z<>0) then
desc.dwFlags:=desc.dwFlags or DSBCAPS_CTRL3D;
if vol<>0 then
desc.dwFlags:=desc.dwFlags or DSBCAPS_CTRLVOLUME;
wfx2:=wfx;
if samplerate<>0 then
wfx2.nSamplesPerSec:=samplerate;
if channels<>0 then
wfx2.nchannels:=channels;
wfx2.nAvgBytesPerSec:=wfx2.nSamplesPerSec*2*wfx2.nChannels;
wfx2.nBlockalign:=2*wfx2.nChannels;
desc.lpwfxFormat:=@wfx2;
bufferbytes:=wfx2.nAvgBytesPerSec*3;
desc.dwBufferBytes:=bufferbytes;
if DS=nil then exit;
DS.CreateSoundBuffer(desc,DSBuf,nil);
if DSbuf<>nil then
begin
DSBuf.QueryInterface(IID_IDirectSound3DBuffer, DS3D);
if DS3D <> nil then
begin
DS3D.SetMinDistance(1,DS3D_DEFERRED);
DS3D.SetMaxDistance(50,DS3D_DEFERRED);
end
end;
end;
hol:=high(streams);
end;
with streams[hol] do
begin
if DSbuf=nil then exit;
DSBuf.SetVolume(vol+mainVolume);
DSbuf.GetCurrentPosition(@playpos,@writepos);
{ if korbekozott(lastwrite,(lastwrite+length(mit)*2) mod bufferbytes,(playpos +bufferbytes shr 1) mod bufferbytes) then
begin
result:=true;
exit;
end; //}
if korbekozott(lastwrite,(lastwrite+cardinal(length(mit))*2) mod bufferbytes,playpos)
or korbekozott(lastwrite,(lastwrite+cardinal(length(mit))*2) mod bufferbytes,lastpos)
then
begin
//lastwrite:=writepos;
result:=true;
exit;
end;
{ if korbekozott(lastwrite,(lastwrite+length(mit)*2) mod bufferbytes,writepos) then
lastwrite:=writepos; }
played:=gettickcount;
DSbuf.Lock(lastwrite,length(mit)*2,@LB1,@lbs1,@lb2,@lbs2,0);
writing:=0;
if LB1<>nil then
begin
copymemory(LB1,@mit[writing shr 1],lbs1);
inc(writing,lbs1);
end;
if LB2<>nil then
begin
copymemory(LB2,@mit[writing shr 1],lbs2);
inc(writing,lbs2);
end;
inc(lastwrite,writing);
DSBuf.Unlock(LB1,lbs1,lb2,lbs2);
if lastwrite>=bufferbytes then lastwrite:=lastwrite-bufferbytes;
if not playing then
DSBuf.Play(0,0,DSBPLAY_LOOPING);
if DS3d<>nil then
DS3D.SetPosition(hely.x,hely.y,hely.z,DS3D_DEFERRED);
end;
end;
function StopStream(aid:integer):boolean;
var
i:integer;
hol:integer;
begin
result:=false;
if DS=nil then exit;
hol:=-1;
for i:=0 to high(streams) do
if (streams[i].id=aid) then
hol:=i;
if hol<0 then exit;
with streams[hol] do
begin
if DSbuf=nil then exit;
DSBuf.Stop;
end;
end;
procedure WriteToStreamSmallAmounts(aid:integer;var mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0;channels:integer = 0);
var
tmp:Tsmallintdynarr;
i:integer;
begin
// repeat
if (length(mit)<=0) then exit;
if samplerate=0 then
i:=min(length(mit),22000 div 4)
else
i:=min(length(mit),samplerate div 4);
setlength(tmp,i);
copymemory(@tmp[0],@mit[0],length(tmp)*sizeof(smallint));
if writetostream(aid,d3dxvector3zero,tmp,samplerate,vol,channels) then
exit;
for i:=0 to high(mit)-length(tmp) do
// if (i+length(tmp))<=high(mit) then
mit[i]:=mit[i+length(tmp)];
setlength(mit,max(length(mit)-length(tmp),0));
// until false;
end;
procedure WriteToStreamBuffered(aid:integer;const mit:Tsmallintdynarr;samplerate:integer = 0;vol:integer =0);
var
hol:integer;
tmp:Tsmallintdynarr;
i:integer;
ind:integer;
begin
if length(mit)=0 then exit;
hol:=-1;
for i:=0 to high(streams) do
if (streams[i].id=aid) then
hol:=i;
if hol<0 then
begin
setlength(tmp,length(mit));
copymemory(@tmp[0],@mit[0],length(mit)*sizeof(smallint));
WriteToStreamSmallAmounts(aid,tmp,samplerate,vol);
hol:=-1;
for i:=0 to high(streams) do
if (streams[i].id=aid) then
hol:=i;
if hol<0 then exit;
setlength(streams[hol].buffered,length(tmp));
for i:=0 to high(tmp) do
streams[hol].buffered[i]:=tmp[i];
end
else
begin
ind:=length(streams[hol].buffered);
setlength(streams[hol].buffered,length(streams[hol].buffered)+length(mit));
for i:=0 to high(mit) do
streams[hol].buffered[ind+i]:=mit[i];
end;
setlengtH(tmp,0)
end;
procedure delstream(i:integer);
begin
if streams[i].DSBuf<>nil then
begin
streams[i].DSBuf.Stop;
end;
if streams[i].DS3D <>nil then streams[i].DS3D:=nil;
if streams[i].DSbuf<>nil then streams[i].DSbuf:=nil;
streams[i]:=streams[high(streams)];
if streams[high(streams)].DS3D <>nil then streams[high(streams)].DS3D:=nil;
if streams[high(streams)].DSbuf<>nil then streams[high(streams)].DSbuf:=nil;
setlength(streams,high(streams));
end;
procedure PlayStrm(mit:integer;aid:integer;vol:integer=0;onlycreate:boolean=false);
var
tmp:Tsmallintdynarr;
i:integer;
cucc1:Psmallintarray;
cucc2:Pshortintarray;
begin
if DS=nil then exit;
if onlycreate then
for i:=0 to high(streams) do
if (streams[i].id=aid) then
begin
delstream(i);
break;