rixplay.cpp 13 KB

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