Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
miniaudio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
miniaudio
Commits
10f0aebe
Commit
10f0aebe
authored
Feb 23, 2020
by
David Reid
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add 1-pole low-pass filter.
parent
63a1535e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
279 additions
and
4 deletions
+279
-4
miniaudio.h
miniaudio.h
+154
-1
tests/test_filtering/ma_test_filtering_lpf.c
tests/test_filtering/ma_test_filtering_lpf.c
+125
-3
No files found.
miniaudio.h
View file @
10f0aebe
...
...
@@ -1662,10 +1662,23 @@ typedef struct
ma_uint32 channels;
ma_uint32 sampleRate;
double cutoffFrequency;
} ma_lpf_config;
} ma_lpf_config
, ma_lpf1_config
;
ma_lpf_config ma_lpf_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_lpf1;
ma_result ma_lpf1_init(const ma_lpf1_config* pConfig, ma_lpf1* pLPF);
ma_result ma_lpf1_reinit(const ma_lpf1_config* pConfig, ma_lpf1* pLPF);
ma_result ma_lpf1_process_pcm_frames(ma_lpf1* pLPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount);
ma_uint32 ma_lpf1_get_latency(ma_lpf1* pLPF);
typedef struct
{
ma_biquad bq; /* The low-pass filter is implemented as a biquad filter. */
...
...
@@ -5508,6 +5521,12 @@ static MA_INLINE double ma_cos(double x)
return ma_sin((MA_PI*0.5) - x);
}
static MA_INLINE double ma_exp(double x)
{
/* TODO: Implement custom exp(x). */
return exp(x);
}
static MA_INLINE double ma_log(double x)
{
/* TODO: Implement custom log(x). */
...
...
@@ -29324,6 +29343,140 @@ ma_lpf_config ma_lpf_config_init(ma_format format, ma_uint32 channels, ma_uint32
return config;
}
ma_result ma_lpf1_init(const ma_lpf1_config* pConfig, ma_lpf1* pLPF)
{
if (pLPF == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pLPF);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
return ma_lpf1_reinit(pConfig, pLPF);
}
ma_result ma_lpf1_reinit(const ma_lpf1_config* pConfig, ma_lpf1* pLPF)
{
double a;
if (pLPF == 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 (pLPF->format != ma_format_unknown && pLPF->format != pConfig->format) {
return MA_INVALID_OPERATION;
}
/* The channel count cannot be changed after initialization. */
if (pLPF->channels != 0 && pLPF->channels != pConfig->channels) {
return MA_INVALID_OPERATION;
}
pLPF->format = pConfig->format;
pLPF->channels = pConfig->channels;
a = ma_exp(-2 * MA_PI_D * pConfig->cutoffFrequency / pConfig->sampleRate);
if (pConfig->format == ma_format_f32) {
pLPF->a.f32 = (float)a;
} else {
pLPF->a.s32 = ma_biquad_float_to_fp(a);
}
return MA_SUCCESS;
}
static MA_INLINE void ma_lpf1_process_pcm_frame_f32(ma_lpf1* pLPF, float* pY, const float* pX)
{
ma_uint32 c;
const float a = pLPF->a.f32;
const float b = 1 - a;
for (c = 0; c < pLPF->channels; c += 1) {
float r1 = pLPF->r1[c].f32;
float x = pX[c];
float y;
y = b*x + a*r1;
pY[c] = y;
pLPF->r1[c].f32 = y;
}
}
static MA_INLINE void ma_lpf1_process_pcm_frame_s16(ma_lpf1* pLPF, ma_int16* pY, const ma_int16* pX)
{
ma_uint32 c;
const ma_int32 a = pLPF->a.s32;
const ma_int32 b = ((1 << MA_BIQUAD_FIXED_POINT_SHIFT) - a);
for (c = 0; c < pLPF->channels; c += 1) {
ma_int32 r1 = pLPF->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;
pLPF->r1[c].s32 = (ma_int32)y;
}
}
ma_result ma_lpf1_process_pcm_frames(ma_lpf1* pLPF, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount)
{
ma_uint32 n;
if (pLPF == 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 (pLPF->format == ma_format_f32) {
/* */ float* pY = ( float*)pFramesOut;
const float* pX = (const float*)pFramesIn;
for (n = 0; n < frameCount; n += 1) {
ma_lpf1_process_pcm_frame_f32(pLPF, pY, pX);
pY += pLPF->channels;
pX += pLPF->channels;
}
} else if (pLPF->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_lpf1_process_pcm_frame_s16(pLPF, pY, pX);
pY += pLPF->channels;
pX += pLPF->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_lpf1_get_latency(ma_lpf1* pLPF)
{
if (pLPF == NULL) {
return 0;
}
return 1;
}
static MA_INLINE ma_biquad_config ma_lpf__get_biquad_config(const ma_lpf_config* pConfig)
{
ma_biquad_config bqConfig;
tests/test_filtering/ma_test_filtering_lpf.c
View file @
10f0aebe
ma_result
test_lpf__f32
(
const
char
*
pInputFilePath
)
ma_result
test_lpf
1
__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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment