use crate::dom::bindings::codegen::Bindings::AudioDataBinding::AudioSampleFormat;
pub(crate) fn sample_size(format: AudioSampleFormat) -> usize {
match format {
AudioSampleFormat::U8 | AudioSampleFormat::U8_planar => 1,
AudioSampleFormat::S16 | AudioSampleFormat::S16_planar => 2,
AudioSampleFormat::S32 | AudioSampleFormat::S32_planar => 4,
AudioSampleFormat::F32 | AudioSampleFormat::F32_planar => 4,
}
}
pub(crate) fn is_planar(format: AudioSampleFormat) -> bool {
matches!(
format,
AudioSampleFormat::U8_planar |
AudioSampleFormat::S16_planar |
AudioSampleFormat::S32_planar |
AudioSampleFormat::F32_planar
)
}
#[derive(Clone, Copy, PartialEq)]
enum SampleFamily {
U8,
S16,
S32,
F32,
}
fn family(format: AudioSampleFormat) -> SampleFamily {
match format {
AudioSampleFormat::U8 | AudioSampleFormat::U8_planar => SampleFamily::U8,
AudioSampleFormat::S16 | AudioSampleFormat::S16_planar => SampleFamily::S16,
AudioSampleFormat::S32 | AudioSampleFormat::S32_planar => SampleFamily::S32,
AudioSampleFormat::F32 | AudioSampleFormat::F32_planar => SampleFamily::F32,
}
}
pub(crate) fn sample_format_for_pcm_codec(codec: &str) -> Option<AudioSampleFormat> {
match codec {
"pcm-u8" => Some(AudioSampleFormat::U8),
"pcm-s16" => Some(AudioSampleFormat::S16),
"pcm-s32" => Some(AudioSampleFormat::S32),
"pcm-f32" => Some(AudioSampleFormat::F32),
_ => None,
}
}
pub(crate) fn read_sample_at(format: AudioSampleFormat, data: &[u8], index: usize) -> f64 {
let size = sample_size(format);
let start = index * size;
if start + size > data.len() {
return 0.0;
}
match family(format) {
SampleFamily::U8 => data[start] as f64,
SampleFamily::S16 => i16::from_le_bytes([data[start], data[start + 1]]) as f64,
SampleFamily::S32 => {
i32::from_le_bytes([
data[start],
data[start + 1],
data[start + 2],
data[start + 3],
]) as f64
},
SampleFamily::F32 => {
f32::from_le_bytes([
data[start],
data[start + 1],
data[start + 2],
data[start + 3],
]) as f64
},
}
}
fn clamp(value: f64, min: f64, max: f64) -> f64 {
value.max(min).min(max)
}
pub(crate) fn write_sample_at(
format: AudioSampleFormat,
data: &mut [u8],
index: usize,
value: f64,
) {
let size = sample_size(format);
let start = index * size;
if start + size > data.len() {
return;
}
match family(format) {
SampleFamily::U8 => {
data[start] = clamp(value, 0.0, 255.0) as u8;
},
SampleFamily::S16 => {
let value = clamp(value, -32768.0, 32767.0) as i16;
data[start..start + 2].copy_from_slice(&value.to_le_bytes());
},
SampleFamily::S32 => {
let value = clamp(value, -2147483648.0, 2147483647.0) as i32;
data[start..start + 4].copy_from_slice(&value.to_le_bytes());
},
SampleFamily::F32 => {
let value = value as f32;
data[start..start + 4].copy_from_slice(&value.to_le_bytes());
},
}
}
pub(crate) fn convert_sample(value: f64, from: AudioSampleFormat, to: AudioSampleFormat) -> f64 {
match (family(from), family(to)) {
(SampleFamily::U8, SampleFamily::U8) |
(SampleFamily::S16, SampleFamily::S16) |
(SampleFamily::S32, SampleFamily::S32) |
(SampleFamily::F32, SampleFamily::F32) => value,
(SampleFamily::U8, SampleFamily::S16) => (value - 128.0) * 256.0,
(SampleFamily::U8, SampleFamily::S32) => (value - 128.0) * 16777216.0,
(SampleFamily::U8, SampleFamily::F32) => (value - 128.0) / 128.0,
(SampleFamily::S16, SampleFamily::U8) => (value / 256.0).floor() + 128.0,
(SampleFamily::S16, SampleFamily::S32) => value * 65536.0,
(SampleFamily::S16, SampleFamily::F32) => value / 32768.0,
(SampleFamily::S32, SampleFamily::U8) => (value / 16777216.0).floor() + 128.0,
(SampleFamily::S32, SampleFamily::S16) => (value / 65536.0).floor(),
(SampleFamily::S32, SampleFamily::F32) => value / 2147483648.0,
(SampleFamily::F32, SampleFamily::U8) => (value * 128.0).floor() + 128.0,
(SampleFamily::F32, SampleFamily::S16) => (value * 32768.0).floor(),
(SampleFamily::F32, SampleFamily::S32) => (value * 2147483648.0).floor(),
}
}
pub(crate) fn channel_plane_base(
format: AudioSampleFormat,
channel: usize,
frames: usize,
) -> usize {
if is_planar(format) {
channel * frames
} else {
0
}
}
pub(crate) fn copy_samples(
source_format: AudioSampleFormat,
source: &[u8],
channels: usize,
source_frames: usize,
frame_offset: usize,
frame_count: usize,
destination_format: AudioSampleFormat,
destination: &mut [u8],
destination_plane: usize,
) {
let source_planar = is_planar(source_format);
let destination_planar = is_planar(destination_format);
if channels == 0 {
return;
}
if destination_planar {
let channel = destination_plane.min(channels - 1);
let base = channel_plane_base(source_format, channel, source_frames);
for frame_index in 0..frame_count {
let source_frame = frame_offset + frame_index;
let source_index = if source_planar {
base + source_frame
} else {
source_frame * channels + channel
};
let value = read_sample_at(source_format, source, source_index);
let value = convert_sample(value, source_format, destination_format);
write_sample_at(destination_format, destination, frame_index, value);
}
} else {
for frame_index in 0..frame_count {
let source_frame = frame_offset + frame_index;
for channel in 0..channels {
let base = channel_plane_base(source_format, channel, source_frames);
let source_index = if source_planar {
base + source_frame
} else {
source_frame * channels + channel
};
let value = read_sample_at(source_format, source, source_index);
let value = convert_sample(value, source_format, destination_format);
write_sample_at(
destination_format,
destination,
frame_index * channels + channel,
value,
);
}
}
}
}