rixplay.cpp 10.0 KB

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