ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
spectral.h File Reference

Go to the source code of this file.

Data Structures

struct  spectral_config_t
 For spectral config, this is the live record that ties sample_rate, fft_size, noise_gate, compressor together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. More...

Macros

#define SPECTRAL_BAND_COUNT   4

Typedefs

typedef struct spectral_processor spectral_processor_t

Functions

spectral_config_t spectral_default_config (int sample_rate)
asciichat_error_t spectral_create (const spectral_config_t *config, spectral_processor_t **out)
void spectral_destroy (spectral_processor_t *processor)
void spectral_reset (spectral_processor_t *processor)
asciichat_error_t spectral_process (spectral_processor_t *processor, const float *input, float *output, size_t count)
float spectral_pitch_hz (const spectral_processor_t *processor)
const float * spectral_spectrum (const spectral_processor_t *processor, size_t *bins)

Macro Definition Documentation

◆ SPECTRAL_BAND_COUNT

#define SPECTRAL_BAND_COUNT   4

Definition at line 4 of file spectral.h.

Typedef Documentation

◆ spectral_processor_t

Mono streaming STFT processor. One owner thread per instance.

Definition at line 15 of file spectral.h.

Function Documentation

◆ spectral_create()

asciichat_error_t spectral_create ( const spectral_config_t * config,
spectral_processor_t ** out )

Definition at line 73 of file spectral.c.

74 {
75 if (!out)
76 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing spectral output pointer");
77 *out = NULL;
78 if (!c || c->sample_rate < 16000 || c->sample_rate > 192000 ||
79 (c->fft_size != 512 && c->fft_size != 1024 && c->fft_size != 2048) ||
80 !isfinite(c->reduction_db) || c->reduction_db < 0 || c->reduction_db > 60)
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spectral configuration");
82 for (int b = 0; b < 4; ++b) {
83 if (!isfinite(c->ratios[b]) || c->ratios[b] < 1 || c->ratios[b] > 20 ||
84 !isfinite(c->thresholds_db[b]) || c->thresholds_db[b] > 0 ||
85 c->thresholds_db[b] < -90)
86 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spectral compressor band");
87 if (b < 3 && (!isfinite(c->crossovers[b]) || c->crossovers[b] <= 0 ||
88 c->crossovers[b] > c->sample_rate / 2.0f ||
89 (b && c->crossovers[b] <= c->crossovers[b - 1])))
90 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spectral crossover");
91 }
93 if (!p)
94 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate spectral processor");
95 p->config = *c;
96 (void)NAMED_REGISTER_MUTEX(&audio_fftw_planner_lock, "audio.fftw_planner",
97 NULL);
98 static_mutex_lock(&audio_fftw_planner_lock);
99 p->forward =
100 fftwf_plan_dft_r2c_1d(c->fft_size, p->time, p->frequency, FFTW_ESTIMATE);
101 p->inverse = fftwf_plan_dft_c2r_1d(c->fft_size, p->frequency, p->restored,
102 FFTW_ESTIMATE);
103 static_mutex_unlock(&audio_fftw_planner_lock);
104 if (!p->forward || !p->inverse) {
106 return SET_ERRNO(ERROR_MEMORY, "Cannot create FFTW plans");
107 }
108 for (int i = 0; i < c->fft_size; ++i)
109 p->window[i] =
110 (float)(0.5 - 0.5 * cos(6.283185307179586 * i / c->fft_size));
112 *out = p;
113 return ASCIICHAT_OK;
114}
#define SET_ERRNO(code,...)
#define SAFE_CALLOC(count, size, cast)
#define NAMED_REGISTER_MUTEX(mutex, name, parent_ptr)
Register a mutex with automatic format specifier.
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
static_mutex_t audio_fftw_planner_lock
Definition spectral.c:39
void spectral_destroy(spectral_processor_t *p)
Definition spectral.c:116
void spectral_reset(spectral_processor_t *p)
Definition spectral.c:51
struct spectral_processor spectral_processor_t
Definition spectral.h:15
fftwf_plan forward
Definition spectral.c:20
spectral_config_t config
Definition spectral.c:19
float restored[2048]
Definition spectral.c:21
float window[2048]
Definition spectral.c:25
float time[2048]
Definition spectral.c:21
fftwf_complex frequency[(2048/2+1)]
Definition spectral.c:22
fftwf_plan inverse
Definition spectral.c:20

References ASCIICHAT_OK, audio_fftw_planner_lock, spectral_processor::config, spectral_config_t::crossovers, ERROR_INVALID_PARAM, ERROR_MEMORY, spectral_config_t::fft_size, spectral_processor::forward, spectral_processor::frequency, spectral_processor::inverse, NAMED_REGISTER_MUTEX, out, spectral_config_t::ratios, spectral_config_t::reduction_db, spectral_processor::restored, SAFE_CALLOC, spectral_config_t::sample_rate, SET_ERRNO, spectral_destroy(), spectral_reset(), spectral_config_t::thresholds_db, spectral_processor::time, and spectral_processor::window.

Referenced by audio_processor_create(), and mixer_create_channels().

◆ spectral_default_config()

