use orfail::OrFail;
use crate::audio::{AudioData, AudioFormat, SAMPLE_RATE};
const DECODED_CHANNELS: u8 = 2;
#[derive(Debug)]
pub struct OpusDecoder {
inner: shiguredo_opus::Decoder,
}
impl OpusDecoder {
pub fn new() -> orfail::Result<Self> {
Ok(Self {
inner: shiguredo_opus::Decoder::new(SAMPLE_RATE, DECODED_CHANNELS).or_fail()?,
})
}
pub fn decode(&mut self, data: &AudioData) -> orfail::Result<AudioData> {
(data.format == AudioFormat::Opus).or_fail()?;
let decoded_samples = self.inner.decode(&data.data).or_fail()?;
let decoded = AudioData {
source_id: data.source_id.clone(),
data: decoded_samples
.iter()
.flat_map(|v| v.to_be_bytes().into_iter())
.collect(),
format: AudioFormat::I16Be,
stereo: true,
sample_rate: SAMPLE_RATE,
timestamp: data.timestamp,
duration: data.duration,
sample_entry: None,
};
Ok(decoded)
}
}