Skip to content

Commit ccc1197

Browse files
author
Kevin Ahrendt
authored
Add Biquad IIR DSP function and use a namespace (#12)
* add biquad IIR filter assembly functions * use esp_audio_libs global namespace
1 parent 146077c commit ccc1197

19 files changed

+298
-68
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# esp-audio-libs
22

33
A collection of libraries and functions that are useful for playing audio on ESP32 devices. It includes code based on the following:
4-
- [esp-dsp](https://github.com/espressif/esp-dsp) assembly functions for floating point dot products and Q15 fixed point addition and constant multipliction.
4+
- [esp-dsp](https://github.com/espressif/esp-dsp) assembly functions for floating point dot product and biquad IIR filters and Q15 fixed point addition and constant multipliction.
55
- Author: Espressif
66
- License: Apache v2.0
77
- [ART-resampler](https://github.com/dbry/audio-resampler) for resampling audio, optimized with assembly dot product functions.

include/art_biquad.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,34 @@
77
// Distributed under the BSD Software License (see license.txt) //
88
////////////////////////////////////////////////////////////////////////////
99

10-
// biquad.h
10+
#pragma once
1111

1212
#include <stdlib.h>
1313
#include <stdint.h>
1414
#include <stdio.h>
1515
#include <math.h>
1616

17-
typedef struct {
18-
float a0, a1, a2, b1, b2;
19-
} BiquadCoefficients;
17+
namespace esp_audio_libs {
18+
namespace art_resampler {
2019

21-
typedef struct {
22-
BiquadCoefficients coeffs; // coefficients
23-
float in_d1, in_d2; // delayed input
24-
float out_d1, out_d2; // delayed output
25-
int first_order; // optimization
26-
} Biquad;
20+
typedef struct {
21+
float a0, a1, a2, b1, b2;
22+
} BiquadCoefficients;
2723

28-
#ifdef __cplusplus
29-
extern "C" {
30-
#endif
24+
typedef struct {
25+
BiquadCoefficients coeffs; // coefficients
26+
float in_d1, in_d2; // delayed input
27+
float out_d1, out_d2; // delayed output
28+
int first_order; // optimization
29+
} Biquad;
3130

32-
void biquad_init(Biquad *f, const BiquadCoefficients *coeffs, float gain);
31+
void biquad_init(Biquad * f, const BiquadCoefficients *coeffs, float gain);
3332

34-
void biquad_lowpass(BiquadCoefficients *filter, double frequency);
35-
void biquad_highpass(BiquadCoefficients *filter, double frequency);
33+
void biquad_lowpass(BiquadCoefficients * filter, double frequency);
34+
void biquad_highpass(BiquadCoefficients * filter, double frequency);
3635

37-
void biquad_apply_buffer(Biquad *f, float *buffer, int num_samples, int stride);
38-
float biquad_apply_sample(Biquad *f, float input);
36+
void biquad_apply_buffer(Biquad * f, float *buffer, int num_samples, int stride);
37+
float biquad_apply_sample(Biquad * f, float input);
3938

40-
#ifdef __cplusplus
4139
}
42-
#endif
40+
} // namespace esp_audio_libs

include/art_resampler.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <math.h>
1717
#include <stdbool.h>
1818

19+
namespace esp_audio_libs {
20+
namespace art_resampler {
21+
1922
#define SUBSAMPLE_INTERPOLATE 0x1
2023
#define BLACKMAN_HARRIS 0x2
2124
#define INCLUDE_LOWPASS 0x4
@@ -30,10 +33,6 @@ typedef struct {
3033
unsigned int input_used, output_generated;
3134
} ResampleResult;
3235

33-
#ifdef __cplusplus
34-
extern "C" {
35-
#endif
36-
3736
Resample *resampleInit(int numChannels, int numTaps, int numFilters, float lowpassRatio, int flags);
3837
ResampleResult resampleProcess(Resample *cxt, const float *const *input, int numInputFrames, float *const *output,
3938
int numOutputFrames, float ratio);
@@ -46,6 +45,5 @@ float resampleGetPosition(Resample *cxt);
4645
void resampleReset(Resample *cxt);
4746
void resampleFree(Resample *cxt);
4847

49-
#ifdef __cplusplus
50-
}
51-
#endif
48+
} // namespace art_resampler
49+
} // namespace esp_audio_libs

include/flac_decoder.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
// Uses some small parts from: https://github.com/schreibfaul1/ESP32-audioI2S/
55
// See also: https://xiph.org/flac/format.html
66

7-
#ifndef _FLAC_DECODER_H
8-
#define _FLAC_DECODER_H
7+
#pragma once
98

109
#include <cstddef>
1110
#include <cstdint>
1211
#include <vector>
1312

13+
namespace esp_audio_libs {
1414
namespace flac {
1515

1616
// 'fLaC'
@@ -185,5 +185,4 @@ class FLACDecoder {
185185
};
186186

187187
} // namespace flac
188-
189-
#endif
188+
} // namespace esp_audio_libs

include/mp3_decoder.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
#ifndef MP3_DECODER_H_
2-
#define MP3_DECODER_H_
1+
#pragma once
32

43
#include <stdlib.h>
54
#include <string.h>
65
#include <xtensa/config/core-isa.h>
76

7+
namespace esp_audio_libs {
8+
namespace helix_decoder {
9+
810
#define ASSERT(x) /* do nothing */
911

1012
/* determining MAINBUF_SIZE:
@@ -379,4 +381,5 @@ void MP3GetLastFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo);
379381
int MP3GetNextFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo, unsigned char *buf);
380382
int MP3FindSyncWord(unsigned char *buf, int nBytes);
381383

382-
#endif // MP3_DECODER_H_
384+
} // namespace helix_decoder
385+
} // namespace esp_audio_libs

include/resampler.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <algorithm>
99
#include <esp_heap_caps.h>
1010

11+
namespace esp_audio_libs {
1112
namespace resampler {
1213

1314
// Class that wraps the ART resampler for straightforward use
@@ -59,10 +60,10 @@ class Resampler {
5960
float *float_output_buffer_{nullptr};
6061
size_t output_buffer_samples_;
6162

62-
Resample *resampler_{nullptr};
63+
art_resampler::Resample *resampler_{nullptr};
6364

64-
Biquad lowpass_[2][2];
65-
BiquadCoefficients lowpass_coeff_;
65+
art_resampler::Biquad lowpass_[2][2];
66+
art_resampler::BiquadCoefficients lowpass_coeff_;
6667

6768
uint16_t number_of_taps_;
6869
uint16_t number_of_filters_;
@@ -79,3 +80,4 @@ class Resampler {
7980
uint8_t channels_;
8081
};
8182
} // namespace resampler
83+
} // namespace esp_audio_libs

include/utils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <cmath>
44
#include <stdint.h>
55

6+
namespace esp_audio_libs {
7+
68
/// @brief Converts an array of quantized samples with the specified number of bits into floating point samples.
79
/// @param input_buffer Pointer to the input quantized samples aligned to the byte
810
/// @param output_buffer Poitner to the output floating point samples
@@ -18,4 +20,7 @@ void quantized_to_float(const uint8_t *input_buffer, float *output_buffer, uint3
1820
/// @param num_samples Number of samples to convert
1921
/// @param output_bits Number of bits per sample for the quantized samples
2022
/// @return Number of clipped samples
21-
uint32_t float_to_quantized(const float *input_buffer, uint8_t *output_buffer, uint32_t num_samples, uint8_t output_bits);
23+
uint32_t float_to_quantized(const float *input_buffer, uint8_t *output_buffer, uint32_t num_samples,
24+
uint8_t output_bits);
25+
26+
} // namespace esp_audio_libs

include/wav_decoder.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// data portion of the file.
33
// Skips over extraneous chunks like LIST and INFO.
44

5-
#ifndef WAV_DECODER_H_
6-
#define WAV_DECODER_H_
5+
#pragma once
76

87
#include <cstdint>
98
#include <string>
@@ -29,6 +28,7 @@
2928
* (optional RIFF chunks)
3029
* */
3130

31+
namespace esp_audio_libs {
3232
namespace wav_decoder {
3333

3434
enum WAVDecoderState {
@@ -91,5 +91,4 @@ class WAVDecoder {
9191
uint16_t bits_per_sample_ = 0;
9292
};
9393
} // namespace wav_decoder
94-
95-
#endif // WAV_DECODER_H_
94+
} // namespace esp_audio_libs

src/decode/flac_decoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <esp_heap_caps.h>
77
#include "flac_decoder.h"
88

9+
namespace esp_audio_libs {
910
namespace flac {
1011

1112
FLACDecoderResult FLACDecoder::read_header(uint8_t *buffer, size_t buffer_length) {
@@ -241,7 +242,8 @@ FLACDecoderResult FLACDecoder::decode_frame_header_() {
241242
return FLAC_DECODER_SUCCESS;
242243
}
243244

244-
FLACDecoderResult FLACDecoder::decode_frame(uint8_t *buffer, size_t buffer_length, int16_t *output_buffer, uint32_t *num_samples) {
245+
FLACDecoderResult FLACDecoder::decode_frame(uint8_t *buffer, size_t buffer_length, int16_t *output_buffer,
246+
uint32_t *num_samples) {
245247
this->buffer_ = buffer;
246248
this->buffer_index_ = 0;
247249
this->bytes_left_ = buffer_length;
@@ -606,3 +608,4 @@ int64_t FLACDecoder::read_rice_sint(uint8_t param) {
606608
void FLACDecoder::align_to_byte() { this->bit_buffer_length_ -= (this->bit_buffer_length_ % 8); } // align_to_byte
607609

608610
} // namespace flac
611+
} // namespace esp_audio_libs

src/decode/mp3_decoder.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
#include "mp3_decoder.h"
4444

4545
#include <esp_heap_caps.h>
46-
// #include "esphome/core/helpers.h"
46+
47+
namespace esp_audio_libs {
48+
namespace helix_decoder {
4749

4850
/* indexing = [version][samplerate index]
4951
* sample rate of frame (Hz)
@@ -8851,3 +8853,5 @@ int MP3Decode(HMP3Decoder hMP3Decoder, unsigned char **inbuf, int *bytesLeft, sh
88518853
}
88528854
return ERR_MP3_NONE;
88538855
}
8856+
} // namespace helix_decoder
8857+
} // namespace esp_audio_libs

0 commit comments

Comments
 (0)