av_data/audiosample.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
//! Audio sample format definitions.
use std::fmt;
use std::string::*;
/// Audio format definition.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Soniton {
/// Bits per sample.
pub bits: u8,
/// Tells if audio format is big-endian.
pub be: bool,
/// Audio samples are packed (e.g. 20-bit audio samples) and not padded.
pub packed: bool,
/// Audio data is stored in planar format
/// (channels in sequence i.e. C1 C1 C1... C2 C2 C2) instead of interleaving
/// samples (i.e. C1 C2 C1 C2) for different channels.
pub planar: bool,
/// Audio data is in floating point format.
pub float: bool,
/// Audio data is signed (usually only 8-bit audio is unsigned).
pub signed: bool,
}
// TODO: make it a trait for usize?
/// Aligns a value to a specific number of bytes.
fn align(v: usize, a: usize) -> usize {
(v + a - 1) & !(a - 1)
}
/// Returns the number of bytes necessary to represent the number of bits
/// passed as input.
fn round_to_byte(v: usize) -> usize {
(v + 7) >> 3
}
impl Soniton {
/// Constructs a new audio format definition.
pub fn new(bits: u8, be: bool, packed: bool, planar: bool, float: bool, signed: bool) -> Self {
Soniton {
bits,
be,
packed,
planar,
float,
signed,
}
}
/// Returns the amount of bytes needed to store
/// the audio of requested length (in samples).
pub fn get_audio_size(self, length: usize, alignment: usize) -> usize {
let s = if self.packed {
round_to_byte(length * (self.bits as usize))
} else {
length * round_to_byte(self.bits as usize)
};
align(s, alignment)
}
}
impl fmt::Display for Soniton {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt = if self.float {
"float"
} else if self.signed {
"int"
} else {
"uint"
};
let end = if self.be { "BE" } else { "LE" };
write!(
f,
"({} bps, {} planar: {} packed: {} {})",
self.bits, end, self.packed, self.planar, fmt
)
}
}
/// Known audio channel types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(clippy::upper_case_acronyms)]
pub enum ChannelType {
/// Center front.
C,
/// Left front.
L,
/// Right front.
R,
/// Center surround.
Cs,
/// Left surround.
Ls,
/// Right surround.
Rs,
/// Left surround side.
Lss,
/// Right surround side.
Rss,
/// Low Frequency Effect.
LFE,
/// Left center.
Lc,
/// Right center.
Rc,
/// Left height.
Lh,
/// Right height.
Rh,
/// Center height.
Ch,
/// Second Low Frequency Effect.
LFE2,
/// Left wide.
Lw,
/// Right wide.
Rw,
/// Overhead.
///
/// Known also as:
/// - Over the listener head (Oh) in DTS specification (ETSI TS 102.114)
/// - Top Center Surround (Ts) in SMPTE 428-3-2006 specification
Ov,
/// Left height side.
Lhs,
/// Right height side.
Rhs,
/// Center height side.
Chs,
/// Left in the plane lower then listener's ears
/// (DTS specification ETSI TS 102.114).
Ll,
/// Right in the plane lower then listener's ears
/// (DTS specification ETSI TS 102.114).
Rl,
/// Center in the plane lower then listener's ears
/// (DTS specification ETSI TS 102.114).
Cl,
/// Left total (SMPTE 428-3-2006 specification).
Lt,
/// Right total (SMPTE 428-3-2006 specification).
Rt,
/// Left-only downmix mode (Dolby ETSI TS 102.366 specification).
Lo,
/// Right-only downmix mode (Dolby ETSI TS 102.366 specification).
Ro,
}
impl ChannelType {
/// Tells whether the channel is some center channel.
pub fn is_center(self) -> bool {
matches!(
self,
ChannelType::C
| ChannelType::Ch
| ChannelType::Cl
| ChannelType::Ov
| ChannelType::LFE
| ChannelType::LFE2
| ChannelType::Cs
| ChannelType::Chs
)
}
/// Tells whether the channel is some left channel.
pub fn is_left(self) -> bool {
matches!(
self,
ChannelType::L
| ChannelType::Ls
| ChannelType::Lss
| ChannelType::Lc
| ChannelType::Lh
| ChannelType::Lw
| ChannelType::Lhs
| ChannelType::Ll
| ChannelType::Lt
| ChannelType::Lo
)
}
/// Tells whether the channel is some right channel.
pub fn is_right(self) -> bool {
matches!(
self,
ChannelType::R
| ChannelType::Rs
| ChannelType::Rss
| ChannelType::Rc
| ChannelType::Rh
| ChannelType::Rw
| ChannelType::Rhs
| ChannelType::Rl
| ChannelType::Rt
| ChannelType::Ro
)
}
}
impl fmt::Display for ChannelType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match *self {
ChannelType::C => "C".to_string(),
ChannelType::L => "L".to_string(),
ChannelType::R => "R".to_string(),
ChannelType::Cs => "Cs".to_string(),
ChannelType::Ls => "Ls".to_string(),
ChannelType::Rs => "Rs".to_string(),
ChannelType::Lss => "Lss".to_string(),
ChannelType::Rss => "Rss".to_string(),
ChannelType::LFE => "LFE".to_string(),
ChannelType::Lc => "Lc".to_string(),
ChannelType::Rc => "Rc".to_string(),
ChannelType::Lh => "Lh".to_string(),
ChannelType::Rh => "Rh".to_string(),
ChannelType::Ch => "Ch".to_string(),
ChannelType::LFE2 => "LFE2".to_string(),
ChannelType::Lw => "Lw".to_string(),
ChannelType::Rw => "Rw".to_string(),
ChannelType::Ov => "Ov".to_string(),
ChannelType::Lhs => "Lhs".to_string(),
ChannelType::Rhs => "Rhs".to_string(),
ChannelType::Chs => "Chs".to_string(),
ChannelType::Ll => "Ll".to_string(),
ChannelType::Rl => "Rl".to_string(),
ChannelType::Cl => "Cl".to_string(),
ChannelType::Lt => "Lt".to_string(),
ChannelType::Rt => "Rt".to_string(),
ChannelType::Lo => "Lo".to_string(),
ChannelType::Ro => "Ro".to_string(),
};
write!(f, "{}", name)
}
}
/// An ordered sequence of channels.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct ChannelMap {
ids: Vec<ChannelType>,
}
impl ChannelMap {
/// Creates a new sequence of channels.
pub fn new() -> Self {
ChannelMap { ids: Vec::new() }
}
/// Adds a single channel to the map.
pub fn add_channel(&mut self, ch: ChannelType) {
self.ids.push(ch);
}
/// Adds several channels to the map in order of occurrence.
pub fn add_channels(&mut self, chs: &[ChannelType]) {
for ch in chs {
self.ids.push(*ch);
}
}
/// Returns the total number of channels of the map.
pub fn len(&self) -> usize {
self.ids.len()
}
/// Tells if the channel map is empty.
pub fn is_empty(&self) -> bool {
self.ids.is_empty()
}
/// Gets the channel type for a requested index.
pub fn get_channel(&self, idx: usize) -> ChannelType {
self.ids[idx]
}
/// Tries to find the position of a determined type of channel in the map.
pub fn find_channel_id(&self, t: ChannelType) -> Option<u8> {
for i in 0..self.ids.len() {
if self.ids[i] as i32 == t as i32 {
return Some(i as u8);
}
}
None
}
/// Creates a default channel map.
///
/// Depending on the `count` value, the channel map is defined differently.
///
/// When `count` is 1 --> the channel map is composed by a single centered
/// channel.
///
/// When `count` is 2 --> the channel map is composed by a right and a left
/// channel respectively.
///
/// For other `count` values, no other implementations are given for now.
pub fn default_map(count: usize) -> Self {
use self::ChannelType::*;
let ids = match count {
1 => vec![C],
2 => vec![R, L],
_ => unimplemented!(),
};
ChannelMap { ids }
}
}
/// A set of default constant channels for general use.
pub mod formats {
use super::*;
/// Predefined format for interleaved 8-bit unsigned audio.
pub const U8: Soniton = Soniton {
bits: 8,
be: false,
packed: false,
planar: false,
float: false,
signed: false,
};
/// Predefined format for interleaved 16-bit signed audio.
pub const S16: Soniton = Soniton {
bits: 16,
be: false,
packed: false,
planar: false,
float: false,
signed: true,
};
/// Predefined format for interleaved 32-bit signed audio.
pub const S32: Soniton = Soniton {
bits: 32,
be: false,
packed: false,
planar: false,
float: false,
signed: true,
};
/// Predefined format for interleaved floating points 32-bit signed audio.
pub const F32: Soniton = Soniton {
bits: 32,
be: false,
packed: false,
planar: false,
float: true,
signed: true,
};
/// Predefined format for interleaved floating points 64-bit signed audio.
pub const F64: Soniton = Soniton {
bits: 64,
be: false,
packed: false,
planar: false,
float: true,
signed: true,
};
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn fmt() {
println!("{}", formats::S16);
println!("{}", formats::U8);
println!("{}", formats::F32);
}
}