rixplay.cpp 12 KB

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