rixplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL 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 "audio.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. typedef struct tagRIXPLAYER :
  35. public AUDIOPLAYER
  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 (audio.c: AUDIO_FillBuffer).
  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. 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 = SDL_MIX_MAXVOLUME;
  91. }
  92. else
  93. {
  94. volume = (INT)(SDL_MIX_MAXVOLUME * (1.0 - (double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples));
  95. delta_samples = pRixPlayer->iTotalFadeInSamples / SDL_MIX_MAXVOLUME; 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) * AUDIO_GetDeviceSpec()->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)(SDL_MIX_MAXVOLUME * ((double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeOutSamples));
  138. delta_samples = pRixPlayer->iTotalFadeOutSamples / SDL_MIX_MAXVOLUME; 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 = SDL_MIX_MAXVOLUME;
  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->rewindReInit(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. int temp_buf_read = 0;
  201. pRixPlayer->opl->update(tempBuf, to_write);
  202. for (int i = 0; i < to_write * gConfig.iAudioChannels; i++)
  203. resampler_write_sample(pRixPlayer->resampler[i % gConfig.iAudioChannels], tempBuf[temp_buf_read++]);
  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 * gConfig.iAudioChannels; i++)
  208. finalBuf[samples_written++] = resampler_get_and_remove_sample(pRixPlayer->resampler[i % gConfig.iAudioChannels]);
  209. sample_count -= to_get;
  210. }
  211. }
  212. else
  213. {
  214. pRixPlayer->opl->update((short *)(pRixPlayer->buf), sample_count);
  215. }
  216. }
  217. int l = buf_max_len - (pRixPlayer->pos - pRixPlayer->buf);
  218. l = (l > len) ? len / sizeof(short) : l / sizeof(short);
  219. //
  220. // Put audio data into buffer and adjust volume
  221. //
  222. if (pRixPlayer->FadeType != RIXPLAYER::NONE)
  223. {
  224. short* ptr = (short*)stream;
  225. for (int i = 0; i < l && pRixPlayer->iRemainingFadeSamples > 0; volume += vol_delta)
  226. {
  227. int j = 0;
  228. for (j = 0; i < l && j < delta_samples; i++, j++)
  229. {
  230. *ptr++ = *(short*)pRixPlayer->pos * volume / SDL_MIX_MAXVOLUME;
  231. pRixPlayer->pos += sizeof(short);
  232. }
  233. pRixPlayer->iRemainingFadeSamples -= j;
  234. }
  235. fContinue = (pRixPlayer->iRemainingFadeSamples > 0);
  236. len -= (LPBYTE)ptr - stream; stream = (LPBYTE)ptr;
  237. }
  238. else
  239. {
  240. memcpy(stream, pRixPlayer->pos, l * sizeof(short));
  241. pRixPlayer->pos += l * sizeof(short);
  242. stream += l * sizeof(short);
  243. len -= l * sizeof(short);
  244. }
  245. }
  246. }
  247. }
  248. static VOID
  249. RIX_Shutdown(
  250. VOID *object
  251. )
  252. /*++
  253. Purpose:
  254. Shutdown the RIX player subsystem.
  255. Parameters:
  256. None.
  257. Return value:
  258. None.
  259. --*/
  260. {
  261. if (object != NULL)
  262. {
  263. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  264. pRixPlayer->fReady = FALSE;
  265. for (int i = 0; i < gConfig.iAudioChannels; i++)
  266. if (pRixPlayer->resampler[i])
  267. resampler_delete(pRixPlayer->resampler[i]);
  268. delete pRixPlayer->rix;
  269. delete pRixPlayer->opl;
  270. delete pRixPlayer;
  271. }
  272. }
  273. static BOOL
  274. RIX_Play(
  275. VOID *object,
  276. INT iNumRIX,
  277. BOOL fLoop,
  278. FLOAT flFadeTime
  279. )
  280. /*++
  281. Purpose:
  282. Start playing the specified music.
  283. Parameters:
  284. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  285. [IN] fLoop - Whether the music should be looped or not.
  286. [IN] flFadeTime - the fade in/out time when switching music.
  287. Return value:
  288. None.
  289. --*/
  290. {
  291. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  292. //
  293. // Check for NULL pointer.
  294. //
  295. if (pRixPlayer == NULL)
  296. {
  297. return FALSE;
  298. }
  299. if (iNumRIX == pRixPlayer->iCurrentMusic && pRixPlayer->iNextMusic == -1)
  300. {
  301. /* Will play the same music without any pending play changes,
  302. just change the loop attribute */
  303. pRixPlayer->fLoop = fLoop;
  304. return TRUE;
  305. }
  306. if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT)
  307. {
  308. if (pRixPlayer->FadeType == RIXPLAYER::FADE_IN && pRixPlayer->iTotalFadeInSamples > 0 && pRixPlayer->iRemainingFadeSamples > 0)
  309. {
  310. pRixPlayer->dwStartFadeTime = SDL_GetTicks() - (int)((float)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples * flFadeTime * (1000 / 2));
  311. }
  312. else
  313. {
  314. pRixPlayer->dwStartFadeTime = SDL_GetTicks();
  315. }
  316. pRixPlayer->iTotalFadeOutSamples = (int)round(flFadeTime / 2.0f * gConfig.iSampleRate) * gConfig.iAudioChannels;
  317. pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeOutSamples;
  318. pRixPlayer->iTotalFadeInSamples = pRixPlayer->iTotalFadeOutSamples;
  319. }
  320. else
  321. {
  322. pRixPlayer->iTotalFadeInSamples = (int)round(flFadeTime / 2.0f * gConfig.iSampleRate) * gConfig.iAudioChannels;
  323. }
  324. pRixPlayer->iNextMusic = iNumRIX;
  325. pRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  326. pRixPlayer->fNextLoop = fLoop;
  327. pRixPlayer->fReady = TRUE;
  328. return TRUE;
  329. }
  330. LPAUDIOPLAYER
  331. RIX_Init(
  332. LPCSTR szFileName
  333. )
  334. /*++
  335. Purpose:
  336. Initialize the RIX player subsystem.
  337. Parameters:
  338. [IN] szFileName - Filename of the mus.mkf file.
  339. Return value:
  340. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  341. --*/
  342. {
  343. if (!szFileName) return NULL;
  344. LPRIXPLAYER pRixPlayer = new RIXPLAYER;
  345. if (pRixPlayer == NULL)
  346. {
  347. return NULL;
  348. }
  349. else
  350. {
  351. memset(pRixPlayer, 0, sizeof(RIXPLAYER));
  352. pRixPlayer->FillBuffer = RIX_FillBuffer;
  353. pRixPlayer->Shutdown = RIX_Shutdown;
  354. pRixPlayer->Play = RIX_Play;
  355. }
  356. if (gConfig.fUseSurroundOPL)
  357. {
  358. switch (gConfig.eOPLType)
  359. {
  360. case OPL_DOSBOX:
  361. pRixPlayer->opl = new CSurroundopl(
  362. new CDemuopl(gConfig.iOPLSampleRate, true, false),
  363. new CDemuopl(gConfig.iOPLSampleRate, true, false),
  364. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  365. break;
  366. case OPL_DOSBOX_NEW:
  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 CDemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  386. break;
  387. case OPL_DOSBOX_NEW:
  388. pRixPlayer->opl = new CDBemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  389. break;
  390. case OPL_MAME:
  391. pRixPlayer->opl = new CEmuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  392. break;
  393. }
  394. }
  395. if (pRixPlayer->opl == NULL)
  396. {
  397. delete pRixPlayer;
  398. return NULL;
  399. }
  400. pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
  401. if (pRixPlayer->rix == NULL)
  402. {
  403. delete pRixPlayer->opl;
  404. delete pRixPlayer;
  405. return NULL;
  406. }
  407. //
  408. // Load the MKF file.
  409. //
  410. if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  411. {
  412. delete pRixPlayer->rix;
  413. delete pRixPlayer->opl;
  414. delete pRixPlayer;
  415. pRixPlayer = NULL;
  416. return NULL;
  417. }
  418. if (gConfig.iOPLSampleRate != gConfig.iSampleRate)
  419. {
  420. for (int i = 0; i < gConfig.iAudioChannels; i++)
  421. {
  422. pRixPlayer->resampler[i] = resampler_create();
  423. resampler_set_quality(pRixPlayer->resampler[i], AUDIO_IsIntegerConversion(gConfig.iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
  424. resampler_set_rate(pRixPlayer->resampler[i], (double)gConfig.iOPLSampleRate / (double)gConfig.iSampleRate);
  425. }
  426. }
  427. #if USE_RIX_EXTRA_INIT
  428. if (gConfig.pExtraFMRegs && gConfig.pExtraFMVals)
  429. {
  430. pRixPlayer->rix->set_extra_init(gConfig.pExtraFMRegs, gConfig.pExtraFMVals, gConfig.dwExtraLength);
  431. }
  432. #endif
  433. //
  434. // Success.
  435. //
  436. pRixPlayer->FadeType = RIXPLAYER::NONE;
  437. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
  438. pRixPlayer->pos = NULL;
  439. pRixPlayer->fLoop = FALSE;
  440. pRixPlayer->fNextLoop = FALSE;
  441. pRixPlayer->fReady = FALSE;
  442. return pRixPlayer;
  443. }