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
|
unit UAudioPlayback_SoftMixer;
interface
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$I switches.inc}
uses
Classes,
SysUtils,
sdl,
UMusic;
type
TAudioPlayback_SoftMixer = class;
TGenericPlaybackStream = class(TAudioPlaybackStream)
private
Engine: TAudioPlayback_SoftMixer;
DecodeStream: TAudioDecodeStream;
SampleBuffer : PChar;
SampleBufferCount: integer; // number of available bytes in SampleBuffer
SampleBufferPos : cardinal;
BytesAvail: integer;
cvt: TSDL_AudioCVT;
Status: TStreamStatus;
Loop: boolean;
_volume: integer;
InternalLock: PSDL_Mutex;
procedure Reset();
class function ConvertAudioFormatToSDL(fmt: TAudioSampleFormat): UInt16;
function InitFormatConversion(): boolean;
procedure Lock(); {$IFDEF HasInline}inline;{$ENDIF}
procedure Unlock(); {$IFDEF HasInline}inline;{$ENDIF}
public
constructor Create(Engine: TAudioPlayback_SoftMixer);
destructor Destroy(); override;
function SetDecodeStream(decodeStream: TAudioDecodeStream): boolean;
procedure Play(); override;
procedure Pause(); override;
procedure Stop(); override;
procedure Close(); override;
function GetLoop(): boolean; override;
procedure SetLoop(Enabled: boolean); override;
function GetLength(): real; override;
function GetStatus(): TStreamStatus; override;
function IsLoaded(): boolean;
function GetVolume(): integer; override;
procedure SetVolume(Volume: integer); override;
// functions delegated to the decode stream
function GetPosition: real;
procedure SetPosition(Time: real);
function ReadData(Buffer: PChar; BufSize: integer): integer;
function GetPCMData(var data: TPCMData): Cardinal;
procedure GetFFTData(var data: TFFTData);
end;
TAudioMixerStream = class
private
Engine: TAudioPlayback_SoftMixer;
activeStreams: TList;
mixerBuffer: PChar;
internalLock: PSDL_Mutex;
_volume: integer;
procedure Lock(); {$IFDEF HasInline}inline;{$ENDIF}
procedure Unlock(); {$IFDEF HasInline}inline;{$ENDIF}
function GetVolume(): integer;
procedure SetVolume(volume: integer);
public
constructor Create(Engine: TAudioPlayback_SoftMixer);
destructor Destroy(); override;
procedure AddStream(stream: TAudioPlaybackStream);
procedure RemoveStream(stream: TAudioPlaybackStream);
function ReadData(Buffer: PChar; BufSize: integer): integer;
property Volume: integer READ GetVolume WRITE SetVolume;
end;
TAudioPlayback_SoftMixer = class( TInterfacedObject, IAudioPlayback )
private
MusicStream: TGenericPlaybackStream;
MixerStream: TAudioMixerStream;
protected
FormatInfo: TAudioFormatInfo;
function InitializeAudioPlaybackEngine(): boolean; virtual; abstract;
function StartAudioPlaybackEngine(): boolean; virtual; abstract;
procedure StopAudioPlaybackEngine(); virtual; abstract;
procedure AudioCallback(buffer: PChar; size: integer); {$IFDEF HasInline}inline;{$ENDIF}
public
function GetName: String; virtual; abstract;
function InitializePlayback(): boolean;
destructor Destroy; override;
function Load(const Filename: String): TGenericPlaybackStream;
procedure SetVolume(Volume: integer);
procedure SetMusicVolume(Volume: integer);
procedure SetLoop(Enabled: boolean);
function Open(const Filename: string): boolean; // true if succeed
procedure Rewind;
procedure SetPosition(Time: real);
procedure Play;
procedure Pause;
procedure Stop;
procedure Close;
function Finished: boolean;
function Length: real;
function GetPosition: real;
// Equalizer
procedure GetFFTData(var data: TFFTData);
// Interface for Visualizer
function GetPCMData(var data: TPCMData): Cardinal;
function GetMixer(): TAudioMixerStream; {$IFDEF HasInline}inline;{$ENDIF}
function GetAudioFormatInfo(): TAudioFormatInfo;
procedure MixBuffers(dst, src: PChar; size: Cardinal; volume: Integer); virtual;
// Sounds
function OpenSound(const Filename: String): TAudioPlaybackStream;
procedure PlaySound(stream: TAudioPlaybackStream);
procedure StopSound(stream: TAudioPlaybackStream);
end;
implementation
uses
Math,
//samplerate,
UFFT,
ULog,
UIni,
UMain;
{ TAudioMixerStream }
constructor TAudioMixerStream.Create(Engine: TAudioPlayback_SoftMixer);
begin
Self.Engine := Engine;
activeStreams := TList.Create;
internalLock := SDL_CreateMutex();
_volume := 100;
end;
destructor TAudioMixerStream.Destroy();
begin
if assigned(mixerBuffer) then
Freemem(mixerBuffer);
activeStreams.Free;
SDL_DestroyMutex(internalLock);
end;
procedure TAudioMixerStream.Lock();
begin
SDL_mutexP(internalLock);
end;
procedure TAudioMixerStream.Unlock();
begin
SDL_mutexV(internalLock);
end;
function TAudioMixerStream.GetVolume(): integer;
begin
Lock();
result := _volume;
Unlock();
end;
procedure TAudioMixerStream.SetVolume(volume: integer);
begin
Lock();
_volume := volume;
Unlock();
end;
procedure TAudioMixerStream.AddStream(stream: TAudioPlaybackStream);
begin
if not assigned(stream) then
Exit;
Lock();
// check if stream is already in list to avoid duplicates
if (activeStreams.IndexOf(Pointer(stream)) = -1) then
activeStreams.Add(Pointer(stream));
Unlock();
end;
(*
* Sets the entry of stream in the activeStreams-List to nil
* but does not remove it from the list (count is not changed!).
* Otherwise iterations over the elements might fail due to a
* changed count-property.
* Call activeStreams.Pack() to remove the nil-pointers
* or check for nil-pointers when accessing activeStreams.
*)
procedure TAudioMixerStream.RemoveStream(stream: TAudioPlaybackStream);
var
index: integer;
begin
Lock();
index := activeStreams.IndexOf(Pointer(stream));
if (index <> -1) then
begin
// remove entry but do not decrease count-property
activeStreams[index] := nil;
end;
Unlock();
end;
function TAudioMixerStream.ReadData(Buffer: PChar; BufSize: integer): integer;
var
i: integer;
size: integer;
stream: TGenericPlaybackStream;
needsPacking: boolean;
begin
result := BufSize;
// zero target-buffer (silence)
FillChar(Buffer^, BufSize, 0);
// resize mixer-buffer if necessary
ReallocMem(mixerBuffer, BufSize);
if not assigned(mixerBuffer) then
Exit;
Lock();
needsPacking := false;
// mix streams to one stream
for i := 0 to activeStreams.Count-1 do
begin
if (activeStreams[i] = nil) then
begin
needsPacking := true;
continue;
end;
stream := TGenericPlaybackStream(activeStreams[i]);
// fetch data from current stream
size := stream.ReadData(mixerBuffer, BufSize);
if (size > 0) then
begin
// mix stream-data with mixer-buffer
// Note: use _volume (Application-Volume) instead of Volume to prevent recursive locking
Engine.MixBuffers(Buffer, mixerBuffer, size, _volume * stream.Volume div 100);
end;
end;
// remove nil-pointers from list
if (needsPacking) then
begin
activeStreams.Pack();
end;
Unlock();
end;
{ TGenericPlaybackStream }
constructor TGenericPlaybackStream.Create(Engine: TAudioPlayback_SoftMixer);
begin
inherited Create();
Self.Engine := Engine;
internalLock := SDL_CreateMutex();
Reset();
end;
destructor TGenericPlaybackStream.Destroy();
begin
Close();
SDL_DestroyMutex(internalLock);
inherited Destroy();
end;
procedure TGenericPlaybackStream.Reset();
begin
Stop();
Loop := false;
// TODO: use DecodeStream.Unref() instead of Free();
FreeAndNil(DecodeStream);
FreeMem(SampleBuffer);
SampleBuffer := nil;
SampleBufferPos := 0;
BytesAvail := 0;
_volume := 0;
end;
procedure TGenericPlaybackStream.Lock();
begin
SDL_mutexP(internalLock);
end;
procedure TGenericPlaybackStream.Unlock();
begin
SDL_mutexV(internalLock);
end;
class function TGenericPlaybackStream.ConvertAudioFormatToSDL(fmt: TAudioSampleFormat): UInt16;
begin
case fmt of
asfU8: Result := AUDIO_U8;
asfS8: Result := AUDIO_S8;
asfU16LSB: Result := AUDIO_U16LSB;
asfS16LSB: Result := AUDIO_S16LSB;
asfU16MSB: Result := AUDIO_U16MSB;
asfS16MSB: Result := AUDIO_S16MSB;
asfU16: Result := AUDIO_U16;
asfS16: Result := AUDIO_S16;
else Result := 0;
end;
end;
function TGenericPlaybackStream.InitFormatConversion(): boolean;
var
//err: integer;
srcFormat: UInt16;
dstFormat: UInt16;
srcFormatInfo: TAudioFormatInfo;
dstFormatInfo: TAudioFormatInfo;
begin
Result := false;
srcFormatInfo := DecodeStream.GetAudioFormatInfo();
dstFormatInfo := Engine.GetAudioFormatInfo();
srcFormat := ConvertAudioFormatToSDL(srcFormatInfo.Format);
dstFormat := ConvertAudioFormatToSDL(dstFormatInfo.Format);
if ((srcFormat = 0) or (dstFormat = 0)) then
begin
Log.LogError('Audio-format not supported by SDL', 'TSoftMixerPlaybackStream.InitFormatConversion');
Exit;
end;
if (SDL_BuildAudioCVT(@cvt,
srcFormat, srcFormatInfo.Channels, srcFormatInfo.SampleRate,
dstFormat, dstFormatInfo.Channels, dstFormatInfo.SampleRate) = -1) then
begin
Log.LogError(SDL_GetError(), 'TSoftMixerPlaybackStream.InitFormatConversion');
Exit;
end;
Result := true;
end;
function TGenericPlaybackStream.SetDecodeStream(decodeStream: TAudioDecodeStream): boolean;
begin
result := false;
Reset();
if not assigned(decodeStream) then
Exit;
Self.DecodeStream := decodeStream;
if not InitFormatConversion() then
Exit;
_volume := 100;
result := true;
end;
procedure TGenericPlaybackStream.Close();
begin
Reset();
end;
procedure TGenericPlaybackStream.Play();
var
mixer: TAudioMixerStream;
begin
if (status <> ssPaused) then
begin
// rewind
if assigned(DecodeStream) then
DecodeStream.Position := 0;
end;
status := ssPlaying;
mixer := Engine.GetMixer();
if (mixer <> nil) then
mixer.AddStream(Self);
end;
procedure TGenericPlaybackStream.Pause();
var
mixer: TAudioMixerStream;
begin
status := ssPaused;
mixer := Engine.GetMixer();
if (mixer <> nil) then
mixer.RemoveStream(Self);
end;
procedure TGenericPlaybackStream.Stop();
var
mixer: TAudioMixerStream;
begin
status := ssStopped;
mixer := Engine.GetMixer();
if (mixer <> nil) then
mixer.RemoveStream(Self);
end;
function TGenericPlaybackStream.IsLoaded(): boolean;
begin
result := assigned(DecodeStream);
end;
function TGenericPlaybackStream.GetLoop(): boolean;
begin
result := Loop;
end;
procedure TGenericPlaybackStream.SetLoop(Enabled: boolean);
begin
Loop := Enabled;
end;
function TGenericPlaybackStream.GetLength(): real;
begin
if assigned(DecodeStream) then
result := DecodeStream.Length
else
result := -1;
end;
function TGenericPlaybackStream.GetStatus(): TStreamStatus;
begin
result := status;
end;
{*
* Note: 44.1kHz to 48kHz conversion or vice versa is not supported
* by SDL at the moment. No conversion takes place in this cases.
* This is because SDL just converts differences in powers of 2.
* So the result might not be that accurate. Although this is not
* audible in most cases it needs synchronization with the video
* or the lyrics timer.
* Using libsamplerate might give better results.
*}
function TGenericPlaybackStream.ReadData(Buffer: PChar; BufSize: integer): integer;
var
decodeBufSize: integer;
sampleBufSize: integer;
nBytesDecoded: integer;
frameSize: integer;
remFrameBytes: integer;
copyCnt: integer;
BytesNeeded: integer;
begin
Result := -1;
BytesNeeded := BufSize;
// copy remaining data from the last call to the result-buffer
if (BytesAvail > 0) then
begin
copyCnt := Min(BufSize, BytesAvail);
Move(SampleBuffer[SampleBufferPos], Buffer[0], copyCnt);
Dec(BytesAvail, copyCnt);
Dec(BytesNeeded, copyCnt);
if (BytesNeeded = 0) then
begin
// Result-Buffer is full -> no need to decode more data.
// The sample-buffer might even contain some data for the next call
Inc(SampleBufferPos, copyCnt);
Result := BufSize;
Exit;
end;
end;
if not assigned(DecodeStream) then
Exit;
// calc number of bytes to decode
decodeBufSize := Ceil(BufSize / cvt.len_ratio);
// assure that the decode-size is a multiple of the frame size
frameSize := DecodeStream.GetAudioFormatInfo().FrameSize;
remFrameBytes := decodeBufSize mod frameSize;
if (remFrameBytes > 0) then
decodeBufSize := decodeBufSize + (frameSize - remFrameBytes);
Lock();
try
// calc buffer size
sampleBufSize := decodeBufSize * cvt.len_mult;
// resize buffer if necessary.
// The required buffer-size will be smaller than the result-buffer
// in most cases (if the decoded signal is mono or has a lesser bitrate).
// If the output-rate is 44.1kHz and the decode-rate is 48kHz or 96kHz it
// will be ~1.09 or ~2.18 times bigger. Those extra memory consumption
// should be reasonable. If not we should call TDecodeStream.ReadData()
// multiple times.
// Note: we do not decrease the buffer by the count of bytes used from
// the previous call of this function (bytesAvail). Otherwise the
// buffer will be reallocated each time this function is called just to
// add or remove a few bytes from the buffer.
// By not doing this the buffer's size should be rather stable and it
// will not be reallocated/resized at all if the BufSize params does not
// change in consecutive calls.
ReallocMem(SampleBuffer, sampleBufSize);
if not assigned(SampleBuffer) then
Exit;
// decode data
nBytesDecoded := DecodeStream.ReadData(SampleBuffer, decodeBufSize);
if (nBytesDecoded = -1) then
Exit;
// end-of-file reached -> stop playback
if (DecodeStream.EOF) then
Stop();
// resample decoded data
cvt.buf := PUint8(SampleBuffer);
cvt.len := nBytesDecoded;
if (SDL_ConvertAudio(@cvt) = -1) then
Exit;
SampleBufferCount := cvt.len_cvt;
finally
Unlock();
end;
BytesAvail := SampleBufferCount;
SampleBufferPos := 0;
// copy data to result buffer
copyCnt := Min(BytesNeeded, BytesAvail);
Move(SampleBuffer[0], Buffer[BufSize - BytesNeeded], copyCnt);
Dec(BytesAvail, copyCnt);
Dec(BytesNeeded, copyCnt);
Inc(SampleBufferPos, copyCnt);
Result := BufSize - BytesNeeded;
end;
(* TODO: libsamplerate support
function TGenericPlaybackStream.ReadData(Buffer: PChar; BufSize: integer): integer;
var
convState: PSRC_STATE;
convData: SRC_DATA;
error: integer;
begin
// Note: needs mono->stereo conversion, multi-channel->stereo, etc.
// maybe we should use SDL for the channel-conversion stuff
// and use libsamplerate afterwards for the frequency-conversion
//convState := src_new(SRC_SINC_MEDIUM_QUALITY, 2, @error);
//src_short_to_float_array(input, output, len);
convData.
if (src_process(convState, @convData) <> 0) then
begin
Log.LogError(src_strerror(src_error(convState)), 'TSoftMixerPlaybackStream.ReadData');
Exit;
end;
src_float_to_short_array();
//src_delete(convState);
end;
*)
function TGenericPlaybackStream.GetPCMData(var data: TPCMData): Cardinal;
var
nBytes: integer;
begin
Result := 0;
// just SInt16 stereo support for now
if ((Engine.GetAudioFormatInfo().Format <> asfS16) or
(Engine.GetAudioFormatInfo().Channels <> 2)) then
begin
Exit;
end;
// zero memory
FillChar(data, SizeOf(data), 0);
// TODO: At the moment just the first samples of the SampleBuffer
// are returned, even if there is newer data in the upper samples.
Lock();
nBytes := Min(SizeOf(data), SampleBufferCount);
if (nBytes > 0) then
begin
Move(SampleBuffer[0], data, nBytes);
end;
Unlock();
Result := nBytes div SizeOf(TPCMStereoSample);
end;
procedure TGenericPlaybackStream.GetFFTData(var data: TFFTData);
var
i: integer;
Frames: integer;
DataIn: PSingleArray;
AudioFormat: TAudioFormatInfo;
begin
// only works with SInt16 and Float values at the moment
AudioFormat := Engine.GetAudioFormatInfo();
DataIn := AllocMem(FFTSize * SizeOf(Single));
if (DataIn = nil) then
Exit;
Lock();
// TODO: We just use the first Frames frames, the others are ignored.
// This is OK for the equalizer display but not if we want to use
// this function for voice-analysis someday (I don't think we want).
Frames := Min(FFTSize, SampleBufferCount div AudioFormat.FrameSize);
// use only first channel and convert data to float-values
case AudioFormat.Format of
asfS16:
begin
for i := 0 to Frames-1 do
DataIn[i] := PSmallInt(@SampleBuffer[i*AudioFormat.FrameSize])^ / -Low(SmallInt);
end;
asfFloat:
begin
for i := 0 to Frames-1 do
DataIn[i] := PSingle(@SampleBuffer[i*AudioFormat.FrameSize])^;
end;
end;
Unlock();
WindowFunc(fwfHanning, FFTSize, DataIn);
PowerSpectrum(FFTSize, DataIn, @data);
FreeMem(DataIn);
// resize data to a 0..1 range
for i := 0 to High(TFFTData) do
begin
// TODO: this might need some work
data[i] := Sqrt(data[i]) / 100;
end;
end;
function TGenericPlaybackStream.GetPosition: real;
begin
if assigned(DecodeStream) then
result := DecodeStream.Position
else
result := -1;
end;
procedure TGenericPlaybackStream.SetPosition(Time: real);
begin
if assigned(DecodeStream) then
DecodeStream.Position := Time;
end;
function TGenericPlaybackStream.GetVolume(): integer;
begin
result := _volume;
end;
procedure TGenericPlaybackStream.SetVolume(volume: integer);
begin
// clamp volume
if (volume > 100) then
_volume := 100
else if (volume < 0) then
_volume := 0
else
_volume := volume;
end;
{ TAudioPlayback_SoftMixer }
function TAudioPlayback_SoftMixer.InitializePlayback: boolean;
begin
result := false;
//Log.LogStatus('InitializePlayback', 'UAudioPlayback_SoftMixer');
if(not InitializeAudioPlaybackEngine()) then
Exit;
MixerStream := TAudioMixerStream.Create(Self);
if(not StartAudioPlaybackEngine()) then
Exit;
result := true;
end;
destructor TAudioPlayback_SoftMixer.Destroy;
begin
StopAudioPlaybackEngine();
FreeAndNil(MusicStream);
FreeAndNil(MixerStream);
FreeAndNil(FormatInfo);
inherited Destroy();
end;
procedure TAudioPlayback_SoftMixer.AudioCallback(buffer: PChar; size: integer);
begin
MixerStream.ReadData(buffer, size);
end;
function TAudioPlayback_SoftMixer.GetMixer(): TAudioMixerStream;
begin
Result := MixerStream;
end;
function TAudioPlayback_SoftMixer.GetAudioFormatInfo(): TAudioFormatInfo;
begin
Result := FormatInfo;
end;
function TAudioPlayback_SoftMixer.Load(const Filename: String): TGenericPlaybackStream;
var
decodeStream: TAudioDecodeStream;
playbackStream: TGenericPlaybackStream;
begin
Result := nil;
decodeStream := AudioDecoder.Open(Filename);
if not assigned(decodeStream) then
begin
Log.LogStatus('LoadSoundFromFile: Sound not found "' + Filename + '"', 'UAudioPlayback_SoftMixer');
Exit;
end;
playbackStream := TGenericPlaybackStream.Create(Self);
if (not playbackStream.SetDecodeStream(decodeStream)) then
Exit;
result := playbackStream;
end;
procedure TAudioPlayback_SoftMixer.SetVolume(Volume: integer);
begin
// sets volume only for this application
MixerStream.Volume := Volume;
end;
procedure TAudioPlayback_SoftMixer.SetMusicVolume(Volume: Integer);
begin
if assigned(MusicStream) then
MusicStream.Volume := Volume;
end;
procedure TAudioPlayback_SoftMixer.SetLoop(Enabled: boolean);
begin
if assigned(MusicStream) then
MusicStream.SetLoop(Enabled);
end;
function TAudioPlayback_SoftMixer.Open(const Filename: string): boolean;
//var
// decodeStream: TAudioDecodeStream;
begin
Result := false;
// free old MusicStream
MusicStream.Free();
// and load new one
MusicStream := Load(Filename);
if not assigned(MusicStream) then
Exit;
//Set Max Volume
SetMusicVolume(100);
Result := true;
end;
procedure TAudioPlayback_SoftMixer.Rewind;
begin
SetPosition(0);
end;
procedure TAudioPlayback_SoftMixer.SetPosition(Time: real);
begin
if assigned(MusicStream) then
MusicStream.SetPosition(Time);
end;
function TAudioPlayback_SoftMixer.GetPosition: real;
begin
if assigned(MusicStream) then
Result := MusicStream.GetPosition()
else
Result := -1;
end;
function TAudioPlayback_SoftMixer.Length: real;
begin
if assigned(MusicStream) then
Result := MusicStream.GetLength()
else
Result := -1;
end;
procedure TAudioPlayback_SoftMixer.Play;
begin
if assigned(MusicStream) then
MusicStream.Play();
end;
procedure TAudioPlayback_SoftMixer.Pause;
begin
if assigned(MusicStream) then
MusicStream.Pause();
end;
procedure TAudioPlayback_SoftMixer.Stop;
begin
if assigned(MusicStream) then
MusicStream.Stop();
end;
procedure TAudioPlayback_SoftMixer.Close;
begin
if assigned(MusicStream) then
begin
MusicStream.Close();
end;
end;
function TAudioPlayback_SoftMixer.Finished: boolean;
begin
if assigned(MusicStream) then
Result := (MusicStream.GetStatus() = ssStopped)
else
Result := true;
end;
procedure TAudioPlayback_SoftMixer.MixBuffers(dst, src: PChar; size: Cardinal; volume: Integer);
var
SampleIndex: Cardinal;
SampleInt: Integer;
SampleFlt: Single;
begin
// TODO: optimize this code, e.g. with assembler (MMX)
SampleIndex := 0;
case FormatInfo.Format of
asfS16:
begin
while (SampleIndex < size) do
begin
// apply volume and sum with previous mixer value
SampleInt := PSmallInt(@dst[SampleIndex])^ + PSmallInt(@src[SampleIndex])^ * volume div 100;
// clip result
if (SampleInt > High(SmallInt)) then
SampleInt := High(SmallInt)
else if (SampleInt < Low(SmallInt)) then
SampleInt := Low(SmallInt);
// assign result
PSmallInt(@dst[SampleIndex])^ := SampleInt;
// increase index by one sample
Inc(SampleIndex, SizeOf(SmallInt));
end;
end;
asfFloat:
begin
while (SampleIndex < size) do
begin
// apply volume and sum with previous mixer value
SampleFlt := PSingle(@dst[SampleIndex])^ + PSingle(@src[SampleIndex])^ * volume/100;
// clip result
if (SampleFlt > 1.0) then
SampleFlt := 1.0
else if (SampleFlt < -1.0) then
SampleFlt := -1.0;
// assign result
PSingle(@dst[SampleIndex])^ := SampleFlt;
// increase index by one sample
Inc(SampleIndex, SizeOf(Single));
end;
end;
else
begin
Log.LogError('Incompatible format', 'TAudioMixerStream.MixAudio');
end;
end;
end;
//Equalizer
procedure TAudioPlayback_SoftMixer.GetFFTData(var data: TFFTData);
begin
if assigned(MusicStream) then
MusicStream.GetFFTData(data);
end;
// Interface for Visualizer
function TAudioPlayback_SoftMixer.GetPCMData(var data: TPCMData): Cardinal;
begin
if assigned(MusicStream) then
Result := MusicStream.GetPCMData(data)
else
Result := 0;
end;
function TAudioPlayback_SoftMixer.OpenSound(const Filename: String): TAudioPlaybackStream;
begin
Result := Load(Filename);
end;
procedure TAudioPlayback_SoftMixer.PlaySound(stream: TAudioPlaybackStream);
begin
if assigned(stream) then
stream.Play();
end;
procedure TAudioPlayback_SoftMixer.StopSound(stream: TAudioPlaybackStream);
begin
if assigned(stream) then
stream.Stop();
end;
end.
|