rixplay.cpp 9.3 KB

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