use crate::error;
use audio_core_bsd::AudioFrame;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FormatKind {
Flac,
Wav,
Pcm,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct StreamInfo {
pub format: FormatKind,
pub sample_rate: u32,
pub channels: u16,
pub bits_per_sample: u32,
pub total_frames: Option<u64>,
}
pub trait ContainerDecoder: Send {
fn open(&mut self, path: &std::path::Path) -> error::Result<StreamInfo>;
fn next_frame(&mut self) -> error::Result<Option<AudioFrame>>;
}
#[cfg(test)]
mod tests {
use super::{FormatKind, StreamInfo};
#[test]
fn format_kind_variants_are_distinct() {
assert_ne!(FormatKind::Flac, FormatKind::Wav);
assert_ne!(FormatKind::Wav, FormatKind::Pcm);
assert_ne!(FormatKind::Flac, FormatKind::Pcm);
}
#[test]
fn format_kind_copy_and_clone_round_trip() {
let a = FormatKind::Flac;
let b = a;
assert_eq!(a, b);
assert_eq!(a.clone(), b);
}
#[test]
fn format_kind_hash_is_consistent() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
fn hash_of<T: Hash>(v: &T) -> u64 {
let mut h = DefaultHasher::new();
v.hash(&mut h);
h.finish()
}
assert_eq!(hash_of(&FormatKind::Wav), hash_of(&FormatKind::Wav));
assert_ne!(hash_of(&FormatKind::Flac), hash_of(&FormatKind::Wav));
}
#[test]
fn stream_info_round_trips_all_fields() {
let info = StreamInfo {
format: FormatKind::Flac,
sample_rate: 96_000,
channels: 2,
bits_per_sample: 24,
total_frames: Some(4_800_000),
};
assert_eq!(info.format, FormatKind::Flac);
assert_eq!(info.sample_rate, 96_000);
assert_eq!(info.channels, 2);
assert_eq!(info.bits_per_sample, 24);
assert_eq!(info.total_frames, Some(4_800_000));
}
#[test]
fn stream_info_with_unknown_total_frames() {
let info = StreamInfo {
format: FormatKind::Pcm,
sample_rate: 48_000,
channels: 1,
bits_per_sample: 16,
total_frames: None,
};
assert!(info.total_frames.is_none());
}
#[test]
fn stream_info_clone_and_eq_hold() {
let a = StreamInfo {
format: FormatKind::Wav,
sample_rate: 44_100,
channels: 2,
bits_per_sample: 16,
total_frames: Some(100),
};
assert_eq!(a.clone(), a);
let mut b = a.clone();
b.bits_per_sample = 24;
assert_ne!(a, b);
}
}