rixplay.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* -*- mode: c++; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. //
  19. #include "rixplay.h"
  20. #define USE_SURROUNDOPL 1
  21. #define USE_DEMUOPL 1
  22. #define USE_KEMUOPL 0
  23. #define USE_RESAMPLER 1
  24. #define OPL_SAMPLERATE 49716
  25. #include "resampler.h"
  26. #include "adplug/opl.h"
  27. #include "adplug/emuopl.h"
  28. #if USE_KEMUOPL
  29. #include "adplug/kemuopl.h"
  30. #endif
  31. #if USE_DEMUOPL
  32. #include "adplug/demuopl.h"
  33. #endif
  34. #include "adplug/surroundopl.h"
  35. #include "adplug/rix.h"
  36. extern "C" BOOL g_fNoMusic;
  37. extern "C" INT g_iVolume;
  38. #include "sound.h"
  39. typedef struct tagRIXPLAYER
  40. {
  41. tagRIXPLAYER() : iCurrentMusic(-1) {}
  42. Copl *opl;
  43. CrixPlayer *rix;
  44. void *resampler[2];
  45. INT iCurrentMusic; // current playing music number
  46. INT iNextMusic; // the next music number to switch to
  47. DWORD dwStartFadeTime;
  48. DWORD dwEndFadeTime;
  49. enum { FADE_IN, FADE_OUT } FadeType; // fade in or fade out ?
  50. BOOL fLoop;
  51. BOOL fNextLoop;
  52. BYTE buf[PAL_SAMPLE_RATE / 70 * sizeof(short) * PAL_CHANNELS];
  53. LPBYTE pos;
  54. } RIXPLAYER, *LPRIXPLAYER;
  55. static LPRIXPLAYER gpRixPlayer = NULL;
  56. VOID
  57. RIX_FillBuffer(
  58. LPBYTE stream,
  59. INT len
  60. )
  61. /*++
  62. Purpose:
  63. Fill the background music into the sound buffer. Called by the SDL sound
  64. callback function only (sound.c: SOUND_FillAudio).
  65. Parameters:
  66. [OUT] stream - pointer to the stream buffer.
  67. [IN] len - Length of the buffer.
  68. Return value:
  69. None.
  70. --*/
  71. {
  72. INT i, l, oldlen, volume = SDL_MIX_MAXVOLUME / 2;
  73. UINT t = SDL_GetTicks();
  74. #ifdef __SYMBIAN32__
  75. volume = g_iVolume / 2;
  76. #endif
  77. oldlen = len;
  78. if (gpRixPlayer == NULL)
  79. {
  80. //
  81. // Not initialized
  82. //
  83. return;
  84. }
  85. //
  86. // fading in or fading out
  87. //
  88. if (gpRixPlayer->dwEndFadeTime > 0)
  89. {
  90. switch (gpRixPlayer->FadeType)
  91. {
  92. case RIXPLAYER::FADE_IN:
  93. if (t >= gpRixPlayer->dwEndFadeTime)
  94. {
  95. gpRixPlayer->dwEndFadeTime = 0;
  96. }
  97. else
  98. {
  99. volume = (INT)(volume * (t - gpRixPlayer->dwStartFadeTime) /
  100. (FLOAT)(gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime));
  101. }
  102. break;
  103. case RIXPLAYER::FADE_OUT:
  104. if (gpRixPlayer->iCurrentMusic == -1)
  105. {
  106. //
  107. // There is no current playing music. Just start playing the next one.
  108. //
  109. gpRixPlayer->iCurrentMusic = gpRixPlayer->iNextMusic;
  110. gpRixPlayer->fLoop = gpRixPlayer->fNextLoop;
  111. gpRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  112. gpRixPlayer->dwEndFadeTime = t +
  113. (gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime);
  114. gpRixPlayer->dwStartFadeTime = t;
  115. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  116. gpRixPlayer->opl->init();
  117. if (gpRixPlayer->resampler[0]) resampler_clear(gpRixPlayer->resampler[0]);
  118. if (gpRixPlayer->resampler[1]) resampler_clear(gpRixPlayer->resampler[1]);
  119. return;
  120. }
  121. else if (t >= gpRixPlayer->dwEndFadeTime)
  122. {
  123. if (gpRixPlayer->iNextMusic <= 0)
  124. {
  125. gpRixPlayer->iCurrentMusic = -1;
  126. gpRixPlayer->dwEndFadeTime = 0;
  127. }
  128. else
  129. {
  130. //
  131. // Fade to the next music
  132. //
  133. gpRixPlayer->iCurrentMusic = gpRixPlayer->iNextMusic;
  134. gpRixPlayer->fLoop = gpRixPlayer->fNextLoop;
  135. gpRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  136. gpRixPlayer->dwEndFadeTime = t +
  137. (gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime);
  138. gpRixPlayer->dwStartFadeTime = t;
  139. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  140. gpRixPlayer->opl->init();
  141. if (gpRixPlayer->resampler[0]) resampler_clear(gpRixPlayer->resampler[0]);
  142. if (gpRixPlayer->resampler[1]) resampler_clear(gpRixPlayer->resampler[1]);
  143. }
  144. return;
  145. }
  146. volume = (INT)(volume * (1.0f - (t - gpRixPlayer->dwStartFadeTime) /
  147. (FLOAT)(gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime)));
  148. break;
  149. }
  150. }
  151. if (gpRixPlayer->iCurrentMusic <= 0)
  152. {
  153. //
  154. // No current playing music
  155. //
  156. return;
  157. }
  158. //
  159. // Fill the buffer with sound data
  160. //
  161. while (len > 0)
  162. {
  163. if (gpRixPlayer->pos == NULL ||
  164. gpRixPlayer->pos - gpRixPlayer->buf >= (int)sizeof(gpRixPlayer->buf))
  165. {
  166. gpRixPlayer->pos = gpRixPlayer->buf;
  167. if (!gpRixPlayer->rix->update())
  168. {
  169. if (!gpRixPlayer->fLoop)
  170. {
  171. //
  172. // Not loop, simply terminate the music
  173. //
  174. gpRixPlayer->iCurrentMusic = -1;
  175. return;
  176. }
  177. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  178. if (!gpRixPlayer->rix->update())
  179. {
  180. //
  181. // Something must be wrong
  182. //
  183. gpRixPlayer->iCurrentMusic = -1;
  184. return;
  185. }
  186. }
  187. int sample_count = PAL_SAMPLE_RATE / 70;
  188. if( gpRixPlayer->resampler[0] ) {
  189. unsigned int samples_written = 0;
  190. short tempBuf[64 * PAL_CHANNELS]; // hard code on resampler defination
  191. short *finalBuf = (short*)gpRixPlayer->buf;
  192. while ( sample_count )
  193. {
  194. int to_write = resampler_get_free_count( gpRixPlayer->resampler[0] );
  195. if ( to_write )
  196. {
  197. gpRixPlayer->opl->update( tempBuf, to_write );
  198. for ( int i = 0; i < to_write; i++ )
  199. {
  200. resampler_write_sample( gpRixPlayer->resampler[0], tempBuf[i * 2] );
  201. resampler_write_sample( gpRixPlayer->resampler[1], tempBuf[i * 2 + 1] );
  202. }
  203. }
  204. /* if ( !lanczos_resampler_ready( m_resampler ) ) break; */ /* We assume that by filling the input buffer completely every pass, there will always be samples ready. */
  205. //int ls = resampler_get_sample( gpRixPlayer->resampler[0] );
  206. //int rs = resampler_get_sample( gpRixPlayer->resampler[1] );
  207. //resampler_remove_sample( gpRixPlayer->resampler[0] );
  208. //resampler_remove_sample( gpRixPlayer->resampler[1] );
  209. finalBuf[samples_written++] = resampler_get_and_remove_sample(gpRixPlayer->resampler[0]);
  210. finalBuf[samples_written++] = resampler_get_and_remove_sample(gpRixPlayer->resampler[1]);
  211. --sample_count;
  212. }
  213. // p_chunk.set_data_size( samples_written );
  214. // p_chunk.set_sample_rate( srate );
  215. // p_chunk.set_channels( 2, audio_chunk::channel_config_stereo );
  216. // p_chunk.set_sample_count( samples_written / 2 );
  217. //
  218. // audio_math::convert_from_int32( sample_buffer.get_ptr(), samples_written, p_chunk.get_data(), 1 << 8 );
  219. }else
  220. gpRixPlayer->opl->update((short *)(gpRixPlayer->buf), sample_count);
  221. }
  222. l = sizeof(gpRixPlayer->buf) - (gpRixPlayer->pos - gpRixPlayer->buf);
  223. if (len < l)
  224. {
  225. l = len;
  226. }
  227. //
  228. // Put audio data into buffer and adjust volume
  229. // WARNING: for signed 16-bit little-endian only
  230. //
  231. for (i = 0; i < (int)(l / sizeof(SHORT)); i++)
  232. {
  233. SHORT s = SWAP16((int)(*(SHORT *)(gpRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME);
  234. {
  235. *(SHORT *)(stream) = s;
  236. stream += sizeof(SHORT);
  237. }
  238. gpRixPlayer->pos += sizeof(SHORT);
  239. }
  240. len -= l;
  241. }
  242. stream -= oldlen;
  243. }
  244. INT
  245. RIX_Init(
  246. LPCSTR szFileName
  247. )
  248. /*++
  249. Purpose:
  250. Initialize the RIX player subsystem.
  251. Parameters:
  252. [IN] szFileName - Filename of the mus.mkf file.
  253. Return value:
  254. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  255. --*/
  256. {
  257. gpRixPlayer = new RIXPLAYER;
  258. if (gpRixPlayer == NULL)
  259. {
  260. return -1;
  261. }
  262. #if PAL_CHANNELS == 2 && !USE_SURROUNDOPL
  263. const bool opl_stereo = true;
  264. #else
  265. const bool opl_stereo = false;
  266. #endif
  267. #if USE_DEMUOPL
  268. typedef CDemuopl COpl;
  269. #elif USE_KEMUOPL
  270. typedef CKemuopl COpl;
  271. #else
  272. typedef CEmuopl COpl;
  273. #endif
  274. #if PAL_CHANNELS == 2 && USE_SURROUNDOPL
  275. gpRixPlayer->opl = new CSurroundopl(new COpl(OPL_SAMPLERATE, true, false),
  276. new COpl(OPL_SAMPLERATE, true, false), true);
  277. # if OPL_SAMPLERATE != PAL_SAMPLE_RATE && USE_RESAMPLER
  278. resampler_init();
  279. gpRixPlayer->resampler[0] = resampler_create();
  280. gpRixPlayer->resampler[1] = resampler_create();
  281. resampler_set_quality( gpRixPlayer->resampler[0], RESAMPLER_QUALITY_MAX );
  282. resampler_set_quality( gpRixPlayer->resampler[1], RESAMPLER_QUALITY_MAX );
  283. resampler_set_rate( gpRixPlayer->resampler[0], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  284. resampler_set_rate( gpRixPlayer->resampler[1], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  285. # else
  286. gpRixPlayer->resampler[0] = NULL;
  287. gpRixPlayer->resampler[1] = NULL;
  288. # endif
  289. #else
  290. gpRixPlayer->opl = new COpl(OPL_SAMPLERATE, true, opl_stereo);
  291. #endif
  292. if (gpRixPlayer->opl == NULL)
  293. {
  294. delete gpRixPlayer;
  295. return -1;
  296. }
  297. gpRixPlayer->rix = new CrixPlayer(gpRixPlayer->opl);
  298. if (gpRixPlayer->rix == NULL)
  299. {
  300. delete gpRixPlayer->opl;
  301. delete gpRixPlayer;
  302. return -1;
  303. }
  304. //
  305. // Load the MKF file.
  306. //
  307. if (!gpRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  308. {
  309. delete gpRixPlayer->rix;
  310. delete gpRixPlayer->opl;
  311. delete gpRixPlayer;
  312. gpRixPlayer = NULL;
  313. return -2;
  314. }
  315. //
  316. // Success.
  317. //
  318. gpRixPlayer->iCurrentMusic = -1;
  319. gpRixPlayer->dwEndFadeTime = 0;
  320. gpRixPlayer->pos = NULL;
  321. gpRixPlayer->fLoop = FALSE;
  322. gpRixPlayer->fNextLoop = FALSE;
  323. return 0;
  324. }
  325. VOID
  326. RIX_Shutdown(
  327. VOID
  328. )
  329. /*++
  330. Purpose:
  331. Shutdown the RIX player subsystem.
  332. Parameters:
  333. None.
  334. Return value:
  335. None.
  336. --*/
  337. {
  338. if (gpRixPlayer != NULL)
  339. {
  340. delete gpRixPlayer->rix;
  341. delete gpRixPlayer->opl;
  342. delete gpRixPlayer;
  343. gpRixPlayer = NULL;
  344. }
  345. }
  346. VOID
  347. RIX_Play(
  348. INT iNumRIX,
  349. BOOL fLoop,
  350. FLOAT flFadeTime
  351. )
  352. /*++
  353. Purpose:
  354. Start playing the specified music.
  355. Parameters:
  356. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  357. [IN] fLoop - Whether the music should be looped or not.
  358. [IN] flFadeTime - the fade in/out time when switching music.
  359. Return value:
  360. None.
  361. --*/
  362. {
  363. //
  364. // Check for NULL pointer.
  365. //
  366. if (gpRixPlayer == NULL)
  367. {
  368. return;
  369. }
  370. //
  371. // Stop the current CD music.
  372. //
  373. SOUND_PlayCDA(-1);
  374. DWORD t = SDL_GetTicks();
  375. gpRixPlayer->fNextLoop = fLoop;
  376. if (iNumRIX == gpRixPlayer->iCurrentMusic && !g_fNoMusic)
  377. {
  378. return;
  379. }
  380. gpRixPlayer->iNextMusic = iNumRIX;
  381. gpRixPlayer->dwStartFadeTime = t;
  382. gpRixPlayer->dwEndFadeTime = t + (DWORD)(flFadeTime * 1000) / 2;
  383. gpRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  384. }