AudioThread.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <windows.h>
  2. #include "main.h"
  3. #include "../Winamp/wa_ipc.h"
  4. #include "config.h"
  5. #include "api__in_wave.h"
  6. #include <shlwapi.h>
  7. #include "VirtualIO.h"
  8. // {B6CB4A7C-A8D0-4c55-8E60-9F7A7A23DA0F}
  9. static const GUID playbackConfigGroupGUID =
  10. { 0xb6cb4a7c, 0xa8d0, 0x4c55, { 0x8e, 0x60, 0x9f, 0x7a, 0x7a, 0x23, 0xda, 0xf } };
  11. HANDLE audioThread = INVALID_HANDLE_VALUE;
  12. DWORD WINAPI ThreadProcedure( void *data );
  13. #define kill events[0]
  14. #define running events[1]
  15. HANDLE stopped = 0;
  16. HANDLE events[ 2 ] = { 0 };
  17. static size_t bufferSize = 0;
  18. static char *audioBuffer = 0;
  19. static int frameSize = 0; // in bytes
  20. static int endOfFile = 0;
  21. static int bits = 0;
  22. static SF_INFO info;
  23. static void *reader = 0;
  24. int CalcBits()
  25. {
  26. switch (info.format & SF_FORMAT_SUBMASK)
  27. {
  28. case SF_FORMAT_DOUBLE:
  29. return 64;
  30. case SF_FORMAT_PCM_32:
  31. case SF_FORMAT_FLOAT:
  32. return 32;
  33. case SF_FORMAT_DWVW_24:
  34. case SF_FORMAT_PCM_24:
  35. return 24;
  36. case SF_FORMAT_DPCM_16:
  37. case SF_FORMAT_DWVW_16:
  38. case SF_FORMAT_PCM_16:
  39. return 16;
  40. //case SF_FORMAT_PCM_S8: // cut, because 8bits is assumed unsigned
  41. case SF_FORMAT_PCM_U8:
  42. case SF_FORMAT_DPCM_8:
  43. return 8;
  44. default: return 16;
  45. }
  46. }
  47. int CalcBitRate( const SF_INFO *info )
  48. {
  49. switch ( info->format & SF_FORMAT_SUBMASK )
  50. {
  51. case SF_FORMAT_PCM_S8:
  52. case SF_FORMAT_PCM_U8:
  53. case SF_FORMAT_DPCM_8:
  54. return MulDiv( 8 * info->channels, info->samplerate, 1000 );
  55. case SF_FORMAT_DWVW_12:
  56. return MulDiv( 12 * info->channels, info->samplerate, 1000 );
  57. case SF_FORMAT_DPCM_16:
  58. case SF_FORMAT_DWVW_16:
  59. case SF_FORMAT_PCM_16:
  60. return MulDiv( 16 * info->channels, info->samplerate, 1000 );
  61. case SF_FORMAT_DWVW_24:
  62. case SF_FORMAT_PCM_24:
  63. return MulDiv( 24 * info->channels, info->samplerate, 1000 );
  64. case SF_FORMAT_PCM_32:
  65. case SF_FORMAT_FLOAT:
  66. return MulDiv( 32 * info->channels, info->samplerate, 1000 );
  67. case SF_FORMAT_DOUBLE:
  68. return MulDiv( 64 * info->channels, info->samplerate, 1000 );
  69. case SF_FORMAT_G721_32:
  70. return 32;
  71. case SF_FORMAT_G723_24:
  72. return 24;
  73. case SF_FORMAT_G723_40:
  74. return 40;
  75. case SF_FORMAT_MS_ADPCM:
  76. case SF_FORMAT_VOX_ADPCM:
  77. case SF_FORMAT_IMA_ADPCM:
  78. return MulDiv( 4 * info->channels, info->samplerate, 1000 );
  79. default:
  80. return MulDiv( 16 * info->channels, info->samplerate, 1000 );
  81. }
  82. }
  83. void CALLBACK APCSeek( ULONG_PTR p_data )
  84. {
  85. endOfFile = 0;
  86. int time_in_ms = (int)p_data;
  87. int frames = MulDiv( time_in_ms, info.samplerate, 1000 ); // TODO: verify calculation
  88. sf_seek( sndFile, frames, SEEK_SET );
  89. plugin.outMod->Flush( time_in_ms );
  90. }
  91. void CALLBACK APCPause( ULONG_PTR p_data )
  92. {
  93. int pause = (int)p_data;
  94. if ( pause )
  95. ResetEvent( running );
  96. else
  97. SetEvent( running );
  98. plugin.outMod->Pause( !!pause );
  99. }
  100. void CALLBACK APCStart( ULONG_PTR p_data )
  101. {
  102. endOfFile = 0;
  103. const wchar_t *file = (const wchar_t *)p_data;
  104. info.format = 0;
  105. if ( PathIsURLW( file ) )
  106. {
  107. reader = CreateReader( file );
  108. if ( reader )
  109. sndFile = sf_open_virtual( &httpIO, SFM_READ, &info, reader );
  110. }
  111. else // It's a local file
  112. {
  113. sndFile = sf_wchar_open( file, SFM_READ, &info );
  114. }
  115. if ( !sndFile )
  116. {
  117. if ( WaitForSingleObject( kill, 200 ) == WAIT_TIMEOUT )
  118. PostMessage( plugin.hMainWindow, WM_WA_MPEG_EOF, 0, 0 );
  119. return;
  120. }
  121. currentSongLength = MulDiv( (int)info.frames, 1000, info.samplerate ); // TODO: is this correct?
  122. switch ( info.format & SF_FORMAT_SUBMASK )
  123. {
  124. case SF_FORMAT_FLOAT:
  125. case SF_FORMAT_DOUBLE:
  126. sf_command( sndFile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE );
  127. break;
  128. }
  129. bits = CalcBits();
  130. size_t config_bits = AGAVE_API_CONFIG->GetUnsigned( playbackConfigGroupGUID, L"bits", 16 );
  131. if ( config_bits == 16 && config_bits < (size_t)bits )
  132. bits = (int)config_bits;
  133. if ( bits < 16 && config_upsample8bit )
  134. bits = 16;
  135. int latency = plugin.outMod->Open( info.samplerate, info.channels, bits, -1, -1 );
  136. if ( latency < 0 )
  137. {
  138. sf_close( sndFile );
  139. if ( reader )
  140. DestroyReader( reader );
  141. reader = 0;
  142. sndFile = NULL;
  143. if ( WaitForSingleObject( kill, 200 ) == WAIT_TIMEOUT )
  144. PostMessage( plugin.hMainWindow, WM_WA_MPEG_EOF, 0, 0 );
  145. return;
  146. }
  147. frameSize = ( bits / 8 ) * info.channels;
  148. plugin.SAVSAInit( latency, info.samplerate );
  149. plugin.VSASetInfo( info.samplerate, info.channels );
  150. int bitrate = CalcBitRate( &info );
  151. plugin.SetInfo( bitrate, info.samplerate / 1000, info.channels, 1 );
  152. plugin.is_seekable = info.seekable;
  153. plugin.outMod->SetVolume( volume );
  154. plugin.outMod->SetPan( pan );
  155. size_t requiredBufferSize = 576 * frameSize * 2; // * 2 for dsp bullshit
  156. if ( requiredBufferSize > bufferSize )
  157. {
  158. free( audioBuffer );
  159. audioBuffer = (char *)calloc( requiredBufferSize, sizeof( char ) );
  160. bufferSize = requiredBufferSize;
  161. }
  162. SetEvent( running );
  163. }
  164. void CALLBACK APCStop( ULONG_PTR p_data )
  165. {
  166. if ( sndFile )
  167. {
  168. sf_close( sndFile );
  169. if ( reader )
  170. DestroyReader( reader );
  171. reader = 0;
  172. sndFile = NULL;
  173. }
  174. ResetEvent( running );
  175. SetEvent( stopped );
  176. }
  177. void Kill()
  178. {
  179. SetEvent( kill );
  180. }
  181. void AudioThreadInit()
  182. {
  183. DWORD id;
  184. kill = CreateEvent( NULL, TRUE, FALSE, NULL );
  185. running = CreateEvent( NULL, TRUE, FALSE, NULL );
  186. stopped = CreateEvent( NULL, FALSE, FALSE, NULL );
  187. audioThread = CreateThread( NULL, 0, ThreadProcedure, 0, 0, &id );
  188. if ( audioThread )
  189. SetThreadPriority( audioThread, (int)AGAVE_API_CONFIG->GetInt( playbackConfigGroupGUID, L"priority", THREAD_PRIORITY_HIGHEST ) );
  190. }
  191. void AudioThreadQuit()
  192. {
  193. free( audioBuffer );
  194. audioBuffer = 0;
  195. bufferSize = 0;
  196. CloseHandle( running );
  197. running = 0;
  198. CloseHandle( kill );
  199. kill = 0;
  200. CloseHandle( stopped );
  201. stopped = 0;
  202. CloseHandle( audioThread );
  203. audioThread = 0;
  204. }
  205. DWORD WINAPI ThreadProcedure( void *data )
  206. {
  207. DWORD result;
  208. sf_count_t framesRead;
  209. while ( true )
  210. {
  211. result = WaitForMultipleObjectsEx( 2, events, FALSE, INFINITE, TRUE );
  212. if ( result == WAIT_OBJECT_0 ) // kill thread
  213. return 0;
  214. if ( result == ( WAIT_OBJECT_0 + 1 ) )
  215. {
  216. if ( endOfFile ) // if we hit the end of file previously ...
  217. {
  218. if ( plugin.outMod->IsPlaying() ) // see if we're still going
  219. SleepEx( 10, TRUE ); // sleep for a bit
  220. else // yay done playing
  221. {
  222. PostMessage( plugin.hMainWindow, WM_WA_MPEG_EOF, 0, 0 ); // tell winamp we're stopped
  223. // don't shut down completely yet (mpegeof will trigger a call to stop)
  224. ResetEvent( running ); // but we can at least sit in waitformultipleobjects ...
  225. }
  226. }
  227. else if ( plugin.outMod->CanWrite() > ( 576 * frameSize ) )
  228. {
  229. switch ( bits )
  230. {
  231. case 16:
  232. framesRead = sf_readf_short( sndFile, (short *)audioBuffer, 576 );
  233. break;
  234. case 32:
  235. framesRead = sf_readf_int( sndFile, (int *)audioBuffer, 576 );
  236. break;
  237. default:
  238. framesRead = sf_read_raw( sndFile, (int *)audioBuffer, 576 * frameSize ) / frameSize;
  239. break;
  240. }
  241. if ( framesRead == 0 )
  242. {
  243. endOfFile = 1;
  244. plugin.outMod->Write( NULL, 0 );
  245. }
  246. else
  247. {
  248. framesRead = plugin.dsp_dosamples( (short *)audioBuffer, (int)framesRead, bits, info.channels, info.samplerate );
  249. if ( framesRead >= 576 )
  250. {
  251. int timestamp = plugin.outMod->GetWrittenTime();
  252. plugin.SAAddPCMData( (char *)audioBuffer, info.channels, bits, timestamp );
  253. plugin.VSAAddPCMData( (char *)audioBuffer, info.channels, bits, timestamp );
  254. }
  255. plugin.outMod->Write( audioBuffer, (int)framesRead * frameSize );
  256. }
  257. }
  258. else
  259. {
  260. SleepEx( 10, TRUE );
  261. }
  262. }
  263. }
  264. }