rixplay.cpp 8.9 KB

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