ez_audi/audio_codecs/
mod.rs

1mod l_pcm;
2pub use l_pcm::*;
3
4use crate::errors::{PlayError, Error};
5
6use crate::traits::AudioMetadataTrait;
7
8/// The trait implemented on all audio decoders, allows to decode bytes into LPcm samples
9pub trait AudioCodecTrait {
10    /// Transforms bytes into u8 samples
11    fn bytes_to_u8_samples(&self, _bytes: &Vec<u8>, _metadata: &dyn AudioMetadataTrait) -> Error<Vec<u8>> {
12        Err(PlayError::Unsupported("Bytes to u8 samples is not supported for the audio codec".to_string()))
13    }
14
15    /// Transforms bytes into i16 samples
16    fn bytes_to_i16_samples(&self, _bytes: &Vec<u8>, _metadata: &dyn AudioMetadataTrait) -> Error<Vec<i16>> {
17        Err(PlayError::Unsupported("Bytes to i16 samples is not supported for the audio codec".to_string()))
18    }
19}
20
21#[derive(Debug, Clone, PartialEq)]
22/// Enumeration of all the audio codecs, allows static dispatch on decoding rather than using
23/// a trait object
24pub enum AudioCodec {
25    /// The Linear Pulse-Code Modulation encoding. Often refered to as just PCM.
26    LPcm,
27}
28
29impl AudioCodecTrait for AudioCodec {
30    fn bytes_to_u8_samples(&self, bytes: &Vec<u8>, metadata: &dyn AudioMetadataTrait) -> Error<Vec<u8>> {
31        match self {
32            AudioCodec::LPcm => LPcm.bytes_to_u8_samples(bytes, metadata)
33        }
34    }
35
36    fn bytes_to_i16_samples(&self, bytes: &Vec<u8>, metadata: &dyn AudioMetadataTrait) -> Error<Vec<i16>> {
37        match self {
38            AudioCodec::LPcm => LPcm.bytes_to_i16_samples(bytes, metadata)
39        }
40    }
41}