rixplay.cpp 13 KB

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