sound.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 "common.h"
  23. #include "sound.h"
  24. #include "rixplay.h"
  25. #include "util.h"
  26. #ifdef PAL_HAS_NATIVEMIDI
  27. #include "midi.h"
  28. #endif
  29. #ifdef PAL_HAS_MP3
  30. #include "libmad/music_mad.h"
  31. #endif
  32. static BOOL gSndOpened = FALSE;
  33. BOOL g_fNoSound = FALSE;
  34. BOOL g_fNoMusic = FALSE;
  35. #ifdef PAL_HAS_NATIVEMIDI
  36. BOOL g_fUseMidi = FALSE;
  37. #endif
  38. static BOOL g_fUseWav = FALSE;
  39. #ifdef __SYMBIAN32__
  40. INT g_iVolume = SDL_MIX_MAXVOLUME * 0.1;
  41. #endif
  42. typedef struct tagSNDPLAYER
  43. {
  44. FILE *mkf;
  45. SDL_AudioSpec spec;
  46. LPBYTE buf[2], pos[2];
  47. INT audio_len[2];
  48. #ifdef PAL_HAS_CD
  49. SDL_CD *pCD;
  50. #endif
  51. #ifdef PAL_HAS_MP3
  52. mad_data *pMP3;
  53. BOOL fMP3Loop;
  54. INT iCurrentMP3;
  55. SDL_mutex *lock;
  56. #endif
  57. } SNDPLAYER;
  58. static SNDPLAYER gSndPlayer;
  59. static SDL_AudioSpec *
  60. SOUND_LoadVOCFromBuffer(
  61. LPCBYTE lpVOC,
  62. DWORD dwLen,
  63. SDL_AudioSpec *lpSpec,
  64. LPBYTE *lppBuffer
  65. )
  66. /*++
  67. Purpose:
  68. Load a VOC file in a buffer. Currently supports type 01 block only.
  69. Parameters:
  70. [IN] lpVOC - pointer to the buffer of the VOC file.
  71. [IN] dwLen - length of the buffer of the VOC file.
  72. [OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  73. some basic information about the VOC file.
  74. [OUT] lppBuffer - the output buffer.
  75. Return value:
  76. Pointer to the SDL_AudioSpec structure, NULL if failed.
  77. --*/
  78. {
  79. INT freq, len, x, i, l;
  80. SDL_RWops *rw;
  81. if (g_fUseWav)
  82. {
  83. rw = SDL_RWFromConstMem(lpVOC, dwLen);
  84. if (rw == NULL) return NULL;
  85. len = dwLen;
  86. SDL_LoadWAV_RW(rw, 1, lpSpec, lppBuffer, (Uint32 *)&len);
  87. lpSpec->size = len;
  88. return lpSpec;
  89. }
  90. else
  91. {
  92. //
  93. // Skip header
  94. //
  95. lpVOC += 0x1B;
  96. //
  97. // Length is 3 bytes long
  98. //
  99. len = (lpVOC[0] | (lpVOC[1] << 8) | (lpVOC[2] << 16)) - 2;
  100. lpVOC += 3;
  101. //
  102. // One byte for frequency
  103. //
  104. freq = 1000000 / (256 - *lpVOC);
  105. #if 1
  106. lpVOC += 2;
  107. //
  108. // Convert the sample manually, as SDL doesn't like "strange" sample rates.
  109. //
  110. x = (INT)(len * ((FLOAT)PAL_SAMPLE_RATE / freq));
  111. *lppBuffer = (LPBYTE)calloc(1, x);
  112. if (*lppBuffer == NULL)
  113. {
  114. return NULL;
  115. }
  116. for (i = 0; i < x; i++)
  117. {
  118. l = (INT)(i * (freq / (FLOAT)PAL_SAMPLE_RATE));
  119. if (l >= len)
  120. {
  121. l = len - 1;
  122. }
  123. (*lppBuffer)[i] = lpVOC[l];
  124. }
  125. lpSpec->channels = 1;
  126. lpSpec->format = AUDIO_U8;
  127. lpSpec->freq = PAL_SAMPLE_RATE;
  128. lpSpec->size = x;
  129. #else
  130. *lppBuffer = (unsigned char *)malloc(len);
  131. if (*lppBuffer == NULL)
  132. {
  133. return NULL;
  134. }
  135. lpSpec->channels = 1;
  136. lpSpec->format = AUDIO_U8;
  137. lpSpec->freq = freq;
  138. lpSpec->size = len;
  139. lpVOC += 2;
  140. memcpy(*lppBuffer, lpVOC, len);
  141. #endif
  142. return lpSpec;
  143. }
  144. }
  145. static VOID SDLCALL
  146. SOUND_FillAudio(
  147. LPVOID udata,
  148. LPBYTE stream,
  149. INT len
  150. )
  151. /*++
  152. Purpose:
  153. SDL sound callback function.
  154. Parameters:
  155. [IN] udata - pointer to user-defined parameters (Not used).
  156. [OUT] stream - pointer to the stream buffer.
  157. [IN] len - Length of the buffer.
  158. Return value:
  159. None.
  160. --*/
  161. {
  162. int i;
  163. //
  164. // Play music
  165. //
  166. if (!g_fNoMusic)
  167. {
  168. #ifdef PAL_HAS_MP3
  169. if (gSndPlayer.pMP3 != NULL)
  170. {
  171. SDL_mutexP(gSndPlayer.lock);
  172. mad_getSamples(gSndPlayer.pMP3, stream, len);
  173. if (!mad_isPlaying(gSndPlayer.pMP3) && gSndPlayer.fMP3Loop)
  174. {
  175. mad_seek(gSndPlayer.pMP3, 0);
  176. mad_start(gSndPlayer.pMP3);
  177. mad_getSamples(gSndPlayer.pMP3, stream, len);
  178. }
  179. SDL_mutexV(gSndPlayer.lock);
  180. }
  181. #endif
  182. RIX_FillBuffer(stream, len);
  183. }
  184. //
  185. // No current playing sound
  186. //
  187. if (g_fNoSound)
  188. {
  189. return;
  190. }
  191. for (i = 0; i < 2; i++)
  192. {
  193. //
  194. // Only play if we have data left
  195. //
  196. if (gSndPlayer.buf[i] == NULL)
  197. {
  198. continue;
  199. }
  200. if (gSndPlayer.audio_len[i] == 0)
  201. {
  202. //
  203. // Delete the audio buffer from memory
  204. //
  205. free(gSndPlayer.buf[i]);
  206. gSndPlayer.buf[i] = NULL;
  207. continue;
  208. }
  209. //
  210. // Mix as much data as possible
  211. //
  212. len = (len > gSndPlayer.audio_len[i]) ? gSndPlayer.audio_len[i] : len;
  213. #ifdef __SYMBIAN32__
  214. SDL_MixAudio(stream, gSndPlayer.pos[i], len, g_iVolume);
  215. #else
  216. SDL_MixAudio(stream, gSndPlayer.pos[i], len, SDL_MIX_MAXVOLUME);
  217. #endif
  218. gSndPlayer.pos[i] += len;
  219. gSndPlayer.audio_len[i] -= len;
  220. }
  221. }
  222. INT
  223. SOUND_OpenAudio(
  224. VOID
  225. )
  226. /*++
  227. Purpose:
  228. Initialize the audio subsystem.
  229. Parameters:
  230. None.
  231. Return value:
  232. 0 if succeed, others if failed.
  233. --*/
  234. {
  235. SDL_AudioSpec spec;
  236. if (gSndOpened)
  237. {
  238. //
  239. // Already opened
  240. //
  241. return -1;
  242. }
  243. gSndOpened = FALSE;
  244. //
  245. // Load the MKF file.
  246. //
  247. gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "voc.mkf"), "rb");
  248. if (gSndPlayer.mkf == NULL)
  249. {
  250. gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "sounds.mkf"), "rb");
  251. if (gSndPlayer.mkf == NULL)
  252. {
  253. return -2;
  254. }
  255. g_fUseWav = TRUE;
  256. }
  257. else
  258. {
  259. g_fUseWav = FALSE;
  260. }
  261. //
  262. // Open the sound subsystem.
  263. //
  264. gSndPlayer.spec.freq = PAL_SAMPLE_RATE;
  265. gSndPlayer.spec.format = AUDIO_S16;
  266. gSndPlayer.spec.channels = PAL_CHANNELS;
  267. gSndPlayer.spec.samples = 1024;
  268. gSndPlayer.spec.callback = SOUND_FillAudio;
  269. if (SDL_OpenAudio(&gSndPlayer.spec, &spec) < 0)
  270. {
  271. //
  272. // Failed
  273. //
  274. return -3;
  275. }
  276. memcpy(&gSndPlayer.spec, &spec, sizeof(SDL_AudioSpec));
  277. gSndPlayer.buf[0] = NULL;
  278. gSndPlayer.pos[0] = NULL;
  279. gSndPlayer.audio_len[0] = 0;
  280. gSndPlayer.buf[1] = NULL;
  281. gSndPlayer.pos[1] = NULL;
  282. gSndPlayer.audio_len[1] = 0;
  283. gSndOpened = TRUE;
  284. //
  285. // Initialize the music subsystem.
  286. //
  287. RIX_Init(va("%s%s", PAL_PREFIX, "mus.mkf"));
  288. #ifdef PAL_HAS_CD
  289. //
  290. // Initialize the CD audio.
  291. //
  292. {
  293. int i;
  294. gSndPlayer.pCD = NULL;
  295. for (i = 0; i < SDL_CDNumDrives(); i++)
  296. {
  297. gSndPlayer.pCD = SDL_CDOpen(i);
  298. if (gSndPlayer.pCD != NULL)
  299. {
  300. if (!CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
  301. {
  302. SDL_CDClose(gSndPlayer.pCD);
  303. gSndPlayer.pCD = NULL;
  304. }
  305. else
  306. {
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. #endif
  313. #ifdef PAL_HAS_MP3
  314. gSndPlayer.iCurrentMP3 = -1;
  315. gSndPlayer.lock = SDL_CreateMutex();
  316. #endif
  317. //
  318. // Let the callback function run so that musics will be played.
  319. //
  320. SDL_PauseAudio(0);
  321. return 0;
  322. }
  323. #ifdef PSP
  324. void
  325. SOUND_ReloadVOC(
  326. void
  327. )
  328. {
  329. fclose(gSndPlayer.mkf);
  330. gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "voc.mkf"), "rb");
  331. g_fUseWav = FALSE;
  332. }
  333. #endif
  334. VOID
  335. SOUND_CloseAudio(
  336. VOID
  337. )
  338. /*++
  339. Purpose:
  340. Close the audio subsystem.
  341. Parameters:
  342. None.
  343. Return value:
  344. None.
  345. --*/
  346. {
  347. SDL_CloseAudio();
  348. if (gSndPlayer.buf[0] != NULL)
  349. {
  350. free(gSndPlayer.buf[0]);
  351. gSndPlayer.buf[0] = NULL;
  352. }
  353. if (gSndPlayer.buf[1] != NULL)
  354. {
  355. free(gSndPlayer.buf[1]);
  356. gSndPlayer.buf[1] = NULL;
  357. }
  358. if (gSndPlayer.mkf != NULL)
  359. {
  360. fclose(gSndPlayer.mkf);
  361. gSndPlayer.mkf = NULL;
  362. }
  363. #ifdef PAL_HAS_MP3
  364. SDL_mutexP(gSndPlayer.lock);
  365. if (gSndPlayer.pMP3 != NULL)
  366. {
  367. mad_stop(gSndPlayer.pMP3);
  368. mad_closeFile(gSndPlayer.pMP3);
  369. gSndPlayer.pMP3 = NULL;
  370. }
  371. SDL_DestroyMutex(gSndPlayer.lock);
  372. #endif
  373. RIX_Shutdown();
  374. #ifdef PAL_HAS_CD
  375. if (gSndPlayer.pCD != NULL)
  376. {
  377. SOUND_PlayCDA(-1);
  378. SDL_CDClose(gSndPlayer.pCD);
  379. }
  380. #endif
  381. #ifdef PAL_HAS_NATIVEMIDI
  382. MIDI_Play(0, FALSE);
  383. #endif
  384. }
  385. #ifdef __SYMBIAN32__
  386. VOID
  387. SOUND_AdjustVolume(
  388. INT iDirectory
  389. )
  390. /*++
  391. Purpose:
  392. SDL sound volume adjust function.
  393. Parameters:
  394. [IN] iDirectory - value, Increase (>0) or decrease (<=0) 3% volume.
  395. Return value:
  396. None.
  397. --*/
  398. {
  399. if (iDirectory > 0)
  400. {
  401. if (g_iVolume <= SDL_MIX_MAXVOLUME)
  402. {
  403. g_iVolume += SDL_MIX_MAXVOLUME * 0.03;
  404. }
  405. else
  406. {
  407. g_iVolume = SDL_MIX_MAXVOLUME;
  408. }
  409. }
  410. else
  411. {
  412. if (g_iVolume > 0)
  413. {
  414. g_iVolume -= SDL_MIX_MAXVOLUME * 0.03;
  415. }
  416. else
  417. {
  418. g_iVolume = 0;
  419. }
  420. }
  421. }
  422. #endif
  423. VOID
  424. SOUND_PlayChannel(
  425. INT iSoundNum,
  426. INT iChannel
  427. )
  428. /*++
  429. Purpose:
  430. Play a sound in voc.mkf file.
  431. Parameters:
  432. [IN] iSoundNum - number of the sound.
  433. [IN] iChannel - the number of channel (0 or 1).
  434. Return value:
  435. None.
  436. --*/
  437. {
  438. SDL_AudioCVT wavecvt;
  439. SDL_AudioSpec wavespec;
  440. LPBYTE buf, bufdec;
  441. UINT samplesize;
  442. int len;
  443. if (!gSndOpened || g_fNoSound)
  444. {
  445. return;
  446. }
  447. //
  448. // Stop playing current sound.
  449. //
  450. if (gSndPlayer.buf[iChannel] != NULL)
  451. {
  452. LPBYTE p = gSndPlayer.buf[iChannel];
  453. gSndPlayer.buf[iChannel] = NULL;
  454. free(p);
  455. }
  456. if (iSoundNum < 0)
  457. {
  458. return;
  459. }
  460. //
  461. // Get the length of the sound file.
  462. //
  463. len = PAL_MKFGetChunkSize(iSoundNum, gSndPlayer.mkf);
  464. if (len <= 0)
  465. {
  466. return;
  467. }
  468. buf = (LPBYTE)calloc(len, 1);
  469. if (buf == NULL)
  470. {
  471. return;
  472. }
  473. //
  474. // Read the sound file from the MKF archive.
  475. //
  476. PAL_MKFReadChunk(buf, len, iSoundNum, gSndPlayer.mkf);
  477. SOUND_LoadVOCFromBuffer(buf, len, &wavespec, &bufdec);
  478. free(buf);
  479. //
  480. // Build the audio converter and create conversion buffers
  481. //
  482. if (SDL_BuildAudioCVT(&wavecvt, wavespec.format, wavespec.channels, wavespec.freq,
  483. gSndPlayer.spec.format, gSndPlayer.spec.channels, gSndPlayer.spec.freq) < 0)
  484. {
  485. free(bufdec);
  486. return;
  487. }
  488. samplesize = ((wavespec.format & 0xFF) / 8) * wavespec.channels;
  489. wavecvt.len = wavespec.size & ~(samplesize - 1);
  490. wavecvt.buf = (LPBYTE)malloc(wavecvt.len * wavecvt.len_mult);
  491. if (wavecvt.buf == NULL)
  492. {
  493. free(bufdec);
  494. return;
  495. }
  496. memcpy(wavecvt.buf, bufdec, wavespec.size);
  497. free(bufdec);
  498. //
  499. // Run the audio converter
  500. //
  501. if (SDL_ConvertAudio(&wavecvt) < 0)
  502. {
  503. return;
  504. }
  505. gSndPlayer.buf[iChannel] = wavecvt.buf;
  506. gSndPlayer.audio_len[iChannel] = wavecvt.len * wavecvt.len_mult;
  507. gSndPlayer.pos[iChannel] = wavecvt.buf;
  508. }
  509. VOID
  510. PAL_PlayMUS(
  511. INT iNumRIX,
  512. BOOL fLoop,
  513. FLOAT flFadeTime
  514. )
  515. {
  516. #ifdef PAL_HAS_NATIVEMIDI
  517. if (g_fUseMidi)
  518. {
  519. MIDI_Play(iNumRIX, fLoop);
  520. return;
  521. }
  522. #endif
  523. #ifdef PAL_HAS_MP3
  524. if (gSndPlayer.pMP3 != NULL)
  525. {
  526. if (iNumRIX == gSndPlayer.iCurrentMP3 && !g_fNoMusic)
  527. {
  528. return;
  529. }
  530. SDL_mutexP(gSndPlayer.lock);
  531. mad_stop(gSndPlayer.pMP3);
  532. mad_closeFile(gSndPlayer.pMP3);
  533. gSndPlayer.pMP3 = NULL;
  534. SDL_mutexV(gSndPlayer.lock);
  535. }
  536. gSndPlayer.iCurrentMP3 = -1;
  537. if (iNumRIX > 0)
  538. {
  539. SDL_mutexP(gSndPlayer.lock);
  540. gSndPlayer.pMP3 = mad_openFile(va("%s/mp3/%.2d.mp3", PAL_PREFIX, iNumRIX), &gSndPlayer.spec);
  541. if (gSndPlayer.pMP3 != NULL)
  542. {
  543. RIX_Play(0, FALSE, flFadeTime);
  544. mad_start(gSndPlayer.pMP3);
  545. gSndPlayer.fMP3Loop = fLoop;
  546. gSndPlayer.iCurrentMP3 = iNumRIX;
  547. SDL_mutexV(gSndPlayer.lock);
  548. return;
  549. }
  550. SDL_mutexV(gSndPlayer.lock);
  551. }
  552. #endif
  553. RIX_Play(iNumRIX, fLoop, flFadeTime);
  554. }
  555. BOOL
  556. SOUND_PlayCDA(
  557. INT iNumTrack
  558. )
  559. /*++
  560. Purpose:
  561. Play a CD Audio Track.
  562. Parameters:
  563. [IN] iNumTrack - number of the CD Audio Track.
  564. Return value:
  565. TRUE if the track can be played, FALSE if not.
  566. --*/
  567. {
  568. #ifdef PAL_HAS_CD
  569. if (gSndPlayer.pCD != NULL)
  570. {
  571. if (CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
  572. {
  573. SDL_CDStop(gSndPlayer.pCD);
  574. if (iNumTrack != -1)
  575. {
  576. PAL_PlayMUS(-1, FALSE, 0);
  577. if (SDL_CDPlayTracks(gSndPlayer.pCD, iNumTrack - 1, 0, 1, 0) == 0)
  578. {
  579. return TRUE;
  580. }
  581. }
  582. }
  583. }
  584. #endif
  585. return FALSE;
  586. }