ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
audio/format.c
Go to the documentation of this file.
2#include <math.h>
3
4asciichat_error_t audio_route_capture(const float *input, size_t frames,
5 unsigned channels, int first_channel,
6 unsigned selected_channels,
7 float *output) {
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}
34
35asciichat_error_t audio_convert_channels(const float *input, size_t frames,
36 unsigned input_channels, float *output,
37 unsigned output_channels) {
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}
61
62size_t audio_resample_stereo(audio_resampler_t *state, const float *input,
63 size_t frames, double input_rate, float *output,
64 size_t capacity, double output_rate) {
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}
#define SET_ERRNO(code,...)
asciichat_error_t audio_route_capture(const float *input, size_t frames, unsigned channels, int first_channel, unsigned selected_channels, float *output)
Definition audio/format.c:4
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)
#define AUDIO_DEVICE_MAX_CHANNELS
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
🔢 Mathematical Utility Functions
When code needs to work with audio resampler, it keeps phase, has_previous here instead of scattering...
float previous[AUDIO_CHANNELS]