rixplay.cpp 13 KB

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