SDL_audiocvt.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. /* Functions for audio drivers to perform runtime conversion of audio format */
  20. #include "SDL_audio_c.h"
  21. #include "../SDL_dataqueue.h"
  22. #include "../SDL_intrin.h"
  23. #define DEBUG_AUDIOSTREAM 0
  24. #ifdef __ARM_NEON
  25. #define HAVE_NEON_INTRINSICS 1
  26. #endif
  27. #ifdef __SSE__
  28. #define HAVE_SSE_INTRINSICS 1
  29. #endif
  30. #ifdef __SSE3__
  31. #define HAVE_SSE3_INTRINSICS 1
  32. #endif
  33. #if defined(HAVE_IMMINTRIN_H) && !defined(SDL_DISABLE_IMMINTRIN_H)
  34. #define HAVE_AVX_INTRINSICS 1
  35. #endif
  36. #if defined __clang__
  37. #if (!__has_attribute(target))
  38. #undef HAVE_AVX_INTRINSICS
  39. #endif
  40. #if (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX__)
  41. #undef HAVE_AVX_INTRINSICS
  42. #endif
  43. #elif defined __GNUC__
  44. #if (__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)
  45. #undef HAVE_AVX_INTRINSICS
  46. #endif
  47. #endif
  48. /*
  49. * CHANNEL LAYOUTS AS SDL EXPECTS THEM:
  50. *
  51. * (Even if the platform expects something else later, that
  52. * SDL will swizzle between the app and the platform).
  53. *
  54. * Abbreviations:
  55. * - FRONT=single mono speaker
  56. * - FL=front left speaker
  57. * - FR=front right speaker
  58. * - FC=front center speaker
  59. * - BL=back left speaker
  60. * - BR=back right speaker
  61. * - SR=surround right speaker
  62. * - SL=surround left speaker
  63. * - BC=back center speaker
  64. * - LFE=low-frequency speaker
  65. *
  66. * These are listed in the order they are laid out in
  67. * memory, so "FL+FR" means "the front left speaker is
  68. * layed out in memory first, then the front right, then
  69. * it repeats for the next audio frame".
  70. *
  71. * 1 channel (mono) layout: FRONT
  72. * 2 channels (stereo) layout: FL+FR
  73. * 3 channels (2.1) layout: FL+FR+LFE
  74. * 4 channels (quad) layout: FL+FR+BL+BR
  75. * 5 channels (4.1) layout: FL+FR+LFE+BL+BR
  76. * 6 channels (5.1) layout: FL+FR+FC+LFE+BL+BR
  77. * 7 channels (6.1) layout: FL+FR+FC+LFE+BC+SL+SR
  78. * 8 channels (7.1) layout: FL+FR+FC+LFE+BL+BR+SL+SR
  79. */
  80. #if HAVE_SSE3_INTRINSICS
  81. /* Convert from stereo to mono. Average left and right. */
  82. static void SDLCALL SDL_ConvertStereoToMono_SSE3(SDL_AudioCVT *cvt, SDL_AudioFormat format)
  83. {
  84. const __m128 divby2 = _mm_set1_ps(0.5f);
  85. float *dst = (float *)cvt->buf;
  86. const float *src = dst;
  87. int i = cvt->len_cvt / 8;
  88. LOG_DEBUG_CONVERT("stereo", "mono (using SSE3)");
  89. SDL_assert(format == AUDIO_F32SYS);
  90. /* Do SSE blocks as long as we have 16 bytes available.
  91. Just use unaligned load/stores, if the memory at runtime is
  92. aligned it'll be just as fast on modern processors */
  93. while (i >= 4) { /* 4 * float32 */
  94. _mm_storeu_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_loadu_ps(src), _mm_loadu_ps(src + 4)), divby2));
  95. i -= 4;
  96. src += 8;
  97. dst += 4;
  98. }
  99. /* Finish off any leftovers with scalar operations. */
  100. while (i) {
  101. *dst = (src[0] + src[1]) * 0.5f;
  102. dst++;
  103. i--;
  104. src += 2;
  105. }
  106. cvt->len_cvt /= 2;
  107. if (cvt->filters[++cvt->filter_index]) {
  108. cvt->filters[cvt->filter_index](cvt, format);
  109. }
  110. }
  111. #endif
  112. #if HAVE_SSE_INTRINSICS
  113. /* Convert from mono to stereo. Duplicate to stereo left and right. */
  114. static void SDLCALL SDL_ConvertMonoToStereo_SSE(SDL_AudioCVT *cvt, SDL_AudioFormat format)
  115. {
  116. float *dst = ((float *)(cvt->buf + (cvt->len_cvt * 2))) - 8;
  117. const float *src = ((const float *)(cvt->buf + cvt->len_cvt)) - 4;
  118. int i = cvt->len_cvt / sizeof(float);
  119. LOG_DEBUG_CONVERT("mono", "stereo (using SSE)");
  120. SDL_assert(format == AUDIO_F32SYS);
  121. /* Do SSE blocks as long as we have 16 bytes available.
  122. Just use unaligned load/stores, if the memory at runtime is
  123. aligned it'll be just as fast on modern processors */
  124. /* convert backwards, since output is growing in-place. */
  125. while (i >= 4) { /* 4 * float32 */
  126. const __m128 input = _mm_loadu_ps(src); /* A B C D */
  127. _mm_storeu_ps(dst, _mm_unpacklo_ps(input, input)); /* A A B B */
  128. _mm_storeu_ps(dst + 4, _mm_unpackhi_ps(input, input)); /* C C D D */
  129. i -= 4;
  130. src -= 4;
  131. dst -= 8;
  132. }
  133. /* Finish off any leftovers with scalar operations. */
  134. src += 3;
  135. dst += 6; /* adjust for smaller buffers. */
  136. while (i) { /* convert backwards, since output is growing in-place. */
  137. const float srcFC = src[0];
  138. dst[1] /* FR */ = srcFC;
  139. dst[0] /* FL */ = srcFC;
  140. i--;
  141. src--;
  142. dst -= 2;
  143. }
  144. cvt->len_cvt *= 2;
  145. if (cvt->filters[++cvt->filter_index]) {
  146. cvt->filters[cvt->filter_index](cvt, format);
  147. }
  148. }
  149. #endif
  150. /* Include the autogenerated channel converters... */
  151. #include "SDL_audio_channel_converters.h"
  152. /* SDL's resampler uses a "bandlimited interpolation" algorithm:
  153. https://ccrma.stanford.edu/~jos/resample/ */
  154. #include "SDL_audio_resampler_filter.h"
  155. static int GetResamplerPadding(const int inrate, const int outrate)
  156. {
  157. if (inrate == outrate) {
  158. return 0;
  159. }
  160. if (inrate > outrate) {
  161. return (int)SDL_ceilf(((float)(RESAMPLER_SAMPLES_PER_ZERO_CROSSING * inrate) / ((float)outrate)));
  162. }
  163. return RESAMPLER_SAMPLES_PER_ZERO_CROSSING;
  164. }
  165. /* lpadding and rpadding are expected to be buffers of (ResamplePadding(inrate, outrate) * chans * sizeof (float)) bytes. */
  166. static int SDL_ResampleAudio(const int chans, const int inrate, const int outrate,
  167. const float *lpadding, const float *rpadding,
  168. const float *inbuf, const int inbuflen,
  169. float *outbuf, const int outbuflen)
  170. {
  171. /* Note that this used to be double, but it looks like we can get by with float in most cases at
  172. almost twice the speed on Intel processors, and orders of magnitude more
  173. on CPUs that need a software fallback for double calculations. */
  174. typedef float ResampleFloatType;
  175. const ResampleFloatType finrate = (ResampleFloatType)inrate;
  176. const ResampleFloatType ratio = ((float)outrate) / ((float)inrate);
  177. const int paddinglen = GetResamplerPadding(inrate, outrate);
  178. const int framelen = chans * (int)sizeof(float);
  179. const int inframes = inbuflen / framelen;
  180. const int wantedoutframes = (int)(inframes * ratio); /* outbuflen isn't total to write, it's total available. */
  181. const int maxoutframes = outbuflen / framelen;
  182. const int outframes = SDL_min(wantedoutframes, maxoutframes);
  183. ResampleFloatType outtime = 0.0f;
  184. float *dst = outbuf;
  185. int i, j, chan;
  186. for (i = 0; i < outframes; i++) {
  187. const int srcindex = (int)(outtime * inrate);
  188. const ResampleFloatType intime = ((ResampleFloatType)srcindex) / finrate;
  189. const ResampleFloatType innexttime = ((ResampleFloatType)(srcindex + 1)) / finrate;
  190. const ResampleFloatType indeltatime = innexttime - intime;
  191. const ResampleFloatType interpolation1 = (indeltatime == 0.0f) ? 1.0f : (1.0f - ((innexttime - outtime) / indeltatime));
  192. const int filterindex1 = (int)(interpolation1 * RESAMPLER_SAMPLES_PER_ZERO_CROSSING);
  193. const ResampleFloatType interpolation2 = 1.0f - interpolation1;
  194. const int filterindex2 = (int)(interpolation2 * RESAMPLER_SAMPLES_PER_ZERO_CROSSING);
  195. for (chan = 0; chan < chans; chan++) {
  196. float outsample = 0.0f;
  197. /* do this twice to calculate the sample, once for the "left wing" and then same for the right. */
  198. for (j = 0; (filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)) < RESAMPLER_FILTER_SIZE; j++) {
  199. const int srcframe = srcindex - j;
  200. /* !!! FIXME: we can bubble this conditional out of here by doing a pre loop. */
  201. const float insample = (srcframe < 0) ? lpadding[((paddinglen + srcframe) * chans) + chan] : inbuf[(srcframe * chans) + chan];
  202. outsample += (insample * (ResamplerFilter[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)] + (interpolation1 * ResamplerFilterDifference[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)])));
  203. }
  204. /* Do the right wing! */
  205. for (j = 0; (filterindex2 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)) < RESAMPLER_FILTER_SIZE; j++) {
  206. const int jsamples = j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING;
  207. const int srcframe = srcindex + 1 + j;
  208. /* !!! FIXME: we can bubble this conditional out of here by doing a post loop. */
  209. const float insample = (srcframe >= inframes) ? rpadding[((srcframe - inframes) * chans) + chan] : inbuf[(srcframe * chans) + chan];
  210. outsample += (insample * (ResamplerFilter[filterindex2 + jsamples] + (interpolation2 * ResamplerFilterDifference[filterindex2 + jsamples])));
  211. }
  212. *(dst++) = outsample;
  213. }
  214. outtime = ((ResampleFloatType)i) / ((ResampleFloatType)outrate);
  215. }
  216. return outframes * chans * sizeof(float);
  217. }
  218. int SDL_ConvertAudio(SDL_AudioCVT *cvt)
  219. {
  220. /* !!! FIXME: (cvt) should be const; stack-copy it here. */
  221. /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
  222. /* Make sure there's data to convert */
  223. if (cvt->buf == NULL) {
  224. return SDL_SetError("No buffer allocated for conversion");
  225. }
  226. /* Return okay if no conversion is necessary */
  227. cvt->len_cvt = cvt->len;
  228. if (cvt->filters[0] == NULL) {
  229. return 0;
  230. }
  231. /* Set up the conversion and go! */
  232. cvt->filter_index = 0;
  233. cvt->filters[0](cvt, cvt->src_format);
  234. return 0;
  235. }
  236. static void SDLCALL SDL_Convert_Byteswap(SDL_AudioCVT *cvt, SDL_AudioFormat format)
  237. {
  238. #if DEBUG_CONVERT
  239. SDL_Log("SDL_AUDIO_CONVERT: Converting byte order\n");
  240. #endif
  241. switch (SDL_AUDIO_BITSIZE(format)) {
  242. #define CASESWAP(b) \
  243. case b: \
  244. { \
  245. Uint##b *ptr = (Uint##b *)cvt->buf; \
  246. int i; \
  247. for (i = cvt->len_cvt / sizeof(*ptr); i; --i, ++ptr) { \
  248. *ptr = SDL_Swap##b(*ptr); \
  249. } \
  250. break; \
  251. }
  252. CASESWAP(16);
  253. CASESWAP(32);
  254. CASESWAP(64);
  255. #undef CASESWAP
  256. default:
  257. SDL_assert(!"unhandled byteswap datatype!");
  258. break;
  259. }
  260. if (cvt->filters[++cvt->filter_index]) {
  261. /* flip endian flag for data. */
  262. if (format & SDL_AUDIO_MASK_ENDIAN) {
  263. format &= ~SDL_AUDIO_MASK_ENDIAN;
  264. } else {
  265. format |= SDL_AUDIO_MASK_ENDIAN;
  266. }
  267. cvt->filters[cvt->filter_index](cvt, format);
  268. }
  269. }
  270. static int SDL_AddAudioCVTFilter(SDL_AudioCVT *cvt, SDL_AudioFilter filter)
  271. {
  272. if (cvt->filter_index >= SDL_AUDIOCVT_MAX_FILTERS) {
  273. return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS);
  274. }
  275. SDL_assert(filter != NULL);
  276. cvt->filters[cvt->filter_index++] = filter;
  277. cvt->filters[cvt->filter_index] = NULL; /* Moving terminator */
  278. return 0;
  279. }
  280. static int SDL_BuildAudioTypeCVTToFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat src_fmt)
  281. {
  282. int retval = 0; /* 0 == no conversion necessary. */
  283. if ((SDL_AUDIO_ISBIGENDIAN(src_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN) && SDL_AUDIO_BITSIZE(src_fmt) > 8) {
  284. if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
  285. return -1;
  286. }
  287. retval = 1; /* added a converter. */
  288. }
  289. if (!SDL_AUDIO_ISFLOAT(src_fmt)) {
  290. const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
  291. const Uint16 dst_bitsize = 32;
  292. SDL_AudioFilter filter = NULL;
  293. switch (src_fmt & ~SDL_AUDIO_MASK_ENDIAN) {
  294. case AUDIO_S8:
  295. filter = SDL_Convert_S8_to_F32;
  296. break;
  297. case AUDIO_U8:
  298. filter = SDL_Convert_U8_to_F32;
  299. break;
  300. case AUDIO_S16:
  301. filter = SDL_Convert_S16_to_F32;
  302. break;
  303. case AUDIO_U16:
  304. filter = SDL_Convert_U16_to_F32;
  305. break;
  306. case AUDIO_S32:
  307. filter = SDL_Convert_S32_to_F32;
  308. break;
  309. default:
  310. SDL_assert(!"Unexpected audio format!");
  311. break;
  312. }
  313. if (!filter) {
  314. return SDL_SetError("No conversion from source format to float available");
  315. }
  316. if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
  317. return -1;
  318. }
  319. if (src_bitsize < dst_bitsize) {
  320. const int mult = (dst_bitsize / src_bitsize);
  321. cvt->len_mult *= mult;
  322. cvt->len_ratio *= mult;
  323. } else if (src_bitsize > dst_bitsize) {
  324. const int div = (src_bitsize / dst_bitsize);
  325. cvt->len_ratio /= div;
  326. }
  327. retval = 1; /* added a converter. */
  328. }
  329. return retval;
  330. }
  331. static int SDL_BuildAudioTypeCVTFromFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat dst_fmt)
  332. {
  333. int retval = 0; /* 0 == no conversion necessary. */
  334. if (!SDL_AUDIO_ISFLOAT(dst_fmt)) {
  335. const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
  336. const Uint16 src_bitsize = 32;
  337. SDL_AudioFilter filter = NULL;
  338. switch (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN) {
  339. case AUDIO_S8:
  340. filter = SDL_Convert_F32_to_S8;
  341. break;
  342. case AUDIO_U8:
  343. filter = SDL_Convert_F32_to_U8;
  344. break;
  345. case AUDIO_S16:
  346. filter = SDL_Convert_F32_to_S16;
  347. break;
  348. case AUDIO_U16:
  349. filter = SDL_Convert_F32_to_U16;
  350. break;
  351. case AUDIO_S32:
  352. filter = SDL_Convert_F32_to_S32;
  353. break;
  354. default:
  355. SDL_assert(!"Unexpected audio format!");
  356. break;
  357. }
  358. if (!filter) {
  359. return SDL_SetError("No conversion from float to format 0x%.4x available", dst_fmt);
  360. }
  361. if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
  362. return -1;
  363. }
  364. if (src_bitsize < dst_bitsize) {
  365. const int mult = (dst_bitsize / src_bitsize);
  366. cvt->len_mult *= mult;
  367. cvt->len_ratio *= mult;
  368. } else if (src_bitsize > dst_bitsize) {
  369. const int div = (src_bitsize / dst_bitsize);
  370. cvt->len_ratio /= div;
  371. }
  372. retval = 1; /* added a converter. */
  373. }
  374. if ((SDL_AUDIO_ISBIGENDIAN(dst_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN) && SDL_AUDIO_BITSIZE(dst_fmt) > 8) {
  375. if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
  376. return -1;
  377. }
  378. retval = 1; /* added a converter. */
  379. }
  380. return retval;
  381. }
  382. #ifdef HAVE_LIBSAMPLERATE_H
  383. static void SDL_ResampleCVT_SRC(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format)
  384. {
  385. const float *src = (const float *)cvt->buf;
  386. const int srclen = cvt->len_cvt;
  387. float *dst = (float *)(cvt->buf + srclen);
  388. const int dstlen = (cvt->len * cvt->len_mult) - srclen;
  389. const int framelen = sizeof(float) * chans;
  390. int result = 0;
  391. SRC_DATA data;
  392. SDL_zero(data);
  393. data.data_in = (float *)src; /* Older versions of libsamplerate had a non-const pointer, but didn't write to it */
  394. data.input_frames = srclen / framelen;
  395. data.data_out = dst;
  396. data.output_frames = dstlen / framelen;
  397. data.src_ratio = cvt->rate_incr;
  398. result = SRC_src_simple(&data, SRC_converter, chans); /* Simple API converts the whole buffer at once. No need for initialization. */
  399. /* !!! FIXME: Handle library failures? */
  400. #ifdef DEBUG_CONVERT
  401. if (result != 0) {
  402. SDL_Log("src_simple() failed: %s", SRC_src_strerror(result));
  403. }
  404. #endif
  405. cvt->len_cvt = data.output_frames_gen * framelen;
  406. SDL_memmove(cvt->buf, dst, cvt->len_cvt);
  407. if (cvt->filters[++cvt->filter_index]) {
  408. cvt->filters[cvt->filter_index](cvt, format);
  409. }
  410. }
  411. #endif /* HAVE_LIBSAMPLERATE_H */
  412. static void SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format)
  413. {
  414. /* !!! FIXME in 2.1: there are ten slots in the filter list, and the theoretical maximum we use is six (seven with NULL terminator).
  415. !!! FIXME in 2.1: We need to store data for this resampler, because the cvt structure doesn't store the original sample rates,
  416. !!! FIXME in 2.1: so we steal the ninth and tenth slot. :( */
  417. const int inrate = (int)(size_t)cvt->filters[SDL_AUDIOCVT_MAX_FILTERS - 1];
  418. const int outrate = (int)(size_t)cvt->filters[SDL_AUDIOCVT_MAX_FILTERS];
  419. const float *src = (const float *)cvt->buf;
  420. const int srclen = cvt->len_cvt;
  421. /*float *dst = (float *) cvt->buf;
  422. const int dstlen = (cvt->len * cvt->len_mult);*/
  423. /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
  424. float *dst = (float *)(cvt->buf + srclen);
  425. const int dstlen = (cvt->len * cvt->len_mult) - srclen;
  426. const int requestedpadding = GetResamplerPadding(inrate, outrate);
  427. int paddingsamples;
  428. float *padding;
  429. if (requestedpadding < SDL_MAX_SINT32 / chans) {
  430. paddingsamples = requestedpadding * chans;
  431. } else {
  432. paddingsamples = 0;
  433. }
  434. SDL_assert(format == AUDIO_F32SYS);
  435. /* we keep no streaming state here, so pad with silence on both ends. */
  436. padding = (float *)SDL_calloc(paddingsamples ? paddingsamples : 1, sizeof(float));
  437. if (padding == NULL) {
  438. SDL_OutOfMemory();
  439. return;
  440. }
  441. cvt->len_cvt = SDL_ResampleAudio(chans, inrate, outrate, padding, padding, src, srclen, dst, dstlen);
  442. SDL_free(padding);
  443. SDL_memmove(cvt->buf, dst, cvt->len_cvt); /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
  444. if (cvt->filters[++cvt->filter_index]) {
  445. cvt->filters[cvt->filter_index](cvt, format);
  446. }
  447. }
  448. /* !!! FIXME: We only have this macro salsa because SDL_AudioCVT doesn't
  449. !!! FIXME: store channel info, so we have to have function entry
  450. !!! FIXME: points for each supported channel count and multiple
  451. !!! FIXME: vs arbitrary. When we rev the ABI, clean this up. */
  452. #define RESAMPLER_FUNCS(chans) \
  453. static void SDLCALL \
  454. SDL_ResampleCVT_c##chans(SDL_AudioCVT *cvt, SDL_AudioFormat format) \
  455. { \
  456. SDL_ResampleCVT(cvt, chans, format); \
  457. }
  458. RESAMPLER_FUNCS(1)
  459. RESAMPLER_FUNCS(2)
  460. RESAMPLER_FUNCS(4)
  461. RESAMPLER_FUNCS(6)
  462. RESAMPLER_FUNCS(8)
  463. #undef RESAMPLER_FUNCS
  464. #ifdef HAVE_LIBSAMPLERATE_H
  465. #define RESAMPLER_FUNCS(chans) \
  466. static void SDLCALL \
  467. SDL_ResampleCVT_SRC_c##chans(SDL_AudioCVT *cvt, SDL_AudioFormat format) \
  468. { \
  469. SDL_ResampleCVT_SRC(cvt, chans, format); \
  470. }
  471. RESAMPLER_FUNCS(1)
  472. RESAMPLER_FUNCS(2)
  473. RESAMPLER_FUNCS(4)
  474. RESAMPLER_FUNCS(6)
  475. RESAMPLER_FUNCS(8)
  476. #undef RESAMPLER_FUNCS
  477. #endif /* HAVE_LIBSAMPLERATE_H */
  478. static SDL_AudioFilter ChooseCVTResampler(const int dst_channels)
  479. {
  480. #ifdef HAVE_LIBSAMPLERATE_H
  481. if (SRC_available) {
  482. switch (dst_channels) {
  483. case 1:
  484. return SDL_ResampleCVT_SRC_c1;
  485. case 2:
  486. return SDL_ResampleCVT_SRC_c2;
  487. case 4:
  488. return SDL_ResampleCVT_SRC_c4;
  489. case 6:
  490. return SDL_ResampleCVT_SRC_c6;
  491. case 8:
  492. return SDL_ResampleCVT_SRC_c8;
  493. default:
  494. break;
  495. }
  496. }
  497. #endif /* HAVE_LIBSAMPLERATE_H */
  498. switch (dst_channels) {
  499. case 1:
  500. return SDL_ResampleCVT_c1;
  501. case 2:
  502. return SDL_ResampleCVT_c2;
  503. case 4:
  504. return SDL_ResampleCVT_c4;
  505. case 6:
  506. return SDL_ResampleCVT_c6;
  507. case 8:
  508. return SDL_ResampleCVT_c8;
  509. default:
  510. break;
  511. }
  512. return NULL;
  513. }
  514. static int SDL_BuildAudioResampleCVT(SDL_AudioCVT *cvt, const int dst_channels,
  515. const int src_rate, const int dst_rate)
  516. {
  517. SDL_AudioFilter filter;
  518. if (src_rate == dst_rate) {
  519. return 0; /* no conversion necessary. */
  520. }
  521. filter = ChooseCVTResampler(dst_channels);
  522. if (filter == NULL) {
  523. return SDL_SetError("No conversion available for these rates");
  524. }
  525. /* Update (cvt) with filter details... */
  526. if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
  527. return -1;
  528. }
  529. /* !!! FIXME in 2.1: there are ten slots in the filter list, and the theoretical maximum we use is six (seven with NULL terminator).
  530. !!! FIXME in 2.1: We need to store data for this resampler, because the cvt structure doesn't store the original sample rates,
  531. !!! FIXME in 2.1: so we steal the ninth and tenth slot. :( */
  532. if (cvt->filter_index >= (SDL_AUDIOCVT_MAX_FILTERS - 2)) {
  533. return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS - 2);
  534. }
  535. cvt->filters[SDL_AUDIOCVT_MAX_FILTERS - 1] = (SDL_AudioFilter)(uintptr_t)src_rate;
  536. cvt->filters[SDL_AUDIOCVT_MAX_FILTERS] = (SDL_AudioFilter)(uintptr_t)dst_rate;
  537. if (src_rate < dst_rate) {
  538. const double mult = ((double)dst_rate) / ((double)src_rate);
  539. cvt->len_mult *= (int)SDL_ceil(mult);
  540. cvt->len_ratio *= mult;
  541. } else {
  542. cvt->len_ratio /= ((double)src_rate) / ((double)dst_rate);
  543. }
  544. /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
  545. /* the buffer is big enough to hold the destination now, but
  546. we need it large enough to hold a separate scratch buffer. */
  547. cvt->len_mult *= 2;
  548. return 1; /* added a converter. */
  549. }
  550. static SDL_bool SDL_IsSupportedAudioFormat(const SDL_AudioFormat fmt)
  551. {
  552. switch (fmt) {
  553. case AUDIO_U8:
  554. case AUDIO_S8:
  555. case AUDIO_U16LSB:
  556. case AUDIO_S16LSB:
  557. case AUDIO_U16MSB:
  558. case AUDIO_S16MSB:
  559. case AUDIO_S32LSB:
  560. case AUDIO_S32MSB:
  561. case AUDIO_F32LSB:
  562. case AUDIO_F32MSB:
  563. return SDL_TRUE; /* supported. */
  564. default:
  565. break;
  566. }
  567. return SDL_FALSE; /* unsupported. */
  568. }
  569. static SDL_bool SDL_IsSupportedChannelCount(const int channels)
  570. {
  571. return ((channels >= 1) && (channels <= 8)) ? SDL_TRUE : SDL_FALSE;
  572. }
  573. /* Creates a set of audio filters to convert from one format to another.
  574. Returns 0 if no conversion is needed, 1 if the audio filter is set up,
  575. or -1 if an error like invalid parameter, unsupported format, etc. occurred.
  576. */
  577. int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
  578. SDL_AudioFormat src_format, Uint8 src_channels, int src_rate,
  579. SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)
  580. {
  581. SDL_AudioFilter channel_converter = NULL;
  582. /* Sanity check target pointer */
  583. if (cvt == NULL) {
  584. return SDL_InvalidParamError("cvt");
  585. }
  586. /* Make sure we zero out the audio conversion before error checking */
  587. SDL_zerop(cvt);
  588. if (!SDL_IsSupportedAudioFormat(src_format)) {
  589. return SDL_SetError("Invalid source format");
  590. }
  591. if (!SDL_IsSupportedAudioFormat(dst_format)) {
  592. return SDL_SetError("Invalid destination format");
  593. }
  594. if (!SDL_IsSupportedChannelCount(src_channels)) {
  595. return SDL_SetError("Invalid source channels");
  596. }
  597. if (!SDL_IsSupportedChannelCount(dst_channels)) {
  598. return SDL_SetError("Invalid destination channels");
  599. }
  600. if (src_rate <= 0) {
  601. return SDL_SetError("Source rate is equal to or less than zero");
  602. }
  603. if (dst_rate <= 0) {
  604. return SDL_SetError("Destination rate is equal to or less than zero");
  605. }
  606. if (src_rate >= SDL_MAX_SINT32 / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
  607. return SDL_SetError("Source rate is too high");
  608. }
  609. if (dst_rate >= SDL_MAX_SINT32 / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
  610. return SDL_SetError("Destination rate is too high");
  611. }
  612. #if DEBUG_CONVERT
  613. SDL_Log("SDL_AUDIO_CONVERT: Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
  614. src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);
  615. #endif
  616. /* Start off with no conversion necessary */
  617. cvt->src_format = src_format;
  618. cvt->dst_format = dst_format;
  619. cvt->needed = 0;
  620. cvt->filter_index = 0;
  621. SDL_zeroa(cvt->filters);
  622. cvt->len_mult = 1;
  623. cvt->len_ratio = 1.0;
  624. cvt->rate_incr = ((double)dst_rate) / ((double)src_rate);
  625. /* Make sure we've chosen audio conversion functions (SIMD, scalar, etc.) */
  626. SDL_ChooseAudioConverters();
  627. /* Type conversion goes like this now:
  628. - byteswap to CPU native format first if necessary.
  629. - convert to native Float32 if necessary.
  630. - resample and change channel count if necessary.
  631. - convert to final data format.
  632. - byteswap back to foreign format if necessary.
  633. The expectation is we can process data faster in float32
  634. (possibly with SIMD), and making several passes over the same
  635. buffer is likely to be CPU cache-friendly, avoiding the
  636. biggest performance hit in modern times. Previously we had
  637. (script-generated) custom converters for every data type and
  638. it was a bloat on SDL compile times and final library size. */
  639. /* see if we can skip float conversion entirely. */
  640. if (src_rate == dst_rate && src_channels == dst_channels) {
  641. if (src_format == dst_format) {
  642. return 0;
  643. }
  644. /* just a byteswap needed? */
  645. if ((src_format & ~SDL_AUDIO_MASK_ENDIAN) == (dst_format & ~SDL_AUDIO_MASK_ENDIAN)) {
  646. if (SDL_AUDIO_BITSIZE(dst_format) == 8) {
  647. return 0;
  648. }
  649. if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
  650. return -1;
  651. }
  652. cvt->needed = 1;
  653. return 1;
  654. }
  655. }
  656. /* Convert data types, if necessary. Updates (cvt). */
  657. if (SDL_BuildAudioTypeCVTToFloat(cvt, src_format) < 0) {
  658. return -1; /* shouldn't happen, but just in case... */
  659. }
  660. /* Channel conversion */
  661. /* SDL_IsSupportedChannelCount should have caught these asserts, or we added a new format and forgot to update the table. */
  662. SDL_assert(src_channels <= SDL_arraysize(channel_converters));
  663. SDL_assert(dst_channels <= SDL_arraysize(channel_converters[0]));
  664. channel_converter = channel_converters[src_channels - 1][dst_channels - 1];
  665. if ((channel_converter == NULL) != (src_channels == dst_channels)) {
  666. /* All combinations of supported channel counts should have been handled by now, but let's be defensive */
  667. return SDL_SetError("Invalid channel combination");
  668. } else if (channel_converter != NULL) {
  669. /* swap in some SIMD versions for a few of these. */
  670. if (channel_converter == SDL_ConvertStereoToMono) {
  671. SDL_AudioFilter filter = NULL;
  672. #if HAVE_SSE3_INTRINSICS
  673. if (!filter && SDL_HasSSE3()) {
  674. filter = SDL_ConvertStereoToMono_SSE3;
  675. }
  676. #endif
  677. if (filter) {
  678. channel_converter = filter;
  679. }
  680. } else if (channel_converter == SDL_ConvertMonoToStereo) {
  681. SDL_AudioFilter filter = NULL;
  682. #if HAVE_SSE_INTRINSICS
  683. if (!filter && SDL_HasSSE()) {
  684. filter = SDL_ConvertMonoToStereo_SSE;
  685. }
  686. #endif
  687. if (filter) {
  688. channel_converter = filter;
  689. }
  690. }
  691. if (SDL_AddAudioCVTFilter(cvt, channel_converter) < 0) {
  692. return -1;
  693. }
  694. if (src_channels < dst_channels) {
  695. cvt->len_mult = ((cvt->len_mult * dst_channels) + (src_channels - 1)) / src_channels;
  696. }
  697. cvt->len_ratio = (cvt->len_ratio * dst_channels) / src_channels;
  698. src_channels = dst_channels;
  699. }
  700. /* Do rate conversion, if necessary. Updates (cvt). */
  701. if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) < 0) {
  702. return -1; /* shouldn't happen, but just in case... */
  703. }
  704. /* Move to final data type. */
  705. if (SDL_BuildAudioTypeCVTFromFloat(cvt, dst_format) < 0) {
  706. return -1; /* shouldn't happen, but just in case... */
  707. }
  708. cvt->needed = (cvt->filter_index != 0);
  709. return cvt->needed;
  710. }
  711. typedef int (*SDL_ResampleAudioStreamFunc)(SDL_AudioStream *stream, const void *inbuf, const int inbuflen, void *outbuf, const int outbuflen);
  712. typedef void (*SDL_ResetAudioStreamResamplerFunc)(SDL_AudioStream *stream);
  713. typedef void (*SDL_CleanupAudioStreamResamplerFunc)(SDL_AudioStream *stream);
  714. struct SDL_AudioStream
  715. {
  716. SDL_AudioCVT cvt_before_resampling;
  717. SDL_AudioCVT cvt_after_resampling;
  718. SDL_DataQueue *queue;
  719. SDL_bool first_run;
  720. Uint8 *staging_buffer;
  721. int staging_buffer_size;
  722. int staging_buffer_filled;
  723. Uint8 *work_buffer_base; /* maybe unaligned pointer from SDL_realloc(). */
  724. int work_buffer_len;
  725. int src_sample_frame_size;
  726. SDL_AudioFormat src_format;
  727. Uint8 src_channels;
  728. int src_rate;
  729. int dst_sample_frame_size;
  730. SDL_AudioFormat dst_format;
  731. Uint8 dst_channels;
  732. int dst_rate;
  733. double rate_incr;
  734. Uint8 pre_resample_channels;
  735. int packetlen;
  736. int resampler_padding_samples;
  737. float *resampler_padding;
  738. void *resampler_state;
  739. SDL_ResampleAudioStreamFunc resampler_func;
  740. SDL_ResetAudioStreamResamplerFunc reset_resampler_func;
  741. SDL_CleanupAudioStreamResamplerFunc cleanup_resampler_func;
  742. };
  743. static Uint8 *EnsureStreamBufferSize(SDL_AudioStream *stream, int newlen)
  744. {
  745. Uint8 *ptr;
  746. size_t offset;
  747. if (stream->work_buffer_len >= newlen) {
  748. ptr = stream->work_buffer_base;
  749. } else {
  750. ptr = (Uint8 *)SDL_realloc(stream->work_buffer_base, (size_t)newlen + 32);
  751. if (ptr == NULL) {
  752. SDL_OutOfMemory();
  753. return NULL;
  754. }
  755. /* Make sure we're aligned to 16 bytes for SIMD code. */
  756. stream->work_buffer_base = ptr;
  757. stream->work_buffer_len = newlen;
  758. }
  759. offset = ((size_t)ptr) & 15;
  760. return offset ? ptr + (16 - offset) : ptr;
  761. }
  762. #ifdef HAVE_LIBSAMPLERATE_H
  763. static int SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const void *_inbuf, const int inbuflen, void *_outbuf, const int outbuflen)
  764. {
  765. const float *inbuf = (const float *)_inbuf;
  766. float *outbuf = (float *)_outbuf;
  767. const int framelen = sizeof(float) * stream->pre_resample_channels;
  768. SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
  769. SRC_DATA data;
  770. int result;
  771. SDL_assert(inbuf != ((const float *)outbuf)); /* SDL_PutAudioStreamData() shouldn't allow in-place resamples. */
  772. data.data_in = (float *)inbuf; /* Older versions of libsamplerate had a non-const pointer, but didn't write to it */
  773. data.input_frames = inbuflen / framelen;
  774. data.input_frames_used = 0;
  775. data.data_out = outbuf;
  776. data.output_frames = outbuflen / framelen;
  777. data.end_of_input = 0;
  778. data.src_ratio = stream->rate_incr;
  779. result = SRC_src_process(state, &data);
  780. if (result != 0) {
  781. SDL_SetError("src_process() failed: %s", SRC_src_strerror(result));
  782. return 0;
  783. }
  784. /* If this fails, we need to store them off somewhere */
  785. SDL_assert(data.input_frames_used == data.input_frames);
  786. return data.output_frames_gen * (sizeof(float) * stream->pre_resample_channels);
  787. }
  788. static void SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *stream)
  789. {
  790. SRC_src_reset((SRC_STATE *)stream->resampler_state);
  791. }
  792. static void SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream)
  793. {
  794. SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
  795. if (state) {
  796. SRC_src_delete(state);
  797. }
  798. stream->resampler_state = NULL;
  799. stream->resampler_func = NULL;
  800. stream->reset_resampler_func = NULL;
  801. stream->cleanup_resampler_func = NULL;
  802. }
  803. static SDL_bool SetupLibSampleRateResampling(SDL_AudioStream *stream)
  804. {
  805. int result = 0;
  806. SRC_STATE *state = NULL;
  807. if (SRC_available) {
  808. state = SRC_src_new(SRC_converter, stream->pre_resample_channels, &result);
  809. if (state == NULL) {
  810. SDL_SetError("src_new() failed: %s", SRC_src_strerror(result));
  811. }
  812. }
  813. if (state == NULL) {
  814. SDL_CleanupAudioStreamResampler_SRC(stream);
  815. return SDL_FALSE;
  816. }
  817. stream->resampler_state = state;
  818. stream->resampler_func = SDL_ResampleAudioStream_SRC;
  819. stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC;
  820. stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC;
  821. return SDL_TRUE;
  822. }
  823. #endif /* HAVE_LIBSAMPLERATE_H */
  824. static int SDL_ResampleAudioStream(SDL_AudioStream *stream, const void *_inbuf, const int inbuflen, void *_outbuf, const int outbuflen)
  825. {
  826. const Uint8 *inbufend = ((const Uint8 *)_inbuf) + inbuflen;
  827. const float *inbuf = (const float *)_inbuf;
  828. float *outbuf = (float *)_outbuf;
  829. const int chans = (int)stream->pre_resample_channels;
  830. const int inrate = stream->src_rate;
  831. const int outrate = stream->dst_rate;
  832. const int paddingsamples = stream->resampler_padding_samples;
  833. const int paddingbytes = paddingsamples * sizeof(float);
  834. float *lpadding = (float *)stream->resampler_state;
  835. const float *rpadding = (const float *)inbufend; /* we set this up so there are valid padding samples at the end of the input buffer. */
  836. const int cpy = SDL_min(inbuflen, paddingbytes);
  837. int retval;
  838. SDL_assert(inbuf != ((const float *)outbuf)); /* SDL_PutAudioStreamData() shouldn't allow in-place resamples. */
  839. retval = SDL_ResampleAudio(chans, inrate, outrate, lpadding, rpadding, inbuf, inbuflen, outbuf, outbuflen);
  840. /* update our left padding with end of current input, for next run. */
  841. SDL_memcpy((lpadding + paddingsamples) - (cpy / sizeof(float)), inbufend - cpy, cpy);
  842. return retval;
  843. }
  844. static void SDL_ResetAudioStreamResampler(SDL_AudioStream *stream)
  845. {
  846. /* set all the padding to silence. */
  847. const int len = stream->resampler_padding_samples;
  848. SDL_memset(stream->resampler_state, '\0', len * sizeof(float));
  849. }
  850. static void SDL_CleanupAudioStreamResampler(SDL_AudioStream *stream)
  851. {
  852. SDL_free(stream->resampler_state);
  853. }
  854. SDL_AudioStream *
  855. SDL_CreateAudioStream(SDL_AudioFormat src_format,
  856. Uint8 src_channels,
  857. int src_rate,
  858. SDL_AudioFormat dst_format,
  859. Uint8 dst_channels,
  860. int dst_rate)
  861. {
  862. int packetlen = 4096; /* !!! FIXME: good enough for now. */
  863. Uint8 pre_resample_channels;
  864. SDL_AudioStream *retval;
  865. retval = (SDL_AudioStream *)SDL_calloc(1, sizeof(SDL_AudioStream));
  866. if (retval == NULL) {
  867. SDL_OutOfMemory();
  868. return NULL;
  869. }
  870. /* If increasing channels, do it after resampling, since we'd just
  871. do more work to resample duplicate channels. If we're decreasing, do
  872. it first so we resample the interpolated data instead of interpolating
  873. the resampled data (!!! FIXME: decide if that works in practice, though!). */
  874. pre_resample_channels = SDL_min(src_channels, dst_channels);
  875. retval->first_run = SDL_TRUE;
  876. retval->src_sample_frame_size = (SDL_AUDIO_BITSIZE(src_format) / 8) * src_channels;
  877. retval->src_format = src_format;
  878. retval->src_channels = src_channels;
  879. retval->src_rate = src_rate;
  880. retval->dst_sample_frame_size = (SDL_AUDIO_BITSIZE(dst_format) / 8) * dst_channels;
  881. retval->dst_format = dst_format;
  882. retval->dst_channels = dst_channels;
  883. retval->dst_rate = dst_rate;
  884. retval->pre_resample_channels = pre_resample_channels;
  885. retval->packetlen = packetlen;
  886. retval->rate_incr = ((double)dst_rate) / ((double)src_rate);
  887. retval->resampler_padding_samples = GetResamplerPadding(retval->src_rate, retval->dst_rate) * pre_resample_channels;
  888. retval->resampler_padding = (float *)SDL_calloc(retval->resampler_padding_samples ? retval->resampler_padding_samples : 1, sizeof(float));
  889. if (retval->resampler_padding == NULL) {
  890. SDL_DestroyAudioStream(retval);
  891. SDL_OutOfMemory();
  892. return NULL;
  893. }
  894. retval->staging_buffer_size = ((retval->resampler_padding_samples / retval->pre_resample_channels) * retval->src_sample_frame_size);
  895. if (retval->staging_buffer_size > 0) {
  896. retval->staging_buffer = (Uint8 *)SDL_malloc(retval->staging_buffer_size);
  897. if (retval->staging_buffer == NULL) {
  898. SDL_DestroyAudioStream(retval);
  899. SDL_OutOfMemory();
  900. return NULL;
  901. }
  902. }
  903. /* Not resampling? It's an easy conversion (and maybe not even that!) */
  904. if (src_rate == dst_rate) {
  905. retval->cvt_before_resampling.needed = SDL_FALSE;
  906. if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, src_format, src_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) {
  907. SDL_DestroyAudioStream(retval);
  908. return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */
  909. }
  910. } else {
  911. /* Don't resample at first. Just get us to Float32 format. */
  912. /* !!! FIXME: convert to int32 on devices without hardware float. */
  913. if (SDL_BuildAudioCVT(&retval->cvt_before_resampling, src_format, src_channels, src_rate, AUDIO_F32SYS, pre_resample_channels, src_rate) < 0) {
  914. SDL_DestroyAudioStream(retval);
  915. return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */
  916. }
  917. #ifdef HAVE_LIBSAMPLERATE_H
  918. SetupLibSampleRateResampling(retval);
  919. #endif
  920. if (!retval->resampler_func) {
  921. retval->resampler_state = SDL_calloc(retval->resampler_padding_samples, sizeof(float));
  922. if (!retval->resampler_state) {
  923. SDL_DestroyAudioStream(retval);
  924. SDL_OutOfMemory();
  925. return NULL;
  926. }
  927. retval->resampler_func = SDL_ResampleAudioStream;
  928. retval->reset_resampler_func = SDL_ResetAudioStreamResampler;
  929. retval->cleanup_resampler_func = SDL_CleanupAudioStreamResampler;
  930. }
  931. /* Convert us to the final format after resampling. */
  932. if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, AUDIO_F32SYS, pre_resample_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) {
  933. SDL_DestroyAudioStream(retval);
  934. return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */
  935. }
  936. }
  937. retval->queue = SDL_CreateDataQueue(packetlen, (size_t)packetlen * 2);
  938. if (!retval->queue) {
  939. SDL_DestroyAudioStream(retval);
  940. return NULL; /* SDL_CreateDataQueue should have called SDL_SetError. */
  941. }
  942. return retval;
  943. }
  944. static int SDL_PutAudioStreamInternal(SDL_AudioStream *stream, const void *buf, int len, int *maxputbytes)
  945. {
  946. int buflen = len;
  947. int workbuflen;
  948. Uint8 *workbuf;
  949. Uint8 *resamplebuf = NULL;
  950. int resamplebuflen = 0;
  951. int neededpaddingbytes;
  952. int paddingbytes;
  953. /* !!! FIXME: several converters can take advantage of SIMD, but only
  954. !!! FIXME: if the data is aligned to 16 bytes. EnsureStreamBufferSize()
  955. !!! FIXME: guarantees the buffer will align, but the
  956. !!! FIXME: converters will iterate over the data backwards if
  957. !!! FIXME: the output grows, and this means we won't align if buflen
  958. !!! FIXME: isn't a multiple of 16. In these cases, we should chop off
  959. !!! FIXME: a few samples at the end and convert them separately. */
  960. /* no padding prepended on first run. */
  961. neededpaddingbytes = stream->resampler_padding_samples * sizeof(float);
  962. paddingbytes = stream->first_run ? 0 : neededpaddingbytes;
  963. stream->first_run = SDL_FALSE;
  964. /* Make sure the work buffer can hold all the data we need at once... */
  965. workbuflen = buflen;
  966. if (stream->cvt_before_resampling.needed) {
  967. workbuflen *= stream->cvt_before_resampling.len_mult;
  968. }
  969. if (stream->dst_rate != stream->src_rate) {
  970. /* resamples can't happen in place, so make space for second buf. */
  971. const int framesize = stream->pre_resample_channels * sizeof(float);
  972. const int frames = workbuflen / framesize;
  973. resamplebuflen = ((int)SDL_ceil(frames * stream->rate_incr)) * framesize;
  974. #if DEBUG_AUDIOSTREAM
  975. SDL_Log("AUDIOSTREAM: will resample %d bytes to %d (ratio=%.6f)\n", workbuflen, resamplebuflen, stream->rate_incr);
  976. #endif
  977. workbuflen += resamplebuflen;
  978. }
  979. if (stream->cvt_after_resampling.needed) {
  980. /* !!! FIXME: buffer might be big enough already? */
  981. workbuflen *= stream->cvt_after_resampling.len_mult;
  982. }
  983. workbuflen += neededpaddingbytes;
  984. #if DEBUG_AUDIOSTREAM
  985. SDL_Log("AUDIOSTREAM: Putting %d bytes of preconverted audio, need %d byte work buffer\n", buflen, workbuflen);
  986. #endif
  987. workbuf = EnsureStreamBufferSize(stream, workbuflen);
  988. if (workbuf == NULL) {
  989. return -1; /* probably out of memory. */
  990. }
  991. resamplebuf = workbuf; /* default if not resampling. */
  992. SDL_memcpy(workbuf + paddingbytes, buf, buflen);
  993. if (stream->cvt_before_resampling.needed) {
  994. stream->cvt_before_resampling.buf = workbuf + paddingbytes;
  995. stream->cvt_before_resampling.len = buflen;
  996. if (SDL_ConvertAudio(&stream->cvt_before_resampling) == -1) {
  997. return -1; /* uhoh! */
  998. }
  999. buflen = stream->cvt_before_resampling.len_cvt;
  1000. #if DEBUG_AUDIOSTREAM
  1001. SDL_Log("AUDIOSTREAM: After initial conversion we have %d bytes\n", buflen);
  1002. #endif
  1003. }
  1004. if (stream->dst_rate != stream->src_rate) {
  1005. /* save off some samples at the end; they are used for padding now so
  1006. the resampler is coherent and then used at the start of the next
  1007. put operation. Prepend last put operation's padding, too. */
  1008. /* prepend prior put's padding. :P */
  1009. if (paddingbytes) {
  1010. SDL_memcpy(workbuf, stream->resampler_padding, paddingbytes);
  1011. buflen += paddingbytes;
  1012. }
  1013. /* save off the data at the end for the next run. */
  1014. SDL_memcpy(stream->resampler_padding, workbuf + (buflen - neededpaddingbytes), neededpaddingbytes);
  1015. resamplebuf = workbuf + buflen; /* skip to second piece of workbuf. */
  1016. SDL_assert(buflen >= neededpaddingbytes);
  1017. if (buflen > neededpaddingbytes) {
  1018. buflen = stream->resampler_func(stream, workbuf, buflen - neededpaddingbytes, resamplebuf, resamplebuflen);
  1019. } else {
  1020. buflen = 0;
  1021. }
  1022. #if DEBUG_AUDIOSTREAM
  1023. SDL_Log("AUDIOSTREAM: After resampling we have %d bytes\n", buflen);
  1024. #endif
  1025. }
  1026. if (stream->cvt_after_resampling.needed && (buflen > 0)) {
  1027. stream->cvt_after_resampling.buf = resamplebuf;
  1028. stream->cvt_after_resampling.len = buflen;
  1029. if (SDL_ConvertAudio(&stream->cvt_after_resampling) == -1) {
  1030. return -1; /* uhoh! */
  1031. }
  1032. buflen = stream->cvt_after_resampling.len_cvt;
  1033. #if DEBUG_AUDIOSTREAM
  1034. SDL_Log("AUDIOSTREAM: After final conversion we have %d bytes\n", buflen);
  1035. #endif
  1036. }
  1037. #if DEBUG_AUDIOSTREAM
  1038. SDL_Log("AUDIOSTREAM: Final output is %d bytes\n", buflen);
  1039. #endif
  1040. if (maxputbytes) {
  1041. const int maxbytes = *maxputbytes;
  1042. if (buflen > maxbytes) {
  1043. buflen = maxbytes;
  1044. }
  1045. *maxputbytes -= buflen;
  1046. }
  1047. /* resamplebuf holds the final output, even if we didn't resample. */
  1048. return buflen ? SDL_WriteToDataQueue(stream->queue, resamplebuf, buflen) : 0;
  1049. }
  1050. int SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
  1051. {
  1052. /* !!! FIXME: several converters can take advantage of SIMD, but only
  1053. !!! FIXME: if the data is aligned to 16 bytes. EnsureStreamBufferSize()
  1054. !!! FIXME: guarantees the buffer will align, but the
  1055. !!! FIXME: converters will iterate over the data backwards if
  1056. !!! FIXME: the output grows, and this means we won't align if buflen
  1057. !!! FIXME: isn't a multiple of 16. In these cases, we should chop off
  1058. !!! FIXME: a few samples at the end and convert them separately. */
  1059. #if DEBUG_AUDIOSTREAM
  1060. SDL_Log("AUDIOSTREAM: wants to put %d preconverted bytes\n", buflen);
  1061. #endif
  1062. if (stream == NULL) {
  1063. return SDL_InvalidParamError("stream");
  1064. }
  1065. if (buf == NULL) {
  1066. return SDL_InvalidParamError("buf");
  1067. }
  1068. if (len == 0) {
  1069. return 0; /* nothing to do. */
  1070. }
  1071. if ((len % stream->src_sample_frame_size) != 0) {
  1072. return SDL_SetError("Can't add partial sample frames");
  1073. }
  1074. if (!stream->cvt_before_resampling.needed &&
  1075. (stream->dst_rate == stream->src_rate) &&
  1076. !stream->cvt_after_resampling.needed) {
  1077. #if DEBUG_AUDIOSTREAM
  1078. SDL_Log("AUDIOSTREAM: no conversion needed at all, queueing %d bytes.\n", len);
  1079. #endif
  1080. return SDL_WriteToDataQueue(stream->queue, buf, len);
  1081. }
  1082. while (len > 0) {
  1083. int amount;
  1084. /* If we don't have a staging buffer or we're given enough data that
  1085. we don't need to store it for later, skip the staging process.
  1086. */
  1087. if (!stream->staging_buffer_filled && len >= stream->staging_buffer_size) {
  1088. return SDL_PutAudioStreamInternal(stream, buf, len, NULL);
  1089. }
  1090. /* If there's not enough data to fill the staging buffer, just save it */
  1091. if ((stream->staging_buffer_filled + len) < stream->staging_buffer_size) {
  1092. SDL_memcpy(stream->staging_buffer + stream->staging_buffer_filled, buf, len);
  1093. stream->staging_buffer_filled += len;
  1094. return 0;
  1095. }
  1096. /* Fill the staging buffer, process it, and continue */
  1097. amount = (stream->staging_buffer_size - stream->staging_buffer_filled);
  1098. SDL_assert(amount > 0);
  1099. SDL_memcpy(stream->staging_buffer + stream->staging_buffer_filled, buf, amount);
  1100. stream->staging_buffer_filled = 0;
  1101. if (SDL_PutAudioStreamInternal(stream, stream->staging_buffer, stream->staging_buffer_size, NULL) < 0) {
  1102. return -1;
  1103. }
  1104. buf = (void *)((Uint8 *)buf + amount);
  1105. len -= amount;
  1106. }
  1107. return 0;
  1108. }
  1109. int SDL_FlushAudioStream(SDL_AudioStream *stream)
  1110. {
  1111. if (stream == NULL) {
  1112. return SDL_InvalidParamError("stream");
  1113. }
  1114. #if DEBUG_AUDIOSTREAM
  1115. SDL_Log("AUDIOSTREAM: flushing! staging_buffer_filled=%d bytes\n", stream->staging_buffer_filled);
  1116. #endif
  1117. /* shouldn't use a staging buffer if we're not resampling. */
  1118. SDL_assert((stream->dst_rate != stream->src_rate) || (stream->staging_buffer_filled == 0));
  1119. if (stream->staging_buffer_filled > 0) {
  1120. /* push the staging buffer + silence. We need to flush out not just
  1121. the staging buffer, but the piece that the stream was saving off
  1122. for right-side resampler padding. */
  1123. const SDL_bool first_run = stream->first_run;
  1124. const int filled = stream->staging_buffer_filled;
  1125. int actual_input_frames = filled / stream->src_sample_frame_size;
  1126. if (!first_run) {
  1127. actual_input_frames += stream->resampler_padding_samples / stream->pre_resample_channels;
  1128. }
  1129. if (actual_input_frames > 0) { /* don't bother if nothing to flush. */
  1130. /* This is how many bytes we're expecting without silence appended. */
  1131. int flush_remaining = ((int)SDL_ceil(actual_input_frames * stream->rate_incr)) * stream->dst_sample_frame_size;
  1132. #if DEBUG_AUDIOSTREAM
  1133. SDL_Log("AUDIOSTREAM: flushing with padding to get max %d bytes!\n", flush_remaining);
  1134. #endif
  1135. SDL_memset(stream->staging_buffer + filled, '\0', stream->staging_buffer_size - filled);
  1136. if (SDL_PutAudioStreamInternal(stream, stream->staging_buffer, stream->staging_buffer_size, &flush_remaining) < 0) {
  1137. return -1;
  1138. }
  1139. /* we have flushed out (or initially filled) the pending right-side
  1140. resampler padding, but we need to push more silence to guarantee
  1141. the staging buffer is fully flushed out, too. */
  1142. SDL_memset(stream->staging_buffer, '\0', filled);
  1143. if (SDL_PutAudioStreamInternal(stream, stream->staging_buffer, stream->staging_buffer_size, &flush_remaining) < 0) {
  1144. return -1;
  1145. }
  1146. }
  1147. }
  1148. stream->staging_buffer_filled = 0;
  1149. stream->first_run = SDL_TRUE;
  1150. return 0;
  1151. }
  1152. /* get converted/resampled data from the stream */
  1153. int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len)
  1154. {
  1155. #if DEBUG_AUDIOSTREAM
  1156. SDL_Log("AUDIOSTREAM: want to get %d converted bytes\n", len);
  1157. #endif
  1158. if (stream == NULL) {
  1159. return SDL_InvalidParamError("stream");
  1160. }
  1161. if (buf == NULL) {
  1162. return SDL_InvalidParamError("buf");
  1163. }
  1164. if (len <= 0) {
  1165. return 0; /* nothing to do. */
  1166. }
  1167. if ((len % stream->dst_sample_frame_size) != 0) {
  1168. return SDL_SetError("Can't request partial sample frames");
  1169. }
  1170. return (int)SDL_ReadFromDataQueue(stream->queue, buf, len);
  1171. }
  1172. /* number of converted/resampled bytes available */
  1173. int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
  1174. {
  1175. return stream ? (int)SDL_GetDataQueueSize(stream->queue) : 0;
  1176. }
  1177. void SDL_ClearAudioStream(SDL_AudioStream *stream)
  1178. {
  1179. if (stream == NULL) {
  1180. SDL_InvalidParamError("stream");
  1181. } else {
  1182. SDL_ClearDataQueue(stream->queue, (size_t)stream->packetlen * 2);
  1183. if (stream->reset_resampler_func) {
  1184. stream->reset_resampler_func(stream);
  1185. }
  1186. stream->first_run = SDL_TRUE;
  1187. stream->staging_buffer_filled = 0;
  1188. }
  1189. }
  1190. /* dispose of a stream */
  1191. void SDL_DestroyAudioStream(SDL_AudioStream *stream)
  1192. {
  1193. if (stream) {
  1194. if (stream->cleanup_resampler_func) {
  1195. stream->cleanup_resampler_func(stream);
  1196. }
  1197. SDL_DestroyDataQueue(stream->queue);
  1198. SDL_free(stream->staging_buffer);
  1199. SDL_free(stream->work_buffer_base);
  1200. SDL_free(stream->resampler_padding);
  1201. SDL_free(stream);
  1202. }
  1203. }