use burn::{
module::Module,
prelude::Backend,
};
use crate::{
errors::{
BunsenError,
BunsenResult,
LookupError,
WithOkOrPanic,
},
kits::speech::silero_vad::SileroVad,
};
#[derive(Module, Debug)]
pub struct SileroVadCollection<B: Backend> {
pub branches: Vec<(usize, SileroVad<B>)>,
}
impl<B: Backend> SileroVadCollection<B> {
pub fn try_branch(
&self,
sample_rate: usize,
) -> BunsenResult<&SileroVad<B>> {
self.branches
.iter()
.find(|(rate, _)| *rate == sample_rate)
.map(|(_, vad)| vad)
.ok_or_else(|| {
BunsenError::lookup(
LookupError::missing("sample-rate branch", sample_rate.to_string())
.with_candidates(self.branches.iter().map(|(rate, _)| rate.to_string())),
)
})
}
pub fn expect_branch(
&self,
sample_rate: usize,
) -> &SileroVad<B> {
self.try_branch(sample_rate).ok_or_panic()
}
}