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

Go to the source code of this file.

Data Structures

struct  audio_resampler_t
 When code needs to work with audio resampler, it keeps phase, has_previous here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. More...

Functions

asciichat_error_t audio_route_capture (const float *input, size_t frames, unsigned channels, int first_channel, unsigned selected_channels, float *output)
asciichat_error_t audio_convert_channels (const float *input, size_t frames, unsigned input_channels, float *output, unsigned output_channels)
size_t audio_resample_stereo (audio_resampler_t *state, const float *input, size_t frames, double input_rate, float *output, size_t capacity, double output_rate)

Function Documentation

◆ audio_convert_channels()

asciichat_error_t audio_convert_channels ( const float * input,
size_t frames,
unsigned input_channels,
float * output,
unsigned output_channels )

Convert interleaved mono/stereo PCM. In-place conversion is supported in both directions.

Definition at line 35 of file audio/format.c.

37 {
38 if (!input || !output || (input_channels != 1 && input_channels != 2) ||
39 (output_channels != 1 && output_channels != 2))
41 "Only mono and stereo PCM are supported");
42 // Backwards iteration also permits in-place mono expansion.
43 if (input_channels == 1 && output_channels == 2) {
44 for (size_t frame = frames; frame > 0; frame--)
45 output[(frame - 1) * 2] = output[(frame - 1) * 2 + 1] = input[frame - 1];
46 } else {
47 for (size_t frame = 0; frame < frames; frame++) {
48 float left = input[frame * input_channels];
49 float right =
50 input_channels == 2 ? input[frame * input_channels + 1] : left;
51 if (output_channels == 1)
52 output[frame] = left * 0.5f + right * 0.5f;
53 else {
54 output[frame * 2] = left;
55 output[frame * 2 + 1] = right;
56 }
57 }
58 }
59 return ASCIICHAT_OK;
60}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by client_audio_pipeline_capture(), handle_audio_batch_packet(), and handle_audio_packet().

◆ audio_resample_stereo()

size_t audio_resample_stereo ( audio_resampler_t * state,
const float * input,
size_t frames,
double input_rate,
float * output,
size_t capacity,
double output_rate )

Stateful interleaved resampling; capacities and return value are frames. Invalid arguments return zero and set ERROR_INVALID_PARAM. On insufficient capacity the state and output are left unchanged.

Definition at line 62 of file audio/format.c.

64 {
65 if (!state || !input || !output || !frames || !isfinite(input_rate) ||
66 !isfinite(output_rate) || input_rate < 8000 || input_rate > 192000 ||
67 output_rate < 8000 || output_rate > 192000) {
70 "Invalid stereo resampling buffers, frame count, or sample rates");
71 return 0;
72 }
73 const double step = input_rate / output_rate;
74 double position = state->phase;
75 if (!state->has_previous && position < 0)
76 position += ceil(-position / step) * step;
77 double limit = (double)frames - 1;
78 size_t needed =
79 position < limit ? (size_t)ceil((limit - position) / step) : 0;
80 if (needed > capacity)
81 return 0;
82 size_t produced = 0;
83 while (position < limit) {
84 size_t first = position < 0 ? 0 : (size_t)position;
85 double fraction = position < 0 ? position + 1 : position - (double)first;
86 for (unsigned channel = 0; channel < 2; channel++) {
87 float a =
88 position < 0 ? state->previous[channel] : input[first * 2 + channel];
89 float b =
90 position < 0 ? input[channel] : input[(first + 1) * 2 + channel];
91 output[produced * 2 + channel] = (float)(a + fraction * (b - a));
92 }
93 produced++;
94 position += step;
95 }
96 state->phase = position - (double)frames;
97 state->previous[0] = input[(frames - 1) * 2];
98 state->previous[1] = input[(frames - 1) * 2 + 1];
99 state->has_previous = true;
100 return produced;
101}
float previous[AUDIO_CHANNELS]

References ERROR_INVALID_PARAM, audio_resampler_t::has_previous, audio_resampler_t::phase, audio_resampler_t::previous, and SET_ERRNO.

◆ audio_route_capture()

asciichat_error_t audio_route_capture ( const float * input,
size_t frames,
unsigned channels,
int first_channel,
unsigned selected_channels,
float * output )

Internal PCM is interleaved stereo. PCM sample counts include both channels. Route a zero-based mono input or adjacent stereo pair to internal PCM. A negative first_channel downmixes all input channels to centered mono. Input and output must not overlap. No allocation or locking is performed.

Definition at line 4 of file audio/format.c.

7 {
8 if (!input || !output || channels == 0 ||
9 channels > AUDIO_DEVICE_MAX_CHANNELS ||
10 (selected_channels != 1 && selected_channels != 2) ||
11 first_channel < -1 ||
12 (first_channel >= 0 &&
13 ((unsigned)first_channel >= channels ||
14 selected_channels > channels - (unsigned)first_channel)))
15 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid microphone channel routing");
16 for (size_t frame = 0; frame < frames; frame++) {
17 float left, right;
18 if (first_channel < 0) {
19 double sum = 0;
20 for (unsigned channel = 0; channel < channels; channel++)
21 sum += input[frame * channels + channel];
22 left = right = (float)(sum / channels);
23 } else {
24 left = input[frame * channels + (unsigned)first_channel];
25 right = selected_channels == 2
26 ? input[frame * channels + (unsigned)first_channel + 1]
27 : left;
28 }
29 output[frame * 2] = left;
30 output[frame * 2 + 1] = right;
31 }
32 return ASCIICHAT_OK;
33}
#define AUDIO_DEVICE_MAX_CHANNELS

References ASCIICHAT_OK, AUDIO_DEVICE_MAX_CHANNELS, ERROR_INVALID_PARAM, and SET_ERRNO.