rixplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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[OPL_SAMPLERATE / 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 = OPL_SAMPLERATE / 70;
  182. if( gpRixPlayer->resampler[0] ) {
  183. unsigned int samples_written = 0;
  184. short *tempBuf = (short*)_alloca(64 * 2 * sizeof(short));
  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
  257. # if USE_SURROUNDOPL
  258. # if USE_DEMUOPL
  259. gpRixPlayer->opl = new CSurroundopl(new CDemuopl(OPL_SAMPLERATE, true, false),
  260. new CDemuopl(OPL_SAMPLERATE, true, false), true);
  261. # elif USE_KEMUOPL
  262. gpRixPlayer->opl = new CSurroundopl(new CKemuopl(OPL_SAMPLERATE, true, false),
  263. new CKemuopl(OPL_SAMPLERATE, true, false), true);
  264. # else
  265. gpRixPlayer->opl = new CSurroundopl(new CEmuopl(OPL_SAMPLERATE, true, false),
  266. new CEmuopl(OPL_SAMPLERATE, true, false), true);
  267. # endif
  268. # if OPL_SAMPLERATE != PAL_SAMPLE_RATE && USE_SSRC
  269. resampler_init();
  270. gpRixPlayer->resampler[0] = resampler_create();
  271. gpRixPlayer->resampler[1] = resampler_create();
  272. resampler_set_quality( gpRixPlayer->resampler[0], RESAMPLER_QUALITY_MAX );
  273. resampler_set_quality( gpRixPlayer->resampler[1], RESAMPLER_QUALITY_MAX );
  274. resampler_set_rate( gpRixPlayer->resampler[0], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  275. resampler_set_rate( gpRixPlayer->resampler[1], OPL_SAMPLERATE / (double)PAL_SAMPLE_RATE );
  276. # else
  277. gpRixPlayer->resampler[0] = 0;
  278. gpRixPlayer->resampler[1] = 0;
  279. # endif
  280. # else
  281. # if USE_DEMUOPL
  282. gpRixPlayer->opl = new CDemuopl(OPL_SAMPLERATE, true, true);
  283. # elif USE_KEMUOPL
  284. gpRixPlayer->opl = new CKemuopl(OPL_SAMPLERATE, true, true);
  285. # else
  286. gpRixPlayer->opl = new CEmuopl(OPL_SAMPLERATE, true, true);
  287. # endif
  288. # endif
  289. #else
  290. # if USE_DEMUOPL
  291. gpRixPlayer->opl = new CDemuopl(OPL_SAMPLERATE, true, false);
  292. # elif USE_KEMUOPL
  293. gpRixPlayer->opl = new CKemuopl(OPL_SAMPLERATE, true, false);
  294. # else
  295. gpRixPlayer->opl = new CEmuopl(OPL_SAMPLERATE, true, false);
  296. # endif
  297. #endif
  298. if (gpRixPlayer->opl == NULL)
  299. {
  300. delete gpRixPlayer;
  301. return -1;
  302. }
  303. gpRixPlayer->rix = new CrixPlayer(gpRixPlayer->opl);
  304. if (gpRixPlayer->rix == NULL)
  305. {
  306. delete gpRixPlayer->opl;
  307. delete gpRixPlayer;
  308. return -1;
  309. }
  310. //
  311. // Load the MKF file.
  312. //
  313. if (!gpRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  314. {
  315. delete gpRixPlayer->rix;
  316. delete gpRixPlayer->opl;
  317. delete gpRixPlayer;
  318. gpRixPlayer = NULL;
  319. return -2;
  320. }
  321. //
  322. // Success.
  323. //
  324. gpRixPlayer->iCurrentMusic = -1;
  325. gpRixPlayer->dwEndFadeTime = 0;
  326. gpRixPlayer->pos = NULL;
  327. gpRixPlayer->fLoop = FALSE;
  328. gpRixPlayer->fNextLoop = FALSE;
  329. return 0;
  330. }
  331. VOID
  332. RIX_Shutdown(
  333. VOID
  334. )
  335. /*++
  336. Purpose:
  337. Shutdown the RIX player subsystem.
  338. Parameters:
  339. None.
  340. Return value:
  341. None.
  342. --*/
  343. {
  344. if (gpRixPlayer != NULL)
  345. {
  346. delete gpRixPlayer->rix;
  347. delete gpRixPlayer->opl;
  348. delete gpRixPlayer;
  349. gpRixPlayer = NULL;
  350. }
  351. }
  352. VOID
  353. RIX_Play(
  354. INT iNumRIX,
  355. BOOL fLoop,
  356. FLOAT flFadeTime
  357. )
  358. /*++
  359. Purpose:
  360. Start playing the specified music.
  361. Parameters:
  362. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  363. [IN] fLoop - Whether the music should be looped or not.
  364. [IN] flFadeTime - the fade in/out time when switching music.
  365. Return value:
  366. None.
  367. --*/
  368. {
  369. //
  370. // Check for NULL pointer.
  371. //
  372. if (gpRixPlayer == NULL)
  373. {
  374. return;
  375. }
  376. //
  377. // Stop the current CD music.
  378. //
  379. SOUND_PlayCDA(-1);
  380. gpRixPlayer->opl->init();
  381. if ( gpRixPlayer->resampler[0] )
  382. resampler_clear( gpRixPlayer->resampler[0] );
  383. if ( gpRixPlayer->resampler[1] )
  384. resampler_clear( gpRixPlayer->resampler[1] );
  385. // gpRixPlayer->rix->rewind(iNumRIX);
  386. DWORD t = SDL_GetTicks();
  387. gpRixPlayer->fNextLoop = fLoop;
  388. if (iNumRIX == gpRixPlayer->iCurrentMusic && !g_fNoMusic)
  389. {
  390. return;
  391. }
  392. gpRixPlayer->iNextMusic = iNumRIX;
  393. gpRixPlayer->dwStartFadeTime = t;
  394. gpRixPlayer->dwEndFadeTime = t + (DWORD)(flFadeTime * 1000) / 2;
  395. gpRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  396. }