spectral_config_t spectral_default_config ( int sample_rate)

Definition at line 41 of file spectral.c.

41 {
42 return (spectral_config_t){.sample_rate = sample_rate,
43 .fft_size = 1024,
44 .noise_gate = true,
45 .reduction_db = 18.0f,
46 .crossovers = {250, 2000, 8000},
47 .thresholds_db = {-18, -18, -18, -18},
48 .ratios = {2, 3, 3, 2}};
49}
For spectral config, this is the live record that ties sample_rate, fft_size, noise_gate,...
Definition spectral.h:22

Referenced by audio_processor_default_config(), and mixer_create_channels().

◆ spectral_destroy()

void spectral_destroy ( spectral_processor_t * processor)

Definition at line 116 of file spectral.c.

116 {
117 if (!p)
118 return;
119 static_mutex_lock(&audio_fftw_planner_lock);
120 if (p->forward)
121 fftwf_destroy_plan(p->forward);
122 if (p->inverse)
123 fftwf_destroy_plan(p->inverse);
124 static_mutex_unlock(&audio_fftw_planner_lock);
125 SAFE_FREE(p);
126}
#define SAFE_FREE(ptr)

References audio_fftw_planner_lock, spectral_processor::forward, spectral_processor::inverse, and SAFE_FREE.

Referenced by audio_processor_create(), audio_processor_destroy(), mixer_destroy(), and spectral_create().

◆ spectral_pitch_hz()

float spectral_pitch_hz ( const spectral_processor_t * processor)

Latest input spectrum, normalized to sinusoidal peak amplitude; owner thread only.

Definition at line 332 of file spectral.c.

332 {
333 return p ? p->pitch_hz : 0;
334}

References spectral_processor::pitch_hz.

◆ spectral_process()

asciichat_error_t spectral_process ( spectral_processor_t * processor,
const float * input,
float * output,
size_t count )

Arbitrary block sizes and in-place operation; no allocation or locks.

Definition at line 309 of file spectral.c.

310 {
311 if (!p || (!input && count) || (!output && count))
312 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spectral buffer");
313 int n = p->config.fft_size, hop = n / 4;
314 for (size_t i = 0; i < count; ++i) {
315 float sample = input[i];
316 output[i] = p->ready[p->cursor++];
317 p->input[n - hop + p->pending++] = isfinite(sample) ? sample : 0;
318 if (p->pending == hop) {
319 process_frame(p);
320 p->pending = p->cursor = 0;
321 }
322 }
323 return ASCIICHAT_OK;
324}

References ASCIICHAT_OK, spectral_processor::config, spectral_processor::cursor, ERROR_INVALID_PARAM, spectral_config_t::fft_size, spectral_processor::input, spectral_processor::pending, spectral_processor::ready, and SET_ERRNO.

Referenced by audio_processor_process().

◆ spectral_reset()

void spectral_reset ( spectral_processor_t * processor)

Definition at line 51 of file spectral.c.

51 {
52 if (!p)
53 return;
54 memset(p->input, 0, sizeof(p->input));
55 memset(p->overlap, 0, sizeof(p->overlap));
56 memset(p->ready, 0, sizeof(p->ready));
57 memset(p->noise, 0, sizeof(p->noise));
58 memset(p->smoothed, 0, sizeof(p->smoothed));
59 memset(p->magnitude, 0, sizeof(p->magnitude));
60 memset(p->band_average, 0, sizeof(p->band_average));
61 for (int i = 0; i < SPECTRAL_MAX_BINS; ++i)
62 p->gains[i] = 1;
63 for (int i = 0; i < 4; ++i)
64 p->band_gain[i] = p->eq_gain[i] = 1;
65 p->pending = p->cursor = 0;
66 memset(p->previous_phase, 0, sizeof(p->previous_phase));
67 memset(p->synthesis_phase, 0, sizeof(p->synthesis_phase));
68 p->pitch_hz = 0;
69 p->auto_gain = 1;
70 p->frames = 0;
71}
#define SPECTRAL_MAX_BINS
Definition spectral.c:10

References spectral_processor::auto_gain, spectral_processor::band_average, spectral_processor::band_gain, spectral_processor::cursor, spectral_processor::eq_gain, spectral_processor::frames, spectral_processor::gains, spectral_processor::input, spectral_processor::magnitude, spectral_processor::noise, spectral_processor::overlap, spectral_processor::pending, spectral_processor::pitch_hz, spectral_processor::previous_phase, spectral_processor::ready, spectral_processor::smoothed, SPECTRAL_MAX_BINS, and spectral_processor::synthesis_phase.

Referenced by audio_processor_reset(), mixer_remove_source(), and spectral_create().

◆ spectral_spectrum()

const float * spectral_spectrum ( const spectral_processor_t * processor,
size_t * bins )

Definition at line 326 of file spectral.c.

326 {
327 if (bins)
328 *bins = p ? (size_t)p->config.fft_size / 2 + 1 : 0;
329 return p ? p->magnitude : NULL;
330}

References spectral_processor::config, spectral_config_t::fft_size, and spectral_processor::magnitude.