audio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. #if SDL_VERSION_ATLEAST(2,0,0)
  107. memset(stream, 0, len);
  108. #endif
  109. gAudioDevice.cvt.buf = stream;
  110. gAudioDevice.cvt.len = len;
  111. //
  112. // Play music
  113. //
  114. if (gAudioDevice.fMusicEnabled && gAudioDevice.iMusicVolume > 0)
  115. {
  116. if (gAudioDevice.pMusPlayer)
  117. {
  118. gAudioDevice.pMusPlayer->FillBuffer(gAudioDevice.pMusPlayer, stream, len);
  119. }
  120. if (gAudioDevice.pCDPlayer)
  121. {
  122. gAudioDevice.pCDPlayer->FillBuffer(gAudioDevice.pCDPlayer, stream, len);
  123. }
  124. //
  125. // Adjust volume for music
  126. //
  127. AUDIO_AdjustVolume((short *)stream, gAudioDevice.iMusicVolume, len >> 1);
  128. }
  129. //
  130. // Play sound
  131. //
  132. if (gAudioDevice.fSoundEnabled && gAudioDevice.pSoundPlayer && gAudioDevice.iSoundVolume > 0)
  133. {
  134. memset(gAudioDevice.pSoundBuffer, 0, len);
  135. gAudioDevice.pSoundPlayer->FillBuffer(gAudioDevice.pSoundPlayer, gAudioDevice.pSoundBuffer, len);
  136. //
  137. // Adjust volume for sound
  138. //
  139. AUDIO_AdjustVolume((short *)gAudioDevice.pSoundBuffer, gAudioDevice.iSoundVolume, len >> 1);
  140. //
  141. // Mix sound & music
  142. //
  143. AUDIO_MixNative((short *)stream, gAudioDevice.pSoundBuffer, len >> 1);
  144. }
  145. //
  146. // Convert audio from native byte-order to actual byte-order
  147. //
  148. SDL_ConvertAudio(&gAudioDevice.cvt);
  149. }
  150. INT
  151. AUDIO_OpenDevice(
  152. VOID
  153. )
  154. /*++
  155. Purpose:
  156. Initialize the audio subsystem.
  157. Parameters:
  158. None.
  159. Return value:
  160. 0 if succeed, others if failed.
  161. --*/
  162. {
  163. SDL_AudioSpec spec;
  164. if (gAudioDevice.fOpened)
  165. {
  166. //
  167. // Already opened
  168. //
  169. return -1;
  170. }
  171. gAudioDevice.fOpened = FALSE;
  172. gAudioDevice.fMusicEnabled = TRUE;
  173. gAudioDevice.fSoundEnabled = TRUE;
  174. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  175. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  176. //
  177. // Initialize the resampler module
  178. //
  179. resampler_init();
  180. //
  181. // Open the audio device.
  182. //
  183. gAudioDevice.spec.freq = gConfig.iSampleRate;
  184. gAudioDevice.spec.format = AUDIO_S16;
  185. gAudioDevice.spec.channels = gConfig.iAudioChannels;
  186. gAudioDevice.spec.samples = gConfig.wAudioBufferSize;
  187. gAudioDevice.spec.callback = AUDIO_FillBuffer;
  188. if (SDL_OpenAudio(&gAudioDevice.spec, &spec) < 0)
  189. {
  190. //
  191. // Failed
  192. //
  193. return -3;
  194. }
  195. else
  196. {
  197. gAudioDevice.spec = spec;
  198. gAudioDevice.pSoundBuffer = malloc(spec.size);
  199. }
  200. SDL_BuildAudioCVT(&gAudioDevice.cvt, AUDIO_S16SYS, spec.channels, spec.freq, spec.format, spec.channels, spec.freq);
  201. gAudioDevice.fOpened = TRUE;
  202. //
  203. // Initialize the sound subsystem.
  204. //
  205. gAudioDevice.pSoundPlayer = SOUND_Init();
  206. //
  207. // Initialize the music subsystem.
  208. //
  209. switch (gConfig.eMusicType)
  210. {
  211. case MUSIC_RIX:
  212. if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"))))
  213. {
  214. gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"));
  215. }
  216. break;
  217. case MUSIC_MP3:
  218. #if PAL_HAS_MP3
  219. gAudioDevice.pMusPlayer = MP3_Init();
  220. #else
  221. gAudioDevice.pMusPlayer = NULL;
  222. #endif
  223. break;
  224. case MUSIC_OGG:
  225. #if PAL_HAS_OGG
  226. gAudioDevice.pMusPlayer = OGG_Init();
  227. #else
  228. gAudioDevice.pMusPlayer = NULL;
  229. #endif
  230. break;
  231. case MUSIC_MIDI:
  232. gAudioDevice.pMusPlayer = NULL;
  233. break;
  234. }
  235. //
  236. // Initialize the CD audio.
  237. //
  238. switch (gConfig.eCDType)
  239. {
  240. case MUSIC_SDLCD:
  241. {
  242. #if PAL_HAS_SDLCD
  243. int i;
  244. gAudioDevice.pCD = NULL;
  245. for (i = 0; i < SDL_CDNumDrives(); i++)
  246. {
  247. gAudioDevice.pCD = SDL_CDOpen(i);
  248. if (gAudioDevice.pCD != NULL)
  249. {
  250. if (!CD_INDRIVE(SDL_CDStatus(gAudioDevice.pCD)))
  251. {
  252. SDL_CDClose(gAudioDevice.pCD);
  253. gAudioDevice.pCD = NULL;
  254. }
  255. else
  256. {
  257. break;
  258. }
  259. }
  260. }
  261. #endif
  262. gAudioDevice.pCDPlayer = NULL;
  263. break;
  264. }
  265. case MUSIC_MP3:
  266. #if PAL_HAS_MP3
  267. gAudioDevice.pCDPlayer = MP3_Init();
  268. #else
  269. gAudioDevice.pCDPlayer = NULL;
  270. #endif
  271. break;
  272. case MUSIC_OGG:
  273. #if PAL_HAS_OGG
  274. gAudioDevice.pCDPlayer = OGG_Init();
  275. #else
  276. gAudioDevice.pCDPlayer = NULL;
  277. #endif
  278. break;
  279. }
  280. //
  281. // Let the callback function run so that musics will be played.
  282. //
  283. SDL_PauseAudio(0);
  284. return 0;
  285. }
  286. VOID
  287. AUDIO_CloseDevice(
  288. VOID
  289. )
  290. /*++
  291. Purpose:
  292. Close the audio subsystem.
  293. Parameters:
  294. None.
  295. Return value:
  296. None.
  297. --*/
  298. {
  299. SDL_CloseAudio();
  300. if (gAudioDevice.pSoundPlayer != NULL)
  301. {
  302. gAudioDevice.pSoundPlayer->Shutdown(gAudioDevice.pSoundPlayer);
  303. gAudioDevice.pSoundPlayer = NULL;
  304. }
  305. if (gAudioDevice.pMusPlayer)
  306. {
  307. gAudioDevice.pMusPlayer->Shutdown(gAudioDevice.pMusPlayer);
  308. gAudioDevice.pMusPlayer = NULL;
  309. }
  310. if (gAudioDevice.pCDPlayer)
  311. {
  312. gAudioDevice.pCDPlayer->Shutdown(gAudioDevice.pCDPlayer);
  313. gAudioDevice.pCDPlayer = NULL;
  314. }
  315. #if PAL_HAS_SDLCD
  316. if (gAudioDevice.pCD != NULL)
  317. {
  318. AUDIO_PlayCDTrack(-1);
  319. SDL_CDClose(gAudioDevice.pCD);
  320. }
  321. #endif
  322. if (gAudioDevice.pSoundBuffer != NULL)
  323. {
  324. free(gAudioDevice.pSoundBuffer);
  325. gAudioDevice.pSoundBuffer = NULL;
  326. }
  327. #if PAL_HAS_NATIVEMIDI
  328. if (gConfig.eMusicType == MUSIC_MIDI) MIDI_Play(0, FALSE);
  329. #endif
  330. }
  331. SDL_AudioSpec*
  332. AUDIO_GetDeviceSpec(
  333. VOID
  334. )
  335. {
  336. return &gAudioDevice.spec;
  337. }
  338. static INT
  339. AUDIO_ChangeVolumeByValue(
  340. INT *iVolume,
  341. INT iValue
  342. )
  343. {
  344. *iVolume += iValue;
  345. if (*iVolume > PAL_MAX_VOLUME)
  346. *iVolume = PAL_MAX_VOLUME;
  347. else if (*iVolume < 0)
  348. *iVolume = 0;
  349. return *iVolume;
  350. }
  351. VOID
  352. AUDIO_IncreaseVolume(
  353. VOID
  354. )
  355. /*++
  356. Purpose:
  357. Increase global volume by 3%.
  358. Parameters:
  359. None.
  360. Return value:
  361. None.
  362. --*/
  363. {
  364. AUDIO_ChangeVolumeByValue(&gConfig.iMusicVolume, 3);
  365. AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, 3);
  366. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  367. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  368. }
  369. VOID
  370. AUDIO_DecreaseVolume(
  371. VOID
  372. )
  373. /*++
  374. Purpose:
  375. Decrease global volume by 3%.
  376. Parameters:
  377. None.
  378. Return value:
  379. None.
  380. --*/
  381. {
  382. AUDIO_ChangeVolumeByValue(&gConfig.iMusicVolume, -3);
  383. AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, -3);
  384. gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  385. gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
  386. }
  387. VOID
  388. AUDIO_PlaySound(
  389. INT iSoundNum
  390. )
  391. /*++
  392. Purpose:
  393. Play a sound in voc.mkf/sounds.mkf file.
  394. Parameters:
  395. [IN] iSoundNum - number of the sound; the absolute value is used.
  396. Return value:
  397. None.
  398. --*/
  399. {
  400. // Unlike musics that use the 'load as required' strategy, sound player
  401. // load the entire sound file at once, which may cause about 0.5s or longer
  402. // latency for large sound files. To prevent this latency affects audio playing,
  403. // the mutex lock is obtained inside the SOUND_Play function rather than here.
  404. if (gAudioDevice.pSoundPlayer)
  405. {
  406. gAudioDevice.pSoundPlayer->Play(gAudioDevice.pSoundPlayer, abs(iSoundNum), FALSE, 0.0f);
  407. }
  408. }
  409. VOID
  410. AUDIO_PlayMusic(
  411. INT iNumRIX,
  412. BOOL fLoop,
  413. FLOAT flFadeTime
  414. )
  415. {
  416. #if PAL_HAS_NATIVEMIDI
  417. if (gConfig.eMusicType == MUSIC_MIDI)
  418. {
  419. MIDI_Play(iNumRIX, fLoop);
  420. return;
  421. }
  422. #endif
  423. SDL_LockAudio();
  424. if (gAudioDevice.pMusPlayer)
  425. {
  426. gAudioDevice.pMusPlayer->Play(gAudioDevice.pMusPlayer, iNumRIX, fLoop, flFadeTime);
  427. }
  428. SDL_UnlockAudio();
  429. }
  430. BOOL
  431. AUDIO_PlayCDTrack(
  432. INT iNumTrack
  433. )
  434. /*++
  435. Purpose:
  436. Play a CD Audio Track.
  437. Parameters:
  438. [IN] iNumTrack - number of the CD Audio Track.
  439. Return value:
  440. TRUE if the track can be played, FALSE if not.
  441. --*/
  442. {
  443. BOOL ret = FALSE;
  444. #if PAL_HAS_SDLCD
  445. if (gAudioDevice.pCD != NULL)
  446. {
  447. if (CD_INDRIVE(SDL_CDStatus(gAudioDevice.pCD)))
  448. {
  449. SDL_CDStop(gAudioDevice.pCD);
  450. if (iNumTrack != -1)
  451. {
  452. AUDIO_PlayMusic(-1, FALSE, 0);
  453. if (SDL_CDPlayTracks(gAudioDevice.pCD, iNumTrack - 1, 0, 1, 0) == 0)
  454. {
  455. return TRUE;
  456. }
  457. }
  458. }
  459. }
  460. #endif
  461. SDL_LockAudio();
  462. if (gAudioDevice.pCDPlayer)
  463. {
  464. if (iNumTrack != -1)
  465. {
  466. AUDIO_PlayMusic(-1, FALSE, 0);
  467. ret = gAudioDevice.pCDPlayer->Play(gAudioDevice.pCDPlayer, PAL_CDTRACK_BASE + iNumTrack, TRUE, 0);
  468. }
  469. else
  470. {
  471. ret = gAudioDevice.pCDPlayer->Play(gAudioDevice.pCDPlayer, -1, FALSE, 0);
  472. }
  473. }
  474. SDL_UnlockAudio();
  475. return ret;
  476. }
  477. VOID
  478. AUDIO_EnableMusic(
  479. BOOL fEnable
  480. )
  481. {
  482. gAudioDevice.fMusicEnabled = fEnable;
  483. }
  484. BOOL
  485. AUDIO_MusicEnabled(
  486. VOID
  487. )
  488. {
  489. return gAudioDevice.fMusicEnabled;
  490. }
  491. VOID
  492. AUDIO_EnableSound(
  493. BOOL fEnable
  494. )
  495. {
  496. gAudioDevice.fSoundEnabled = fEnable;
  497. }
  498. BOOL
  499. AUDIO_SoundEnabled(
  500. VOID
  501. )
  502. {
  503. return gAudioDevice.fSoundEnabled;
  504. }