sound.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "palcommon.h"
  23. #include "global.h"
  24. #include "palcfg.h"
  25. #include "audio.h"
  26. #include "players.h"
  27. #include "util.h"
  28. #include "resampler.h"
  29. #include "midi.h"
  30. #include <math.h>
  31. #if PAL_HAS_OGG
  32. #include <vorbis/codec.h>
  33. #endif
  34. #define PAL_CDTRACK_BASE 10000
  35. typedef struct tagWAVESPEC
  36. {
  37. int size;
  38. int freq;
  39. #if SDL_VERSION_ATLEAST(2,0,0)
  40. SDL_AudioFormat format;
  41. #else
  42. uint16_t format;
  43. #endif
  44. uint8_t channels;
  45. uint8_t align;
  46. } WAVESPEC;
  47. typedef const void * (*SoundLoader)(LPCBYTE, DWORD, WAVESPEC *);
  48. typedef int(*ResampleMixer)(void *[2], const void *, const WAVESPEC *, void *, int, const void **);
  49. typedef struct tagWAVEDATA
  50. {
  51. struct tagWAVEDATA *next;
  52. void *resampler[2]; /* The resampler used for sound data */
  53. ResampleMixer ResampleMix;
  54. const void *base;
  55. const void *current;
  56. const void *end;
  57. WAVESPEC spec;
  58. } WAVEDATA;
  59. typedef struct tagSOUNDPLAYER
  60. {
  61. AUDIOPLAYER_COMMONS;
  62. FILE *mkf; /* File pointer to the MKF file */
  63. SoundLoader LoadSound; /* The function pointer for load WAVE/VOC data */
  64. WAVEDATA soundlist;
  65. int cursounds;
  66. } SOUNDPLAYER, *LPSOUNDPLAYER;
  67. typedef struct tagRIFFHEADER
  68. {
  69. DWORD riff_sig; /* 'RIFF' */
  70. DWORD data_length; /* Total length minus eight, little-endian */
  71. DWORD riff_type; /* 'WAVE' */
  72. } RIFFHEADER, *LPRIFFHEADER;
  73. typedef const RIFFHEADER *LPCRIFFHEADER;
  74. typedef struct tagRIFFCHUNK
  75. {
  76. DWORD chunk_type; /* 'fmt ' and so on */
  77. DWORD chunk_length; /* Total chunk length minus eight, little-endian */
  78. } RIFFCHUNK, *LPRIFFCHUNK;
  79. typedef const RIFFCHUNK *LPCRIFFCHUNK;
  80. typedef struct tagWAVEFORMATPCM
  81. {
  82. WORD wFormatTag; /* format type */
  83. WORD nChannels; /* number of channels (i.e. mono, stereo, etc.) */
  84. DWORD nSamplesPerSec; /* sample rate */
  85. DWORD nAvgBytesPerSec; /* for buffer estimation */
  86. WORD nBlockAlign; /* block size of data */
  87. WORD wBitsPerSample;
  88. } WAVEFORMATPCM, *LPWAVEFORMATPCM;
  89. typedef const WAVEFORMATPCM *LPCWAVEFORMATPCM;
  90. static const void *
  91. SOUND_LoadWAVEData(
  92. LPCBYTE lpData,
  93. DWORD dwLen,
  94. WAVESPEC *lpSpec
  95. )
  96. /*++
  97. Purpose:
  98. Return the WAVE data pointer inside the input buffer.
  99. Parameters:
  100. [IN] lpData - pointer to the buffer of the WAVE file.
  101. [IN] dwLen - length of the buffer of the WAVE file.
  102. [OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  103. some basic information about the WAVE file.
  104. Return value:
  105. Pointer to the WAVE data inside the input buffer, NULL if failed.
  106. --*/
  107. {
  108. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  109. # define RIFF 'RIFF'
  110. # define WAVE 'WAVE'
  111. # define FMT 'fmt '
  112. # define DATA 'data'
  113. # define PCM 0x0100
  114. #else
  115. # define RIFF 'FFIR'
  116. # define WAVE 'EVAW'
  117. # define FMT ' tmf'
  118. # define DATA 'atad'
  119. # define PCM 0x0001
  120. #endif
  121. LPCRIFFHEADER lpRiff = (LPCRIFFHEADER)lpData;
  122. LPCRIFFCHUNK lpChunk;
  123. LPCWAVEFORMATPCM lpFormat = NULL;
  124. LPCBYTE lpWaveData = NULL;
  125. DWORD len;
  126. if (dwLen < sizeof(RIFFHEADER) || lpRiff->riff_sig != RIFF || lpRiff->riff_type != WAVE || dwLen < SDL_SwapLE32(lpRiff->data_length) + 8)
  127. {
  128. return NULL;
  129. }
  130. lpChunk = (LPCRIFFCHUNK)(lpRiff + 1); dwLen -= sizeof(RIFFHEADER);
  131. while (dwLen >= sizeof(RIFFCHUNK))
  132. {
  133. len = SDL_SwapLE32(lpChunk->chunk_length);
  134. if (dwLen >= sizeof(RIFFCHUNK) + len)
  135. dwLen -= sizeof(RIFFCHUNK) + len;
  136. else
  137. return NULL;
  138. switch (lpChunk->chunk_type)
  139. {
  140. case FMT:
  141. lpFormat = (LPCWAVEFORMATPCM)(lpChunk + 1);
  142. if (len != sizeof(WAVEFORMATPCM) || lpFormat->wFormatTag != PCM)
  143. {
  144. return NULL;
  145. }
  146. break;
  147. case DATA:
  148. lpWaveData = (LPCBYTE)(lpChunk + 1);
  149. dwLen = 0;
  150. break;
  151. }
  152. lpChunk = (LPCRIFFCHUNK)((LPCBYTE)(lpChunk + 1) + len);
  153. }
  154. if (lpFormat == NULL || lpWaveData == NULL)
  155. {
  156. return NULL;
  157. }
  158. lpSpec->channels = lpFormat->nChannels;
  159. lpSpec->format = (lpFormat->wBitsPerSample == 16) ? AUDIO_S16 : AUDIO_U8;
  160. lpSpec->freq = lpFormat->nSamplesPerSec;
  161. lpSpec->size = len;
  162. lpSpec->align = lpFormat->nChannels * lpFormat->wBitsPerSample >> 3;
  163. return lpWaveData;
  164. #undef RIFF
  165. #undef WAVE
  166. #undef FMT
  167. }
  168. typedef struct tagVOCHEADER
  169. {
  170. char signature[0x14]; /* "Creative Voice File\x1A" */
  171. WORD data_offset; /* little endian */
  172. WORD version;
  173. WORD version_checksum;
  174. } VOCHEADER, *LPVOCHEADER;
  175. typedef const VOCHEADER *LPCVOCHEADER;
  176. static const void *
  177. SOUND_LoadVOCData(
  178. LPCBYTE lpData,
  179. DWORD dwLen,
  180. WAVESPEC *lpSpec
  181. )
  182. /*++
  183. Purpose:
  184. Return the VOC data pointer inside the input buffer. Currently supports type 01 block only.
  185. Parameters:
  186. [IN] lpData - pointer to the buffer of the VOC file.
  187. [IN] dwLen - length of the buffer of the VOC file.
  188. [OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
  189. some basic information about the VOC file.
  190. Return value:
  191. Pointer to the WAVE data inside the input buffer, NULL if failed.
  192. Reference: http://sox.sourceforge.net/AudioFormats-11.html
  193. --*/
  194. {
  195. LPCVOCHEADER lpVOC = (LPCVOCHEADER)lpData;
  196. if (dwLen < sizeof(VOCHEADER) || memcmp(lpVOC->signature, "Creative Voice File\x1A", 0x14) || SDL_SwapLE16(lpVOC->data_offset) >= dwLen)
  197. {
  198. return NULL;
  199. }
  200. lpData += SDL_SwapLE16(lpVOC->data_offset);
  201. dwLen -= SDL_SwapLE16(lpVOC->data_offset);
  202. while (dwLen && *lpData)
  203. {
  204. DWORD len;
  205. if (dwLen >= 4)
  206. {
  207. len = lpData[1] | (lpData[2] << 8) | (lpData[3] << 16);
  208. if (dwLen >= len + 4)
  209. dwLen -= len + 4;
  210. else
  211. return NULL;
  212. }
  213. else
  214. {
  215. return NULL;
  216. }
  217. if (*lpData == 0x01)
  218. {
  219. if (lpData[5] != 0) return NULL; /* Only 8-bit is supported */
  220. lpSpec->format = AUDIO_U8;
  221. lpSpec->channels = 1;
  222. lpSpec->freq = ((1000000 / (256 - lpData[4]) + 99) / 100) * 100; /* Round to next 100Hz */
  223. lpSpec->size = len - 2;
  224. lpSpec->align = 1;
  225. return lpData + 6;
  226. }
  227. else
  228. {
  229. lpData += len + 4;
  230. }
  231. }
  232. return NULL;
  233. }
  234. static int
  235. SOUND_ResampleMix_U8_Mono_Mono(
  236. void *resampler[2],
  237. const void *lpData,
  238. const WAVESPEC *lpSpec,
  239. void *lpBuffer,
  240. int iBufLen,
  241. const void **llpData
  242. )
  243. /*++
  244. Purpose:
  245. Resample 8-bit unsigned mono PCM data into 16-bit signed (native-endian) mono PCM data.
  246. Parameters:
  247. [IN] resampler - array of pointers to the resampler instance.
  248. [IN] lpData - pointer to the buffer of the input PCM data.
  249. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  250. some basic information about the input PCM data.
  251. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  252. [IN] iBufLen - length of the buffer of the output PCM data.
  253. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  254. Return value:
  255. The number of output buffer used, in bytes.
  256. --*/
  257. {
  258. int src_samples = lpSpec->size;
  259. const uint8_t * src = (const uint8_t *)lpData;
  260. short *dst = (short *)lpBuffer;
  261. int channel_len = iBufLen, total_bytes = 0;
  262. while (total_bytes < channel_len && src_samples > 0)
  263. {
  264. int j, to_write = resampler_get_free_count(resampler[0]);
  265. if (to_write > src_samples) to_write = src_samples;
  266. for (j = 0; j < to_write; j++)
  267. resampler_write_sample(resampler[0], (*src++ ^ 0x80) << 8);
  268. src_samples -= to_write;
  269. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  270. {
  271. int sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  272. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  273. total_bytes += sizeof(short);
  274. resampler_remove_sample(resampler[0]);
  275. }
  276. }
  277. if (llpData) *llpData = src;
  278. return total_bytes;
  279. }
  280. static int
  281. SOUND_ResampleMix_U8_Mono_Stereo(
  282. void *resampler[2],
  283. const void *lpData,
  284. const WAVESPEC *lpSpec,
  285. void *lpBuffer,
  286. int iBufLen,
  287. const void **llpData
  288. )
  289. /*++
  290. Purpose:
  291. Resample 8-bit unsigned mono PCM data into 16-bit signed (native-endian) stereo PCM data.
  292. Parameters:
  293. [IN] resampler - array of pointers to the resampler instance.
  294. [IN] lpData - pointer to the buffer of the input PCM data.
  295. [IN] lpSpec - pointer to the WAVESPEC 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] iBufLen - length of the buffer of the output PCM data.
  299. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  300. Return value:
  301. The number of output buffer used, in bytes.
  302. --*/
  303. {
  304. int src_samples = lpSpec->size;
  305. const uint8_t * src = (const uint8_t *)lpData;
  306. short *dst = (short *)lpBuffer;
  307. int channel_len = iBufLen >> 1, total_bytes = 0;
  308. while (total_bytes < channel_len && src_samples > 0)
  309. {
  310. int j, to_write = resampler_get_free_count(resampler[0]);
  311. if (to_write > src_samples) to_write = src_samples;
  312. for (j = 0; j < to_write; j++)
  313. resampler_write_sample(resampler[0], (*src++ ^ 0x80) << 8);
  314. src_samples -= to_write;
  315. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  316. {
  317. int sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  318. dst[0] = dst[1] = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  319. total_bytes += sizeof(short); dst += 2;
  320. resampler_remove_sample(resampler[0]);
  321. }
  322. }
  323. if (llpData) *llpData = src;
  324. return total_bytes;
  325. }
  326. static int
  327. SOUND_ResampleMix_U8_Stereo_Mono(
  328. void *resampler[2],
  329. const void *lpData,
  330. const WAVESPEC *lpSpec,
  331. void *lpBuffer,
  332. int iBufLen,
  333. const void **llpData
  334. )
  335. /*++
  336. Purpose:
  337. Resample 8-bit unsigned stereo PCM data into 16-bit signed (native-endian) mono PCM data.
  338. Parameters:
  339. [IN] resampler - array of pointers to the resampler instance.
  340. [IN] lpData - pointer to the buffer of the input PCM data.
  341. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  342. some basic information about the input PCM data.
  343. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  344. [IN] iBufLen - length of the buffer of the output PCM data.
  345. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  346. Return value:
  347. The number of output buffer used, in bytes.
  348. --*/
  349. {
  350. int src_samples = lpSpec->size >> 1;
  351. const uint8_t * src = (const uint8_t *)lpData;
  352. short *dst = (short *)lpBuffer;
  353. int channel_len = iBufLen, total_bytes = 0;
  354. while (total_bytes < channel_len && src_samples > 0)
  355. {
  356. int j, to_write = resampler_get_free_count(resampler[0]);
  357. if (to_write > src_samples) to_write = src_samples;
  358. for (j = 0; j < to_write; j++)
  359. {
  360. resampler_write_sample(resampler[0], (*src++ ^ 0x80) << 8);
  361. resampler_write_sample(resampler[1], (*src++ ^ 0x80) << 8);
  362. }
  363. src_samples -= to_write;
  364. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  365. {
  366. int sample = (((resampler_get_sample(resampler[0]) >> 8) + (resampler_get_sample(resampler[1]) >> 8)) >> 1) + *dst;
  367. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  368. total_bytes += sizeof(short);
  369. resampler_remove_sample(resampler[0]);
  370. resampler_remove_sample(resampler[1]);
  371. }
  372. }
  373. if (llpData) *llpData = src;
  374. return total_bytes;
  375. }
  376. static int
  377. SOUND_ResampleMix_U8_Stereo_Stereo(
  378. void *resampler[2],
  379. const void *lpData,
  380. const WAVESPEC *lpSpec,
  381. void *lpBuffer,
  382. int iBufLen,
  383. const void **llpData
  384. )
  385. /*++
  386. Purpose:
  387. Resample 8-bit unsigned stereo PCM data into 16-bit signed (native-endian) stereo PCM data.
  388. Parameters:
  389. [IN] resampler - array of pointers to the resampler instance.
  390. [IN] lpData - pointer to the buffer of the input PCM data.
  391. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  392. some basic information about the input PCM data.
  393. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  394. [IN] iBufLen - length of the buffer of the output PCM data.
  395. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  396. Return value:
  397. The number of output buffer used, in bytes.
  398. --*/
  399. {
  400. int src_samples = lpSpec->size >> 1;
  401. const uint8_t * src = (const uint8_t *)lpData;
  402. short *dst = (short *)lpBuffer;
  403. int channel_len = iBufLen >> 1, total_bytes = 0;
  404. while (total_bytes < channel_len && src_samples > 0)
  405. {
  406. int j, to_write = resampler_get_free_count(resampler[0]);
  407. if (to_write > src_samples) to_write = src_samples;
  408. for (j = 0; j < to_write; j++)
  409. {
  410. resampler_write_sample(resampler[0], (*src++ ^ 0x80) << 8);
  411. resampler_write_sample(resampler[1], (*src++ ^ 0x80) << 8);
  412. }
  413. src_samples -= to_write;
  414. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  415. {
  416. int sample;
  417. sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  418. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  419. sample = (resampler_get_sample(resampler[1]) >> 8) + *dst;
  420. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  421. total_bytes += sizeof(short);
  422. resampler_remove_sample(resampler[0]);
  423. resampler_remove_sample(resampler[1]);
  424. }
  425. }
  426. if (llpData) *llpData = src;
  427. return total_bytes;
  428. }
  429. static int
  430. SOUND_ResampleMix_S16_Mono_Mono(
  431. void *resampler[2],
  432. const void *lpData,
  433. const WAVESPEC *lpSpec,
  434. void *lpBuffer,
  435. int iBufLen,
  436. const void **llpData
  437. )
  438. /*++
  439. Purpose:
  440. Resample 16-bit signed (little-endian) mono PCM data into 16-bit signed (native-endian) mono PCM data.
  441. Parameters:
  442. [IN] resampler - array of pointers to the resampler instance.
  443. [IN] lpData - pointer to the buffer of the input PCM data.
  444. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  445. some basic information about the input PCM data.
  446. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  447. [IN] iBufLen - length of the buffer of the output PCM data.
  448. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  449. Return value:
  450. The number of output buffer used, in bytes.
  451. --*/
  452. {
  453. int src_samples = lpSpec->size >> 1;
  454. const short * src = (const short *)lpData;
  455. short *dst = (short *)lpBuffer;
  456. int channel_len = iBufLen, total_bytes = 0;
  457. while (total_bytes < channel_len && src_samples > 0)
  458. {
  459. int j, to_write = resampler_get_free_count(resampler[0]);
  460. if (to_write > src_samples) to_write = src_samples;
  461. for (j = 0; j < to_write; j++)
  462. resampler_write_sample(resampler[0], SDL_SwapLE16(*src++));
  463. src_samples -= to_write;
  464. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  465. {
  466. int sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  467. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  468. total_bytes += sizeof(short);
  469. resampler_remove_sample(resampler[0]);
  470. }
  471. }
  472. if (llpData) *llpData = src;
  473. return total_bytes;
  474. }
  475. static int
  476. SOUND_ResampleMix_S16_Mono_Stereo(
  477. void *resampler[2],
  478. const void *lpData,
  479. const WAVESPEC *lpSpec,
  480. void *lpBuffer,
  481. int iBufLen,
  482. const void **llpData
  483. )
  484. /*++
  485. Purpose:
  486. Resample 16-bit signed (little-endian) mono PCM data into 16-bit signed (native-endian) stereo PCM data.
  487. Parameters:
  488. [IN] resampler - array of pointers to the resampler instance.
  489. [IN] lpData - pointer to the buffer of the input PCM data.
  490. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  491. some basic information about the input PCM data.
  492. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  493. [IN] iBufLen - length of the buffer of the output PCM data.
  494. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  495. Return value:
  496. The number of output buffer used, in bytes.
  497. --*/
  498. {
  499. int src_samples = lpSpec->size >> 1;
  500. const short * src = (const short *)lpData;
  501. short *dst = (short *)lpBuffer;
  502. int channel_len = iBufLen >> 1, total_bytes = 0;
  503. while (total_bytes < channel_len && src_samples > 0)
  504. {
  505. int j, to_write = resampler_get_free_count(resampler[0]);
  506. if (to_write > src_samples) to_write = src_samples;
  507. for (j = 0; j < to_write; j++)
  508. resampler_write_sample(resampler[0], SDL_SwapLE16(*src++));
  509. src_samples -= to_write;
  510. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  511. {
  512. int sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  513. dst[0] = dst[1] = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  514. total_bytes += sizeof(short); dst += 2;
  515. resampler_remove_sample(resampler[0]);
  516. }
  517. }
  518. if (llpData) *llpData = src;
  519. return total_bytes;
  520. }
  521. static int
  522. SOUND_ResampleMix_S16_Stereo_Mono(
  523. void *resampler[2],
  524. const void *lpData,
  525. const WAVESPEC *lpSpec,
  526. void *lpBuffer,
  527. int iBufLen,
  528. const void **llpData
  529. )
  530. /*++
  531. Purpose:
  532. Resample 16-bit signed (little-endian) stereo PCM data into 16-bit signed (native-endian) mono PCM data.
  533. Parameters:
  534. [IN] resampler - array of pointers to the resampler instance.
  535. [IN] lpData - pointer to the buffer of the input PCM data.
  536. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  537. some basic information about the input PCM data.
  538. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  539. [IN] iBufLen - length of the buffer of the output PCM data.
  540. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  541. Return value:
  542. The number of output buffer used, in bytes.
  543. --*/
  544. {
  545. int src_samples = lpSpec->size >> 2;
  546. const short * src = (const short *)lpData;
  547. short *dst = (short *)lpBuffer;
  548. int channel_len = iBufLen, total_bytes = 0;
  549. while (total_bytes < channel_len && src_samples > 0)
  550. {
  551. int j, to_write = resampler_get_free_count(resampler[0]);
  552. if (to_write > src_samples) to_write = src_samples;
  553. for (j = 0; j < to_write; j++)
  554. {
  555. resampler_write_sample(resampler[0], SDL_SwapLE16(*src++));
  556. resampler_write_sample(resampler[1], SDL_SwapLE16(*src++));
  557. }
  558. src_samples -= to_write;
  559. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  560. {
  561. int sample = (((resampler_get_sample(resampler[0]) >> 8) + (resampler_get_sample(resampler[1]) >> 8)) >> 1) + *dst;
  562. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  563. total_bytes += sizeof(short);
  564. resampler_remove_sample(resampler[0]);
  565. resampler_remove_sample(resampler[1]);
  566. }
  567. }
  568. if (llpData) *llpData = src;
  569. return total_bytes;
  570. }
  571. static int
  572. SOUND_ResampleMix_S16_Stereo_Stereo(
  573. void *resampler[2],
  574. const void *lpData,
  575. const WAVESPEC *lpSpec,
  576. void *lpBuffer,
  577. int iBufLen,
  578. const void **llpData
  579. )
  580. /*++
  581. Purpose:
  582. Resample 16-bit signed (little-endian) stereo PCM data into 16-bit signed (native-endian) stereo PCM data.
  583. Parameters:
  584. [IN] resampler - array of pointers to the resampler instance.
  585. [IN] lpData - pointer to the buffer of the input PCM data.
  586. [IN] lpSpec - pointer to the WAVESPEC structure, which contains
  587. some basic information about the input PCM data.
  588. [IN] lpBuffer - pointer of the buffer of the output PCM data.
  589. [IN] iBufLen - length of the buffer of the output PCM data.
  590. [OUT] llpData - pointer to receive the pointer of remaining input PCM data.
  591. Return value:
  592. The number of output buffer used, in bytes.
  593. --*/
  594. {
  595. int src_samples = lpSpec->size >> 2;
  596. const short * src = (const short *)lpData;
  597. short *dst = (short *)lpBuffer;
  598. int channel_len = iBufLen >> 1, total_bytes = 0;
  599. while (total_bytes < channel_len && src_samples > 0)
  600. {
  601. int j, to_write = resampler_get_free_count(resampler[0]);
  602. if (to_write > src_samples) to_write = src_samples;
  603. for (j = 0; j < to_write; j++)
  604. {
  605. resampler_write_sample(resampler[0], SDL_SwapLE16(*src++));
  606. resampler_write_sample(resampler[1], SDL_SwapLE16(*src++));
  607. }
  608. src_samples -= to_write;
  609. while (total_bytes < channel_len && resampler_get_sample_count(resampler[0]) > 0)
  610. {
  611. int sample;
  612. sample = (resampler_get_sample(resampler[0]) >> 8) + *dst;
  613. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  614. sample = (resampler_get_sample(resampler[1]) >> 8) + *dst;
  615. *dst++ = (sample <= 32767) ? ((sample >= -32768) ? sample : -32768) : 32767;
  616. total_bytes += sizeof(short);
  617. resampler_remove_sample(resampler[0]);
  618. resampler_remove_sample(resampler[1]);
  619. }
  620. }
  621. if (llpData) *llpData = src;
  622. return total_bytes;
  623. }
  624. static BOOL
  625. SOUND_Play(
  626. VOID *object,
  627. INT iSoundNum,
  628. BOOL fLoop,
  629. FLOAT flFadeTime
  630. )
  631. /*++
  632. Purpose:
  633. Play a sound in voc.mkf/sounds.mkf file.
  634. Parameters:
  635. [IN] object - Pointer to the SOUNDPLAYER instance.
  636. [IN] iSoundNum - number of the sound; the absolute value is used.
  637. [IN] fLoop - Not used, should be zero.
  638. [IN] flFadeTime - Not used, should be zero.
  639. Return value:
  640. None.
  641. --*/
  642. {
  643. LPSOUNDPLAYER player = (LPSOUNDPLAYER)object;
  644. const SDL_AudioSpec *devspec = AUDIO_GetDeviceSpec();
  645. WAVESPEC wavespec;
  646. ResampleMixer mixer;
  647. WAVEDATA *cursnd;
  648. void *buf;
  649. const void *snddata;
  650. int len, i;
  651. //
  652. // Check for NULL pointer.
  653. //
  654. if (player == NULL)
  655. {
  656. return FALSE;
  657. }
  658. //
  659. // Get the length of the sound file.
  660. //
  661. len = PAL_MKFGetChunkSize(iSoundNum, player->mkf);
  662. if (len <= 0)
  663. {
  664. return FALSE;
  665. }
  666. buf = malloc(len);
  667. if (buf == NULL)
  668. {
  669. return FALSE;
  670. }
  671. //
  672. // Read the sound file from the MKF archive.
  673. //
  674. PAL_MKFReadChunk(buf, len, iSoundNum, player->mkf);
  675. snddata = player->LoadSound(buf, len, &wavespec);
  676. if (snddata == NULL)
  677. {
  678. free(buf);
  679. return FALSE;
  680. }
  681. if (wavespec.channels == 1 && devspec->channels == 1)
  682. mixer = (wavespec.format == AUDIO_S16) ? SOUND_ResampleMix_S16_Mono_Mono : SOUND_ResampleMix_U8_Mono_Mono;
  683. else if (wavespec.channels == 1 && devspec->channels == 2)
  684. mixer = (wavespec.format == AUDIO_S16) ? SOUND_ResampleMix_S16_Mono_Stereo : SOUND_ResampleMix_U8_Mono_Stereo;
  685. else if (wavespec.channels == 2 && devspec->channels == 1)
  686. mixer = (wavespec.format == AUDIO_S16) ? SOUND_ResampleMix_S16_Stereo_Mono : SOUND_ResampleMix_U8_Stereo_Mono;
  687. else if (wavespec.channels == 2 && devspec->channels == 2)
  688. mixer = (wavespec.format == AUDIO_S16) ? SOUND_ResampleMix_S16_Stereo_Stereo : SOUND_ResampleMix_U8_Stereo_Stereo;
  689. else
  690. {
  691. free(buf);
  692. return FALSE;
  693. }
  694. SDL_LockAudio();
  695. cursnd = &player->soundlist;
  696. while (cursnd->next && cursnd->base)
  697. cursnd = cursnd->next;
  698. if (cursnd->base)
  699. {
  700. WAVEDATA *obj = (WAVEDATA *)malloc(sizeof(WAVEDATA));
  701. memset(obj, 0, sizeof(WAVEDATA));
  702. cursnd->next = obj;
  703. cursnd = cursnd->next;
  704. }
  705. for (i = 0; i < wavespec.channels; i++)
  706. {
  707. if (!cursnd->resampler[i])
  708. cursnd->resampler[i] = resampler_create();
  709. else
  710. resampler_clear(cursnd->resampler[i]);
  711. resampler_set_quality(cursnd->resampler[i], AUDIO_IsIntegerConversion(wavespec.freq) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
  712. resampler_set_rate(cursnd->resampler[i], (double)wavespec.freq / (double)devspec->freq);
  713. }
  714. cursnd->base = buf;
  715. cursnd->current = snddata;
  716. cursnd->end = (const uint8_t *)snddata + wavespec.size;
  717. cursnd->spec = wavespec;
  718. cursnd->ResampleMix = mixer;
  719. player->cursounds++;
  720. SDL_UnlockAudio();
  721. return TRUE;
  722. }
  723. VOID
  724. SOUND_Shutdown(
  725. VOID *object
  726. )
  727. /*++
  728. Purpose:
  729. Shutdown the sound subsystem.
  730. Parameters:
  731. None.
  732. Return value:
  733. None.
  734. --*/
  735. {
  736. LPSOUNDPLAYER player = (LPSOUNDPLAYER)object;
  737. if (player)
  738. {
  739. WAVEDATA *cursnd = &player->soundlist;
  740. do
  741. {
  742. if (cursnd->resampler[0]) resampler_delete(cursnd->resampler[0]);
  743. if (cursnd->resampler[1]) resampler_delete(cursnd->resampler[1]);
  744. if (cursnd->base) free((void *)cursnd->base);
  745. } while (cursnd = cursnd->next);
  746. cursnd = player->soundlist.next;
  747. while (cursnd)
  748. {
  749. WAVEDATA *old = cursnd;
  750. cursnd = cursnd->next;
  751. free(old);
  752. }
  753. if (player->mkf) fclose(player->mkf);
  754. }
  755. }
  756. static VOID
  757. SOUND_FillBuffer(
  758. VOID *object,
  759. LPBYTE stream,
  760. INT len
  761. )
  762. /*++
  763. Purpose:
  764. Fill the background music into the sound buffer. Called by the SDL sound
  765. callback function only (audio.c: AUDIO_FillBuffer).
  766. Parameters:
  767. [OUT] stream - pointer to the stream buffer.
  768. [IN] len - Length of the buffer.
  769. Return value:
  770. None.
  771. --*/
  772. {
  773. LPSOUNDPLAYER player = (LPSOUNDPLAYER)object;
  774. if (player)
  775. {
  776. WAVEDATA *cursnd = &player->soundlist;
  777. int sounds = 0;
  778. do
  779. {
  780. if (cursnd->base)
  781. {
  782. cursnd->ResampleMix(cursnd->resampler, cursnd->current, &cursnd->spec, stream, len, &cursnd->current);
  783. cursnd->spec.size = (const uint8_t *)cursnd->end - (const uint8_t *)cursnd->current;
  784. if (cursnd->spec.size < cursnd->spec.align)
  785. {
  786. free((void *)cursnd->base);
  787. cursnd->base = cursnd->current = cursnd->end = NULL;
  788. player->cursounds--;
  789. }
  790. else
  791. sounds++;
  792. }
  793. } while ((cursnd = cursnd->next) && sounds < player->cursounds);
  794. }
  795. }
  796. LPAUDIOPLAYER
  797. SOUND_Init(
  798. VOID
  799. )
  800. /*++
  801. Purpose:
  802. Initialize the sound subsystem.
  803. Parameters:
  804. None.
  805. Return value:
  806. None.
  807. --*/
  808. {
  809. char *mkfs[2];
  810. SoundLoader func[2];
  811. int i;
  812. if (gConfig.fIsWIN95)
  813. {
  814. mkfs[0] = "sounds.mkf"; func[0] = SOUND_LoadWAVEData;
  815. mkfs[1] = "voc.mkf"; func[1] = SOUND_LoadVOCData;
  816. }
  817. else
  818. {
  819. mkfs[0] = "voc.mkf"; func[0] = SOUND_LoadVOCData;
  820. mkfs[1] = "sounds.mkf"; func[1] = SOUND_LoadWAVEData;
  821. }
  822. for (i = 0; i < 2; i++)
  823. {
  824. FILE *mkf = UTIL_OpenFile(mkfs[i]);
  825. if (mkf)
  826. {
  827. LPSOUNDPLAYER player = (LPSOUNDPLAYER)malloc(sizeof(SOUNDPLAYER));
  828. memset(&player->soundlist, 0, sizeof(WAVEDATA));
  829. player->Play = SOUND_Play;
  830. player->FillBuffer = SOUND_FillBuffer;
  831. player->Shutdown = SOUND_Shutdown;
  832. player->LoadSound = func[i];
  833. player->mkf = mkf;
  834. player->soundlist.resampler[0] = resampler_create();
  835. player->soundlist.resampler[1] = resampler_create();
  836. player->cursounds = 0;
  837. return (LPAUDIOPLAYER)player;
  838. }
  839. }
  840. return NULL;
  841. }