rixplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. 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. static const int max_volume = PAL_MAX_VOLUME;
  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) * AUDIO_GetDeviceSpec()->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. int temp_buf_read = 0;
  202. pRixPlayer->opl->update(tempBuf, to_write);
  203. for (int i = 0; i < to_write * gConfig.iAudioChannels; i++)
  204. resampler_write_sample(pRixPlayer->resampler[i % gConfig.iAudioChannels], tempBuf[temp_buf_read++]);
  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 * gConfig.iAudioChannels; i++)
  209. finalBuf[samples_written++] = resampler_get_and_remove_sample(pRixPlayer->resampler[i % gConfig.iAudioChannels]);
  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. //
  223. SHORT* ptr = (SHORT*)stream;
  224. if (pRixPlayer->FadeType == RIXPLAYER::NONE)
  225. {
  226. for (int i = 0; i < l; i++)
  227. {
  228. *ptr++ = *(SHORT *)pRixPlayer->pos * volume / max_volume;
  229. pRixPlayer->pos += sizeof(SHORT);
  230. }
  231. }
  232. else
  233. {
  234. for (int i = 0; i < l && pRixPlayer->iRemainingFadeSamples > 0; volume += vol_delta)
  235. {
  236. int j = 0;
  237. for (j = 0; i < l && j < delta_samples; i++, j++)
  238. {
  239. *ptr++ = *(SHORT *)pRixPlayer->pos * volume / max_volume;
  240. pRixPlayer->pos += sizeof(SHORT);
  241. }
  242. pRixPlayer->iRemainingFadeSamples -= j;
  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. AUDIO_PlayCDTrack(-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 CDemuopl(gConfig.iOPLSampleRate, true, false),
  369. new CDemuopl(gConfig.iOPLSampleRate, true, false),
  370. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  371. break;
  372. case OPL_DOSBOX_NEW:
  373. pRixPlayer->opl = new CSurroundopl(
  374. new CDBemuopl(gConfig.iOPLSampleRate, true, false),
  375. new CDBemuopl(gConfig.iOPLSampleRate, true, false),
  376. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  377. break;
  378. case OPL_MAME:
  379. pRixPlayer->opl = new CSurroundopl(
  380. new CEmuopl(gConfig.iOPLSampleRate, true, false),
  381. new CEmuopl(gConfig.iOPLSampleRate, true, false),
  382. true, gConfig.iOPLSampleRate, gConfig.iSurroundOPLOffset);
  383. break;
  384. }
  385. }
  386. else
  387. {
  388. switch (gConfig.eOPLType)
  389. {
  390. case OPL_DOSBOX:
  391. pRixPlayer->opl = new CDemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  392. break;
  393. case OPL_DOSBOX_NEW:
  394. pRixPlayer->opl = new CDBemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  395. break;
  396. case OPL_MAME:
  397. pRixPlayer->opl = new CEmuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
  398. break;
  399. }
  400. }
  401. if (pRixPlayer->opl == NULL)
  402. {
  403. delete pRixPlayer;
  404. return NULL;
  405. }
  406. pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
  407. if (pRixPlayer->rix == NULL)
  408. {
  409. delete pRixPlayer->opl;
  410. delete pRixPlayer;
  411. return NULL;
  412. }
  413. //
  414. // Load the MKF file.
  415. //
  416. if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  417. {
  418. delete pRixPlayer->rix;
  419. delete pRixPlayer->opl;
  420. delete pRixPlayer;
  421. pRixPlayer = NULL;
  422. return NULL;
  423. }
  424. if (gConfig.iOPLSampleRate != gConfig.iSampleRate)
  425. {
  426. for (int i = 0; i < gConfig.iAudioChannels; i++)
  427. {
  428. pRixPlayer->resampler[i] = resampler_create();
  429. resampler_set_quality(pRixPlayer->resampler[i], AUDIO_IsIntegerConversion(gConfig.iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
  430. resampler_set_rate(pRixPlayer->resampler[i], (double)gConfig.iOPLSampleRate / (double)gConfig.iSampleRate);
  431. }
  432. }
  433. #if USE_RIX_EXTRA_INIT
  434. if (gConfig.pExtraFMRegs && gConfig.pExtraFMVals)
  435. {
  436. pRixPlayer->rix->set_extra_init(gConfig.pExtraFMRegs, gConfig.pExtraFMVals, gConfig.dwExtraLength);
  437. }
  438. #endif
  439. //
  440. // Success.
  441. //
  442. pRixPlayer->FadeType = RIXPLAYER::NONE;
  443. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
  444. pRixPlayer->pos = NULL;
  445. pRixPlayer->fLoop = FALSE;
  446. pRixPlayer->fNextLoop = FALSE;
  447. pRixPlayer->fReady = FALSE;
  448. return pRixPlayer;
  449. }