esp32-audio-player 0.1.0
Audio player engine for ESP32
Loading...
Searching...
No Matches
dsp.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
8#pragma once
9
10#include "audio_player/audio.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#define DSP_MAX_BANDS 10
17#define DSP_PRESET_NAME_MAX 48
18
20
21typedef struct {
23 float freq_hz;
24 float gain_db;
25 float q;
26 bool enabled;
28
29typedef struct {
31 float preamp_db;
32 uint8_t band_count;
35
40int eq_preset_parse_autoeq(const char *text, eq_preset_t *out);
42int eq_preset_format_autoeq(const eq_preset_t *p, char *out, size_t out_size);
44uint32_t eq_builtin_count(void);
45const eq_preset_t *eq_builtin_get(uint32_t index);
46
47typedef enum { RG_OFF = 0, RG_TRACK, RG_ALBUM, RG_AUTO /* album gain when playing an album in order */ } rg_mode_t;
48
64
66
68enum {
69 DSP_FLAG_RG = 1u << 0,
70 DSP_FLAG_EQ = 1u << 1,
74 DSP_FLAG_DITHER = 1u << 5,
76};
77
78typedef struct dsp dsp_t;
82void dsp_configure(dsp_t *d, const dsp_config_t *cfg, uint32_t sample_rate, uint8_t channels);
84void dsp_set_replaygain(dsp_t *d, float track_gain_db, float track_peak, float album_gain_db, float album_peak,
85 bool album_context);
86void dsp_process(dsp_t *d, int32_t *pcm, uint32_t frames);
88#define DSP_DRAIN_MAX 128u
97uint32_t dsp_process_out(dsp_t *d, int32_t *pcm, uint32_t frames, uint32_t cap_frames);
98void dsp_reset(dsp_t *d);
99bool dsp_is_bypass(const dsp_t *d);
100uint32_t dsp_active_flags(const dsp_t *d);
101
102// ---------------------------------------------------------------------------
104// ---------------------------------------------------------------------------
105typedef struct {
106 float b0, b1, b2, a1, a2;
108
110void biquad_design(biquad_coef_t *c, peq_type_t type, float fs, float f0, float gain_db, float q);
112float biquad_response_db(const biquad_coef_t *c, float fs, float f);
113
115void pcm_q31_to_f32(const int32_t *in, float *out, size_t samples);
116void pcm_f32_to_q31(const float *in, int32_t *out, size_t samples);
117
119void pcm_q31_to_s16_dither(const int32_t *in, int16_t *out, size_t samples, uint32_t *state);
121void pcm_mono_to_stereo(const int32_t *in, int32_t *out, size_t frames);
122
123float db_to_lin(float db);
124float lin_to_db(float lin);
125
126#ifdef __cplusplus
127}
128#endif
Audio formats and PCM conventions.
@ DSP_FLAG_EQ
Definition dsp.h:70
@ DSP_FLAG_LIMITER
Definition dsp.h:73
@ DSP_FLAG_RG
Definition dsp.h:69
@ DSP_FLAG_CROSSFEED
Definition dsp.h:71
@ DSP_FLAG_DITHER
Definition dsp.h:74
@ DSP_FLAG_RESAMPLE
set by the player, not by dsp
Definition dsp.h:75
@ DSP_FLAG_SW_VOLUME
Definition dsp.h:72
void dsp_configure(dsp_t *d, const dsp_config_t *cfg, uint32_t sample_rate, uint8_t channels)
(Re)configure for a format. Keeps filter state when only gains change.
#define DSP_PRESET_NAME_MAX
Definition dsp.h:17
void biquad_design(biquad_coef_t *c, peq_type_t type, float fs, float f0, float gain_db, float q)
RBJ Audio EQ Cookbook designs.
float lin_to_db(float lin)
void dsp_config_defaults(dsp_config_t *c)
float db_to_lin(float db)
struct dsp dsp_t
Definition dsp.h:78
int eq_preset_parse_autoeq(const char *text, eq_preset_t *out)
Parse AutoEQ "ParametricEQ.txt" (lines like "Preamp: -6.3 dB" and "Filter 1: ON PK Fc 105 Hz Gain -2....
uint32_t eq_builtin_count(void)
Built-in presets: "Flat", "Bass", "Treble", "Vocal", "Loudness". Index 0 is Flat.
rg_mode_t
Definition dsp.h:47
@ RG_TRACK
Definition dsp.h:47
@ RG_AUTO
Definition dsp.h:47
@ RG_ALBUM
Definition dsp.h:47
@ RG_OFF
Definition dsp.h:47
void pcm_mono_to_stereo(const int32_t *in, int32_t *out, size_t frames)
Upmix mono Q31 to stereo. in == out is allowed (the buffer must hold 2*frames samples).
void pcm_f32_to_q31(const float *in, int32_t *out, size_t samples)
saturating
void dsp_process(dsp_t *d, int32_t *pcm, uint32_t frames)
in place
int eq_preset_format_autoeq(const eq_preset_t *p, char *out, size_t out_size)
Write the same text format (so presets round-trip). Returns bytes written or <0.
const eq_preset_t * eq_builtin_get(uint32_t index)
void pcm_q31_to_s16_dither(const int32_t *in, int16_t *out, size_t samples, uint32_t *state)
Reduce to 16 bits with TPDF dither (state = LCG seed, keep per stream).
peq_type_t
Definition dsp.h:19
@ PEQ_LOW_SHELF
Definition dsp.h:19
@ PEQ_HIGH_SHELF
Definition dsp.h:19
@ PEQ_PEAK
Definition dsp.h:19
@ PEQ_HIGH_PASS
Definition dsp.h:19
@ PEQ_LOW_PASS
Definition dsp.h:19
void dsp_set_replaygain(dsp_t *d, float track_gain_db, float track_peak, float album_gain_db, float album_peak, bool album_context)
ReplayGain values of the current track (NAN when absent). Call on track change.
dsp_t * dsp_create(void)
uint32_t dsp_active_flags(const dsp_t *d)
void dsp_reset(dsp_t *d)
clear filter state (seek, track change)
uint32_t dsp_process_out(dsp_t *d, int32_t *pcm, uint32_t frames, uint32_t cap_frames)
dsp_process() with room for more output.
float biquad_response_db(const biquad_coef_t *c, float fs, float f)
Magnitude response in dB at frequency f (for EQ curve drawing and tests).
void dsp_destroy(dsp_t *d)
bool dsp_is_bypass(const dsp_t *d)
void pcm_q31_to_f32(const int32_t *in, float *out, size_t samples)
Q31 <-> float.
#define DSP_MAX_BANDS
Definition dsp.h:16
Low-level building blocks, exported for tests and for the Bluetooth path.
Definition dsp.h:105
float a1
Definition dsp.h:106
Definition dsp.h:49
uint16_t crossfeed_fcut_hz
300..2000, default 700
Definition dsp.h:57
bool limiter_enabled
soft limiter when gains can exceed 0 dBFS
Definition dsp.h:61
float volume_db
used only if sw_volume; <= 0
Definition dsp.h:60
uint8_t out_bits
sink resolution for dither: 16, 24 or 32 (32 = no dither)
Definition dsp.h:62
bool crossfeed_enabled
Definition dsp.h:56
rg_mode_t rg_mode
Definition dsp.h:52
eq_preset_t eq
Definition dsp.h:51
float rg_preamp_db
added to RG gain, default 0
Definition dsp.h:53
bool rg_prevent_clip
limit gain by peak
Definition dsp.h:55
bool eq_enabled
Definition dsp.h:50
float rg_fallback_db
gain for files without RG tags, default 0
Definition dsp.h:54
float crossfeed_level_db
1..15, default 4.5
Definition dsp.h:58
bool sw_volume
true when the sink has no hardware volume
Definition dsp.h:59
Definition dsp.h:29
float preamp_db
usually negative to avoid clipping
Definition dsp.h:31
uint8_t band_count
Definition dsp.h:32
Definition dsp.h:21
bool enabled
Definition dsp.h:26
float freq_hz
Definition dsp.h:23
float q
Definition dsp.h:25
peq_type_t type
Definition dsp.h:22
float gain_db
ignored for pass filters
Definition dsp.h:24