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
//! Type definitions for a format of audio data.
use bitflags::bitflags;
use crate::bind;
bitflags! {
/// A flag to represent what type is used in audio data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AudioFormatFlag: u8 {
/// Whether a type is floating-point number.
const FLOAT = 1 << 0;
/// Whether a type is big endian.
const BIG_ENDIAN = 1 << 4;
/// Whether a type is signed.
const SIGNED = 1 << 7;
}
}
/// A format to represent how is stored samples in audio data.
#[must_use]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AudioFormat {
/// A flag to represent characteristics of a type.
pub flag: AudioFormatFlag,
/// A size in bits of a type.
pub bit_size: u8,
}
impl AudioFormat {
/// A format for signed 8-bit data.
pub const fn signed8() -> Self {
Self {
flag: AudioFormatFlag::SIGNED,
bit_size: 8,
}
}
/// A format for unsigned 8-bit data.
pub const fn unsigned8() -> Self {
Self {
flag: AudioFormatFlag::empty(),
bit_size: 8,
}
}
/// A format for signed little endian 16-bit data.
pub const fn signed16_lsb() -> Self {
Self {
flag: AudioFormatFlag::SIGNED,
bit_size: 16,
}
}
/// A format for unsigned little endian 16-bit data.
pub const fn unsigned16_lsb() -> Self {
Self {
flag: AudioFormatFlag::empty(),
bit_size: 16,
}
}
/// A format for signed big endian 16-bit data.
pub const fn signed16_msb() -> Self {
Self {
flag: AudioFormatFlag::from_bits_truncate(1 << 4 | 1 << 7),
bit_size: 16,
}
}
/// A format for unsigned big endian 16-bit data.
pub const fn unsigned16_msb() -> Self {
Self {
flag: AudioFormatFlag::BIG_ENDIAN,
bit_size: 16,
}
}
/// A format for signed little endian 32-bit data.
pub const fn signed32_lsb() -> Self {
Self {
flag: AudioFormatFlag::SIGNED,
bit_size: 32,
}
}
/// A format for signed big endian 32-bit data.
pub const fn signed32_msb() -> Self {
Self {
flag: AudioFormatFlag::from_bits_truncate(1 << 4 | 1 << 7),
bit_size: 32,
}
}
/// A format for floating-point little endian 32-bit data.
pub const fn float32_lsb() -> Self {
Self {
flag: AudioFormatFlag::from_bits_truncate(1 << 0 | 1 << 7),
bit_size: 32,
}
}
/// A format for floating-point big endian 32-bit data.
pub const fn float32_msb() -> Self {
Self {
flag: AudioFormatFlag::from_bits_truncate(1 << 0 | 1 << 4 | 1 << 7),
bit_size: 32,
}
}
pub(super) fn as_raw(self) -> u16 {
(self.flag.bits() as u16) << 8 | self.bit_size as u16
}
}
impl From<u16> for AudioFormat {
fn from(raw: u16) -> Self {
Self {
flag: AudioFormatFlag::from_bits((raw >> 8) as u8).unwrap(),
bit_size: (raw & 0xff) as u8,
}
}
}