rixplay.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  3. // All rights reserved.
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. #include "rixplay.h"
  19. #define USE_SURROUNDOPL 0
  20. #define USE_DEMUOPL 0
  21. #include "adplug/opl.h"
  22. #include "adplug/emuopl.h"
  23. #include "adplug/demuopl.h"
  24. #include "adplug/surroundopl.h"
  25. #include "adplug/rix.h"
  26. extern "C" BOOL g_fNoMusic;
  27. extern "C" INT g_iVolume;
  28. #include "sound.h"
  29. typedef struct tagRIXPLAYER
  30. {
  31. tagRIXPLAYER() : iCurrentMusic(-1) {}
  32. Copl *opl;
  33. CrixPlayer *rix;
  34. INT iCurrentMusic; // current playing music number
  35. INT iNextMusic; // the next music number to switch to
  36. DWORD dwStartFadeTime;
  37. DWORD dwEndFadeTime;
  38. enum { FADE_IN, FADE_OUT } FadeType; // fade in or fade out ?
  39. BOOL fLoop;
  40. BOOL fNextLoop;
  41. BYTE buf[PAL_SAMPLE_RATE / 70 * 2 * PAL_CHANNELS];
  42. LPBYTE pos;
  43. } RIXPLAYER, *LPRIXPLAYER;
  44. static LPRIXPLAYER gpRixPlayer = NULL;
  45. VOID
  46. RIX_FillBuffer(
  47. LPBYTE stream,
  48. INT len
  49. )
  50. /*++
  51. Purpose:
  52. Fill the background music into the sound buffer. Called by the SDL sound
  53. callback function only (sound.c: SOUND_FillAudio).
  54. Parameters:
  55. [OUT] stream - pointer to the stream buffer.
  56. [IN] len - Length of the buffer.
  57. Return value:
  58. None.
  59. --*/
  60. {
  61. INT i, l, oldlen, volume = SDL_MIX_MAXVOLUME / 2;
  62. UINT t = SDL_GetTicks();
  63. #ifdef __SYMBIAN32__
  64. volume = g_iVolume / 2;
  65. #endif
  66. oldlen = len;
  67. if (gpRixPlayer == NULL)
  68. {
  69. //
  70. // Not initialized
  71. //
  72. return;
  73. }
  74. //
  75. // fading in or fading out
  76. //
  77. if (gpRixPlayer->dwEndFadeTime > 0)
  78. {
  79. switch (gpRixPlayer->FadeType)
  80. {
  81. case RIXPLAYER::FADE_IN:
  82. if (t >= gpRixPlayer->dwEndFadeTime)
  83. {
  84. gpRixPlayer->dwEndFadeTime = 0;
  85. }
  86. else
  87. {
  88. volume = (INT)(volume * (t - gpRixPlayer->dwStartFadeTime) /
  89. (FLOAT)(gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime));
  90. }
  91. break;
  92. case RIXPLAYER::FADE_OUT:
  93. if (gpRixPlayer->iCurrentMusic == -1)
  94. {
  95. //
  96. // There is no current playing music. Just start playing the next one.
  97. //
  98. gpRixPlayer->iCurrentMusic = gpRixPlayer->iNextMusic;
  99. gpRixPlayer->fLoop = gpRixPlayer->fNextLoop;
  100. gpRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  101. gpRixPlayer->dwEndFadeTime = t +
  102. (gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime);
  103. gpRixPlayer->dwStartFadeTime = t;
  104. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  105. return;
  106. }
  107. else if (t >= gpRixPlayer->dwEndFadeTime)
  108. {
  109. if (gpRixPlayer->iNextMusic <= 0)
  110. {
  111. gpRixPlayer->iCurrentMusic = -1;
  112. gpRixPlayer->dwEndFadeTime = 0;
  113. }
  114. else
  115. {
  116. //
  117. // Fade to the next music
  118. //
  119. gpRixPlayer->iCurrentMusic = gpRixPlayer->iNextMusic;
  120. gpRixPlayer->fLoop = gpRixPlayer->fNextLoop;
  121. gpRixPlayer->FadeType = RIXPLAYER::FADE_IN;
  122. gpRixPlayer->dwEndFadeTime = t +
  123. (gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime);
  124. gpRixPlayer->dwStartFadeTime = t;
  125. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  126. }
  127. return;
  128. }
  129. volume = (INT)(volume * (1.0f - (t - gpRixPlayer->dwStartFadeTime) /
  130. (FLOAT)(gpRixPlayer->dwEndFadeTime - gpRixPlayer->dwStartFadeTime)));
  131. break;
  132. }
  133. }
  134. if (gpRixPlayer->iCurrentMusic <= 0)
  135. {
  136. //
  137. // No current playing music
  138. //
  139. return;
  140. }
  141. //
  142. // Fill the buffer with sound data
  143. //
  144. while (len > 0)
  145. {
  146. if (gpRixPlayer->pos == NULL ||
  147. gpRixPlayer->pos - gpRixPlayer->buf >= (int)sizeof(gpRixPlayer->buf))
  148. {
  149. gpRixPlayer->pos = gpRixPlayer->buf;
  150. if (!gpRixPlayer->rix->update())
  151. {
  152. if (!gpRixPlayer->fLoop)
  153. {
  154. //
  155. // Not loop, simply terminate the music
  156. //
  157. gpRixPlayer->iCurrentMusic = -1;
  158. return;
  159. }
  160. gpRixPlayer->rix->rewind(gpRixPlayer->iCurrentMusic);
  161. if (!gpRixPlayer->rix->update())
  162. {
  163. //
  164. // Something must be wrong
  165. //
  166. gpRixPlayer->iCurrentMusic = -1;
  167. return;
  168. }
  169. }
  170. gpRixPlayer->opl->update((short *)(gpRixPlayer->buf), PAL_SAMPLE_RATE / 70);
  171. }
  172. l = sizeof(gpRixPlayer->buf) - (gpRixPlayer->pos - gpRixPlayer->buf);
  173. if (len < l)
  174. {
  175. l = len;
  176. }
  177. //
  178. // Put audio data into buffer and adjust volume
  179. // WARNING: for signed 16-bit little-endian only
  180. //
  181. for (i = 0; i < (int)(l / sizeof(SHORT)); i++)
  182. {
  183. SHORT s = SWAP16((int)(*(SHORT *)(gpRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME);
  184. #if !USE_SURROUNDOPL
  185. for (int j = 0; j < PAL_CHANNELS; j++)
  186. #endif
  187. {
  188. *(SHORT *)(stream) = s;
  189. stream += sizeof(SHORT);
  190. }
  191. gpRixPlayer->pos += sizeof(SHORT);
  192. }
  193. len -= l;
  194. }
  195. stream -= oldlen;
  196. }
  197. INT
  198. RIX_Init(
  199. LPCSTR szFileName
  200. )
  201. /*++
  202. Purpose:
  203. Initialize the RIX player subsystem.
  204. Parameters:
  205. [IN] szFileName - Filename of the mus.mkf file.
  206. Return value:
  207. 0 if success, -1 if cannot allocate memory, -2 if file not found.
  208. --*/
  209. {
  210. gpRixPlayer = new RIXPLAYER;
  211. if (gpRixPlayer == NULL)
  212. {
  213. return -1;
  214. }
  215. #if USE_SURROUNDOPL && (PAL_CHANNELS == 2)
  216. # if USE_DEMUOPL
  217. gpRixPlayer->opl = new CSurroundopl(new CDemuopl(PAL_SAMPLE_RATE, true, false),
  218. new CDemuopl(PAL_SAMPLE_RATE, true, false), true);
  219. # else
  220. gpRixPlayer->opl = new CSurroundopl(new CEmuopl(PAL_SAMPLE_RATE, true, false),
  221. new CEmuopl(PAL_SAMPLE_RATE, true, false), true);
  222. # endif
  223. #else
  224. # if USE_DEMUOPL
  225. gpRixPlayer->opl = new CDemuopl(PAL_SAMPLE_RATE, true, false);
  226. # else
  227. gpRixPlayer->opl = new CEmuopl(PAL_SAMPLE_RATE, true, false);
  228. # endif
  229. #endif
  230. if (gpRixPlayer->opl == NULL)
  231. {
  232. delete gpRixPlayer;
  233. return -1;
  234. }
  235. gpRixPlayer->rix = new CrixPlayer(gpRixPlayer->opl);
  236. if (gpRixPlayer->rix == NULL)
  237. {
  238. delete gpRixPlayer->opl;
  239. delete gpRixPlayer;
  240. return -1;
  241. }
  242. //
  243. // Load the MKF file.
  244. //
  245. if (!gpRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
  246. {
  247. delete gpRixPlayer->rix;
  248. delete gpRixPlayer->opl;
  249. delete gpRixPlayer;
  250. gpRixPlayer = NULL;
  251. return -2;
  252. }
  253. //
  254. // Success.
  255. //
  256. gpRixPlayer->iCurrentMusic = -1;
  257. gpRixPlayer->dwEndFadeTime = 0;
  258. gpRixPlayer->pos = NULL;
  259. gpRixPlayer->fLoop = FALSE;
  260. gpRixPlayer->fNextLoop = FALSE;
  261. return 0;
  262. }
  263. VOID
  264. RIX_Shutdown(
  265. VOID
  266. )
  267. /*++
  268. Purpose:
  269. Shutdown the RIX player subsystem.
  270. Parameters:
  271. None.
  272. Return value:
  273. None.
  274. --*/
  275. {
  276. if (gpRixPlayer != NULL)
  277. {
  278. delete gpRixPlayer->rix;
  279. delete gpRixPlayer->opl;
  280. delete gpRixPlayer;
  281. gpRixPlayer = NULL;
  282. }
  283. }
  284. VOID
  285. RIX_Play(
  286. INT iNumRIX,
  287. BOOL fLoop,
  288. FLOAT flFadeTime
  289. )
  290. /*++
  291. Purpose:
  292. Start playing the specified music.
  293. Parameters:
  294. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  295. [IN] fLoop - Whether the music should be looped or not.
  296. [IN] flFadeTime - the fade in/out time when switching music.
  297. Return value:
  298. None.
  299. --*/
  300. {
  301. //
  302. // Check for NULL pointer.
  303. //
  304. if (gpRixPlayer == NULL)
  305. {
  306. return;
  307. }
  308. //
  309. // Stop the current CD music.
  310. //
  311. SOUND_PlayCDA(-1);
  312. DWORD t = SDL_GetTicks();
  313. gpRixPlayer->fNextLoop = fLoop;
  314. if (iNumRIX == gpRixPlayer->iCurrentMusic && !g_fNoMusic)
  315. {
  316. return;
  317. }
  318. gpRixPlayer->iNextMusic = iNumRIX;
  319. gpRixPlayer->dwStartFadeTime = t;
  320. gpRixPlayer->dwEndFadeTime = t + (DWORD)(flFadeTime * 1000) / 2;
  321. gpRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
  322. }