rixplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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/dbemuopl.h"
  29. #include "adplug/emuopl.h"
  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 = gConfig.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 / gConfig.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 = gConfig.iSampleRate / 70 * gConfig.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 = gConfig.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 * gConfig.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 < gConfig.iAudioChannels; j++)
  203. resampler_write_sample(pRixPlayer->resampler[j], tempBuf[i * gConfig.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 < gConfig.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 < gConfig.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 * gConfig.iSampleRate) * gConfig.iAudioChannels;
  324. pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeOutSamples;
  325. pRixPlayer->iTotalFadeInSamples = pRixPlayer->iTotalFadeOutSamples;
  326. }
  327. else
  328. {
  329. pRixPlayer->iTotalFadeInSamples = (int)round(flFadeTime / 2.0f * gConfig.iSampleRate) * gConfig.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 (gConfig.fUseSurroundOPL)
  363. {
  364. switch (gConfig.eOPLType)
  365. {
  366. case OPL_DOSBOX:
  367. pRixPlayer->opl = new CSurroundopl(
  368. new CDBemuopl(gConfig.iOPLSampleRate, true, false),
  369. new CDBemuopl(gConfig.iOPLSampleRate, true, false),
  370. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  371. break;
  372. case OPL_MAME:
  373. pRixPlayer->opl = new CSurroundopl(
  374. new CEmuopl(gConfig.iOPLSampleRate, true, false),
  375. new CEmuopl(gConfig.iOPLSampleRate, true, false),
  376. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  377. break;
  378. }
  379. }
  380. else
  381. {
  382. switch (gConfig.eOPLType)
  383. {
  384. case OPL_DOSBOX:
  385. pRixPlayer->opl = new CDBemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  386. break;
  387. case OPL_MAME:
  388. pRixPlayer->opl = new CEmuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  389. break;
  390. }
  391. }
  392. if (pRixPlayer->opl == NULL)
  393. {
  394. delete pRixPlayer;
  395. return NULL;
  396. }
  397. pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
  398. if (pRixPlayer->rix == NULL)
  399. {
  400. delete pRixPlayer->opl;
  401. delete pRixPlayer;
  402. return NULL;
  403. }
  404. //
  405. // Load the MKF file.
  406. //
  407. if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  408. {
  409. delete pRixPlayer->rix;
  410. delete pRixPlayer->opl;
  411. delete pRixPlayer;
  412. pRixPlayer = NULL;
  413. return NULL;
  414. }
  415. if (gConfig.iOPLSampleRate != gConfig.iSampleRate)
  416. {
  417. for (int i = 0; i < gConfig.iAudioChannels; i++)
  418. {
  419. pRixPlayer->resampler[i] = resampler_create();
  420. resampler_set_quality(pRixPlayer->resampler[i], SOUND_IsIntegerConversion(gConfig.iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
  421. resampler_set_rate(pRixPlayer->resampler[i], (double)gConfig.iOPLSampleRate / (double)gConfig.iSampleRate);
  422. }
  423. }
  424. #if USE_RIX_EXTRA_INIT
  425. if (gConfig.pExtraFMRegs && gConfig.pExtraFMVals)
  426. {
  427. pRixPlayer->rix->set_extra_init(gConfig.pExtraFMRegs, gConfig.pExtraFMVals, gConfig.dwExtraLength);
  428. }
  429. #endif
  430. //
  431. // Success.
  432. //
  433. pRixPlayer->FadeType = RIXPLAYER::NONE;
  434. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
  435. pRixPlayer->pos = NULL;
  436. pRixPlayer->fLoop = FALSE;
  437. pRixPlayer->fNextLoop = FALSE;
  438. pRixPlayer->fReady = FALSE;
  439. return pRixPlayer;
  440. }