rixplay.cpp 11 KB

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