sound.c 22 KB

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