Module audio::audio_convert
audio sample-format and layout conversion.
The conversions std/audio's streams do between the format/layout a callback asked for and the one the device runs (see audio.h), exposed for application use: integer <-> float scaling, packed 24-bit, (de) interleaving, and the bridge to std/dsp's Q8.24 Fixed samples and its lock-free AudioBuffer. All of them are allocation-free and lock-free — safe inside an audio callback.
Scaling: an N-bit integer sample s maps to s / 2^(N-1) (so -2^(N-1) is exactly -1.0); float -> integer multiplies by 2^(N-1) - 1, rounds to nearest and clamps, so out-of-range floats saturate instead of wrapping. Dithered variants add triangular (TPDF) dither of +-1 LSB before rounding, from a caller-held xorshift state.
Buffers are slices: every function works on as many samples (or frames) as both its input and its output hold, and returns that count — a short output buffer truncates, it never overruns. 24-bit samples are S24 (std/audio/s24.h); a byte buffer of any format is []unsigned char.
Functions
| audio_f32_to_i16 | |
| audio_i16_to_f32 | |
| audio_f32_to_i32 | |
| audio_i32_to_f32 | |
| audio_f32_to_i8 | |
| audio_i8_to_f32 | |
| audio_f32_to_i24 | |
| audio_i24_to_f32 | |
| audio_f32_to_f64 | |
| audio_f64_to_f32 | |
| audio_i16_to_i32 | |
| audio_i32_to_i16 | |
| audio_f32_to_i16_dither | TPDF-dithered float -> 16-bit. 'state' is any non-zero seed, updated. |
| audio_sample_read | One sample of any AUDIO_FORMAT_* at the start of 'bytes', as double (0.0 if 'bytes' is shorter than one sample), and back (nothing written if it is). |
| audio_sample_write | |
| audio_convert | Any format to any format (same layout): as many whole samples as both buffers hold. Returns the samples converted. |
| audio_interleave_f32 | planar: channel-major ('frames' samples per channel, back to back). frames = min(planar.len, out.len) / channels; returns it. |
| audio_deinterleave_f32 | |
| audio_copy_frames | The general copy streams use: 'channels' channels of 'frames' frames, from 'src' (src_channels per frame, src_interleaved, src_format, starting at channel src_first) to 'dst' (likewise). Channels of 'dst' outside [dst_first, dst_first + channels) are left untouched. Returns 'frames', or 0 (copying nothing) when a buffer is smaller than frames * its channels samples or the channel ranges don't fit. |
| audio_gain_f32 | |
| audio_mix_f32 | dst[i] += src[i] * gain for min(dst.len, src.len) samples; returns that. |
| audio_peak_f32 | Peak absolute value. |
| audio_f32_to_fixed | |
| audio_fixed_to_f32 | |
| audio_buffer_write_f32 | Writes in.len / channels interleaved float frames to a dsp AudioBuffer, or reads up to out.len / channels frames from it, converting in fixed-size chunks on the stack. Returns frames transferred (fewer when full/empty). |
| audio_buffer_read_f32 |