rixplay.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. //
  19. #include "global.h"
  20. #include "players.h"
  21. #include "sound.h"
  22. #include "resampler.h"
  23. #include "adplug/opl.h"
  24. #include "adplug/demuopl.h"
  25. #include "adplug/dbemuopl.h"
  26. #if PAL_HAS_MAME
  27. #include "adplug/emuopl.h"
  28. #endif
  29. #include "adplug/surroundopl.h"
  30. #include "adplug/rix.h"
  31. extern "C" BOOL g_fNoMusic;
  32. typedef struct tagRIXPLAYER :
  33. public MUSICPLAYER
  34. {
  35. Copl *opl;
  36. CrixPlayer *rix;
  37. void *resampler[2];
  38. BYTE buf[(PAL_MAX_SAMPLERATE + 69) / 70 * sizeof(short) * 2];
  39. LPBYTE pos;
  40. INT iCurrentMusic; // current playing music number
  41. INT iNextMusic; // the next music number to switch to
  42. DWORD dwStartFadeTime;
  43. DWORD dwFadeLength;
  44. enum { NONE, FADE_IN, FADE_OUT } FadeType; // fade in or fade out ?
  45. BOOL fLoop;
  46. BOOL fNextLoop;
  47. BOOL fReady;
  48. } RIXPLAYER, *LPRIXPLAYER;
  49. static VOID
  50. RIX_FillBuffer(
  51. VOID *object,
  52. LPBYTE stream,
  53. INT len
  54. )
  55. /*++
  56. Purpose:
  57. Fill the background music into the sound buffer. Called by the SDL sound
  58. callback function only (sound.c: SOUND_FillAudio).
  59. Parameters:
  60. [OUT] stream - pointer to the stream buffer.
  61. [IN] len - Length of the buffer.
  62. Return value:
  63. None.
  64. --*/
  65. {
  66. INT i, l, volume = gpGlobals->iVolume * 3 / 4;
  67. UINT t = SDL_GetTicks();
  68. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  69. if (pRixPlayer == NULL || !pRixPlayer->fReady)
  70. {
  71. //
  72. // Not initialized
  73. //
  74. return;
  75. }
  76. //
  77. // fading in or fading out
  78. //
  79. switch (pRixPlayer->FadeType)
  80. {
  81. case RIXPLAYER::FADE_IN:
  82. if (t >= pRixPlayer->dwStartFadeTime + pRixPlayer->dwFadeLength)
  83. {
  84. pRixPlayer->FadeType = RIXPLAYER::NONE;
  85. }
  86. else
  87. {
  88. volume = (INT)(volume * (t - pRixPlayer->dwStartFadeTime) / (FLOAT)pRixPlayer->dwFadeLength);
  89. }
  90. break;
  91. case RIXPLAYER::FADE_OUT:
  92. if (pRixPlayer->iCurrentMusic == -1 || t >= pRixPlayer->dwStartFadeTime + pRixPlayer->dwFadeLength)
  93. {
  94. //
  95. // There is no current playing music, or fading time has passed.
  96. // Start playing the next one.
  97. //
  98. if (pRixPlayer->iNextMusic > 0)
  99. {
  100. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic;
  101. pRixPlayer->iNextMusic = -1;
  102. pRixPlayer->fLoop = pRixPlayer->fNextLoop;
  103. pRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  104. pRixPlayer->dwStartFadeTime = t;
  105. pRixPlayer->opl->init();
  106. pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic);
  107. if (pRixPlayer->resampler[0]) resampler_clear(pRixPlayer->resampler[0]);
  108. if (pRixPlayer->resampler[1]) resampler_clear(pRixPlayer->resampler[1]);
  109. }
  110. else
  111. {
  112. pRixPlayer->iCurrentMusic = -1;
  113. pRixPlayer->FadeType = RIXPLAYER::NONE;
  114. }
  115. return;
  116. }
  117. else
  118. {
  119. volume = (INT)(volume * (1.0f - (t - pRixPlayer->dwStartFadeTime) / (FLOAT)pRixPlayer->dwFadeLength));
  120. }
  121. break;
  122. default:
  123. if (pRixPlayer->iCurrentMusic <= 0)
  124. {
  125. //
  126. // No current playing music
  127. //
  128. return;
  129. }
  130. }
  131. //
  132. // Fill the buffer with sound data
  133. //
  134. int buf_max_len = gpGlobals->iSampleRate / 70 * gpGlobals->iAudioChannels * sizeof(short);
  135. while (len > 0)
  136. {
  137. if (pRixPlayer->pos == NULL ||
  138. pRixPlayer->pos - pRixPlayer->buf >= buf_max_len)
  139. {
  140. pRixPlayer->pos = pRixPlayer->buf;
  141. if (!pRixPlayer->rix->update())
  142. {
  143. if (!pRixPlayer->fLoop)
  144. {
  145. //
  146. // Not loop, simply terminate the music
  147. //
  148. pRixPlayer->iCurrentMusic = -1;
  149. if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT && pRixPlayer->iNextMusic == -1)
  150. {
  151. pRixPlayer->FadeType = RIXPLAYER::NONE;
  152. }
  153. return;
  154. }
  155. pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic);
  156. if (!pRixPlayer->rix->update())
  157. {
  158. //
  159. // Something must be wrong
  160. //
  161. pRixPlayer->iCurrentMusic = -1;
  162. pRixPlayer->FadeType = RIXPLAYER::NONE;
  163. return;
  164. }
  165. }
  166. int sample_count = gpGlobals->iSampleRate / 70;
  167. if (pRixPlayer->resampler[0])
  168. {
  169. unsigned int samples_written = 0;
  170. short *finalBuf = (short*)pRixPlayer->buf;
  171. while (sample_count)
  172. {
  173. int to_write = resampler_get_free_count(pRixPlayer->resampler[0]);
  174. if (to_write)
  175. {
  176. short *tempBuf = (short*)_alloca(to_write * gpGlobals->iAudioChannels * sizeof(short));
  177. pRixPlayer->opl->update(tempBuf, to_write);
  178. for (int i = 0; i < to_write; i++)
  179. for (int j = 0; j < gpGlobals->iAudioChannels; j++)
  180. resampler_write_sample(pRixPlayer->resampler[j], tempBuf[i * gpGlobals->iAudioChannels + j]);
  181. }
  182. int to_get = resampler_get_sample_count(pRixPlayer->resampler[0]);
  183. if (to_get > sample_count) to_get = sample_count;
  184. for (int i = 0; i < to_get; i++)
  185. for (int j = 0; j < gpGlobals->iAudioChannels; j++)
  186. finalBuf[samples_written++] = resampler_get_and_remove_sample(pRixPlayer->resampler[j]);
  187. sample_count -= to_get;
  188. }
  189. }
  190. else
  191. {
  192. pRixPlayer->opl->update((short *)(pRixPlayer->buf), sample_count);
  193. }
  194. }
  195. l = buf_max_len - (pRixPlayer->pos - pRixPlayer->buf);
  196. if (len < l)
  197. {
  198. l = len;
  199. }
  200. //
  201. // Put audio data into buffer and adjust volume
  202. // WARNING: for signed 16-bit little-endian only
  203. //
  204. SHORT* ptr = (SHORT*)stream;
  205. for (i = 0; i < (int)(l / sizeof(SHORT)); i++)
  206. {
  207. *ptr++ = SDL_SwapLE16((int)(*(SHORT *)(pRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME);
  208. pRixPlayer->pos += sizeof(SHORT);
  209. }
  210. stream = (LPBYTE)ptr;
  211. len -= l;
  212. }
  213. }
  214. static VOID
  215. RIX_Shutdown(
  216. VOID *object
  217. )
  218. /*++
  219. Purpose:
  220. Shutdown the RIX player subsystem.
  221. Parameters:
  222. None.
  223. Return value:
  224. None.
  225. --*/
  226. {
  227. if (object != NULL)
  228. {
  229. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  230. pRixPlayer->fReady = FALSE;
  231. for (int i = 0; i < gpGlobals->iAudioChannels; i++)
  232. if (pRixPlayer->resampler[i])
  233. resampler_delete(pRixPlayer->resampler[i]);
  234. delete pRixPlayer->rix;
  235. delete pRixPlayer->opl;
  236. delete pRixPlayer;
  237. }
  238. }
  239. static BOOL
  240. RIX_Play(
  241. VOID *object,
  242. INT iNumRIX,
  243. BOOL fLoop,
  244. FLOAT flFadeTime
  245. )
  246. /*++
  247. Purpose:
  248. Start playing the specified music.
  249. Parameters:
  250. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  251. [IN] fLoop - Whether the music should be looped or not.
  252. [IN] flFadeTime - the fade in/out time when switching music.
  253. Return value:
  254. None.
  255. --*/
  256. {
  257. LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
  258. //
  259. // Check for NULL pointer.
  260. //
  261. if (pRixPlayer == NULL)
  262. {
  263. return FALSE;
  264. }
  265. //
  266. // Stop the current CD music.
  267. //
  268. SOUND_PlayCDA(-1);
  269. pRixPlayer->fNextLoop = fLoop;
  270. if (iNumRIX == pRixPlayer->iCurrentMusic)
  271. {
  272. return TRUE;
  273. }
  274. pRixPlayer->iNextMusic = iNumRIX;
  275. pRixPlayer->dwStartFadeTime = SDL_GetTicks();
  276. pRixPlayer->dwFadeLength = (DWORD)(flFadeTime * 1000) / 2;
  277. pRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  278. pRixPlayer->fReady = TRUE;
  279. return TRUE;
  280. }
  281. LPMUSICPLAYER
  282. RIX_Init(
  283. LPCSTR szFileName
  284. )
  285. /*++
  286. Purpose:
  287. Initialize the RIX player subsystem.
  288. Parameters:
  289. [IN] szFileName - Filename of the mus.mkf file.
  290. Return value:
  291. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  292. --*/
  293. {
  294. LPRIXPLAYER pRixPlayer = new RIXPLAYER;
  295. if (pRixPlayer == NULL)
  296. {
  297. return NULL;
  298. }
  299. else
  300. {
  301. memset(pRixPlayer, 0, sizeof(RIXPLAYER));
  302. pRixPlayer->FillBuffer = RIX_FillBuffer;
  303. pRixPlayer->Shutdown = RIX_Shutdown;
  304. pRixPlayer->Play = RIX_Play;
  305. }
  306. if (gpGlobals->fUseSurroundOPL)
  307. {
  308. switch (gpGlobals->eOPLType)
  309. {
  310. case OPL_DOSBOX_OLD:
  311. pRixPlayer->opl = new CSurroundopl(
  312. new CDemuopl(gpGlobals->iOPLSampleRate, true, false),
  313. new CDemuopl(gpGlobals->iOPLSampleRate, true, false),
  314. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  315. break;
  316. case OPL_DOSBOX:
  317. pRixPlayer->opl = new CSurroundopl(
  318. new CDBemuopl(gpGlobals->iOPLSampleRate, true, false),
  319. new CDBemuopl(gpGlobals->iOPLSampleRate, true, false),
  320. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  321. break;
  322. case OPL_MAME:
  323. pRixPlayer->opl = new CSurroundopl(
  324. new CEmuopl(gpGlobals->iOPLSampleRate, true, false),
  325. new CEmuopl(gpGlobals->iOPLSampleRate, true, false),
  326. true, gpGlobals->iOPLSampleRate, gpGlobals->dSurroundOPLOffset);
  327. break;
  328. }
  329. }
  330. else
  331. {
  332. switch (gpGlobals->eOPLType)
  333. {
  334. case OPL_DOSBOX_OLD:
  335. pRixPlayer->opl = new CDemuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  336. break;
  337. case OPL_DOSBOX:
  338. pRixPlayer->opl = new CDBemuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  339. break;
  340. case OPL_MAME:
  341. pRixPlayer->opl = new CEmuopl(gpGlobals->iOPLSampleRate, true, gpGlobals->iAudioChannels == 2);
  342. break;
  343. }
  344. }
  345. if (pRixPlayer->opl == NULL)
  346. {
  347. delete pRixPlayer;
  348. return NULL;
  349. }
  350. pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
  351. if (pRixPlayer->rix == NULL)
  352. {
  353. delete pRixPlayer->opl;
  354. delete pRixPlayer;
  355. return NULL;
  356. }
  357. //
  358. // Load the MKF file.
  359. //
  360. if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  361. {
  362. delete pRixPlayer->rix;
  363. delete pRixPlayer->opl;
  364. delete pRixPlayer;
  365. pRixPlayer = NULL;
  366. return NULL;
  367. }
  368. if (gpGlobals->iOPLSampleRate != gpGlobals->iSampleRate)
  369. {
  370. for (int i = 0; i < gpGlobals->iAudioChannels; i++)
  371. {
  372. pRixPlayer->resampler[i] = resampler_create();
  373. resampler_set_quality(pRixPlayer->resampler[i], SOUND_IsIntegerConversion(gpGlobals->iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gpGlobals->iResampleQuality);
  374. resampler_set_rate(pRixPlayer->resampler[i], (double)gpGlobals->iOPLSampleRate / (double)gpGlobals->iSampleRate);
  375. }
  376. }
  377. if (gpGlobals->pExtraFMRegs && gpGlobals->pExtraFMVals)
  378. {
  379. pRixPlayer->rix->set_extra_init(gpGlobals->pExtraFMRegs, gpGlobals->pExtraFMVals, gpGlobals->dwExtraLength);
  380. }
  381. //
  382. // Success.
  383. //
  384. pRixPlayer->FadeType = RIXPLAYER::NONE;
  385. pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
  386. pRixPlayer->pos = NULL;
  387. pRixPlayer->fLoop = FALSE;
  388. pRixPlayer->fNextLoop = FALSE;
  389. pRixPlayer->fReady = FALSE;
  390. return pRixPlayer;
  391. }