audio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. #include "palcommon.h"
  22. #include "global.h"
  23. #include "palcfg.h"
  24. #include "audio.h"
  25. #include "players.h"
  26. #include "util.h"
  27. #include "resampler.h"
  28. #include "midi.h"
  29. #include <math.h>
  30. #if PAL_HAS_OGG
  31. #include <vorbis/codec.h>
  32. #endif
  33. #define PAL_CDTRACK_BASE 10000
  34. typedef void(*ResampleMixFunction)(void *, const void *, int, void *, int, int, uint8_t);
  35. typedef struct tagAUDIODEVICE
  36. {
  37. SDL_AudioSpec spec; /* Actual-used sound specification */
  38. SDL_AudioCVT cvt; /* Audio format conversion parameter */
  39. AUDIOPLAYER *pMusPlayer;
  40. AUDIOPLAYER *pCDPlayer;
  41. #if PAL_HAS_SDLCD
  42. SDL_CD *pCD;
  43. #endif
  44. AUDIOPLAYER *pSoundPlayer;
  45. void *pSoundBuffer; /* The output buffer for sound */
  46. INT iMusicVolume; /* The BGM volume ranged in [0, 128] for better performance */
  47. INT iSoundVolume; /* The sound effect volume ranged in [0, 128] for better performance */
  48. BOOL fMusicEnabled; /* Is BGM enabled? */
  49. BOOL fSoundEnabled; /* Is sound effect enabled? */
  50. BOOL fOpened; /* Is the audio device opened? */
  51. } AUDIODEVICE;
  52. static AUDIODEVICE gAudioDevice;
  53. PAL_FORCE_INLINE
  54. void
  55. AUDIO_MixNative(
  56. short *dst,
  57. short *src,
  58. int samples
  59. )
  60. {
  61. while (samples > 0)
  62. {
  63. int val = *src++ + *dst;
  64. if (val > SHRT_MAX)
  65. *dst++ = SHRT_MAX;
  66. else if (val < SHRT_MIN)
  67. *dst++ = SHRT_MIN;
  68. else
  69. *dst++ = (short)val;
  70. samples--;
  71. }
  72. }
  73. PAL_FORCE_INLINE
  74. void
  75. AUDIO_AdjustVolume(
  76. short *srcdst,
  77. int iVolume,
  78. int samples
  79. )
  80. {
  81. if (iVolume == SDL_MIX_MAXVOLUME) return;
  82. if (iVolume == 0) { memset(srcdst, 0, samples << 1); return; }
  83. while (samples > 0)
  84. {
  85. *srcdst = *srcdst * iVolume / SDL_MIX_MAXVOLUME;
  86. samples--; srcdst++;
  87. }
  88. }
  89. static VOID SDLCALL
  90. AUDIO_FillBuffer(
  91. LPVOID udata,
  92. LPBYTE stream,
  93. INT len
  94. )
  95. /*++
  96. Purpose:
  97. SDL sound callback function.
  98. Parameters:
  99. [IN] udata - pointer to user-defined parameters (Not used).
  100. [OUT] stream - pointer to the stream buffer.
  101. [IN] len - Length of the buffer.
  102. Return value:
  103. None.
  104. --*/
  105. {
  106. memset(stream, 0, len);
  107. gAudioDevice.cvt.buf = stream;
  108. gAudioDevice.cvt.len = len;
  109. //
  110. // Play music
  111. //
  112. if (gAudioDevice.fMusicEnabled && gAudioDevice.iMusicVolume > 0)
  113. {
  114. if (gAudioDevice.pMusPlayer)
  115. {
  116. gAudioDevice.pMusPlayer->FillBuffer(gAudioDevice.pMusPlayer, stream, len);
  117. }
  118. if (gAudioDevice.pCDPlayer)
  119. {
  120. gAudioDevice.pCDPlayer->FillBuffer(gAudioDevice.pCDPlayer, stream, len);
  121. }
  122. //
  123. // Adjust volume for music
  124. //
  125. AUDIO_AdjustVolume((short *)stream, gAudioDevice.iMusicVolume, len >> 1);
  126. }
  127. //
  128. // Play sound
  129. //
  130. if (gAudioDevice.fSoundEnabled && gAudioDevice.pSoundPlayer && gAudioDevice.iSoundVolume > 0)
  131. {
  132. memset(gAudioDevice.pSoundBuffer, 0, len);
  133. gAudioDevice.pSoundPlayer->FillBuffer(gAudioDevice.pSoundPlayer, gAudioDevice.pSoundBuffer, len);
  134. //
  135. // Adjust volume for sound
  136. //
  137. AUDIO_AdjustVolume((short *)gAudioDevice.pSoundBuffer, gAudioDevice.iSoundVolume, len >> 1);
  138. //
  139. // Mix sound & music
  140. //
  141. AUDIO_MixNative((short *)stream, gAudioDevice.pSoundBuffer, len >> 1);
  142. }
  143. //
  144. // Convert audio from native byte-order to actual byte-order
  145. //
  146. SDL_ConvertAudio(&gAudioDevice.cvt);
  147. }
  148. INT
  149. AUDIO_OpenDevice(
  150. VOID
  151. )
  152. /*++
  153. Purpose:
  154. Initialize the audio subsystem.
  155. Parameters:
  156. None.
  157. Return value:
  158. 0 if succeed, others if failed.
  159. --*/
  160. {
  161. SDL_AudioSpec spec;
  162. if (gAudioDevice.fOpened)
  163. {
  164. //
  165. // Already opened
  166. //
  167. return -1;
  168. }
  169. #ifdef __EMSCRIPTEN__ // Now either music/sound enabled will makes whole app crash in emscripten. Disabled until a solution is found.
  170. return -1;
  171. #endif
  172. gAudioDevice.fOpened = FALSE;
  173. gAudioDevice.fMusicEnabled = TRUE;
  174. gAudioDevice.fSoundEnabled = TRUE;
  175. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  176. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  177. //
  178. // Initialize the resampler module
  179. //
  180. resampler_init();
  181. //
  182. // Open the audio device.
  183. //
  184. gAudioDevice.spec.freq = gConfig.iSampleRate;
  185. gAudioDevice.spec.format = AUDIO_S16;
  186. gAudioDevice.spec.channels = gConfig.iAudioChannels;
  187. gAudioDevice.spec.samples = gConfig.wAudioBufferSize;
  188. gAudioDevice.spec.callback = AUDIO_FillBuffer;
  189. if (SDL_OpenAudio(&gAudioDevice.spec, &spec) < 0)
  190. {
  191. //
  192. // Failed
  193. //
  194. return -3;
  195. }
  196. else
  197. {
  198. gAudioDevice.spec = spec;
  199. gAudioDevice.pSoundBuffer = malloc(spec.size);
  200. }
  201. SDL_BuildAudioCVT(&gAudioDevice.cvt, AUDIO_S16SYS, spec.channels, spec.freq, spec.format, spec.channels, spec.freq);
  202. gAudioDevice.fOpened = TRUE;
  203. //
  204. // Initialize the sound subsystem.
  205. //
  206. gAudioDevice.pSoundPlayer = SOUND_Init();
  207. //
  208. // Initialize the music subsystem.
  209. //
  210. switch (gConfig.eMusicType)
  211. {
  212. case MUSIC_RIX:
  213. if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"))))
  214. {
  215. gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"));
  216. }
  217. break;
  218. case MUSIC_MP3:
  219. #if PAL_HAS_MP3
  220. gAudioDevice.pMusPlayer = MP3_Init();
  221. #else
  222. gAudioDevice.pMusPlayer = NULL;
  223. #endif
  224. break;
  225. case MUSIC_OGG:
  226. #if PAL_HAS_OGG
  227. gAudioDevice.pMusPlayer = OGG_Init();
  228. #else
  229. gAudioDevice.pMusPlayer = NULL;
  230. #endif
  231. break;
  232. case MUSIC_MIDI:
  233. gAudioDevice.pMusPlayer = NULL;
  234. break;
  235. }
  236. //
  237. // Initialize the CD audio.
  238. //
  239. switch (gConfig.eCDType)
  240. {
  241. case MUSIC_SDLCD:
  242. {
  243. #if PAL_HAS_SDLCD
  244. int i;
  245. gAudioDevice.pCD = NULL;
  246. for (i = 0; i < SDL_CDNumDrives(); i++)
  247. {
  248. gAudioDevice.pCD = SDL_CDOpen(i);
  249. if (gAudioDevice.pCD != NULL)
  250. {
  251. if (!CD_INDRIVE(SDL_CDStatus(gAudioDevice.pCD)))
  252. {
  253. SDL_CDClose(gAudioDevice.pCD);
  254. gAudioDevice.pCD = NULL;
  255. }
  256. else
  257. {
  258. break;
  259. }
  260. }
  261. }
  262. #endif
  263. gAudioDevice.pCDPlayer = NULL;
  264. break;
  265. }
  266. case MUSIC_MP3:
  267. #if PAL_HAS_MP3
  268. gAudioDevice.pCDPlayer = MP3_Init();
  269. #else
  270. gAudioDevice.pCDPlayer = NULL;
  271. #endif
  272. break;
  273. case MUSIC_OGG:
  274. #if PAL_HAS_OGG
  275. gAudioDevice.pCDPlayer = OGG_Init();
  276. #else
  277. gAudioDevice.pCDPlayer = NULL;
  278. #endif
  279. break;
  280. }
  281. //
  282. // Let the callback function run so that musics will be played.
  283. //
  284. SDL_PauseAudio(0);
  285. return 0;
  286. }
  287. VOID
  288. AUDIO_CloseDevice(
  289. VOID
  290. )
  291. /*++
  292. Purpose:
  293. Close the audio subsystem.
  294. Parameters:
  295. None.
  296. Return value:
  297. None.
  298. --*/
  299. {
  300. SDL_CloseAudio();
  301. if (gAudioDevice.pSoundPlayer != NULL)
  302. {
  303. gAudioDevice.pSoundPlayer->Shutdown(gAudioDevice.pSoundPlayer);
  304. gAudioDevice.pSoundPlayer = NULL;
  305. }
  306. if (gAudioDevice.pMusPlayer)
  307. {
  308. gAudioDevice.pMusPlayer->Shutdown(gAudioDevice.pMusPlayer);
  309. gAudioDevice.pMusPlayer = NULL;
  310. }
  311. if (gAudioDevice.pCDPlayer)
  312. {
  313. gAudioDevice.pCDPlayer->Shutdown(gAudioDevice.pCDPlayer);
  314. gAudioDevice.pCDPlayer = NULL;
  315. }
  316. #if PAL_HAS_SDLCD
  317. if (gAudioDevice.pCD != NULL)
  318. {
  319. AUDIO_PlayCDTrack(-1);
  320. SDL_CDClose(gAudioDevice.pCD);
  321. }
  322. #endif
  323. if (gAudioDevice.pSoundBuffer != NULL)
  324. {
  325. free(gAudioDevice.pSoundBuffer);
  326. gAudioDevice.pSoundBuffer = NULL;
  327. }
  328. #if PAL_HAS_NATIVEMIDI
  329. if (gConfig.eMusicType == MUSIC_MIDI) MIDI_Play(0, FALSE);
  330. #endif
  331. }
  332. SDL_AudioSpec*
  333. AUDIO_GetDeviceSpec(
  334. VOID
  335. )
  336. {
  337. return &gAudioDevice.spec;
  338. }
  339. static INT
  340. AUDIO_ChangeVolumeByValue(
  341. INT *iVolume,
  342. INT iValue
  343. )
  344. {
  345. *iVolume += iValue;
  346. if (*iVolume > PAL_MAX_VOLUME)
  347. *iVolume = PAL_MAX_VOLUME;
  348. else if (*iVolume < 0)
  349. *iVolume = 0;
  350. return *iVolume;
  351. }
  352. VOID
  353. AUDIO_IncreaseVolume(
  354. VOID
  355. )
  356. /*++
  357. Purpose:
  358. Increase global volume by 3%.
  359. Parameters:
  360. None.
  361. Return value:
  362. None.
  363. --*/
  364. {
  365. AUDIO_ChangeVolumeByValue(&gConfig.iMusicVolume, 3);
  366. AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, 3);
  367. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  368. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  369. }
  370. VOID
  371. AUDIO_DecreaseVolume(
  372. VOID
  373. )
  374. /*++
  375. Purpose:
  376. Decrease global volume by 3%.
  377. Parameters:
  378. None.
  379. Return value:
  380. None.
  381. --*/
  382. {
  383. AUDIO_ChangeVolumeByValue(&gConfig.iMusicVolume, -3);
  384. AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, -3);
  385. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  386. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  387. }
  388. VOID
  389. AUDIO_PlaySound(
  390. INT iSoundNum
  391. )
  392. /*++
  393. Purpose:
  394. Play a sound in voc.mkf/sounds.mkf file.
  395. Parameters:
  396. [IN] iSoundNum - number of the sound; the absolute value is used.
  397. Return value:
  398. None.
  399. --*/
  400. {
  401. // Unlike musics that use the 'load as required' strategy, sound player
  402. // load the entire sound file at once, which may cause about 0.5s or longer
  403. // latency for large sound files. To prevent this latency affects audio playing,
  404. // the mutex lock is obtained inside the SOUND_Play function rather than here.
  405. if (gAudioDevice.pSoundPlayer)
  406. {
  407. gAudioDevice.pSoundPlayer->Play(gAudioDevice.pSoundPlayer, abs(iSoundNum), FALSE, 0.0f);
  408. }
  409. }
  410. VOID
  411. AUDIO_PlayMusic(
  412. INT iNumRIX,
  413. BOOL fLoop,
  414. FLOAT flFadeTime
  415. )
  416. {
  417. #if PAL_HAS_NATIVEMIDI
  418. if (gConfig.eMusicType == MUSIC_MIDI)
  419. {
  420. MIDI_Play(iNumRIX, fLoop);
  421. return;
  422. }
  423. #endif
  424. SDL_LockAudio();
  425. if (gAudioDevice.pMusPlayer)
  426. {
  427. gAudioDevice.pMusPlayer->Play(gAudioDevice.pMusPlayer, iNumRIX, fLoop, flFadeTime);
  428. }
  429. SDL_UnlockAudio();
  430. }
  431. BOOL
  432. AUDIO_PlayCDTrack(
  433. INT iNumTrack
  434. )
  435. /*++
  436. Purpose:
  437. Play a CD Audio Track.
  438. Parameters:
  439. [IN] iNumTrack - number of the CD Audio Track.
  440. Return value:
  441. TRUE if the track can be played, FALSE if not.
  442. --*/
  443. {
  444. BOOL ret = FALSE;
  445. #if PAL_HAS_SDLCD
  446. if (gAudioDevice.pCD != NULL)
  447. {
  448. if (CD_INDRIVE(SDL_CDStatus(gAudioDevice.pCD)))
  449. {
  450. SDL_CDStop(gAudioDevice.pCD);
  451. if (iNumTrack != -1)
  452. {
  453. AUDIO_PlayMusic(-1, FALSE, 0);
  454. if (SDL_CDPlayTracks(gAudioDevice.pCD, iNumTrack - 1, 0, 1, 0) == 0)
  455. {
  456. return TRUE;
  457. }
  458. }
  459. }
  460. }
  461. #endif
  462. SDL_LockAudio();
  463. if (gAudioDevice.pCDPlayer)
  464. {
  465. if (iNumTrack != -1)
  466. {
  467. AUDIO_PlayMusic(-1, FALSE, 0);
  468. ret = gAudioDevice.pCDPlayer->Play(gAudioDevice.pCDPlayer, PAL_CDTRACK_BASE + iNumTrack, TRUE, 0);
  469. }
  470. else
  471. {
  472. ret = gAudioDevice.pCDPlayer->Play(gAudioDevice.pCDPlayer, -1, FALSE, 0);
  473. }
  474. }
  475. SDL_UnlockAudio();
  476. return ret;
  477. }
  478. VOID
  479. AUDIO_EnableMusic(
  480. BOOL fEnable
  481. )
  482. {
  483. gAudioDevice.fMusicEnabled = fEnable;
  484. }
  485. BOOL
  486. AUDIO_MusicEnabled(
  487. VOID
  488. )
  489. {
  490. return gAudioDevice.fMusicEnabled;
  491. }
  492. VOID
  493. AUDIO_EnableSound(
  494. BOOL fEnable
  495. )
  496. {
  497. gAudioDevice.fSoundEnabled = fEnable;
  498. }
  499. BOOL
  500. AUDIO_SoundEnabled(
  501. VOID
  502. )
  503. {
  504. return gAudioDevice.fSoundEnabled;
  505. }