Add 1-pole low-pass filter.

This commit is contained in:
David Reid
2020-02-23 10:12:29 +10:00
parent 63a1535e75
commit 10f0aebed3
2 changed files with 279 additions and 4 deletions
+125 -3
View File
@@ -1,7 +1,119 @@
ma_result test_lpf__f32(const char* pInputFilePath)
ma_result test_lpf1__f32(const char* pInputFilePath)
{
const char* pOutputFilePath = "output/lpf_f32.wav";
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);
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;
}
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;
}
ma_result test_lpf2__f32(const char* pInputFilePath)
{
const char* pOutputFilePath = "output/lpf2_f32.wav";
ma_result result;
ma_decoder_config decoderConfig;
ma_decoder decoder;
@@ -68,7 +180,17 @@ int test_entry__lpf(int argc, char** argv)
pInputFilePath = argv[1];
result = test_lpf__f32(pInputFilePath);
result = test_lpf1__f32(pInputFilePath);
if (result != MA_SUCCESS) {
hasError = MA_TRUE;
}
result = test_lpf1__s16(pInputFilePath);
if (result != MA_SUCCESS) {
hasError = MA_TRUE;
}
result = test_lpf2__f32(pInputFilePath);
if (result != MA_SUCCESS) {
hasError = MA_TRUE;
}