From 3fd31f29c15fd11924e715f73683d9a2f55bce20 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 9 Dec 2018 10:11:20 +1000 Subject: [PATCH] Add quick and dirty s16 read implementations for the resampler. --- research/mal_resampler.h | 42 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/research/mal_resampler.h b/research/mal_resampler.h index 59c16072..89bc005f 100644 --- a/research/mal_resampler.h +++ b/research/mal_resampler.h @@ -793,11 +793,22 @@ mal_uint64 mal_resampler_read_s16__linear(mal_resampler* pResampler, mal_uint64 mal_assert(frameCount > 0); mal_assert(ppFrames != NULL); - /* TODO: Implement me. */ - (void)pResampler; - (void)frameCount; - (void)ppFrames; - return 0; + /* TODO: Implement an s16 optimized implementation. */ + + /* I'm cheating here and just using the f32 implementation and converting to s16. This will be changed later - for now just focusing on getting it working. */ + float bufferF32[mal_countof(pResampler->cache.s16)]; + float* ppFramesF32[MAL_MAX_CHANNELS]; + for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) { + ppFramesF32[iChannel] = bufferF32 + (pResampler->cacheStrideInFrames*iChannel); + } + + mal_uint64 framesRead = mal_resampler_read_f32__linear(pResampler, frameCount, ppFramesF32); + + for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) { + mal_pcm_f32_to_s16(ppFrames[iChannel], ppFramesF32[iChannel], frameCount, mal_dither_mode_none); /* No dithering - keep it fast for linear. */ + } + + return framesRead; } mal_uint64 mal_resampler_seek__linear(mal_resampler* pResampler, mal_uint64 frameCount, mal_uint32 options) @@ -846,11 +857,22 @@ mal_uint64 mal_resampler_read_s16__sinc(mal_resampler* pResampler, mal_uint64 fr mal_assert(frameCount > 0); mal_assert(ppFrames != NULL); - /* TODO: Implement me. */ - (void)pResampler; - (void)frameCount; - (void)ppFrames; - return 0; + /* TODO: Implement an s16 optimized implementation. */ + + /* I'm cheating here and just using the f32 implementation and converting to s16. This will be changed later - for now just focusing on getting it working. */ + float bufferF32[mal_countof(pResampler->cache.s16)]; + float* ppFramesF32[MAL_MAX_CHANNELS]; + for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) { + ppFramesF32[iChannel] = bufferF32 + (pResampler->cacheStrideInFrames*iChannel); + } + + mal_uint64 framesRead = mal_resampler_read_f32__sinc(pResampler, frameCount, ppFramesF32); + + for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) { + mal_pcm_f32_to_s16(ppFrames[iChannel], ppFramesF32[iChannel], frameCount, mal_dither_mode_triangle); + } + + return framesRead; } mal_uint64 mal_resampler_seek__sinc(mal_resampler* pResampler, mal_uint64 frameCount, mal_uint32 options)