rixplay.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. }
  141. return;
  142. }
  143. volume = (INT)(volume * (1.0f - (t - gpRixPlayer->dwStartFadeTime) /
  144. (FLOAT)(gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime)));
  145. break;
  146. }
  147. }
  148. if (gpRixPlayer->iCurrentMusic <= 0)
  149. {
  150. //
  151. // No current playing music
  152. //
  153. return;
  154. }
  155. //
  156. // Fill the buffer with sound data
  157. //
  158. while (len > 0)
  159. {
  160. if (gpRixPlayer->pos == NULL ||
  161. gpRixPlayer->pos - gpRixPlayer->buf >= (int)sizeof(gpRixPlayer->buf))
  162. {
  163. gpRixPlayer->pos = gpRixPlayer->buf;
  164. if (!gpRixPlayer->rix->update())
  165. {
  166. if (!gpRixPlayer->fLoop)
  167. {
  168. //
  169. // Not loop, simply terminate the music
  170. //
  171. gpRixPlayer->iCurrentMusic = -1;
  172. return;
  173. }
  174. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  175. if (!gpRixPlayer->rix->update())
  176. {
  177. //
  178. // Something must be wrong
  179. //
  180. gpRixPlayer->iCurrentMusic = -1;
  181. return;
  182. }
  183. }
  184. int sample_count = PAL_SAMPLE_RATE / 70;
  185. if( gpRixPlayer->resampler[0] ) {
  186. unsigned int samples_written = 0;
  187. short tempBuf[64 * PAL_CHANNELS]; // hard code on resampler defination
  188. short *finalBuf = (short*)gpRixPlayer->buf;
  189. while ( sample_count )
  190. {
  191. int to_write = resampler_get_free_count( gpRixPlayer->resampler[0] );
  192. if ( to_write )
  193. {
  194. gpRixPlayer->opl->update( tempBuf, to_write );
  195. for ( int i = 0; i < to_write; i++ )
  196. {
  197. resampler_write_sample( gpRixPlayer->resampler[0], tempBuf[i * 2] );
  198. resampler_write_sample( gpRixPlayer->resampler[1], tempBuf[i * 2 + 1] );
  199. }
  200. }
  201. /* if ( !lanczos_resampler_ready( m_resampler ) ) break; */ /* We assume that by filling the input buffer completely every pass, there will always be samples ready. */
  202. //int ls = resampler_get_sample( gpRixPlayer->resampler[0] );
  203. //int rs = resampler_get_sample( gpRixPlayer->resampler[1] );
  204. //resampler_remove_sample( gpRixPlayer->resampler[0] );
  205. //resampler_remove_sample( gpRixPlayer->resampler[1] );
  206. finalBuf[samples_written++] = resampler_get_and_remove_sample(gpRixPlayer->resampler[0]);
  207. finalBuf[samples_written++] = resampler_get_and_remove_sample(gpRixPlayer->resampler[1]);
  208. --sample_count;
  209. }
  210. // p_chunk.set_data_size( samples_written );
  211. // p_chunk.set_sample_rate( srate );
  212. // p_chunk.set_channels( 2, audio_chunk::channel_config_stereo );
  213. // p_chunk.set_sample_count( samples_written / 2 );
  214. //
  215. // audio_math::convert_from_int32( sample_buffer.get_ptr(), samples_written, p_chunk.get_data(), 1 << 8 );
  216. }else
  217. gpRixPlayer->opl->update((short *)(gpRixPlayer->buf), sample_count);
  218. }
  219. l = sizeof(gpRixPlayer->buf) - (gpRixPlayer->pos - gpRixPlayer->buf);
  220. if (len < l)
  221. {
  222. l = len;
  223. }
  224. //
  225. // Put audio data into buffer and adjust volume
  226. // WARNING: for signed 16-bit little-endian only
  227. //
  228. for (i = 0; i < (int)(l / sizeof(SHORT)); i++)
  229. {
  230. SHORT s = SWAP16((int)(*(SHORT *)(gpRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME);
  231. {
  232. *(SHORT *)(stream) = s;
  233. stream += sizeof(SHORT);
  234. }
  235. gpRixPlayer->pos += sizeof(SHORT);
  236. }
  237. len -= l;
  238. }
  239. stream -= oldlen;
  240. }
  241. INT
  242. RIX_Init(
  243. LPCSTR szFileName
  244. )
  245. /*++
  246. Purpose:
  247. Initialize the RIX player subsystem.
  248. Parameters:
  249. [IN] szFileName - Filename of the mus.mkf file.
  250. Return value:
  251. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  252. --*/
  253. {
  254. gpRixPlayer = new RIXPLAYER;
  255. if (gpRixPlayer == NULL)
  256. {
  257. return -1;
  258. }
  259. #if PAL_CHANNELS == 2 && !USE_SURROUNDOPL
  260. const bool opl_stereo = true;
  261. #else
  262. const bool opl_stereo = false;
  263. #endif
  264. #if USE_DEMUOPL
  265. typedef CDemuopl COpl;
  266. #elif USE_KEMUOPL
  267. typedef CKemuopl COpl;
  268. #else
  269. typedef CEmuopl COpl;
  270. #endif
  271. #if PAL_CHANNELS == 2 && USE_SURROUNDOPL
  272. gpRixPlayer->opl = new CSurroundopl(new COpl(OPL_SAMPLERATE, true, false),
  273. new COpl(OPL_SAMPLERATE, true, false), true);
  274. # if OPL_SAMPLERATE != PAL_SAMPLE_RATE && USE_RESAMPLER
  275. resampler_init();
  276. gpRixPlayer->resampler[0] = resampler_create();
  277. gpRixPlayer->resampler[1] = resampler_create();
  278. resampler_set_quality( gpRixPlayer->resampler[0], RESAMPLER_QUALITY_MAX );
  279. resampler_set_quality( gpRixPlayer->resampler[1], RESAMPLER_QUALITY_MAX );
  280. resampler_set_rate( gpRixPlayer->resampler[0], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  281. resampler_set_rate( gpRixPlayer->resampler[1], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  282. # else
  283. gpRixPlayer->resampler[0] = NULL;
  284. gpRixPlayer->resampler[1] = NULL;
  285. # endif
  286. #else
  287. gpRixPlayer->opl = new COpl(OPL_SAMPLERATE, true, opl_stereo);
  288. #endif
  289. if (gpRixPlayer->opl == NULL)
  290. {
  291. delete gpRixPlayer;
  292. return -1;
  293. }
  294. gpRixPlayer->rix = new CrixPlayer(gpRixPlayer->opl);
  295. if (gpRixPlayer->rix == NULL)
  296. {
  297. delete gpRixPlayer->opl;
  298. delete gpRixPlayer;
  299. return -1;
  300. }
  301. //
  302. // Load the MKF file.
  303. //
  304. if (!gpRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  305. {
  306. delete gpRixPlayer->rix;
  307. delete gpRixPlayer->opl;
  308. delete gpRixPlayer;
  309. gpRixPlayer = NULL;
  310. return -2;
  311. }
  312. //
  313. // Success.
  314. //
  315. gpRixPlayer->iCurrentMusic = -1;
  316. gpRixPlayer->dwEndFadeTime = 0;
  317. gpRixPlayer->pos = NULL;
  318. gpRixPlayer->fLoop = FALSE;
  319. gpRixPlayer->fNextLoop = FALSE;
  320. return 0;
  321. }
  322. VOID
  323. RIX_Shutdown(
  324. VOID
  325. )
  326. /*++
  327. Purpose:
  328. Shutdown the RIX player subsystem.
  329. Parameters:
  330. None.
  331. Return value:
  332. None.
  333. --*/
  334. {
  335. if (gpRixPlayer != NULL)
  336. {
  337. delete gpRixPlayer->rix;
  338. delete gpRixPlayer->opl;
  339. delete gpRixPlayer;
  340. gpRixPlayer = NULL;
  341. }
  342. }
  343. VOID
  344. RIX_Play(
  345. INT iNumRIX,
  346. BOOL fLoop,
  347. FLOAT flFadeTime
  348. )
  349. /*++
  350. Purpose:
  351. Start playing the specified music.
  352. Parameters:
  353. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  354. [IN] fLoop - Whether the music should be looped or not.
  355. [IN] flFadeTime - the fade in/out time when switching music.
  356. Return value:
  357. None.
  358. --*/
  359. {
  360. //
  361. // Check for NULL pointer.
  362. //
  363. if (gpRixPlayer == NULL)
  364. {
  365. return;
  366. }
  367. //
  368. // Stop the current CD music.
  369. //
  370. SOUND_PlayCDA(-1);
  371. DWORD t = SDL_GetTicks();
  372. gpRixPlayer->fNextLoop = fLoop;
  373. if (iNumRIX == gpRixPlayer->iCurrentMusic && !g_fNoMusic)
  374. {
  375. return;
  376. }
  377. gpRixPlayer->iNextMusic = iNumRIX;
  378. gpRixPlayer->dwStartFadeTime = t;
  379. gpRixPlayer->dwEndFadeTime = t + (DWORD)(flFadeTime * 1000) / 2;
  380. gpRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  381. }