-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISndStreamWinAmp.cpp
1008 lines (763 loc) · 21.3 KB
/
ISndStreamWinAmp.cpp
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
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 2001 - 2007 Albert L. Faber
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#include "ISndStreamWinAmp.h"
#include "Config.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
INITTRACE( _T( "ISndStreamWinAmp" ) );
static int g_nOutSampleRate = 0;
static int g_nNumChannels = 0;
static int g_nBitsPerSample = 0;
static int g_nBufferLenMs = 0;
static int g_nPreBufferMs = 0;
static int g_nBitRate = 128000;
volatile static int g_bOutOpened = FALSE;
static Out_Module g_OutModule;
static char g_lpszOutModuleName[] = { "CDex WAV Output Emulator" };
static In_Module myInModule;
volatile static int g_bFinished = FALSE;
volatile static int g_bInitialized = FALSE;
CEvent g_eFinished( TRUE, TRUE );
#define WAPI_FIFO_NUM_BUFFERS 32
#define WAPI_FIFO_BUFFER_SIZE 8200
typedef struct WAPI_FIFO_STRUCT_TAG
{
INT nBytesWritten;
BYTE pbtBuffer[ WAPI_FIFO_BUFFER_SIZE ];
} WAPI_FIFO;
WAPI_FIFO g_pFifo[WAPI_FIFO_NUM_BUFFERS];
CMutex g_mFifoAcces;
CEvent g_eFifoNotFull( TRUE, TRUE );
CEvent g_eFifoNotEmpty( TRUE, TRUE );
int g_nFifoFreeBuffers = WAPI_FIFO_NUM_BUFFERS;
int g_nFifoReadIdx = 0;
int g_nFifoWriteIdx = 0;
int g_nFifoTotalBytes = 0;
vector <WINAMPPLUGINPROP> gs_vWinAmpProps;
CUString g_strWinampExt( _T("") );
typedef In_Module* (*WINAMPGETINMODULE2)( void );
typedef Out_Module* (*WINAMPGETOUTMODULE)( void );
CUString GetWinampFileNameExt()
{
return g_strWinampExt;
}
void WinampPlugInFinished()
{
g_bFinished = TRUE;
g_eFinished.SetEvent();
}
int GetNumWinampPlugins()
{
return gs_vWinAmpProps.size();
}
CUString GetWinampPluginInfo( int i )
{
CUString strRet( _W("") );
if ( i< (int)gs_vWinAmpProps.size() )
{
strRet = CUString( gs_vWinAmpProps[i].pModule->description, CP_UTF8);
}
return strRet;
}
CUString GetWinampPluginFileName( int i )
{
CUString strRet( _T("") );
if ( i< (int)gs_vWinAmpProps.size() )
{
strRet = gs_vWinAmpProps[i].strFileName;
}
return strRet;
}
void WinampPluginConfigure( int i, HWND hWnd )
{
if ( i< (int)gs_vWinAmpProps.size() )
{
gs_vWinAmpProps[i].pModule->Config( hWnd );
}
}
void WinampPluginAbout( int i, HWND hWnd )
{
if ( i< (int)gs_vWinAmpProps.size() )
{
gs_vWinAmpProps[i].pModule->About( hWnd );
}
}
void TestOutputPlug( HWND hWnd )
{
WINAMPPLUGINPROP tmpProp;
WIN32_FIND_DATA sFF;
HANDLE hFind = NULL;
CUString strPluginDir( g_config.GetAppPath() + _W( "\\Plugins\\out_*.dll" ) );
LTRACE2( _T( "Scanning for plugins using mask %s" ) , strPluginDir );
CUStringConvert strCnv;
hFind = FindFirstFile( strCnv.ToT( strPluginDir ), &sFF);
g_strWinampExt = _T("");
// setup the Output module Window Handle
g_OutModule.hMainWindow = hWnd;
// setup the Output Module Instance Handle
g_OutModule.hDllInstance = NULL;
while ( NULL != hFind &&
hFind != INVALID_HANDLE_VALUE )
{
if ( ! ( sFF.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
CUStringConvert strCnv;
CUString strFullName( g_config.GetAppPath() + _W( "\\Plugins\\" ) + CUString( sFF.cFileName ) );
LTRACE2( _T( "Getting plugin %s" ), strCnv.ToT( strFullName ));
tmpProp.hDll = NULL;
tmpProp.hDll = CDexLoadLibrary( strFullName );
if ( tmpProp.hDll )
{
WINAMPGETOUTMODULE pGetMod = (WINAMPGETOUTMODULE)
GetProcAddress( tmpProp.hDll,
"winampGetOutModule" );
// call "winampGetInModule" DLL function
Out_Module* pOutModule = pGetMod();
}
// scan for next file
if ( NULL != hFind && !FindNextFile( hFind, &sFF) )
{
FindClose( hFind );
hFind = NULL;
}
}
}
}
void InitWinAmpPlugins( HWND hWnd )
{
WINAMPPLUGINPROP tmpProp;
WIN32_FIND_DATA sFF;
HANDLE hFind = NULL;
CUString strPluginDir( g_config.GetAppPath() + _W( "\\Plugins\\in_*.dll" ) );
LTRACE2( _T( "Scanning for plugins using mask %s"), strPluginDir );
CUStringConvert strCnv;
hFind = FindFirstFile( strCnv.ToT( strPluginDir ), &sFF);
g_strWinampExt = _W("");
// setup the Output module Window Handle
g_OutModule.hMainWindow = hWnd;
// setup the Output Module Instance Handle
g_OutModule.hDllInstance = NULL;
while ( NULL != hFind &&
hFind != INVALID_HANDLE_VALUE )
{
if ( ! ( sFF.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
CUStringConvert strCnv;
CUString strFullName( g_config.GetAppPath() + _W( "\\Plugins\\" ) + CUString( sFF.cFileName ) );
LTRACE2( _T( "Getting plugin %s" ), strFullName );
tmpProp.hDll = NULL;
tmpProp.hDll = CDexLoadLibrary( strFullName );
if ( tmpProp.hDll )
{
WINAMPGETINMODULE2 pGetMod = (WINAMPGETINMODULE2)
GetProcAddress( tmpProp.hDll,
"winampGetInModule2" );
// call "winampGetInModule2" DLL function
tmpProp.pModule = pGetMod();
// set the file name
tmpProp.strFileName = sFF.cFileName;
// setup window handler
tmpProp.pModule->hMainWindow = hWnd;
tmpProp.pModule->hDllInstance = tmpProp.hDll;
tmpProp.pModule->Init( );
tmpProp.strExt = CUString( tmpProp.pModule->FileExtensions, CP_UTF8 );
if ( 0 != tmpProp.pModule->UsesOutputPlug )
{
if ( !g_strWinampExt.IsEmpty() )
g_strWinampExt += _W( ";" );
g_strWinampExt+= tmpProp.strExt;
LTRACE2( _T( "Adding plugin %s to list" ), sFF.cFileName );
gs_vWinAmpProps.push_back( tmpProp );
}
else
{
tmpProp.pModule->Quit( );
LTRACE2( _T( "Plugin (%s) NOT added, since it does not use the output plugin" ), sFF.cFileName );
}
}
// scan for next file
if ( NULL != hFind && !FindNextFile( hFind, &sFF) )
{
FindClose( hFind );
hFind = NULL;
}
}
}
}
void DeInitWinAmpPlugins( )
{
int i = 0;
// free all the input DLLs
for ( i=0; i < (int)gs_vWinAmpProps.size(); i++ )
{
WINAMPPLUGINPROP tmpProp = gs_vWinAmpProps[i];
if ( NULL != tmpProp.hDll )
{
tmpProp.pModule->Quit( );
FreeLibrary( tmpProp.hDll );
}
}
// remove all elements
gs_vWinAmpProps.clear();
}
// CONSTRUCTOR
ISndStreamWinAmp::ISndStreamWinAmp()
:ISndStream()
{
SetFileExtention( _T( "wav" ) );
m_dwSamples=0;
m_dwTotalFileSize = 0;
m_dwCurrentFilePos = 0;
memset( &g_OutModule, 0 , sizeof( g_OutModule ) );
}
// DESRUCTOR
ISndStreamWinAmp::~ISndStreamWinAmp()
{
CloseStream();
}
void SetInfo( int bitrate, int srate, int stereo, int synched )
{
LTRACE2( _T( "SetInfo %d %d %d %d" ), bitrate, srate, stereo, synched );
if ( bitrate > 0 )
{
if ( bitrate < 1000 )
{
g_nBitRate = bitrate * 1000;
}
else
{
g_nBitRate = bitrate ;
}
}
}
void WMIN_SAVSAInit( int maxlatency_in_ms, int srate )
{
LTRACE2( _T( "SAVSAInit %d %d" ), maxlatency_in_ms, srate );
// call once in Play(). maxlatency_in_ms should be the value returned from outMod->Open()
}
// call after opening audio device with max latency in ms and samplerate
void WMIN_SAVSADeInit()
{
LTRACE2( _T( "SAVSADeInit" ) );
// call in Stop()
}
// simple vis supplying mode
void SAAddPCMData( void *PCMData, int nch, int bps, int timestamp )
{
LTRACE2( _T( "SAAddPCMData %08x %d %d %d" ), PCMData, nch, bps, timestamp );
}
// advanced vis supplying mode, only use if you're cool. Use SAAddPCMData for most stuff.
int SAGetMode()
{
LTRACE3( _T( "SAGetMode" ) );
// gets csa (the current type (4=ws,2=osc,1=spec))
// use when calling SAAdd()
return 1;
}
void SAAdd( void *data, int timestamp, int csa)
{
LTRACE3( _T( "SAAdd %08x %d %d" ), data, timestamp, csa );
}
// vis stuff (plug-in)
// simple vis supplying mode
void VSAAddPCMData( void *PCMData, int nch, int bps, int timestamp )
{
LTRACE3( _T( "VSAAddPCMData %08x %d %d %d" ), PCMData, nch, bps, timestamp );
// sets the vis data directly from PCM data
// quick and easy way to get vis working :)
// needs at least 576 samples :)
}
// advanced vis supplying mode, only use if you're cool. Use VSAAddPCMData for most stuff.
int VSAGetMode( int *specNch, int *waveNch )
{
LTRACE3( _T( "VSAAddPCMData %08x %08x " ), specNch, waveNch );
// use to figure out what to give to VSAAdd
return 0;
}
void VSAAdd( void *data, int timestamp )
{
// filled in by winamp, called by plug-in
LTRACE3( _T( "VSAAdd %08x %d" ), data, timestamp );
}
// call this in Play() to tell the vis plug-ins the current output params.
void VSASetInfo(int nch, int srate)
{
LTRACE3( _T( "VSASetInfo %d %d"), nch, srate );
}
// dsp plug-in processing:
// (filled in by winamp, called by input plug)
// returns 1 if active (which means that the number of samples returned by dsp_dosamples
// could be greater than went in.. Use it to estimate if you'll have enough room in the
// output buffer
int dsp_isactive()
{
LTRACE3( _T( "dsp_isactive" ) );
return 0;
}
// returns number of samples to output. This can be as much as twice numsamples.
// be sure to allocate enough buffer for samples, then.
int dsp_dosamples( short int *samples, int numsamples, int bps, int nch, int srate )
{
LTRACE3( _T( "dsp_dosamples %08x %d %d %d %d"), samples, numsamples, bps, nch, srate );
return 0;
}
// eq stuff
// 0-64 each, 31 is +0, 0 is +12, 63 is -12. Do nothing to ignore.
void EQSet( int on, char data[10], int preamp)
{
LTRACE2( _T( "EQSet %d %d %d %d" ), on, data[0], data[1], data[2] );
}
void OutConfig( HWND hWnd )
{
LTRACE2( _T( "OutConfig %d" ), hWnd );
}
void OutAbout( HWND hWnd )
{
LTRACE2( _T( "OutAbout %d" ), hWnd );
}
void OutInit()
{
LTRACE2( _T( "OutInit" ) );
}
void OutQuit()
{
LTRACE2( _T( "OutQuit" ) );
}
int OutOpen( int samplerate, int numchannels, int bitspersamp, int bufferlenms, int prebufferms )
{
LTRACE2( _T( "OutOpen %d %d %d %d %d" ),
samplerate,
numchannels,
bitspersamp,
bufferlenms,
prebufferms );
g_nOutSampleRate = samplerate;
g_nNumChannels = numchannels;
g_nBitsPerSample = bitspersamp;
g_nBufferLenMs = bufferlenms;
g_nPreBufferMs = prebufferms;
g_bOutOpened = TRUE;
return 500;
}
void OutClose()
{
LTRACE2( _T( "OutClose" ) );
}
int OutWrite(char *buf, int len)
{
int nReturn = 1;
// 0 on success. Len == bytes to write (<= 8192 always). buf is straight audio data.
// 1 returns not able to write (yet). Non-blocking, always.
LTRACE2( _T( "OutWrite %08x %d" ), buf, len );
// block for the MOD pluging ?
// wait till buffer is available, or check if were finished yet
HANDLE pWaitEvents[2] = { (HANDLE)g_eFifoNotFull, (HANDLE)g_eFinished };
int nEventIdx = WaitForMultipleObjects( 2, pWaitEvents, FALSE, INFINITE ) - WAIT_OBJECT_0 ;
// check if write buffer has triggered this event
if ( 0 == nEventIdx )
{
// were sure that the write buffer is available !
ASSERT( len <= WAPI_FIFO_BUFFER_SIZE );
if ( 0 == len || NULL == buf )
{
}
else
{
memcpy( g_pFifo[g_nFifoWriteIdx].pbtBuffer, buf, len );
g_pFifo[g_nFifoWriteIdx].nBytesWritten = len;
}
// increase FIFO write index ( no FIFO lock need, since were
// the only ones that are increasing this parameter
g_nFifoWriteIdx = ( ( g_nFifoWriteIdx + 1 ) % WAPI_FIFO_NUM_BUFFERS );
nReturn = 0;
}
else
{
g_pFifo[g_nFifoWriteIdx].nBytesWritten = 0;
g_nFifoWriteIdx = ( ( g_nFifoWriteIdx + 1 ) % WAPI_FIFO_NUM_BUFFERS );
nReturn = 1;
}
// lock access to FIFO parameters
g_mFifoAcces.Lock();
g_nFifoFreeBuffers--;
ASSERT( g_nFifoFreeBuffers >= 0 );
if ( 0 == nReturn )
{
g_nFifoTotalBytes+= len;
}
LTRACE( _T( "WriteBuffer %d FreeBuffers on Write :%d bytes in fifo %d" ),
g_nFifoWriteIdx,
g_nFifoFreeBuffers,
g_nFifoTotalBytes );
if ( 1 == g_nFifoFreeBuffers )
{
LTRACE( _T( "Reset g_eFifoNotFull event" ) );
g_eFifoNotFull.ResetEvent();
}
if ( (WAPI_FIFO_NUM_BUFFERS -1 ) == g_nFifoFreeBuffers )
{
LTRACE( _T( "Set g_eFifoNotEmpty event" ) );
g_eFifoNotEmpty.SetEvent();
}
// unlock access to FIFO parameters
g_mFifoAcces.Unlock();
LTRACE2( _T( "OutWrite Writing %d bytes to buffer %d freebuffers %d" ),
len,
g_nFifoWriteIdx,
g_nFifoFreeBuffers );
::Sleep(0);
return nReturn;
}
int OutCanWrite()
{
int nReturn = 0;
if ( g_nFifoFreeBuffers > 1 )
nReturn = WAPI_FIFO_BUFFER_SIZE;
LTRACE2( _T( "CanWrite returns %d " ), nReturn );
return nReturn;
}
void OutSetVolume( int volume )
{
LTRACE2( _T( "OutSetVolume set to %d " ), volume );
// volume is 0-255
}
void OutSetPan( int pan )
{
LTRACE2( _T( "OutSetPan set to %d" ), pan );
// pan is -128 to 128
}
void OutFlush( int t )
{
LTRACE2( _T( "OutFlush" ) );
// flushes buffers and restarts output at time t (in ms)
// (used for seeking)
}
int OutGetOutputTime()
{
LTRACE3( _T( "OutGetOutputTime" ) );
return 0;
}
int OutGetWrittenTime()
{
LTRACE3( _T( "OutGetWrittenTime" ) );
return 0;
// returns time written in MS (used for synching up vis stuff)
}
int OutIsPlaying()
{
int nReturn = 0;
if ( g_nFifoFreeBuffers != WAPI_FIFO_NUM_BUFFERS )
{
nReturn = 1;
}
LTRACE2( _T( "OutIsPlaying %d" ), nReturn );
// non0 if output is still going or if data in buffers waiting to be
// written (i.e. closing while IsPlaying() returns 1 would truncate the song
return nReturn;
}
int OutPause(int pause) // returns previous pause state
{
return 1-pause;
}
void ISndStreamWinAmp::FlushFIFO( )
{
// lock access to FIFO parameters
g_mFifoAcces.Lock();
g_nFifoFreeBuffers = WAPI_FIFO_NUM_BUFFERS;
g_nFifoReadIdx = 0;
g_nFifoWriteIdx = 0;
g_eFifoNotFull.SetEvent( );
g_eFifoNotEmpty.ResetEvent( );
g_nFifoTotalBytes = 0;
// unlock access to FIFO parameters
g_mFifoAcces.Unlock();
}
BOOL ISndStreamWinAmp::OpenStream( const CUString& strFileName )
{
DWORD i = 0;
BOOL bFound = FALSE;
LTRACE2( _T( "ISndStreamWinAmp::OpenStream( %s )" ), strFileName );
int nPos = 0;
CUString strExt( _T( "wav" ) );
g_bFinished = FALSE;
g_bInitialized = FALSE;
g_bOutOpened = FALSE;
g_eFinished.ResetEvent();
FlushFIFO( );
nPos = strFileName.ReverseFind('.');
if ( nPos >= 0 )
{
strExt = strFileName.Right( strFileName.GetLength() - nPos - 1 );
strExt.MakeLower();
}
// Set the buffer size
SetBufferSize( 9000 );
SetFileName( strFileName );
int nRet = 0;
// loop through the available plugings
for ( i=0; i < gs_vWinAmpProps.size(); i++ )
{
m_WinAmpProp = gs_vWinAmpProps[i];
// m_WinAmpProp.pModule->Init( );
// check if the module can handle the file format
if ( ( nRet = m_WinAmpProp.pModule->IsOurFile( (LPSTR)GetDosFileName( strFileName ) ) ) )
{
bFound = TRUE;
break;
}
/*
if ( ( 0 == strExt.CompareNoCase( _T( "flac" ) ) ) && ( 0 == strExt.CompareNoCase( m_WinAmpProp.strExt ) ) )
{
bFound = TRUE;
break;
}
*/
CUString strPluginExt( m_WinAmpProp.pModule->FileExtensions, CP_UTF8 );
strPluginExt.MakeLower();
// check first on extention
if ( strPluginExt.Find( strExt ) >=0 )
{
bFound = TRUE;
break;
}
// m_WinAmpProp.pModule->Quit( );
}
if ( !bFound )
{
LTRACE2( _T( "ISndStreamWinAmp::OpenStream Failed, no proper plugin found!" ) );
return FALSE;
}
if ( 0 == m_WinAmpProp.pModule->UsesOutputPlug )
{
LTRACE2( _T( "ISndStreamWinAmp::OpenStream; Failed, does not use Output Plugin " ) );
return FALSE;
}
LTRACE2( _T( "ISndStreamWinAmp::OpenStream found proper plugin ( %s )" ) , m_WinAmpProp.pModule->description );
// setup proper function calls
m_WinAmpProp.pModule->SetInfo = SetInfo;
m_WinAmpProp.pModule->SAAddPCMData = SAAddPCMData;
m_WinAmpProp.pModule->SAGetMode = SAGetMode;
m_WinAmpProp.pModule->SAAdd = SAAdd;
m_WinAmpProp.pModule->VSAAddPCMData = VSAAddPCMData;
m_WinAmpProp.pModule->VSAGetMode = VSAGetMode;
m_WinAmpProp.pModule->VSAAdd = VSAAdd;
m_WinAmpProp.pModule->VSASetInfo = VSASetInfo;
m_WinAmpProp.pModule->dsp_isactive = dsp_isactive;
m_WinAmpProp.pModule->dsp_dosamples = dsp_dosamples;
m_WinAmpProp.pModule->EQSet = EQSet;
m_WinAmpProp.pModule->SAVSADeInit = WMIN_SAVSADeInit;
m_WinAmpProp.pModule->SAVSAInit = WMIN_SAVSAInit;
m_WinAmpProp.pModule->outMod = &g_OutModule;
g_OutModule.version = OUT_VER;
g_OutModule.description = g_lpszOutModuleName;
g_OutModule.id = 33;
g_OutModule.Config = OutConfig;
g_OutModule.About = OutAbout;
g_OutModule.Init = OutInit;
g_OutModule.Quit = OutQuit;
g_OutModule.Open = OutOpen;
g_OutModule.Close = OutClose;
g_OutModule.Write = OutWrite;
g_OutModule.CanWrite = OutCanWrite;
g_OutModule.IsPlaying = OutIsPlaying;
g_OutModule.Pause = OutPause;
g_OutModule.SetVolume = OutSetVolume;
g_OutModule.SetPan = OutSetPan;
g_OutModule.Flush = OutFlush;
g_OutModule.GetOutputTime = OutGetOutputTime;
g_OutModule.GetWrittenTime = OutGetWrittenTime;
SetChannels( 2 );
SetSampleRate( 44100 );
SetBitRate( 176000 * 8 );
// m_dwSamples=wfInfo.samples;
m_dwTotalFileSize = 1000;
m_dwCurrentFilePos = 0;
// start the decoding thread
if ( 0 != m_WinAmpProp.pModule->Play( (LPSTR)GetDosFileName( strFileName ) ) )
{
return FALSE;
}
// Wait till output device has been opened by the
// input plugin
while ( FALSE == g_bOutOpened )
{
::Sleep( 5 );
}
SetChannels( g_nNumChannels );
SetSampleRate( g_nOutSampleRate );
SetBitRate( g_nBitRate );
g_bInitialized = TRUE;
// return Success
return TRUE;
}
DWORD ISndStreamWinAmp::GetTotalTime()
{
// return total time in ms
return m_WinAmpProp.pModule->GetLength();
}
DWORD ISndStreamWinAmp::GetCurrentTime()
{
// return total time in ms
return m_WinAmpProp.pModule->GetOutputTime();
}
DWORD ISndStreamWinAmp::Read(PBYTE pbData,DWORD dwNumBytes)
{
int nReturn = 0;
LONG lastcount = 0;
LTRACE3( _T( "ISndStreamWinAmp::Read dwNumBytes %d CurrentFilePos %d TotalFileSize %d " ),
dwNumBytes,
m_dwCurrentFilePos,
m_dwTotalFileSize );
m_dwCurrentFilePos+= dwNumBytes;
int nCurrentTime = m_WinAmpProp.pModule->GetOutputTime();
int nTotalTime = m_WinAmpProp.pModule->GetLength();
if ( nTotalTime )
{
int t = g_nOutSampleRate*g_nNumChannels;
int l = 0;
int ms = g_nFifoTotalBytes;
if ( t )
{
l=ms%t;
ms /= t;
ms *= 1000;
ms += (l*1000)/t;
if (g_nBitsPerSample == 16)
ms/=2;
}
else
{
ms = 0;
}
SetPercent( (INT)( (double) (nCurrentTime - ms) * 100.0 / nTotalTime ) );
}
else
{
SetPercent( 0 );
}
// wait for incoming data
HANDLE pWaitEvents[2] = { (HANDLE)g_eFifoNotEmpty, (HANDLE)g_eFinished };
int nEventIdx = WaitForMultipleObjects( 2, pWaitEvents, FALSE, INFINITE ) - WAIT_OBJECT_0 ;
if ( 1 == nEventIdx )
{
nReturn = 0;
}
else
{
nReturn = g_pFifo[g_nFifoReadIdx].nBytesWritten;
memcpy( pbData, g_pFifo[g_nFifoReadIdx].pbtBuffer, nReturn );
LTRACE2( _T( "ISndStreamWinAmp::Read getting %d bytes from buffer %d (freebuffers = %d)" ),
g_pFifo[g_nFifoReadIdx].nBytesWritten,
g_nFifoReadIdx,
g_nFifoFreeBuffers
);
// lock access to FIFO parameters
g_mFifoAcces.Lock();
if ( g_nFifoFreeBuffers < WAPI_FIFO_NUM_BUFFERS )
{
g_nFifoFreeBuffers++;
}
if ( WAPI_FIFO_NUM_BUFFERS == g_nFifoFreeBuffers )
{
LTRACE( _T( "Reset g_eFifoNotEmpty Event" ) );
g_eFifoNotEmpty.ResetEvent();
}
if ( 2 == g_nFifoFreeBuffers )
{
LTRACE( _T( "Set g_eFifoNotFull Event" ) );
g_eFifoNotFull.SetEvent();
}
g_nFifoTotalBytes-= nReturn;
// unlock access to FIFO parameters
g_mFifoAcces.Unlock();
// increase FIFO read index pointer
g_nFifoReadIdx = ( ( g_nFifoReadIdx + 1 ) % WAPI_FIFO_NUM_BUFFERS );
LTRACE( _T( "FreeBuffers on Read :%d, bytes in fifo %d" ),
g_nFifoFreeBuffers,
g_nFifoTotalBytes );
}
// update the bitrate info
SetBitRate( g_nBitRate );
LTRACE2( _T( "ISndStreamWinAmp::Read returns %d" ), nReturn );
return nReturn;
}
BOOL ISndStreamWinAmp::CloseStream()
{
if ( FALSE == g_bFinished && TRUE == g_bInitialized )
{
g_bFinished = TRUE;
m_WinAmpProp.pModule->Stop( );
}
SetPercent( 0 );
g_bInitialized = FALSE;
return TRUE;
}
__int64 ISndStreamWinAmp::Seek( __int64 ddwOffset, UINT nFrom )
{
LONG lSeekPosition = 0;
int nPercent=(int)ddwOffset;
int nCurrentTime = m_WinAmpProp.pModule->GetOutputTime();
int nTotalTime = m_WinAmpProp.pModule->GetLength();
switch( nFrom )
{
case SEEK_PERCENT:
if (nPercent<0 ) nPercent= 0;
if (nPercent>99) nPercent= 99;
lSeekPosition = (LONG) ( (int)ddwOffset / 100.0 * nTotalTime );
break;
case SEEK_TIME:
lSeekPosition = (int)ddwOffset;
break;
default:
ASSERT( FALSE );
break;
}
if ( FALSE == g_bFinished && TRUE == g_bInitialized )
{
FlushFIFO();
m_WinAmpProp.pModule->SetOutputTime( lSeekPosition );
m_WinAmpProp.pModule->UnPause( );
}
return lSeekPosition;
}
void ISndStreamWinAmp::Flush()
{
if ( FALSE == g_bFinished && TRUE == g_bInitialized )
{
m_WinAmpProp.pModule->Pause();
FlushFIFO();
m_WinAmpProp.pModule->UnPause();
}
}
void ISndStreamWinAmp::InfoBox( HWND hWnd )
{