From f028e65e3824bfcd36cac3e3b56b0e46b1f7b6c3 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 23 Feb 2020 10:36:28 +1000 Subject: [PATCH] Update filtering tests. --- tests/test_filtering/ma_test_filtering.c | 21 ++++ tests/test_filtering/ma_test_filtering_lpf.c | 120 +++++++------------ 2 files changed, 63 insertions(+), 78 deletions(-) diff --git a/tests/test_filtering/ma_test_filtering.c b/tests/test_filtering/ma_test_filtering.c index 5c6d35dc..63d6f80a 100644 --- a/tests/test_filtering/ma_test_filtering.c +++ b/tests/test_filtering/ma_test_filtering.c @@ -1,5 +1,26 @@ #include "../test_common/ma_test_common.c" +ma_result filtering_init_decoder_and_encoder(const char* pInputFilePath, const char* pOutputFilePath, ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_decoder* pDecoder, drwav* pEncoder) +{ + ma_result result; + ma_decoder_config decoderConfig; + drwav_data_format wavFormat; + + decoderConfig = ma_decoder_config_init(format, 0, 0); + result = ma_decoder_init_file(pInputFilePath, &decoderConfig, pDecoder); + if (result != MA_SUCCESS) { + return result; + } + + wavFormat = drwav_data_format_from_minaudio_format(pDecoder->outputFormat, pDecoder->outputChannels, pDecoder->outputSampleRate); + if (!drwav_init_file_write(pEncoder, pOutputFilePath, &wavFormat, NULL)) { + ma_decoder_uninit(pDecoder); + return MA_ERROR; + } + + return MA_SUCCESS; +} + #include "ma_test_filtering_dithering.c" #include "ma_test_filtering_lpf.c" #include "ma_test_filtering_hpf.c" diff --git a/tests/test_filtering/ma_test_filtering_lpf.c b/tests/test_filtering/ma_test_filtering_lpf.c index a6c570b7..215c5e61 100644 --- a/tests/test_filtering/ma_test_filtering_lpf.c +++ b/tests/test_filtering/ma_test_filtering_lpf.c @@ -1,34 +1,32 @@ -ma_result test_lpf1__f32(const char* pInputFilePath) +ma_result lpf_init_decoder_and_encoder(const char* pInputFilePath, const char* pOutputFilePath, ma_format format, ma_decoder* pDecoder, drwav* pEncoder) +{ + return filtering_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, 0, 0, pDecoder, pEncoder); +} + +ma_result test_lpf1__by_format(const char* pInputFilePath, const char* pOutputFilePath, ma_format format) { - const char* pOutputFilePath = "output/lpf1_f32.wav"; ma_result result; - ma_decoder_config decoderConfig; ma_decoder decoder; - drwav_data_format wavFormat; drwav wav; ma_lpf1_config lpfConfig; ma_lpf1 lpf; - - decoderConfig = ma_decoder_config_init(ma_format_f32, 0, 0); - result = ma_decoder_init_file(pInputFilePath, &decoderConfig, &decoder); + + printf(" %s\n", pOutputFilePath); + + result = lpf_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, &decoder, &wav); if (result != MA_SUCCESS) { return result; } - lpfConfig = ma_lpf_config_init(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, 2000); + lpfConfig = ma_lpf1_config_init(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, 2000); result = ma_lpf1_init(&lpfConfig, &lpf); if (result != MA_SUCCESS) { ma_decoder_uninit(&decoder); + drwav_uninit(&wav); return result; } - wavFormat = drwav_data_format_from_minaudio_format(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate); - if (!drwav_init_file_write(&wav, pOutputFilePath, &wavFormat, NULL)) { - ma_decoder_uninit(&decoder); - return MA_ERROR; - } - for (;;) { ma_uint8 tempIn[4096]; ma_uint8 tempOut[4096]; @@ -55,75 +53,28 @@ ma_result test_lpf1__f32(const char* pInputFilePath) return MA_SUCCESS; } +ma_result test_lpf1__f32(const char* pInputFilePath) +{ + return test_lpf1__by_format(pInputFilePath, "output/lpf1_f32.wav", ma_format_f32); +} + ma_result test_lpf1__s16(const char* pInputFilePath) { - const char* pOutputFilePath = "output/lpf1_s16.wav"; - ma_result result; - ma_decoder_config decoderConfig; - ma_decoder decoder; - drwav_data_format wavFormat; - drwav wav; - ma_lpf1_config lpfConfig; - ma_lpf1 lpf; - - decoderConfig = ma_decoder_config_init(ma_format_s16, 0, 0); - result = ma_decoder_init_file(pInputFilePath, &decoderConfig, &decoder); - if (result != MA_SUCCESS) { - return result; - } - - lpfConfig = ma_lpf_config_init(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, 2000); - result = ma_lpf1_init(&lpfConfig, &lpf); - if (result != MA_SUCCESS) { - ma_decoder_uninit(&decoder); - return result; - } - - wavFormat = drwav_data_format_from_minaudio_format(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate); - if (!drwav_init_file_write(&wav, pOutputFilePath, &wavFormat, NULL)) { - ma_decoder_uninit(&decoder); - return MA_ERROR; - } - - 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_lpf1_process_pcm_frames(&lpf, 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; + return test_lpf1__by_format(pInputFilePath, "output/lpf1_s16.wav", ma_format_s16); } -ma_result test_lpf2__f32(const char* pInputFilePath) + +ma_result test_lpf2__by_format(const char* pInputFilePath, const char* pOutputFilePath, ma_format format) { - const char* pOutputFilePath = "output/lpf2_f32.wav"; ma_result result; - ma_decoder_config decoderConfig; ma_decoder decoder; - drwav_data_format wavFormat; drwav wav; ma_lpf_config lpfConfig; ma_lpf lpf; + + printf(" %s\n", pOutputFilePath); - decoderConfig = ma_decoder_config_init(ma_format_f32, 0, 0); - result = ma_decoder_init_file(pInputFilePath, &decoderConfig, &decoder); + result = lpf_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, &decoder, &wav); if (result != MA_SUCCESS) { return result; } @@ -132,15 +83,10 @@ ma_result test_lpf2__f32(const char* pInputFilePath) result = ma_lpf_init(&lpfConfig, &lpf); if (result != MA_SUCCESS) { ma_decoder_uninit(&decoder); + drwav_uninit(&wav); return result; } - wavFormat = drwav_data_format_from_minaudio_format(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate); - if (!drwav_init_file_write(&wav, pOutputFilePath, &wavFormat, NULL)) { - ma_decoder_uninit(&decoder); - return MA_ERROR; - } - for (;;) { ma_uint8 tempIn[4096]; ma_uint8 tempOut[4096]; @@ -167,6 +113,17 @@ ma_result test_lpf2__f32(const char* pInputFilePath) return MA_SUCCESS; } +ma_result test_lpf2__f32(const char* pInputFilePath) +{ + return test_lpf2__by_format(pInputFilePath, "output/lpf2_f32.wav", ma_format_f32); +} + +ma_result test_lpf2__s16(const char* pInputFilePath) +{ + return test_lpf2__by_format(pInputFilePath, "output/lpf2_s16.wav", ma_format_s16); +} + + int test_entry__lpf(int argc, char** argv) { ma_result result; @@ -190,11 +147,18 @@ int test_entry__lpf(int argc, char** argv) hasError = MA_TRUE; } + result = test_lpf2__f32(pInputFilePath); if (result != MA_SUCCESS) { hasError = MA_TRUE; } + result = test_lpf2__s16(pInputFilePath); + if (result != MA_SUCCESS) { + hasError = MA_TRUE; + } + + if (hasError) { return -1; } else {