mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-22 00:06:59 +02:00
Add 1-pole high pass filter.
This commit is contained in:
+162
-1
@@ -1733,10 +1733,24 @@ typedef struct
|
||||
ma_uint32 channels;
|
||||
ma_uint32 sampleRate;
|
||||
double cutoffFrequency;
|
||||
} ma_hpf2_config;
|
||||
} ma_hpf1_config, ma_hpf2_config;
|
||||
|
||||
ma_hpf1_config ma_hpf1_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency);
|
||||
ma_hpf2_config ma_hpf2_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ma_format format;
|
||||
ma_uint32 channels;
|
||||
ma_biquad_coefficient a;
|
||||
ma_biquad_coefficient r1[MA_MAX_CHANNELS];
|
||||
} ma_hpf1;
|
||||
|
||||
ma_result ma_hpf1_init(const ma_hpf1_config* pConfig, ma_hpf1* pHPF);
|
||||
ma_result ma_hpf1_reinit(const ma_hpf1_config* pConfig, ma_hpf1* pHPF);
|
||||
ma_result ma_hpf1_process_pcm_frames(ma_hpf1* pHPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount);
|
||||
ma_uint32 ma_hpf1_get_latency(ma_hpf1* pHPF);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ma_biquad bq; /* The 2-pole high-pass filter is implemented as a biquad filter. */
|
||||
@@ -29828,6 +29842,19 @@ ma_uint32 ma_lpf_get_latency(ma_lpf* pLPF)
|
||||
High-Pass Filtering
|
||||
|
||||
**************************************************************************************************************************************************************/
|
||||
ma_hpf1_config ma_hpf1_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency)
|
||||
{
|
||||
ma_hpf1_config config;
|
||||
|
||||
MA_ZERO_OBJECT(&config);
|
||||
config.format = format;
|
||||
config.channels = channels;
|
||||
config.sampleRate = sampleRate;
|
||||
config.cutoffFrequency = cutoffFrequency;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
ma_hpf2_config ma_hpf2_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, double cutoffFrequency)
|
||||
{
|
||||
ma_hpf2_config config;
|
||||
@@ -29841,6 +29868,140 @@ ma_hpf2_config ma_hpf2_config_init(ma_format format, ma_uint32 channels, ma_uint
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
ma_result ma_hpf1_init(const ma_hpf1_config* pConfig, ma_hpf1* pHPF)
|
||||
{
|
||||
if (pHPF == NULL) {
|
||||
return MA_INVALID_ARGS;
|
||||
}
|
||||
|
||||
MA_ZERO_OBJECT(pHPF);
|
||||
|
||||
if (pConfig == NULL) {
|
||||
return MA_INVALID_ARGS;
|
||||
}
|
||||
|
||||
return ma_hpf1_reinit(pConfig, pHPF);
|
||||
}
|
||||
|
||||
ma_result ma_hpf1_reinit(const ma_hpf1_config* pConfig, ma_hpf1* pHPF)
|
||||
{
|
||||
double a;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
pHPF->format = pConfig->format;
|
||||
pHPF->channels = pConfig->channels;
|
||||
|
||||
a = ma_exp(-2 * MA_PI_D * pConfig->cutoffFrequency / pConfig->sampleRate);
|
||||
if (pConfig->format == ma_format_f32) {
|
||||
pHPF->a.f32 = (float)a;
|
||||
} else {
|
||||
pHPF->a.s32 = ma_biquad_float_to_fp(a);
|
||||
}
|
||||
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
static MA_INLINE void ma_hpf1_process_pcm_frame_f32(ma_hpf1* pHPF, float* pY, const float* pX)
|
||||
{
|
||||
ma_uint32 c;
|
||||
const float a = 1 - pHPF->a.f32;
|
||||
const float b = 1 - a;
|
||||
|
||||
for (c = 0; c < pHPF->channels; c += 1) {
|
||||
float r1 = pHPF->r1[c].f32;
|
||||
float x = pX[c];
|
||||
float y;
|
||||
|
||||
y = b*x - a*r1;
|
||||
|
||||
pY[c] = y;
|
||||
pHPF->r1[c].f32 = y;
|
||||
}
|
||||
}
|
||||
|
||||
static MA_INLINE void ma_hpf1_process_pcm_frame_s16(ma_hpf1* pHPF, ma_int16* pY, const ma_int16* pX)
|
||||
{
|
||||
ma_uint32 c;
|
||||
const ma_int32 a = ((1 << MA_BIQUAD_FIXED_POINT_SHIFT) - pHPF->a.s32);
|
||||
const ma_int32 b = ((1 << MA_BIQUAD_FIXED_POINT_SHIFT) - a);
|
||||
|
||||
for (c = 0; c < pHPF->channels; c += 1) {
|
||||
ma_int32 r1 = pHPF->r1[c].s32;
|
||||
ma_int32 x = pX[c];
|
||||
ma_int32 y;
|
||||
|
||||
y = (b*x - a*r1) >> MA_BIQUAD_FIXED_POINT_SHIFT;
|
||||
|
||||
pY[c] = (ma_int16)y;
|
||||
pHPF->r1[c].s32 = (ma_int32)y;
|
||||
}
|
||||
}
|
||||
|
||||
ma_result ma_hpf1_process_pcm_frames(ma_hpf1* pHPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount)
|
||||
{
|
||||
ma_uint32 n;
|
||||
|
||||
if (pHPF == NULL || pFramesOut == NULL || pFramesIn == NULL) {
|
||||
return MA_INVALID_ARGS;
|
||||
}
|
||||
|
||||
/* Note that the logic below needs to support in-place filtering. That is, it must support the case where pFramesOut and pFramesIn are the same. */
|
||||
|
||||
if (pHPF->format == ma_format_f32) {
|
||||
/* */ float* pY = ( float*)pFramesOut;
|
||||
const float* pX = (const float*)pFramesIn;
|
||||
|
||||
for (n = 0; n < frameCount; n += 1) {
|
||||
ma_hpf1_process_pcm_frame_f32(pHPF, pY, pX);
|
||||
pY += pHPF->channels;
|
||||
pX += pHPF->channels;
|
||||
}
|
||||
} else if (pHPF->format == ma_format_s16) {
|
||||
/* */ ma_int16* pY = ( ma_int16*)pFramesOut;
|
||||
const ma_int16* pX = (const ma_int16*)pFramesIn;
|
||||
|
||||
for (n = 0; n < frameCount; n += 1) {
|
||||
ma_hpf1_process_pcm_frame_s16(pHPF, pY, pX);
|
||||
pY += pHPF->channels;
|
||||
pX += pHPF->channels;
|
||||
}
|
||||
} else {
|
||||
MA_ASSERT(MA_FALSE);
|
||||
return MA_INVALID_ARGS; /* Format not supported. Should never hit this because it's checked in ma_biquad_init() and ma_biquad_reinit(). */
|
||||
}
|
||||
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
ma_uint32 ma_hpf1_get_latency(ma_hpf1* pHPF)
|
||||
{
|
||||
if (pHPF == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static MA_INLINE ma_biquad_config ma_hpf2__get_biquad_config(const ma_hpf2_config* pConfig)
|
||||
{
|
||||
ma_biquad_config bqConfig;
|
||||
|
||||
@@ -1,17 +1,80 @@
|
||||
|
||||
ma_result test_hpf__f32(const char* pInputFilePath)
|
||||
ma_result hpf_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_hpf1__by_format(const char* pInputFilePath, const char* pOutputFilePath, ma_format format)
|
||||
{
|
||||
ma_result result;
|
||||
ma_decoder decoder;
|
||||
drwav wav;
|
||||
ma_hpf1_config hpfConfig;
|
||||
ma_hpf1 hpf;
|
||||
|
||||
printf(" %s\n", pOutputFilePath);
|
||||
|
||||
result = hpf_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, &decoder, &wav);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
hpfConfig = ma_hpf1_config_init(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, 2000);
|
||||
result = ma_hpf1_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_hpf1_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_hpf1__f32(const char* pInputFilePath)
|
||||
{
|
||||
return test_hpf1__by_format(pInputFilePath, "output/hpf1_f32.wav", ma_format_f32);
|
||||
}
|
||||
|
||||
ma_result test_hpf1__s16(const char* pInputFilePath)
|
||||
{
|
||||
return test_hpf1__by_format(pInputFilePath, "output/hpf1_s16.wav", ma_format_f32);
|
||||
}
|
||||
|
||||
|
||||
ma_result test_hpf2__by_format(const char* pInputFilePath, const char* pOutputFilePath, ma_format format)
|
||||
{
|
||||
const char* pOutputFilePath = "output/hpf_f32.wav";
|
||||
ma_result result;
|
||||
ma_decoder_config decoderConfig;
|
||||
ma_decoder decoder;
|
||||
drwav_data_format wavFormat;
|
||||
drwav wav;
|
||||
ma_hpf2_config hpfConfig;
|
||||
ma_hpf2 hpf;
|
||||
|
||||
decoderConfig = ma_decoder_config_init(ma_format_f32, 0, 0);
|
||||
result = ma_decoder_init_file(pInputFilePath, &decoderConfig, &decoder);
|
||||
|
||||
printf(" %s\n", pOutputFilePath);
|
||||
|
||||
result = hpf_init_decoder_and_encoder(pInputFilePath, pOutputFilePath, format, &decoder, &wav);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
@@ -20,15 +83,10 @@ ma_result test_hpf__f32(const char* pInputFilePath)
|
||||
result = ma_hpf2_init(&hpfConfig, &hpf);
|
||||
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,6 +113,16 @@ ma_result test_hpf__f32(const char* pInputFilePath)
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
ma_result test_hpf2__f32(const char* pInputFilePath)
|
||||
{
|
||||
return test_hpf2__by_format(pInputFilePath, "output/hpf2_f32.wav", ma_format_f32);
|
||||
}
|
||||
|
||||
ma_result test_hpf2__s16(const char* pInputFilePath)
|
||||
{
|
||||
return test_hpf2__by_format(pInputFilePath, "output/hpf2_s16.wav", ma_format_f32);
|
||||
}
|
||||
|
||||
int test_entry__hpf(int argc, char** argv)
|
||||
{
|
||||
ma_result result;
|
||||
@@ -68,7 +136,24 @@ int test_entry__hpf(int argc, char** argv)
|
||||
|
||||
pInputFilePath = argv[1];
|
||||
|
||||
result = test_hpf__f32(pInputFilePath);
|
||||
|
||||
result = test_hpf1__f32(pInputFilePath);
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_hpf1__s16(pInputFilePath);
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
|
||||
result = test_hpf2__f32(pInputFilePath);
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_hpf2__s16(pInputFilePath);
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user