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
|
unit UAudio_FFMpeg;
(*******************************************************************************
This unit is primarily based upon -
http://www.dranger.com/ffmpeg/ffmpegtutorial_all.html
and tutorial03.c
http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
*******************************************************************************)
interface
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$I switches.inc}
uses Classes,
{$IFDEF win32}
windows,
{$ENDIF}
Messages,
SysUtils,
{$IFNDEF FPC}
Forms,
{$ENDIF}
SDL, // Used for Audio output Interface
avcodec, // FFMpeg Audio file decoding
avformat,
avutil,
ULog,
UMusic;
type
TPacketQueue = record
first_pkt ,
last_pkt : pAVPacketList;
nb_packets : integer;
size : integer;
mutex : pSDL_mutex;
cond : pSDL_cond;
end;
pPacketQueue = ^TPacketQueue;
function packet_queue_put(var aPacketQueue : TPacketQueue; var AVPacket : TAVPacket): integer;
function packet_queue_get(var aPacketQueue : TPacketQueue; var AVPacket : TAVPacket; block : integer ): integer;
procedure packet_queue_init( var aPacketQueue : TPacketQueue );
procedure audio_callback( userdata: Pointer; stream: PUInt8; len: Integer ); cdecl;
function audio_decode_frame(aCodecCtx : TAVCodecContext; aAudio_buf : PUInt8; buf_size: integer): integer;
var
singleton_MusicFFMpeg : IAudioPlayback = nil;
var
audioq : TPacketQueue;
quit : integer = 0;
// faudio_buf : array[ 0 .. 0 ] of byte; //pUInt8{$ifndef fpc};{$else} = nil;{$endif}
// audio_buf : array[ 0 .. AVCODEC_MAX_AUDIO_FRAME_SIZE ] of byte; //pUInt8{$ifndef fpc};{$else} = nil;{$endif}
type
Taudiobuff = array[ 0 .. AVCODEC_MAX_AUDIO_FRAME_SIZE-1 ] of byte;
PAudioBuff = ^Taudiobuff;
implementation
uses
{$IFDEF LAZARUS}
lclintf,
libc,
{$ENDIF}
// URecord,
UIni,
UMain,
UThemes;
//var
// singleton_MusicFFMpeg : IAudioPlayback = nil;
const
RecordSystem = 1;
SDL_AUDIO_BUFFER_SIZE = 1024;
type
TMPModes = (mpNotReady, mpStopped, mpPlaying, mpRecording, mpSeeking, mpPaused, mpOpen);
const
ModeStr: array[TMPModes] of string = ('Not ready', 'Stopped', 'Playing', 'Recording', 'Seeking', 'Paused', 'Open');
type
TAudio_ffMpeg = class( TInterfacedObject, IAudioPlayback )
private
BassStart: hStream;
BassBack: hStream;
BassSwoosh: hStream;
BassChange: hStream;
BassOption: hStream;
BassClick: hStream;
BassDrum: hStream;
BassHihat: hStream;
BassClap: hStream;
BassShuffle: hStream;
//Custom Sounds
CustomSounds: array of TCustomSoundEntry;
Loaded: boolean;
Loop: boolean;
fHWND: THandle;
function find_stream_ids( const aFormatCtx : PAVFormatContext; Out aFirstVideoStream, aFirstAudioStream : integer ): boolean;
public
// Bass: hStream;
constructor create();
function GetName: String;
procedure InitializePlayback;
procedure SetVolume(Volume: integer);
procedure SetMusicVolume(Volume: integer);
procedure SetLoop(Enabled: boolean);
function Open(Name: string): boolean; // true if succeed
procedure Rewind;
procedure MoveTo(Time: real);
procedure Play;
procedure Pause; //Pause Mod
procedure Stop;
procedure Close;
function Finished: boolean;
function Length: real;
function getPosition: real;
procedure PlayStart;
procedure PlayBack;
procedure PlaySwoosh;
procedure PlayChange;
procedure PlayOption;
procedure PlayClick;
procedure PlayDrum;
procedure PlayHihat;
procedure PlayClap;
procedure PlayShuffle;
procedure StopShuffle;
// procedure CaptureStart;
// procedure CaptureStop;
// procedure CaptureCard(RecordI, PlayerLeft, PlayerRight: byte);
procedure StopCard(Card: byte);
function LoadSoundFromFile(var hStream: hStream; Name: string): boolean;
//Equalizer
function GetFFTData: TFFTData;
//Custom Sounds
function LoadCustomSound(const Filename: String): Cardinal;
procedure PlayCustomSound(const Index: Cardinal );
end;
constructor TAudio_ffMpeg.create();
begin
// writeln( 'UVideo_FFMpeg - av_register_all' );
av_register_all;
end;
function TAudio_ffMpeg.find_stream_ids( const aFormatCtx : PAVFormatContext; Out aFirstVideoStream, aFirstAudioStream : integer ): boolean;
var
i : integer;
st : pAVStream;
begin
// Find the first video stream
aFirstAudioStream := -1;
aFirstVideoStream := -1;
i := 0;
while ( i < aFormatCtx.nb_streams ) do
begin
// writeln( ' aFormatCtx.streams[i] : ' + inttostr( i ) );
st := aFormatCtx.streams[i];
if(st.codec.codec_type = CODEC_TYPE_VIDEO ) AND
(aFirstVideoStream < 0) THEN
begin
// writeln( 'Found Video Stream' );
aFirstVideoStream := i;
end;
if ( st.codec.codec_type = CODEC_TYPE_AUDIO ) AND
( aFirstAudioStream < 0) THEN
begin
// writeln( 'Found Audio Stream' );
aFirstAudioStream := i;
end;
inc( i );
end; // while
result := (aFirstAudioStream > -1) OR
(aFirstVideoStream > -1) ; // Didn't find any streams stream
end;
function TAudio_ffMpeg.GetName: String;
begin
result := 'FFMpeg';
end;
procedure TAudio_ffMpeg.InitializePlayback;
var
// Pet: integer;
S: integer;
begin
// LoadSoundFromFile(BassStart, SoundPath + 'Green Day - American Idiot.mp3');
(*
LoadSoundFromFile(BassStart, SoundPath + 'Common start.mp3');
LoadSoundFromFile(BassBack, SoundPath + 'Common back.mp3');
LoadSoundFromFile(BassSwoosh, SoundPath + 'menu swoosh.mp3');
LoadSoundFromFile(BassChange, SoundPath + 'select music change music 50.mp3');
LoadSoundFromFile(BassOption, SoundPath + 'option change col.mp3');
LoadSoundFromFile(BassClick, SoundPath + 'rimshot022b.mp3');
LoadSoundFromFile(BassDrum, SoundPath + 'bassdrumhard076b.mp3');
LoadSoundFromFile(BassHihat, SoundPath + 'hihatclosed068b.mp3');
LoadSoundFromFile(BassClap, SoundPath + 'claps050b.mp3');
*)
// LoadSoundFromFile(BassShuffle, SoundPath + 'Shuffle.mp3');
// Log.BenchmarkEnd(4);
// Log.LogBenchmark('--> Loading Sounds', 4);
end;
procedure TAudio_ffMpeg.SetVolume(Volume: integer);
begin
//Old Sets Wave Volume
//BASS_SetVolume(Volume);
//New: Sets Volume only for this Application
// TODO : jb_linux replace with something other than bass
(*
BASS_SetConfig(BASS_CONFIG_GVOL_SAMPLE, Volume);
BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, Volume);
BASS_SetConfig(BASS_CONFIG_GVOL_MUSIC, Volume);
*)
end;
procedure TAudio_ffMpeg.SetMusicVolume(Volume: Integer);
begin
//Max Volume Prevention
if Volume > 100 then
Volume := 100;
if Volume < 0 then
Volume := 0;
//Set Volume
// TODO : jb_linux replace with something other than bass
// BASS_ChannelSetAttributes (Bass, -1, Volume, -101);
end;
procedure TAudio_ffMpeg.SetLoop(Enabled: boolean);
begin
Loop := Enabled;
end;
function TAudio_ffMpeg.Open(Name: string): boolean;
begin
Loaded := false;
if FileExists(Name) then
begin
// TODO : jb_linux replace with something other than bass
// Bass := Bass_StreamCreateFile(false, pchar(Name), 0, 0, 0);
Loaded := true;
//Set Max Volume
// SetMusicVolume (100);
end;
Result := Loaded;
end;
procedure TAudio_ffMpeg.Rewind;
begin
if Loaded then
begin
end;
end;
procedure TAudio_ffMpeg.MoveTo(Time: real);
var
bytes: integer;
begin
// TODO : jb_linux replace with something other than bass
// bytes := BASS_ChannelSeconds2Bytes(Bass, Time);
// BASS_ChannelSetPosition(Bass, bytes);
end;
procedure TAudio_ffMpeg.Play;
begin
(*
// TODO : jb_linux replace with something other than bass
if Loaded then
begin
if Loop then
BASS_ChannelPlay(Bass, True); // start from beginning... actually bass itself does not loop, nor does this TAudio_ffMpeg Class
BASS_ChannelPlay(Bass, False); // for setting position before playing
end;
*)
end;
procedure TAudio_ffMpeg.Pause; //Pause Mod
begin
(*
// TODO : jb_linux replace with something other than bass
if Loaded then begin
BASS_ChannelPause(Bass); // Pauses Song
end;
*)
end;
procedure TAudio_ffMpeg.Stop;
begin
// Bass_ChannelStop(Bass);
end;
procedure TAudio_ffMpeg.Close;
begin
// Bass_StreamFree(Bass);
end;
function TAudio_ffMpeg.Length: real;
var
bytes: integer;
begin
Result := 60;
(*
// TODO : jb_linux replace with something other than bass
bytes := BASS_ChannelGetLength(Bass);
Result := BASS_ChannelBytes2Seconds(Bass, bytes);
*)
end;
function TAudio_ffMpeg.getPosition: real;
var
bytes: integer;
begin
Result := 0;
(*
// TODO : jb_linux replace with something other than bass
bytes := BASS_ChannelGetPosition(BASS);
Result := BASS_ChannelBytes2Seconds(BASS, bytes);
*)
end;
function TAudio_ffMpeg.Finished: boolean;
begin
Result := false;
(*
// TODO : jb_linux replace with something other than bass
if BASS_ChannelIsActive(BASS) = BASS_ACTIVE_STOPPED then
begin
Result := true;
end;
*)
end;
procedure TAudio_ffMpeg.PlayStart;
begin
// LoadSoundFromFile(BassStart, SoundPath + 'foo fighters - best of you.mp3');
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassStart, True);
end;
procedure TAudio_ffMpeg.PlayBack;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassBack, True);// then
end;
procedure TAudio_ffMpeg.PlaySwoosh;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassSwoosh, True);
end;
procedure TAudio_ffMpeg.PlayChange;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassChange, True);
end;
procedure TAudio_ffMpeg.PlayOption;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassOption, True);
end;
procedure TAudio_ffMpeg.PlayClick;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassClick, True);
end;
procedure TAudio_ffMpeg.PlayDrum;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassDrum, True);
end;
procedure TAudio_ffMpeg.PlayHihat;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassHihat, True);
end;
procedure TAudio_ffMpeg.PlayClap;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassClap, True);
end;
procedure TAudio_ffMpeg.PlayShuffle;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelPlay(BassShuffle, True);
end;
procedure TAudio_ffMpeg.StopShuffle;
begin
// TODO : jb_linux replace with something other than bass
// BASS_ChannelStop(BassShuffle);
end;
procedure TAudio_ffMpeg.StopCard(Card: byte);
begin
// TODO : jb_linux replace with something other than bass
// BASS_RecordSetDevice(Card);
// BASS_RecordFree;
end;
function audio_decode_frame(aCodecCtx : TAVCodecContext; aAudio_buf : PUInt8; buf_size: integer): integer;
var
pkt : TAVPacket;
audio_pkt_data : pchar;//PUInt8 = nil;
audio_pkt_size : integer;
len1 ,
data_size : integer;
begin
result := -1;
(*
{$ifdef win32}
FillChar(pkt, sizeof(TAVPacket), #0);
{$else}
memset(@pkt, 0, sizeof(TAVPacket)); // todo : jb memset
{$endif}
audio_pkt_data := nil;
audio_pkt_size := 0;
while true do
begin
while ( audio_pkt_size > 0 ) do
begin
// writeln( 'got audio packet' );
data_size := buf_size;
len1 := -1;
if aAudio_buf <> nil then
begin
len1 := avcodec_decode_audio(@aCodecCtx, Pointer( aAudio_buf ), data_size, audio_pkt_data, audio_pkt_size); // Todo.. should be avcodec_decode_audio2 but this wont link on my ubuntu box.
end;
// writeln('avcodec_decode_audio : ' + inttostr( len1 ));
if(len1 < 0) then
begin
//* if error, skip frame */
writeln( 'Skip audio frame' );
audio_pkt_size := 0;
break;
end;
audio_pkt_data := audio_pkt_data + len1;
audio_pkt_size := audio_pkt_size + len1;
if (data_size <= 0) then
begin
//* No data yet, get more frames */
continue;
end;
//* We have data, return it and come back for more later */
result := data_size;
exit;
end;
if ( pkt.data <> nil ) then
av_free_packet( pkt );
if ( quit <> 0 ) then
begin
result := -1;
exit;
end;
if (packet_queue_get(audioq, pkt, 1) < 0) then
begin
result := -1;
exit;
end;
audio_pkt_data := pchar( pkt.data );
audio_pkt_size := pkt.size;
// writeln( 'Audio Packet Size - ' + inttostr(audio_pkt_size) );
end; *)
end;
procedure audio_callback( userdata: Pointer; stream: PUInt8; len: Integer );
var
audio_buf_index : cardinal; // static unsigned int audio_buf_index = 0;
audio_buf_size : cardinal; // static unsigned int audio_buf_size = 0;
audio_size ,
len1 : integer;
aCodecCtx : TAVCodecContext;
lSrc : pointer;
// this is used to emulate ...... static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
lAudio_buf_data : Taudiobuff; // This created the memory we need
laudio_buf : PAudioBuff; // this makes it easy to work with.. since its the pointer to that memeory everywhere
begin
laudio_buf := @lAudio_buf_data ;
aCodecCtx := pAVCodecContext(userdata)^;
audio_size := -1;
audio_buf_index := 0;
audio_buf_size := 0;
while (len > 0) do
begin
if (audio_buf_index >= audio_buf_size) then
begin
// We have already sent all our data; get more */
audio_size := audio_decode_frame(aCodecCtx, pUInt8( laudio_buf ), sizeof(Taudiobuff));
writeln( 'audio_decode_frame : ' + inttostr( audio_size ) + ' / ' + inttostr( sizeof(Taudiobuff) ) );
if(audio_size > 0) then
begin
audio_buf_size := audio_size;
end
else
begin
// If error, output silence */
// audio_buf_size := 1024; // arbitrary?
// {$ifdef win32}
// FillChar(laudio_buf, audio_buf_size, #0);
// {$else}
// memset(laudio_buf, 0, audio_buf_size); // todo : jb memset
// {$endif}
writeln( 'Silence' );
end;
audio_buf_index := 0; // Todo : jb - SegFault ?
end;
len1 := audio_buf_size - audio_buf_index;
if (len1 > len) then
len1 := len;
lSrc := PUInt8( integer( laudio_buf ) + audio_buf_index );
{$ifdef WIN32}
CopyMemory(stream, lSrc , len1);
{$else}
memcpy(stream, lSrc , len1);
{$endif}
len := len - len1;
stream^ := stream^ + len1;
audio_buf_index := audio_buf_index + len1;
end;
end;
function TAudio_ffMpeg.LoadSoundFromFile(var hStream: hStream; Name: string): boolean;
var
L : Integer;
pFormatCtx : PAVFormatContext;
lVidStreamID ,
lAudStreamID : Integer;
aCodecCtx : pAVCodecContext;
wanted_spec ,
spec : TSDL_AudioSpec;
lAudioStream : pAVStream;
aCodec : pAVCodec;
i : integer;
packet : TAVPacket;
event : TSDL_Event;
begin
result := false;
if FileExists(Name) then
begin
writeln('Loading Sound: "' + Name + '"', 'LoadSoundFromFile');
// Open video file
if (av_open_input_file(pFormatCtx, pchar(Name), nil, 0, nil) > 0) then
exit;
// Retrieve stream information
if (av_find_stream_info(pFormatCtx)<0) then
exit;
dump_format(pFormatCtx, 0, pchar(Name), 0);
if not find_stream_ids( pFormatCtx, lVidStreamID, lAudStreamID ) then
exit;
// writeln( 'done searching for stream ids' );
if lAudStreamID > -1 then
begin
// writeln( 'Audio Stream ID is : '+ inttostr( lAudStreamID ) );
lAudioStream := pFormatCtx.streams[lAudStreamID];
aCodecCtx := lAudioStream.codec;
// Set audio settings from codec info
wanted_spec.freq := aCodecCtx.sample_rate;
wanted_spec.format := AUDIO_S16SYS;
wanted_spec.channels := aCodecCtx.channels;
wanted_spec.silence := 0;
wanted_spec.samples := SDL_AUDIO_BUFFER_SIZE;
wanted_spec.callback := audio_callback;
wanted_spec.userdata := aCodecCtx;
end;
if (SDL_OpenAudio(@wanted_spec, @spec) < 0) then
begin
writeln('SDL_OpenAudio: '+SDL_GetError());
exit
end;
// writeln( 'SDL opened audio device' );
aCodec := avcodec_find_decoder(aCodecCtx.codec_id);
if (aCodec = nil) then
begin
writeln('Unsupported codec!');
exit;
end;
avcodec_open(aCodecCtx, aCodec);
// writeln( 'Opened the codec' );
packet_queue_init( audioq );
SDL_PauseAudio(0);
// writeln( 'SDL_PauseAudio' );
i := 0;
while (av_read_frame(pFormatCtx, packet) >= 0) do
begin
// writeln( 'ffmpeg - av_read_frame' );
if (packet.stream_index = lAudStreamID ) then
begin
// writeln( 'packet_queue_put' );
packet_queue_put(audioq, packet);
end
else
begin
av_free_packet(packet);
end;
// Free the packet that was allocated by av_read_frame
SDL_PollEvent(@event);
(*
if event.type_ = SDL_QUIT the
begin
quit := 1;
SDL_Quit();
end
else
break;
*)
end;
writeln( 'Done filling buffer' );
// halt(0);
// Close the codec
// avcodec_close(aCodecCtx);
// Close the video file
// av_close_input_file(pFormatCtx);
(*
try
// TODO : jb_linux replace with something other than bass
hStream := BASS_StreamCreateFile(False, pchar(Name), 0, 0, 0);
//Add CustomSound
L := High(CustomSounds) + 1;
SetLength (CustomSounds, L + 1);
CustomSounds[L].Filename := Name;
CustomSounds[L].Handle := hStream;
except
Log.LogError('Failed to open using BASS', 'LoadSoundFromFile');
end;
*)
end
else
begin
writeln('Sound not found: "' + Name + '"', 'LoadSoundFromFile');
exit;
end;
end;
procedure packet_queue_init(var aPacketQueue : TPacketQueue );
begin
{$ifdef win32}
FillChar(aPacketQueue, sizeof(TPacketQueue), #0);
{$else}
memset(@aPacketQueue, 0, sizeof(TPacketQueue));
{$endif}
aPacketQueue.mutex := SDL_CreateMutex();
aPacketQueue.cond := SDL_CreateCond();
end;
function packet_queue_put(var aPacketQueue : TPacketQueue; var AVPacket : TAVPacket): integer;
var
pkt1 : pAVPacketList;
begin
result := -1;
// writeln( 'TAudio_ffMpeg.packet_queue_put' );
if av_dup_packet(@AVPacket) < 0 then
exit;
pkt1 := av_malloc(sizeof(TAVPacketList));
if (pkt1 = nil) then
exit;
pkt1.pkt := AVPacket;
pkt1.next := nil;
SDL_LockMutex( aPacketQueue.mutex );
try
if (aPacketQueue.last_pkt = nil) then
aPacketQueue.first_pkt := pkt1
else
aPacketQueue.last_pkt.next := pkt1;
aPacketQueue.last_pkt := pkt1;
inc( aPacketQueue.nb_packets );
aPacketQueue.size := aPacketQueue.size + pkt1.pkt.size;
SDL_CondSignal(aPacketQueue.cond);
finally
SDL_UnlockMutex( aPacketQueue.mutex );
end;
result := 0;
end;
function packet_queue_get(var aPacketQueue : TPacketQueue; var AVPacket : TAVPacket; block : integer ): integer;
var
pkt1 : pAVPacketList;
begin
result := -1;
// writeln( 'packet_queue_get' );
SDL_LockMutex(aPacketQueue.mutex);
try
while true do
begin
if (quit <> 0) then
exit;
pkt1 := aPacketQueue.first_pkt;
if ( pkt1 <> nil ) then
begin
aPacketQueue.first_pkt := pkt1.next;
if (aPacketQueue.first_pkt = nil ) then
aPacketQueue.last_pkt := nil;
dec(aPacketQueue.nb_packets);
aPacketQueue.size := aPacketQueue.size - pkt1.pkt.size;
AVPacket := pkt1.pkt;
av_free(pkt1);
result := 1;
break;
end
else
if (block = 0) then
begin
result := 0;
break;
end
else
begin
SDL_CondWait(aPacketQueue.cond, aPacketQueue.mutex);
end;
end;
finally
SDL_UnlockMutex(aPacketQueue.mutex);
end;
end;
//Equalizer
function TAudio_ffMpeg.GetFFTData: TFFTData;
var
Data: TFFTData;
begin
//Get Channel Data Mono and 256 Values
// BASS_ChannelGetData(Bass, @Result, BASS_DATA_FFT512);
end;
function TAudio_ffMpeg.LoadCustomSound(const Filename: String): Cardinal;
var
S: hStream;
I: Integer;
F: String;
begin
(*
//Search for Sound in already loaded Sounds
F := UpperCase(SoundPath + FileName);
For I := 0 to High(CustomSounds) do
begin
if (UpperCase(CustomSounds[I].Filename) = F) then
begin
Result := I;
Exit;
end;
end;
if LoadSoundFromFile(S, SoundPath + Filename) then
Result := High(CustomSounds)
else
Result := 0;
*)
end;
procedure TAudio_ffMpeg.PlayCustomSound(const Index: Cardinal );
begin
// if Index <= High(CustomSounds) then
// BASS_ChannelPlay(CustomSounds[Index].Handle, True);
end;
{*
Sorry guys... this is my mess :(
Im going to try and get ffmpeg to handle audio playback ( at least for linux )
and Im going to implement it nicly along side BASS, in TAudio_ffMpeg ( where I can )
http://www.dranger.com/ffmpeg/ffmpeg.html
http://www.dranger.com/ffmpeg/ffmpegtutorial_all.html
http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
*}
{*
function TAudio_ffMpeg.FFMPeg_StreamCreateFile(abool : boolean; aFileName : pchar ): THandle;
var
lFormatCtx : PAVFormatContext;
begin
(*
if(SDL_OpenAudio(&wanted_spec, &spec) < 0)
begin
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
writeln( 'SDL_OpenAudio' );
exit;
end;
*)
(*
if ( av_open_input_file( lFormatCtx, aFileName, NULL, 0, NULL ) <> 0 )
begin
writeln( 'Unable to open file '+ aFileName );
exit;
end;
// Retrieve stream information
if ( av_find_stream_info(pFormatCtx) < 0 )
begin
writeln( 'Unable to Retrieve stream information' );
exit;
end;
*)
end; *}
initialization
singleton_MusicFFMpeg := TAudio_ffMpeg.create();
writeln( 'UAudio_Bass - Register Playback' );
AudioManager.add( IAudioPlayback( singleton_MusicFFMpeg ) );
finalization
AudioManager.Remove( IAudioPlayback( singleton_MusicFFMpeg ) );
end.
|