ez_audi/
traits.rs

1use crate::Error;
2use crate::audio_codecs::AudioCodec;
3use crate::cpal_abstraction::{Device, SampleType, SamplesTrait};
4use crate::samples_player::SamplesPlayerTrait;
5use crate::errors::PlayError;
6
7/// Trait implemented on every AudioFile structs, that handles playback
8pub trait AudioFileTrait {
9    /// Gets the file's samles
10    fn get_samples(&self) -> Error<Box<dyn SamplesTrait>>;
11
12    /// Creates a SamplesPlayer with the samples of the file, can be exact or not (exact tends to be very slower)
13    fn make_player(&self, is_exact: bool) -> Error<Box<dyn SamplesPlayerTrait>>;
14
15    /// Starts playing the audio from a certain duration
16    fn play(&self, device: Device, is_exact: bool) -> Error<Box<dyn SamplesPlayerTrait>>;
17
18    /// Plays on the default output of the default host
19    fn play_on_default_output(&self, is_exact: bool) -> Error<Box<dyn SamplesPlayerTrait>> {
20        let device = match Device::default_output() {
21            Some(d) => d,
22            None => return Err(PlayError::DeviceDoesNotExist { name: "default".to_string() })
23        };
24
25        self.play(device, is_exact)
26    }
27
28    /// Returns the file's metadata
29    fn metadata(&self) -> Box<dyn AudioMetadataTrait>;
30}
31
32/// Trait implemented on all audio metadata
33pub trait AudioMetadataTrait {
34    /// The path to the file, may be None if the metadata is not about an audio file
35    fn file_path(&self) -> Option<String>;
36    /// The codec used to decode the audio
37    fn audio_codec(&self) -> AudioCodec;
38    /// The number of channels in the audio
39    fn channels(&self) -> u32;
40    /// The number of samples prssocessed in one second (Hz)
41    fn sample_rate(&self) -> u32;
42    /// The underlying type of the samples, may be none if the codec does not use typical types
43    fn sample_type(&self) -> Option<SampleType>;
44}