From cda27514f056e6ef2d1667be400f3346744e6aea Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 23 Feb 2020 14:07:48 +1000 Subject: [PATCH] Add ma_hpf with support for configuring the number of poles. --- miniaudio.h | 237 +++++++++++++++++++ tests/test_filtering/ma_test_filtering_hpf.c | 77 +++++- 2 files changed, 312 insertions(+), 2 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index d914543c..0a8ff606 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -1762,6 +1762,33 @@ ma_result ma_hpf2_process_pcm_frames(ma_hpf2* pHPF, void* pFramesOut, const void ma_uint32 ma_hpf2_get_latency(ma_hpf2* pHPF); +typedef struct +{ + ma_format format; + ma_uint32 channels; + ma_uint32 sampleRate; + double cutoffFrequency; + ma_uint32 poles; /* If set to 0, will be treated as a passthrough (no filtering will be applied). */ +} ma_hpf_config; + +ma_hpf_config ma_hpf_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency, ma_uint32 poles); + +typedef struct +{ + ma_format format; + ma_uint32 channels; + ma_uint32 hpf2Count; + ma_uint32 hpf1Count; + ma_hpf2 hpf2[MA_MAX_FILTER_POLES/2]; + ma_hpf1 hpf1[1]; +} ma_hpf; + +ma_result ma_hpf_init(const ma_hpf_config* pConfig, ma_hpf* pHPF); +ma_result ma_hpf_reinit(const ma_hpf_config* pConfig, ma_hpf* pHPF); +ma_result ma_hpf_process_pcm_frames(ma_hpf* pHPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount); +ma_uint32 ma_hpf_get_latency(ma_hpf* pHPF); + + /************************************************************************************************************************************************************** Band-Pass Filtering @@ -30074,6 +30101,16 @@ ma_result ma_hpf2_reinit(const ma_hpf2_config* pConfig, ma_hpf2* pHPF) return MA_SUCCESS; } +static MA_INLINE void ma_hpf2_process_pcm_frame_s16(ma_hpf2* pHPF, ma_int16* pFrameOut, const ma_int16* pFrameIn) +{ + ma_biquad_process_pcm_frame_s16(&pHPF->bq, pFrameOut, pFrameIn); +} + +static MA_INLINE void ma_hpf2_process_pcm_frame_f32(ma_hpf2* pHPF, float* pFrameOut, const float* pFrameIn) +{ + ma_biquad_process_pcm_frame_f32(&pHPF->bq, pFrameOut, pFrameIn); +} + ma_result ma_hpf2_process_pcm_frames(ma_hpf2* pHPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount) { if (pHPF == NULL) { @@ -30093,6 +30130,206 @@ ma_uint32 ma_hpf2_get_latency(ma_hpf2* pHPF) } +ma_hpf_config ma_hpf_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency, ma_uint32 poles) +{ + ma_hpf_config config; + + MA_ZERO_OBJECT(&config); + config.format = format; + config.channels = channels; + config.sampleRate = sampleRate; + config.cutoffFrequency = cutoffFrequency; + config.poles = ma_min(poles, MA_MAX_FILTER_POLES); + + return config; +} + +static ma_result ma_hpf_reinit__internal(const ma_hpf_config* pConfig, ma_hpf* pHPF, ma_bool32 isNew) +{ + ma_result result; + ma_uint32 hpf2Count; + ma_uint32 hpf1Count; + ma_uint32 ihpf2; + ma_uint32 ihpf1; + + if (pHPF == NULL || pConfig == NULL) { + return MA_INVALID_ARGS; + } + + /* Only supporting f32 and s16. */ + if (pConfig->format != ma_format_f32 && pConfig->format != ma_format_s16) { + return MA_INVALID_ARGS; + } + + /* The format cannot be changed after initialization. */ + if (pHPF->format != ma_format_unknown && pHPF->format != pConfig->format) { + return MA_INVALID_OPERATION; + } + + /* The channel count cannot be changed after initialization. */ + if (pHPF->channels != 0 && pHPF->channels != pConfig->channels) { + return MA_INVALID_OPERATION; + } + + if (pConfig->poles > MA_MAX_FILTER_POLES) { + return MA_INVALID_ARGS; + } + + hpf2Count = pConfig->poles / 2; + hpf1Count = pConfig->poles % 2; + + MA_ASSERT(hpf2Count <= ma_countof(pHPF->hpf2)); + MA_ASSERT(hpf1Count <= ma_countof(pHPF->hpf1)); + + /* The pole count can't change between reinits. */ + if (!isNew) { + if (pHPF->hpf2Count != hpf2Count || pHPF->hpf1Count != hpf1Count) { + return MA_INVALID_OPERATION; + } + } + + for (ihpf2 = 0; ihpf2 < hpf2Count; ihpf2 += 1) { + ma_hpf2_config hpf2Config = ma_hpf2_config_init(pConfig->format, pConfig->channels, pConfig->sampleRate, pConfig->cutoffFrequency); + + if (isNew) { + result = ma_hpf2_init(&hpf2Config, &pHPF->hpf2[ihpf2]); + } else { + result = ma_hpf2_reinit(&hpf2Config, &pHPF->hpf2[ihpf2]); + } + + if (result != MA_SUCCESS) { + return result; + } + } + + for (ihpf1 = 0; ihpf1 < hpf1Count; ihpf1 += 1) { + ma_hpf1_config hpf1Config = ma_hpf1_config_init(pConfig->format, pConfig->channels, pConfig->sampleRate, pConfig->cutoffFrequency); + + if (isNew) { + result = ma_hpf1_init(&hpf1Config, &pHPF->hpf1[ihpf1]); + } else { + result = ma_hpf1_reinit(&hpf1Config, &pHPF->hpf1[ihpf1]); + } + + if (result != MA_SUCCESS) { + return result; + } + } + + pHPF->hpf2Count = hpf2Count; + pHPF->hpf1Count = hpf1Count; + pHPF->format = pConfig->format; + pHPF->channels = pConfig->channels; + + return MA_SUCCESS; +} + +ma_result ma_hpf_init(const ma_hpf_config* pConfig, ma_hpf* pHPF) +{ + if (pHPF == NULL) { + return MA_INVALID_ARGS; + } + + MA_ZERO_OBJECT(pHPF); + + if (pConfig == NULL) { + return MA_INVALID_ARGS; + } + + return ma_hpf_reinit__internal(pConfig, pHPF, /*isNew*/MA_TRUE); +} + +ma_result ma_hpf_reinit(const ma_hpf_config* pConfig, ma_hpf* pHPF) +{ + return ma_hpf_reinit__internal(pConfig, pHPF, /*isNew*/MA_FALSE); +} + +ma_result ma_hpf_process_pcm_frames(ma_hpf* pHPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount) +{ + ma_result result; + ma_uint32 ihpf2; + ma_uint32 ihpf1; + + if (pHPF == NULL) { + return MA_INVALID_ARGS; + } + + /* Faster path for in-place. */ + if (pFramesOut == pFramesIn) { + for (ihpf2 = 0; ihpf2 < pHPF->hpf2Count; ihpf2 += 1) { + result = ma_hpf2_process_pcm_frames(&pHPF->hpf2[ihpf2], pFramesOut, pFramesOut, frameCount); + if (result != MA_SUCCESS) { + return result; + } + } + + for (ihpf1 = 0; ihpf1 < pHPF->hpf1Count; ihpf1 += 1) { + result = ma_hpf1_process_pcm_frames(&pHPF->hpf1[ihpf1], pFramesOut, pFramesOut, frameCount); + if (result != MA_SUCCESS) { + return result; + } + } + } + + /* Slightly slower path for copying. */ + if (pFramesOut != pFramesIn) { + ma_uint32 iFrame; + + /* */ if (pHPF->format == ma_format_f32) { + /* */ float* pFramesOutF32 = ( float*)pFramesOut; + const float* pFramesInF32 = (const float*)pFramesIn; + + for (iFrame = 0; iFrame < frameCount; iFrame += 1) { + MA_COPY_MEMORY(pFramesOutF32, pFramesInF32, ma_get_bytes_per_frame(pHPF->format, pHPF->channels)); + + for (ihpf2 = 0; ihpf2 < pHPF->hpf2Count; ihpf2 += 1) { + ma_hpf2_process_pcm_frame_f32(&pHPF->hpf2[ihpf2], pFramesOutF32, pFramesOutF32); + } + + for (ihpf1 = 0; ihpf1 < pHPF->hpf1Count; ihpf1 += 1) { + ma_hpf1_process_pcm_frame_f32(&pHPF->hpf1[ihpf1], pFramesOutF32, pFramesOutF32); + } + + pFramesOutF32 += pHPF->channels; + pFramesInF32 += pHPF->channels; + } + } else if (pHPF->format == ma_format_s16) { + /* */ ma_int16* pFramesOutS16 = ( ma_int16*)pFramesOut; + const ma_int16* pFramesInS16 = (const ma_int16*)pFramesIn; + + for (iFrame = 0; iFrame < frameCount; iFrame += 1) { + MA_COPY_MEMORY(pFramesOutS16, pFramesInS16, ma_get_bytes_per_frame(pHPF->format, pHPF->channels)); + + for (ihpf2 = 0; ihpf2 < pHPF->hpf2Count; ihpf2 += 1) { + ma_hpf2_process_pcm_frame_s16(&pHPF->hpf2[ihpf2], pFramesOutS16, pFramesOutS16); + } + + for (ihpf1 = 0; ihpf1 < pHPF->hpf1Count; ihpf1 += 1) { + ma_hpf1_process_pcm_frame_s16(&pHPF->hpf1[ihpf1], pFramesOutS16, pFramesOutS16); + } + + pFramesOutS16 += pHPF->channels; + pFramesInS16 += pHPF->channels; + } + } else { + MA_ASSERT(MA_FALSE); + return MA_INVALID_OPERATION; /* Should never hit this. */ + } + } + + return MA_SUCCESS; +} + +ma_uint32 ma_hpf_get_latency(ma_hpf* pHPF) +{ + if (pHPF == NULL) { + return 0; + } + + return pHPF->hpf2Count*2 + pHPF->hpf1Count; +} + + /************************************************************************************************************************************************************** Band-Pass Filtering diff --git a/tests/test_filtering/ma_test_filtering_hpf.c b/tests/test_filtering/ma_test_filtering_hpf.c index d148a8a8..e8bcf9a9 100644 --- a/tests/test_filtering/ma_test_filtering_hpf.c +++ b/tests/test_filtering/ma_test_filtering_hpf.c @@ -60,7 +60,7 @@ ma_result test_hpf1__f32(const char* pInputFilePath) ma_result test_hpf1__s16(const char* pInputFilePath) { - return test_hpf1__by_format(pInputFilePath, "output/hpf1_s16.wav", ma_format_f32); + return test_hpf1__by_format(pInputFilePath, "output/hpf1_s16.wav", ma_format_s16); } @@ -120,9 +120,70 @@ ma_result test_hpf2__f32(const char* pInputFilePath) ma_result test_hpf2__s16(const char* pInputFilePath) { - return test_hpf2__by_format(pInputFilePath, "output/hpf2_s16.wav", ma_format_f32); + return test_hpf2__by_format(pInputFilePath, "output/hpf2_s16.wav", ma_format_s16); } + +ma_result test_hpf3__by_format(const char* pInputFilePath, const char* pOutputFilePath, ma_format format) +{ + ma_result result; + ma_decoder decoder; + drwav wav; + ma_hpf_config hpfConfig; + ma_hpf hpf; + + printf(" %s\n", pOutputFilePath); + + result = hpf_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, &decoder, &wav); + if (result != MA_SUCCESS) { + return result; + } + + hpfConfig = ma_hpf_config_init(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, 2000, 3); + result = ma_hpf_init(&hpfConfig, &hpf); + if (result != MA_SUCCESS) { + ma_decoder_uninit(&decoder); + drwav_uninit(&wav); + return result; + } + + for (;;) { + ma_uint8 tempIn[4096]; + ma_uint8 tempOut[4096]; + ma_uint64 tempCapIn = sizeof(tempIn) / ma_get_bytes_per_frame(decoder.outputFormat, decoder.outputChannels); + ma_uint64 tempCapOut = sizeof(tempOut) / ma_get_bytes_per_frame(decoder.outputFormat, decoder.outputChannels); + ma_uint64 framesToRead; + ma_uint64 framesJustRead; + + framesToRead = ma_min(tempCapIn, tempCapOut); + framesJustRead = ma_decoder_read_pcm_frames(&decoder, tempIn, framesToRead); + + /* Filter */ + ma_hpf_process_pcm_frames(&hpf, tempOut, tempIn, framesJustRead); + + /* Write to the WAV file. */ + drwav_write_pcm_frames(&wav, framesJustRead, tempOut); + + if (framesJustRead < framesToRead) { + break; + } + } + + drwav_uninit(&wav); + return MA_SUCCESS; +} + +ma_result test_hpf3__f32(const char* pInputFilePath) +{ + return test_hpf3__by_format(pInputFilePath, "output/hpf3_f32.wav", ma_format_f32); +} + +ma_result test_hpf3__s16(const char* pInputFilePath) +{ + return test_hpf3__by_format(pInputFilePath, "output/hpf3_s16.wav", ma_format_s16); +} + + int test_entry__hpf(int argc, char** argv) { ma_result result; @@ -158,6 +219,18 @@ int test_entry__hpf(int argc, char** argv) hasError = MA_TRUE; } + + result = test_hpf3__f32(pInputFilePath); + if (result != MA_SUCCESS) { + hasError = MA_TRUE; + } + + result = test_hpf3__s16(pInputFilePath); + if (result != MA_SUCCESS) { + hasError = MA_TRUE; + } + + if (hasError) { return -1; } else {