rixplay.cpp 9.7 KB

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