rixplay.cpp 12 KB

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