sound.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 "sound.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 LPCBYTE(*FNLoadSoundData)(LPCBYTE, DWORD, SDL_AudioSpec *);
  35. typedef struct tagWAVEPLAYER
  36. {
  37. FNLoadSoundData LoadSoundData;
  38. void *resampler;
  39. short *buf;
  40. int buf_len, pos, len;
  41. } WAVEPLAYER;
  42. typedef struct tagSNDPLAYER
  43. {
  44. FILE *mkf;
  45. SDL_AudioSpec spec;
  46. SDL_AudioCVT cvt;
  47. SDL_mutex *mtx;
  48. MUSICPLAYER *pMusPlayer;
  49. MUSICPLAYER *pCDPlayer;
  50. #if PAL_HAS_SDLCD
  51. SDL_CD *pCD;
  52. #endif
  53. WAVEPLAYER wavePlayer;
  54. BOOL fOpened;
  55. BOOL fMusicEnabled;
  56. BOOL fSoundEnabled;
  57. } SNDPLAYER;
  58. static SNDPLAYER gSndPlayer;
  59. PAL_FORCE_INLINE
  60. void
  61. AUDIO_MixNative(
  62. short *dst,
  63. short *src,
  64. int samples
  65. )
  66. {
  67. while (samples > 0)
  68. {
  69. int val = *src++ + *dst;
  70. if (val > SHRT_MAX)
  71. *dst++ = SHRT_MAX;
  72. else if (val < SHRT_MIN)
  73. *dst++ = SHRT_MIN;
  74. else
  75. *dst++ = (short)val;
  76. samples--;
  77. }
  78. }
  79. PAL_FORCE_INLINE
  80. void
  81. AUDIO_MixNativeVolume(
  82. short *dst,
  83. int iDstVolume,
  84. short *src,
  85. int iSrcVolume,
  86. int samples
  87. )
  88. {
  89. while (samples > 0)
  90. {
  91. int val = ((*src++) * iSrcVolume + *dst * iDstVolume) / PAL_MAX_VOLUME;
  92. if (val > SHRT_MAX)
  93. *dst++ = SHRT_MAX;
  94. else if (val < SHRT_MIN)
  95. *dst++ = SHRT_MIN;
  96. else
  97. *dst++ = (short)val;
  98. samples--;
  99. }
  100. }
  101. PAL_FORCE_INLINE
  102. void
  103. AUDIO_AdjustNativeVolume(
  104. short *srcdst,
  105. int iVolume,
  106. int samples
  107. )
  108. {
  109. while (samples > 0)
  110. {
  111. *srcdst = *srcdst * iVolume / PAL_MAX_VOLUME;
  112. samples--; srcdst++;
  113. }
  114. }
  115. typedef struct tagRIFFHEADER
  116. {
  117. DWORD riff_sig; /* 'RIFF' */
  118. DWORD data_length; /* Total length minus eight, little-endian */
  119. DWORD riff_type; /* 'WAVE' */
  120. } RIFFHEADER, *LPRIFFHEADER;
  121. typedef const RIFFHEADER *LPCRIFFHEADER;
  122. typedef struct tagRIFFCHUNK
  123. {
  124. DWORD chunk_type; /* 'fmt ' and so on */
  125. DWORD chunk_length; /* Total chunk length minus eight, little-endian */
  126. } RIFFCHUNK, *LPRIFFCHUNK;
  127. typedef const RIFFCHUNK *LPCRIFFCHUNK;
  128. typedef struct tagWAVEFORMATPCM
  129. {
  130. WORD wFormatTag; /* format type */
  131. WORD nChannels; /* number of channels (i.e. mono, stereo, etc.) */
  132. DWORD nSamplesPerSec; /* sample rate */
  133. DWORD nAvgBytesPerSec; /* for buffer estimation */
  134. WORD nBlockAlign; /* block size of data */
  135. WORD wBitsPerSample;
  136. } WAVEFORMATPCM, *LPWAVEFORMATPCM;
  137. typedef const WAVEFORMATPCM *LPCWAVEFORMATPCM;
  138. static LPCBYTE
  139. SOUND_LoadWAVEData(
  140. LPCBYTE lpData,
  141. DWORD dwLen,
  142. SDL_AudioSpec *lpSpec
  143. )
  144. /*++
  145. Purpose:
  146. Return the WAVE data pointer inside the input buffer.
  147. Parameters:
  148. [IN] lpData - pointer to the buffer of the WAVE file.
  149. [IN] dwLen - length of the buffer of the WAVE file.
  150. [OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  151. some basic information about the WAVE file.
  152. Return value:
  153. Pointer to the WAVE data inside the input buffer, NULL if failed.
  154. --*/
  155. {
  156. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  157. # define RIFF 'RIFF'
  158. # define WAVE 'WAVE'
  159. # define FMT 'fmt '
  160. # define DATA 'data'
  161. # define PCM 0x0100
  162. #else
  163. # define RIFF 'FFIR'
  164. # define WAVE 'EVAW'
  165. # define FMT ' tmf'
  166. # define DATA 'atad'
  167. # define PCM 0x0001
  168. #endif
  169. LPCRIFFHEADER lpRiff = (LPCRIFFHEADER)lpData;
  170. LPCRIFFCHUNK lpChunk;
  171. LPCWAVEFORMATPCM lpFormat = NULL;
  172. LPCBYTE lpWaveData = NULL;
  173. DWORD len;
  174. if (dwLen < sizeof(RIFFHEADER) || lpRiff->riff_sig != RIFF || lpRiff->riff_type != WAVE || dwLen < SDL_SwapLE32(lpRiff->data_length) + 8)
  175. {
  176. return NULL;
  177. }
  178. lpChunk = (LPCRIFFCHUNK)(lpRiff + 1); dwLen -= sizeof(RIFFHEADER);
  179. while (dwLen >= sizeof(RIFFCHUNK))
  180. {
  181. len = SDL_SwapLE32(lpChunk->chunk_length);
  182. if (dwLen >= sizeof(RIFFCHUNK) + len)
  183. dwLen -= sizeof(RIFFCHUNK) + len;
  184. else
  185. return NULL;
  186. switch (lpChunk->chunk_type)
  187. {
  188. case FMT:
  189. lpFormat = (LPCWAVEFORMATPCM)(lpChunk + 1);
  190. if (len != sizeof(WAVEFORMATPCM) || lpFormat->wFormatTag != PCM)
  191. {
  192. return NULL;
  193. }
  194. break;
  195. case DATA:
  196. lpWaveData = (LPCBYTE)(lpChunk + 1);
  197. dwLen = 0;
  198. break;
  199. }
  200. lpChunk = (LPCRIFFCHUNK)((LPCBYTE)(lpChunk + 1) + len);
  201. }
  202. if (lpFormat == NULL || lpWaveData == NULL)
  203. {
  204. return NULL;
  205. }
  206. lpSpec->channels = lpFormat->nChannels;
  207. lpSpec->format = (lpFormat->wBitsPerSample == 16) ? AUDIO_S16 : AUDIO_U8;
  208. lpSpec->freq = lpFormat->nSamplesPerSec;
  209. lpSpec->size = len;
  210. return lpWaveData;
  211. #undef RIFF
  212. #undef WAVE
  213. #undef FMT
  214. }
  215. typedef struct tagVOCHEADER
  216. {
  217. char signature[0x14]; /* "Creative Voice File\x1A" */
  218. WORD data_offset; /* little endian */
  219. WORD version;
  220. WORD version_checksum;
  221. } VOCHEADER, *LPVOCHEADER;
  222. typedef const VOCHEADER *LPCVOCHEADER;
  223. static LPCBYTE
  224. SOUND_LoadVOCData(
  225. LPCBYTE lpData,
  226. DWORD dwLen,
  227. SDL_AudioSpec *lpSpec
  228. )
  229. /*++
  230. Purpose:
  231. Return the VOC data pointer inside the input buffer. Currently supports type 01 block only.
  232. Parameters:
  233. [IN] lpData - pointer to the buffer of the VOC file.
  234. [IN] dwLen - length of the buffer of the VOC file.
  235. [OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  236. some basic information about the VOC file.
  237. Return value:
  238. Pointer to the WAVE data inside the input buffer, NULL if failed.
  239. Reference: http://sox.sourceforge.net/AudioFormats-11.html
  240. --*/
  241. {
  242. LPCVOCHEADER lpVOC = (LPCVOCHEADER)lpData;
  243. if (dwLen < sizeof(VOCHEADER) || memcmp(lpVOC->signature, "Creative Voice File\x1A", 0x14) || SDL_SwapLE16(lpVOC->data_offset) >= dwLen)
  244. {
  245. return NULL;
  246. }
  247. lpData += SDL_SwapLE16(lpVOC->data_offset);
  248. dwLen -= SDL_SwapLE16(lpVOC->data_offset);
  249. while (dwLen && *lpData)
  250. {
  251. DWORD len;
  252. if (dwLen >= 4)
  253. {
  254. len = lpData[1] | (lpData[2] << 8) | (lpData[3] << 16);
  255. if (dwLen >= len + 4)
  256. dwLen -= len + 4;
  257. else
  258. return NULL;
  259. }
  260. else
  261. {
  262. return NULL;
  263. }
  264. if (*lpData == 0x01)
  265. {
  266. if (lpData[5] != 0) return NULL; /* Only 8-bit is supported */
  267. lpSpec->format = AUDIO_U8;
  268. lpSpec->channels = 1;
  269. lpSpec->freq = ((1000000 / (256 - lpData[4]) + 99) / 100) * 100; /* Round to next 100Hz */
  270. lpSpec->size = len - 2;
  271. return lpData + 6;
  272. }
  273. else
  274. {
  275. lpData += len + 4;
  276. }
  277. }
  278. return NULL;
  279. }
  280. static void
  281. SOUND_ResampleU8(
  282. LPCBYTE lpData,
  283. const SDL_AudioSpec *lpSpec,
  284. LPBYTE lpBuffer,
  285. DWORD dwLen,
  286. void *resampler
  287. )
  288. /*++
  289. Purpose:
  290. Resample 8-bit unsigned PCM data into 16-bit signed (little-endian) PCM data.
  291. Parameters:
  292. [IN] lpData - pointer to the buffer of the input PCM data.
  293. [IN] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  294. some basic information about the input PCM data.
  295. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  296. [IN] dwLen - length of the buffer of the output PCM data, should be exactly
  297. the number of bytes needed of the resampled data.
  298. [IN] resampler - pointer of the resampler instance.
  299. Return value:
  300. None.
  301. --*/
  302. {
  303. int src_samples = lpSpec->size / lpSpec->channels, i;
  304. for (i = 0; i < lpSpec->channels; i++)
  305. {
  306. LPCBYTE src = lpData + i;
  307. short *dst = (short *)lpBuffer + i;
  308. int channel_len = dwLen / lpSpec->channels, total_bytes = 0;
  309. resampler_clear(resampler);
  310. while (total_bytes < channel_len && src_samples > 0)
  311. {
  312. int to_write, j;
  313. to_write = resampler_get_free_count(resampler);
  314. if (to_write > src_samples) to_write = src_samples;
  315. for (j = 0; j < to_write; j++)
  316. {
  317. resampler_write_sample(resampler, (*src ^ 0x80) << 8);
  318. src += lpSpec->channels;
  319. }
  320. src_samples -= to_write;
  321. while (total_bytes < channel_len && resampler_get_sample_count(resampler) > 0)
  322. {
  323. *dst = resampler_get_and_remove_sample(resampler);
  324. dst += lpSpec->channels; total_bytes += (SDL_AUDIO_BITSIZE(AUDIO_S16) >> 3);
  325. }
  326. }
  327. /* Flush resampler's output buffer */
  328. while (total_bytes < channel_len)
  329. {
  330. int j, to_write = resampler_get_free_count(resampler);
  331. for (j = 0; j < to_write; j++)
  332. resampler_write_sample(resampler, (src[-lpSpec->channels] ^ 0x80) << 8);
  333. while (total_bytes < channel_len && resampler_get_sample_count(resampler) > 0)
  334. {
  335. *dst = resampler_get_and_remove_sample(resampler);
  336. dst += lpSpec->channels; total_bytes += (SDL_AUDIO_BITSIZE(AUDIO_S16) >> 3);
  337. }
  338. }
  339. }
  340. }
  341. static void
  342. SOUND_ResampleS16(
  343. LPCBYTE lpData,
  344. const SDL_AudioSpec *lpSpec,
  345. LPBYTE lpBuffer,
  346. DWORD dwLen,
  347. void *resampler
  348. )
  349. /*++
  350. Purpose:
  351. Resample 16-bit signed (little-endian) PCM data into 16-bit signed (little-endian) PCM data.
  352. Parameters:
  353. [IN] lpData - pointer to the buffer of the input PCM data.
  354. [IN] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  355. some basic information about the input PCM data.
  356. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  357. [IN] dwLen - length of the buffer of the output PCM data, should be exactly
  358. the number of bytes needed of the resampled data.
  359. [IN] resampler - pointer of the resampler instance.
  360. Return value:
  361. None.
  362. --*/
  363. {
  364. int src_samples = lpSpec->size / lpSpec->channels / 2, i;
  365. for (i = 0; i < lpSpec->channels; i++)
  366. {
  367. const short *src = (short *)lpData + i;
  368. short *dst = (short *)lpBuffer + i;
  369. int channel_len = dwLen / lpSpec->channels, total_bytes = 0;
  370. resampler_clear(resampler);
  371. while (total_bytes < channel_len && src_samples > 0)
  372. {
  373. int to_write, j;
  374. to_write = resampler_get_free_count(resampler);
  375. if (to_write > src_samples) to_write = src_samples;
  376. for (j = 0; j < to_write; j++)
  377. {
  378. resampler_write_sample(resampler, SDL_SwapLE16(*src));
  379. src += lpSpec->channels;
  380. }
  381. src_samples -= to_write;
  382. while (total_bytes < channel_len && resampler_get_sample_count(resampler) > 0)
  383. {
  384. *dst = resampler_get_and_remove_sample(resampler);
  385. dst += lpSpec->channels; total_bytes += (SDL_AUDIO_BITSIZE(AUDIO_S16) >> 3);
  386. }
  387. }
  388. /* Flush resampler's output buffer */
  389. while (total_bytes < channel_len)
  390. {
  391. int j, to_write = resampler_get_free_count(resampler);
  392. short val = SDL_SwapLE16(src[-lpSpec->channels]);
  393. for (j = 0; j < to_write; j++)
  394. resampler_write_sample(resampler, val);
  395. while (total_bytes < channel_len && resampler_get_sample_count(resampler) > 0)
  396. {
  397. *dst = resampler_get_and_remove_sample(resampler);
  398. dst += lpSpec->channels; total_bytes += (SDL_AUDIO_BITSIZE(AUDIO_S16) >> 3);
  399. }
  400. }
  401. }
  402. }
  403. static VOID SDLCALL
  404. AUDIO_FillBuffer(
  405. LPVOID udata,
  406. LPBYTE stream,
  407. INT len
  408. )
  409. /*++
  410. Purpose:
  411. SDL sound callback function.
  412. Parameters:
  413. [IN] udata - pointer to user-defined parameters (Not used).
  414. [OUT] stream - pointer to the stream buffer.
  415. [IN] len - Length of the buffer.
  416. Return value:
  417. None.
  418. --*/
  419. {
  420. #if SDL_VERSION_ATLEAST(2,0,0)
  421. memset(stream, 0, len);
  422. #endif
  423. SDL_mutexP(gSndPlayer.mtx);
  424. gSndPlayer.cvt.buf = stream;
  425. gSndPlayer.cvt.len = len;
  426. //
  427. // Play music
  428. //
  429. if (gSndPlayer.fMusicEnabled)
  430. {
  431. if (gSndPlayer.pMusPlayer)
  432. {
  433. gSndPlayer.pMusPlayer->FillBuffer(gSndPlayer.pMusPlayer, stream, len);
  434. }
  435. if (gSndPlayer.pCDPlayer)
  436. {
  437. gSndPlayer.pCDPlayer->FillBuffer(gSndPlayer.pCDPlayer, stream, len);
  438. }
  439. }
  440. //
  441. // Play sound
  442. //
  443. if (gSndPlayer.fSoundEnabled && gSndPlayer.wavePlayer.len > 0)
  444. {
  445. //
  446. // Mix as much sound data as possible
  447. //
  448. WAVEPLAYER *player = &gSndPlayer.wavePlayer;
  449. int mixlen = min(player->len, len >> 1);
  450. if (player->pos + mixlen > player->buf_len)
  451. {
  452. AUDIO_MixNativeVolume((short *)stream, gConfig.iMusicVolume, player->buf + player->pos, gConfig.iSoundVolume, player->buf_len - player->pos);
  453. stream += (player->buf_len - player->pos) << 1; memset(player->buf + player->pos, 0, (player->buf_len - player->pos) << 1);
  454. AUDIO_MixNativeVolume((short *)stream, gConfig.iMusicVolume, player->buf, gConfig.iSoundVolume, player->pos + mixlen - player->buf_len);
  455. stream += (player->pos + mixlen - player->buf_len) << 1; memset(player->buf, 0, (player->pos + mixlen - player->buf_len) << 1);
  456. }
  457. else
  458. {
  459. AUDIO_MixNativeVolume((short *)stream, gConfig.iMusicVolume, player->buf + player->pos, gConfig.iSoundVolume, mixlen);
  460. stream += mixlen << 1; memset(player->buf + player->pos, 0, mixlen << 1);
  461. }
  462. player->pos = (player->pos + mixlen) % player->buf_len; player->len -= mixlen; len -= (mixlen << 1);
  463. }
  464. //
  465. // Adjust volume in the remaing buffer
  466. //
  467. AUDIO_AdjustNativeVolume((short *)stream, gConfig.iMusicVolume, len >> 1);
  468. //
  469. // Convert audio from native byte-order to actual byte-order
  470. //
  471. SDL_ConvertAudio(&gSndPlayer.cvt);
  472. SDL_mutexV(gSndPlayer.mtx);
  473. }
  474. static VOID
  475. SOUND_LoadMKF(
  476. VOID
  477. )
  478. /*++
  479. Purpose:
  480. Load MKF contents into memory.
  481. Parameters:
  482. None.
  483. Return value:
  484. None.
  485. --*/
  486. {
  487. char *mkfs[2];
  488. FNLoadSoundData func[2];
  489. int i;
  490. if (gConfig.fIsWIN95)
  491. {
  492. mkfs[0] = "sounds.mkf"; func[0] = SOUND_LoadWAVEData;
  493. mkfs[1] = "voc.mkf"; func[1] = SOUND_LoadVOCData;
  494. }
  495. else
  496. {
  497. mkfs[0] = "voc.mkf"; func[0] = SOUND_LoadVOCData;
  498. mkfs[1] = "sounds.mkf"; func[1] = SOUND_LoadWAVEData;
  499. }
  500. for (i = 0; i < 2; i++)
  501. {
  502. gSndPlayer.mkf = UTIL_OpenFile(mkfs[i]);
  503. if (gSndPlayer.mkf)
  504. {
  505. gSndPlayer.wavePlayer.LoadSoundData = func[i];
  506. break;
  507. }
  508. }
  509. }
  510. INT
  511. AUDIO_OpenDevice(
  512. VOID
  513. )
  514. /*++
  515. Purpose:
  516. Initialize the audio subsystem.
  517. Parameters:
  518. None.
  519. Return value:
  520. 0 if succeed, others if failed.
  521. --*/
  522. {
  523. SDL_AudioSpec spec;
  524. if (gSndPlayer.fOpened)
  525. {
  526. //
  527. // Already opened
  528. //
  529. return -1;
  530. }
  531. gSndPlayer.fOpened = FALSE;
  532. gSndPlayer.fMusicEnabled = TRUE;
  533. gSndPlayer.fSoundEnabled = TRUE;
  534. //
  535. // Load the MKF file.
  536. //
  537. SOUND_LoadMKF();
  538. if (gSndPlayer.mkf == NULL)
  539. {
  540. return -2;
  541. }
  542. //
  543. // Initialize the resampler
  544. //
  545. resampler_init();
  546. gSndPlayer.wavePlayer.resampler = resampler_create();
  547. //
  548. // Open the sound subsystem.
  549. //
  550. gSndPlayer.spec.freq = gConfig.iSampleRate;
  551. gSndPlayer.spec.format = AUDIO_S16;
  552. gSndPlayer.spec.channels = gConfig.iAudioChannels;
  553. gSndPlayer.spec.samples = gConfig.wAudioBufferSize;
  554. gSndPlayer.spec.callback = AUDIO_FillBuffer;
  555. if (SDL_OpenAudio(&gSndPlayer.spec, &spec) < 0)
  556. {
  557. //
  558. // Failed
  559. //
  560. return -3;
  561. }
  562. else
  563. gSndPlayer.spec = spec;
  564. SDL_BuildAudioCVT(&gSndPlayer.cvt, AUDIO_S16SYS, spec.channels, spec.freq, spec.format, spec.channels, spec.freq);
  565. gSndPlayer.wavePlayer.buf = NULL;
  566. gSndPlayer.wavePlayer.buf_len = 0;
  567. gSndPlayer.wavePlayer.pos = 0;
  568. gSndPlayer.wavePlayer.len = 0;
  569. gSndPlayer.mtx = SDL_CreateMutex();
  570. gSndPlayer.fOpened = TRUE;
  571. //
  572. // Initialize the music subsystem.
  573. //
  574. switch (gConfig.eMusicType)
  575. {
  576. case MUSIC_RIX:
  577. if (!(gSndPlayer.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"))))
  578. {
  579. gSndPlayer.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"));
  580. }
  581. break;
  582. case MUSIC_MP3:
  583. #if PAL_HAS_MP3
  584. gSndPlayer.pMusPlayer = MP3_Init(NULL);
  585. #else
  586. gSndPlayer.pMusPlayer = NULL;
  587. #endif
  588. break;
  589. case MUSIC_OGG:
  590. #if PAL_HAS_OGG
  591. gSndPlayer.pMusPlayer = OGG_Init(NULL);
  592. #else
  593. gSndPlayer.pMusPlayer = NULL;
  594. #endif
  595. break;
  596. case MUSIC_MIDI:
  597. gSndPlayer.pMusPlayer = NULL;
  598. break;
  599. }
  600. //
  601. // Initialize the CD audio.
  602. //
  603. switch (gConfig.eCDType)
  604. {
  605. case MUSIC_SDLCD:
  606. {
  607. #if PAL_HAS_SDLCD
  608. int i;
  609. gSndPlayer.pCD = NULL;
  610. for (i = 0; i < SDL_CDNumDrives(); i++)
  611. {
  612. gSndPlayer.pCD = SDL_CDOpen(i);
  613. if (gSndPlayer.pCD != NULL)
  614. {
  615. if (!CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
  616. {
  617. SDL_CDClose(gSndPlayer.pCD);
  618. gSndPlayer.pCD = NULL;
  619. }
  620. else
  621. {
  622. break;
  623. }
  624. }
  625. }
  626. #endif
  627. gSndPlayer.pCDPlayer = NULL;
  628. break;
  629. }
  630. case MUSIC_MP3:
  631. #if PAL_HAS_MP3
  632. gSndPlayer.pCDPlayer = MP3_Init(NULL);
  633. #else
  634. gSndPlayer.pCDPlayer = NULL;
  635. #endif
  636. break;
  637. case MUSIC_OGG:
  638. #if PAL_HAS_OGG
  639. gSndPlayer.pCDPlayer = OGG_Init(NULL);
  640. #else
  641. gSndPlayer.pCDPlayer = NULL;
  642. #endif
  643. break;
  644. }
  645. //
  646. // Let the callback function run so that musics will be played.
  647. //
  648. SDL_PauseAudio(0);
  649. return 0;
  650. }
  651. VOID
  652. AUDIO_CloseDevice(
  653. VOID
  654. )
  655. /*++
  656. Purpose:
  657. Close the audio subsystem.
  658. Parameters:
  659. None.
  660. Return value:
  661. None.
  662. --*/
  663. {
  664. SDL_CloseAudio();
  665. SDL_mutexP(gSndPlayer.mtx);
  666. if (gSndPlayer.wavePlayer.buf != NULL)
  667. {
  668. free(gSndPlayer.wavePlayer.buf);
  669. gSndPlayer.wavePlayer.buf = NULL;
  670. gSndPlayer.wavePlayer.buf_len = 0;
  671. gSndPlayer.wavePlayer.pos = 0;
  672. gSndPlayer.wavePlayer.len = 0;
  673. }
  674. if (gSndPlayer.mkf != NULL)
  675. {
  676. fclose(gSndPlayer.mkf);
  677. gSndPlayer.mkf = NULL;
  678. }
  679. if (gSndPlayer.pMusPlayer)
  680. {
  681. gSndPlayer.pMusPlayer->Shutdown(gSndPlayer.pMusPlayer);
  682. gSndPlayer.pMusPlayer = NULL;
  683. }
  684. if (gSndPlayer.pCDPlayer)
  685. {
  686. gSndPlayer.pCDPlayer->Shutdown(gSndPlayer.pCDPlayer);
  687. gSndPlayer.pCDPlayer = NULL;
  688. }
  689. #if PAL_HAS_SDLCD
  690. if (gSndPlayer.pCD != NULL)
  691. {
  692. AUDIO_PlayCDTrack(-1);
  693. SDL_CDClose(gSndPlayer.pCD);
  694. }
  695. #endif
  696. if (gConfig.eMusicType == MUSIC_MIDI) MIDI_Play(0, FALSE);
  697. if (gSndPlayer.wavePlayer.resampler)
  698. {
  699. resampler_delete(gSndPlayer.wavePlayer.resampler);
  700. gSndPlayer.wavePlayer.resampler = NULL;
  701. }
  702. SDL_mutexV(gSndPlayer.mtx);
  703. SDL_DestroyMutex(gSndPlayer.mtx);
  704. }
  705. SDL_AudioSpec*
  706. AUDIO_GetDeviceSpec(
  707. VOID
  708. )
  709. {
  710. return &gSndPlayer.spec;
  711. }
  712. static INT
  713. AUDIO_AdjustVolumeByValue(
  714. INT *iVolume,
  715. INT iValue
  716. )
  717. {
  718. *iVolume += iValue;
  719. if (*iVolume > PAL_MAX_VOLUME)
  720. *iVolume = PAL_MAX_VOLUME;
  721. else if (*iVolume < 0)
  722. *iVolume = 0;
  723. return *iVolume;
  724. }
  725. VOID
  726. AUDIO_AdjustVolume(
  727. INT iDirection
  728. )
  729. /*++
  730. Purpose:
  731. SDL sound volume adjust function.
  732. Parameters:
  733. [IN] iDirection - value, Increase (>0) or decrease (<=0) 3% volume.
  734. Return value:
  735. None.
  736. --*/
  737. {
  738. AUDIO_AdjustVolumeByValue(&gConfig.iMusicVolume, (iDirection > 0) ? 3 : -3);
  739. AUDIO_AdjustVolumeByValue(&gConfig.iSoundVolume, (iDirection > 0) ? 3 : -3);
  740. }
  741. VOID
  742. AUDIO_PlaySound(
  743. INT iSoundNum
  744. )
  745. /*++
  746. Purpose:
  747. Play a sound in voc.mkf/sounds.mkf file.
  748. Parameters:
  749. [IN] iSoundNum - number of the sound; the absolute value is used.
  750. Return value:
  751. None.
  752. --*/
  753. {
  754. SDL_AudioCVT wavecvt;
  755. SDL_AudioSpec wavespec;
  756. LPBYTE buf, bufdec;
  757. LPCBYTE bufsrc;
  758. int len;
  759. if (!gSndPlayer.fOpened || !gSndPlayer.fSoundEnabled)
  760. {
  761. return;
  762. }
  763. if (iSoundNum < 0)
  764. {
  765. iSoundNum = -iSoundNum;
  766. }
  767. //
  768. // Get the length of the sound file.
  769. //
  770. len = PAL_MKFGetChunkSize(iSoundNum, gSndPlayer.mkf);
  771. if (len <= 0)
  772. {
  773. return;
  774. }
  775. buf = (LPBYTE)malloc(len);
  776. if (buf == NULL)
  777. {
  778. return;
  779. }
  780. //
  781. // Read the sound file from the MKF archive.
  782. //
  783. PAL_MKFReadChunk(buf, len, iSoundNum, gSndPlayer.mkf);
  784. bufsrc = gSndPlayer.wavePlayer.LoadSoundData(buf, len, &wavespec);
  785. if (bufsrc == NULL)
  786. {
  787. free(buf);
  788. return;
  789. }
  790. if (wavespec.freq != gSndPlayer.spec.freq)
  791. {
  792. /* Resampler is needed */
  793. resampler_set_quality(gSndPlayer.wavePlayer.resampler, AUDIO_IsIntegerConversion(wavespec.freq) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
  794. resampler_set_rate(gSndPlayer.wavePlayer.resampler, (double)wavespec.freq / (double)gSndPlayer.spec.freq);
  795. len = (int)ceil(wavespec.size * (double)gSndPlayer.spec.freq / (double)wavespec.freq) * (SDL_AUDIO_BITSIZE(AUDIO_S16SYS) / SDL_AUDIO_BITSIZE(wavespec.format));
  796. if (len >= wavespec.channels * 2 && (bufdec = malloc(len)))
  797. {
  798. if (wavespec.format == AUDIO_S16)
  799. SOUND_ResampleS16(bufsrc, &wavespec, bufdec, len, gSndPlayer.wavePlayer.resampler);
  800. else
  801. SOUND_ResampleU8(bufsrc, &wavespec, bufdec, len, gSndPlayer.wavePlayer.resampler);
  802. /* Free the original buffer and reset the pointer for simpler later operations */
  803. free(buf); buf = bufdec;
  804. wavespec.format = AUDIO_S16SYS;
  805. wavespec.freq = gSndPlayer.spec.freq;
  806. }
  807. else
  808. {
  809. free(buf);
  810. return;
  811. }
  812. }
  813. else
  814. {
  815. bufdec = (LPBYTE)bufsrc;
  816. len = wavespec.size;
  817. }
  818. //
  819. // Build the audio converter and create conversion buffers
  820. //
  821. if (SDL_BuildAudioCVT(&wavecvt, wavespec.format, wavespec.channels, wavespec.freq,
  822. AUDIO_S16SYS, gSndPlayer.spec.channels, gSndPlayer.spec.freq) < 0)
  823. {
  824. free(buf);
  825. return;
  826. }
  827. wavecvt.len = len & ~((SDL_AUDIO_BITSIZE(wavespec.format) >> 3) * wavespec.channels - 1);
  828. wavecvt.buf = (LPBYTE)malloc(wavecvt.len * wavecvt.len_mult);
  829. if (wavecvt.buf == NULL)
  830. {
  831. free(buf);
  832. return;
  833. }
  834. memcpy(wavecvt.buf, bufdec, len);
  835. free(buf);
  836. //
  837. // Run the audio converter
  838. //
  839. if (SDL_ConvertAudio(&wavecvt) == 0)
  840. {
  841. WAVEPLAYER *player = &gSndPlayer.wavePlayer;
  842. wavecvt.len = (int)(wavecvt.len * wavecvt.len_ratio) >> 1;
  843. SDL_mutexP(gSndPlayer.mtx);
  844. //
  845. // Check if the current sound buffer is large enough
  846. //
  847. if (gSndPlayer.wavePlayer.buf_len < wavecvt.len)
  848. {
  849. if (player->pos + player->len > player->buf_len)
  850. {
  851. short *old_buf = player->buf;
  852. player->buf = (short *)malloc(wavecvt.len << 1);
  853. memcpy(player->buf, old_buf + player->pos, (player->buf_len - player->pos) << 1);
  854. memcpy(player->buf + player->buf_len - player->pos, old_buf, (player->pos + player->len - player->buf_len) << 1);
  855. player->pos = 0; free(old_buf);
  856. }
  857. else
  858. player->buf = (short *)realloc(player->buf, wavecvt.len << 1);
  859. memset(player->buf + player->pos + player->len, 0, ((player->buf_len = wavecvt.len) - player->pos - player->len) << 1);
  860. }
  861. //
  862. // Mix the current sound buffer with newly played sound and adjust the length of valid data
  863. //
  864. if (player->pos + wavecvt.len > player->buf_len)
  865. {
  866. AUDIO_MixNative(player->buf + player->pos, (short *)wavecvt.buf, player->buf_len - player->pos);
  867. AUDIO_MixNative(player->buf, (short *)wavecvt.buf + player->buf_len - player->pos, player->pos + wavecvt.len - player->buf_len);
  868. }
  869. else
  870. AUDIO_MixNative(player->buf + player->pos, (short *)wavecvt.buf, wavecvt.len);
  871. player->len = max(player->len, wavecvt.len);
  872. SDL_mutexV(gSndPlayer.mtx);
  873. }
  874. free(wavecvt.buf);
  875. }
  876. VOID
  877. AUDIO_PlayMusic(
  878. INT iNumRIX,
  879. BOOL fLoop,
  880. FLOAT flFadeTime
  881. )
  882. {
  883. SDL_mutexP(gSndPlayer.mtx);
  884. if (gConfig.eMusicType == MUSIC_MIDI)
  885. {
  886. MIDI_Play(iNumRIX, fLoop);
  887. }
  888. else if (gSndPlayer.pMusPlayer)
  889. {
  890. gSndPlayer.pMusPlayer->Play(gSndPlayer.pMusPlayer, iNumRIX, fLoop, flFadeTime);
  891. }
  892. SDL_mutexV(gSndPlayer.mtx);
  893. }
  894. BOOL
  895. AUDIO_PlayCDTrack(
  896. INT iNumTrack
  897. )
  898. /*++
  899. Purpose:
  900. Play a CD Audio Track.
  901. Parameters:
  902. [IN] iNumTrack - number of the CD Audio Track.
  903. Return value:
  904. TRUE if the track can be played, FALSE if not.
  905. --*/
  906. {
  907. BOOL ret = FALSE;
  908. #if PAL_HAS_SDLCD
  909. if (gSndPlayer.pCD != NULL)
  910. {
  911. if (CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
  912. {
  913. SDL_CDStop(gSndPlayer.pCD);
  914. if (iNumTrack != -1)
  915. {
  916. AUDIO_PlayMusic(-1, FALSE, 0);
  917. if (SDL_CDPlayTracks(gSndPlayer.pCD, iNumTrack - 1, 0, 1, 0) == 0)
  918. {
  919. return TRUE;
  920. }
  921. }
  922. }
  923. }
  924. #endif
  925. SDL_mutexP(gSndPlayer.mtx);
  926. if (gSndPlayer.pCDPlayer)
  927. {
  928. if (iNumTrack != -1)
  929. {
  930. AUDIO_PlayMusic(-1, FALSE, 0);
  931. ret = gSndPlayer.pCDPlayer->Play(gSndPlayer.pCDPlayer, PAL_CDTRACK_BASE + iNumTrack, TRUE, 0);
  932. }
  933. else
  934. {
  935. ret = gSndPlayer.pCDPlayer->Play(gSndPlayer.pCDPlayer, -1, FALSE, 0);
  936. }
  937. }
  938. SDL_mutexV(gSndPlayer.mtx);
  939. return ret;
  940. }
  941. VOID
  942. AUDIO_EnableMusic(
  943. BOOL fEnable
  944. )
  945. {
  946. gSndPlayer.fMusicEnabled = fEnable;
  947. }
  948. BOOL
  949. AUDIO_MusicEnabled(
  950. VOID
  951. )
  952. {
  953. return gSndPlayer.fMusicEnabled;
  954. }
  955. VOID
  956. AUDIO_EnableSound(
  957. BOOL fEnable
  958. )
  959. {
  960. gSndPlayer.fSoundEnabled = fEnable;
  961. }
  962. BOOL
  963. AUDIO_SoundEnabled(
  964. VOID
  965. )
  966. {
  967. return gSndPlayer.fSoundEnabled;
  968. }
  969. #ifdef PSP
  970. void
  971. SOUND_Reload(
  972. void
  973. )
  974. {
  975. fclose(gSndPlayer.mkf);
  976. SOUND_LoadMKF();
  977. }
  978. #endif