rixplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <math.h>
  20. #include "global.h"
  21. #include "players.h"
  22. #include "sound.h"
  23. #include "resampler.h"
  24. #include "adplug/opl.h"
  25. #include "adplug/demuopl.h"
  26. #include "adplug/dbemuopl.h"
  27. #if PAL_HAS_MAME
  28. #include "adplug/emuopl.h"
  29. #endif
  30. #include "adplug/surroundopl.h"
  31. #include "adplug/rix.h"
  32. extern "C" BOOL g_fNoMusic;
  33. typedef struct tagRIXPLAYER :
  34. public MUSICPLAYER
  35. {
  36. Copl *opl;
  37. CrixPlayer *rix;
  38. void *resampler[2];
  39. BYTE buf[(PAL_MAX_SAMPLERATE + 69) / 70 * sizeof(short) * 2];
  40. LPBYTE pos;
  41. INT iCurrentMusic; // current playing music number
  42. INT iNextMusic; // the next music number to switch to
  43. DWORD dwStartFadeTime;
  44. INT iTotalFadeOutSamples;
  45. INT iTotalFadeInSamples;
  46. INT iRemainingFadeSamples;
  47. enum { NONE, FADE_IN, FADE_OUT } FadeType; // fade in or fade out ?
  48. BOOL fLoop;
  49. BOOL fNextLoop;
  50. BOOL fReady;
  51. } RIXPLAYER, *LPRIXPLAYER;
  52. static VOID
  53. RIX_FillBuffer(
  54. VOID *object,
  55. LPBYTE stream,
  56. INT len
  57. )
  58. /*++
  59. Purpose:
  60. Fill the background music into the sound buffer. Called by the SDL sound
  61. callback function only (sound.c: SOUND_FillAudio).
  62. Parameters:
  63. [OUT] stream - pointer to the stream buffer.
  64. [IN] len - Length of the buffer.
  65. Return value:
  66. None.
  67. --*/
  68. {
  69. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  70. const INT max_volume = gpGlobals->iVolume * 3 / 4;
  71. if (pRixPlayer == NULL || !pRixPlayer->fReady)
  72. {
  73. //
  74. // Not initialized
  75. //
  76. return;
  77. }
  78. while (len > 0)
  79. {
  80. INT volume, delta_samples = 0, vol_delta = 0;
  81. //
  82. // fading in or fading out
  83. //
  84. switch (pRixPlayer->FadeType)
  85. {
  86. case RIXPLAYER::FADE_IN:
  87. if (pRixPlayer->iRemainingFadeSamples <= 0)
  88. {
  89. pRixPlayer->FadeType = RIXPLAYER::NONE;
  90. volume = max_volume;
  91. }
  92. else
  93. {
  94. volume = (INT)(max_volume * (1.0 - (double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples));
  95. delta_samples = pRixPlayer->iTotalFadeInSamples / max_volume; vol_delta = 1;
  96. }
  97. break;
  98. case RIXPLAYER::FADE_OUT:
  99. if (pRixPlayer->iTotalFadeOutSamples == pRixPlayer->iRemainingFadeSamples && pRixPlayer->iTotalFadeOutSamples > 0)
  100. {
  101. UINT now = SDL_GetTicks();
  102. INT passed_samples = ((INT)(now - pRixPlayer->dwStartFadeTime) > 0) ? (INT)((now - pRixPlayer->dwStartFadeTime) * SOUND_GetAudioSpec()->freq / 1000) : 0;
  103. pRixPlayer->iRemainingFadeSamples -= passed_samples;
  104. }
  105. if (pRixPlayer->iCurrentMusic == -1 || pRixPlayer->iRemainingFadeSamples <= 0)
  106. {
  107. //
  108. // There is no current playing music, or fading time has passed.
  109. // Start playing the next one or stop playing.
  110. //
  111. if (pRixPlayer->iNextMusic > 0)
  112. {
  113. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic;
  114. pRixPlayer->iNextMusic = -1;
  115. pRixPlayer->fLoop = pRixPlayer->fNextLoop;
  116. pRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  117. if (pRixPlayer->iCurrentMusic > 0)
  118. pRixPlayer->dwStartFadeTime += pRixPlayer->iTotalFadeOutSamples * 1000 / gpGlobals->iSampleRate;
  119. else
  120. pRixPlayer->dwStartFadeTime = SDL_GetTicks();
  121. pRixPlayer->iTotalFadeOutSamples = 0;
  122. pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeInSamples;
  123. pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic);
  124. if (pRixPlayer->resampler[0]) resampler_clear(pRixPlayer->resampler[0]);
  125. if (pRixPlayer->resampler[1]) resampler_clear(pRixPlayer->resampler[1]);
  126. continue;
  127. }
  128. else
  129. {
  130. pRixPlayer->iCurrentMusic = -1;
  131. pRixPlayer->FadeType = RIXPLAYER::NONE;
  132. return;
  133. }
  134. }
  135. else
  136. {
  137. volume = (INT)(max_volume * ((double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeOutSamples));
  138. delta_samples = pRixPlayer->iTotalFadeOutSamples / max_volume; vol_delta = -1;
  139. }
  140. break;
  141. default:
  142. if (pRixPlayer->iCurrentMusic <= 0)
  143. {
  144. //
  145. // No current playing music
  146. //
  147. return;
  148. }
  149. else
  150. {
  151. volume = max_volume;
  152. }
  153. }
  154. //
  155. // Fill the buffer with sound data
  156. //
  157. int buf_max_len = gpGlobals->iSampleRate / 70 * gpGlobals->iAudioChannels * sizeof(short);
  158. bool fContinue = true;
  159. while (len > 0 && fContinue)
  160. {
  161. if (pRixPlayer->pos == NULL || pRixPlayer->pos - pRixPlayer->buf >= buf_max_len)
  162. {
  163. pRixPlayer->pos = pRixPlayer->buf;
  164. if (!pRixPlayer->rix->update())
  165. {
  166. if (!pRixPlayer->fLoop)
  167. {
  168. //
  169. // Not loop, simply terminate the music
  170. //
  171. pRixPlayer->iCurrentMusic = -1;
  172. if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT && pRixPlayer->iNextMusic == -1)
  173. {
  174. pRixPlayer->FadeType = RIXPLAYER::NONE;
  175. }
  176. return;
  177. }
  178. pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic, false);
  179. if (!pRixPlayer->rix->update())
  180. {
  181. //
  182. // Something must be wrong
  183. //
  184. pRixPlayer->iCurrentMusic = -1;
  185. pRixPlayer->FadeType = RIXPLAYER::NONE;
  186. return;
  187. }
  188. }
  189. int sample_count = gpGlobals->iSampleRate / 70;
  190. if (pRixPlayer->resampler[0])
  191. {
  192. unsigned int samples_written = 0;
  193. short *finalBuf = (short*)pRixPlayer->buf;
  194. while (sample_count)
  195. {
  196. int to_write = resampler_get_free_count(pRixPlayer->resampler[0]);
  197. if (to_write)
  198. {
  199. short *tempBuf = (short*)alloca(to_write * gpGlobals->iAudioChannels * sizeof(short));
  200. pRixPlayer->opl->update(tempBuf, to_write);
  201. for (int i = 0; i < to_write; i++)
  202. for (int j = 0; j < gpGlobals->iAudioChannels; j++)
  203. resampler_write_sample(pRixPlayer->resampler[j], tempBuf[i * gpGlobals->iAudioChannels + j]);
  204. }
  205. int to_get = resampler_get_sample_count(pRixPlayer->resampler[0]);
  206. if (to_get > sample_count) to_get = sample_count;
  207. for (int i = 0; i < to_get; i++)
  208. for (int j = 0; j < gpGlobals->iAudioChannels; j++)
  209. finalBuf[samples_written++] = resampler_get_and_remove_sample(pRixPlayer->resampler[j]);
  210. sample_count -= to_get;
  211. }
  212. }
  213. else
  214. {
  215. pRixPlayer->opl->update((short *)(pRixPlayer->buf), sample_count);
  216. }
  217. }
  218. int l = buf_max_len - (pRixPlayer->pos - pRixPlayer->buf);
  219. l = (l > len) ? len / sizeof(short) : l / sizeof(short);
  220. //
  221. // Put audio data into buffer and adjust volume
  222. // WARNING: for signed 16-bit little-endian only
  223. //
  224. SHORT* ptr = (SHORT*)stream;
  225. if (pRixPlayer->FadeType == RIXPLAYER::NONE)
  226. {
  227. for (int i = 0; i < l; i++)
  228. {
  229. *ptr++ = SDL_SwapLE16((short)((int)(*(SHORT *)(pRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME));
  230. pRixPlayer->pos += sizeof(SHORT);
  231. }
  232. }
  233. else
  234. {
  235. for (int i = 0; i < l && pRixPlayer->iRemainingFadeSamples > 0; volume += vol_delta)
  236. {
  237. for (int j = 0; i < l && j < delta_samples; i++, j++)
  238. {
  239. *ptr++ = SDL_SwapLE16((short)((int)(*(SHORT *)(pRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME));
  240. pRixPlayer->pos += sizeof(SHORT);
  241. }
  242. pRixPlayer->iRemainingFadeSamples -= delta_samples;
  243. }
  244. fContinue = (pRixPlayer->iRemainingFadeSamples > 0);
  245. }
  246. len -= (LPBYTE)ptr - stream;
  247. stream = (LPBYTE)ptr;
  248. }
  249. }
  250. }
  251. static VOID
  252. RIX_Shutdown(
  253. VOID *object
  254. )
  255. /*++
  256. Purpose:
  257. Shutdown the RIX player subsystem.
  258. Parameters:
  259. None.
  260. Return value:
  261. None.
  262. --*/
  263. {
  264. if (object != NULL)
  265. {
  266. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  267. pRixPlayer->fReady = FALSE;
  268. for (int i = 0; i < gpGlobals->iAudioChannels; i++)
  269. if (pRixPlayer->resampler[i])
  270. resampler_delete(pRixPlayer->resampler[i]);
  271. delete pRixPlayer->rix;
  272. delete pRixPlayer->opl;
  273. delete pRixPlayer;
  274. }
  275. }
  276. static BOOL
  277. RIX_Play(
  278. VOID *object,
  279. INT iNumRIX,
  280. BOOL fLoop,
  281. FLOAT flFadeTime
  282. )
  283. /*++
  284. Purpose:
  285. Start playing the specified music.
  286. Parameters:
  287. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  288. [IN] fLoop - Whether the music should be looped or not.
  289. [IN] flFadeTime - the fade in/out time when switching music.
  290. Return value:
  291. None.
  292. --*/
  293. {
  294. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  295. //
  296. // Check for NULL pointer.
  297. //
  298. if (pRixPlayer == NULL)
  299. {
  300. return FALSE;
  301. }
  302. //
  303. // Stop the current CD music.
  304. //
  305. SOUND_PlayCDA(-1);
  306. if (iNumRIX == pRixPlayer->iCurrentMusic && pRixPlayer->iNextMusic == -1)
  307. {
  308. /* Will play the same music without any pending play changes,
  309. just change the loop attribute */
  310. pRixPlayer->fLoop = fLoop;
  311. return TRUE;
  312. }
  313. if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT)
  314. {
  315. if (pRixPlayer->FadeType == RIXPLAYER::FADE_IN && pRixPlayer->iTotalFadeInSamples > 0 && pRixPlayer->iRemainingFadeSamples > 0)
  316. {
  317. pRixPlayer->dwStartFadeTime = SDL_GetTicks() - (int)((float)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples * flFadeTime * (1000 / 2));
  318. }
  319. else
  320. {
  321. pRixPlayer->dwStartFadeTime = SDL_GetTicks();
  322. }
  323. pRixPlayer->iTotalFadeOutSamples = (int)round(flFadeTime / 2.0f * gpGlobals->iSampleRate) * gpGlobals->iAudioChannels;
  324. pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeOutSamples;
  325. pRixPlayer->iTotalFadeInSamples = pRixPlayer->iTotalFadeOutSamples;
  326. }
  327. else
  328. {
  329. pRixPlayer->iTotalFadeInSamples = (int)round(flFadeTime / 2.0f * gpGlobals->iSampleRate) * gpGlobals->iAudioChannels;
  330. }
  331. pRixPlayer->iNextMusic = iNumRIX;
  332. pRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  333. pRixPlayer->fNextLoop = fLoop;
  334. pRixPlayer->fReady = TRUE;
  335. return TRUE;
  336. }
  337. LPMUSICPLAYER
  338. RIX_Init(
  339. LPCSTR szFileName
  340. )
  341. /*++
  342. Purpose:
  343. Initialize the RIX player subsystem.
  344. Parameters:
  345. [IN] szFileName - Filename of the mus.mkf file.
  346. Return value:
  347. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  348. --*/
  349. {
  350. LPRIXPLAYER pRixPlayer = new RIXPLAYER;
  351. if (pRixPlayer == NULL)
  352. {
  353. return NULL;
  354. }
  355. else
  356. {
  357. memset(pRixPlayer, 0, sizeof(RIXPLAYER));
  358. pRixPlayer->FillBuffer = RIX_FillBuffer;
  359. pRixPlayer->Shutdown = RIX_Shutdown;
  360. pRixPlayer->Play = RIX_Play;
  361. }
  362. if (gpGlobals->fUseSurroundOPL)
  363. {
  364. switch (gpGlobals->eOPLType)
  365. {
  366. case OPL_DOSBOX_OLD:
  367. pRixPlayer->opl = new CSurroundopl(
  368. new CDemuopl(gpGlobals->iOPLSampleRate, true, false),
  369. new CDemuopl(gpGlobals->iOPLSampleRate, true, false),
  370. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  371. break;
  372. case OPL_DOSBOX:
  373. pRixPlayer->opl = new CSurroundopl(
  374. new CDBemuopl(gpGlobals->iOPLSampleRate, true, false),
  375. new CDBemuopl(gpGlobals->iOPLSampleRate, true, false),
  376. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  377. break;
  378. case OPL_MAME:
  379. pRixPlayer->opl = new CSurroundopl(
  380. new CEmuopl(gpGlobals->iOPLSampleRate, true, false),
  381. new CEmuopl(gpGlobals->iOPLSampleRate, true, false),
  382. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  383. break;
  384. }
  385. }
  386. else
  387. {
  388. switch (gpGlobals->eOPLType)
  389. {
  390. case OPL_DOSBOX_OLD:
  391. pRixPlayer->opl = new CDemuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  392. break;
  393. case OPL_DOSBOX:
  394. pRixPlayer->opl = new CDBemuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  395. break;
  396. case OPL_MAME:
  397. pRixPlayer->opl = new CEmuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  398. break;
  399. }
  400. }
  401. if (pRixPlayer->opl == NULL)
  402. {
  403. delete pRixPlayer;
  404. return NULL;
  405. }
  406. pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
  407. if (pRixPlayer->rix == NULL)
  408. {
  409. delete pRixPlayer->opl;
  410. delete pRixPlayer;
  411. return NULL;
  412. }
  413. //
  414. // Load the MKF file.
  415. //
  416. if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  417. {
  418. delete pRixPlayer->rix;
  419. delete pRixPlayer->opl;
  420. delete pRixPlayer;
  421. pRixPlayer = NULL;
  422. return NULL;
  423. }
  424. if (gpGlobals->iOPLSampleRate != gpGlobals->iSampleRate)
  425. {
  426. for (int i = 0; i < gpGlobals->iAudioChannels; i++)
  427. {
  428. pRixPlayer->resampler[i] = resampler_create();
  429. resampler_set_quality(pRixPlayer->resampler[i], SOUND_IsIntegerConversion(gpGlobals->iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gpGlobals->iResampleQuality);
  430. resampler_set_rate(pRixPlayer->resampler[i], (double)gpGlobals->iOPLSampleRate / (double)gpGlobals->iSampleRate);
  431. }
  432. }
  433. #if USE_RIX_EXTRA_INIT
  434. if (gpGlobals->pExtraFMRegs && gpGlobals->pExtraFMVals)
  435. {
  436. pRixPlayer->rix->set_extra_init(gpGlobals->pExtraFMRegs, gpGlobals->pExtraFMVals, gpGlobals->dwExtraLength);
  437. }
  438. #endif
  439. //
  440. // Success.
  441. //
  442. pRixPlayer->FadeType = RIXPLAYER::NONE;
  443. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
  444. pRixPlayer->pos = NULL;
  445. pRixPlayer->fLoop = FALSE;
  446. pRixPlayer->fNextLoop = FALSE;
  447. pRixPlayer->fReady = FALSE;
  448. return pRixPlayer;
  449. }