mod.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. pub trait Mixer: Send {
  2. fn open(_: Option<MixerConfig>) -> Self
  3. where
  4. Self: Sized;
  5. fn start(&self);
  6. fn stop(&self);
  7. fn set_volume(&self, volume: u16);
  8. fn volume(&self) -> u16;
  9. fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
  10. None
  11. }
  12. }
  13. pub trait AudioFilter {
  14. fn modify_stream(&self, data: &mut [i16]);
  15. }
  16. #[cfg(feature = "alsa-backend")]
  17. pub mod alsamixer;
  18. #[cfg(feature = "alsa-backend")]
  19. use self::alsamixer::AlsaMixer;
  20. #[derive(Debug, Clone)]
  21. pub struct MixerConfig {
  22. pub card: String,
  23. pub mixer: String,
  24. pub index: u32,
  25. }
  26. impl Default for MixerConfig {
  27. fn default() -> MixerConfig {
  28. MixerConfig {
  29. card: String::from("default"),
  30. mixer: String::from("PCM"),
  31. index: 0,
  32. }
  33. }
  34. }
  35. pub mod softmixer;
  36. use self::softmixer::SoftMixer;
  37. fn mk_sink<M: Mixer + 'static>(device: Option<MixerConfig>) -> Box<dyn Mixer> {
  38. Box::new(M::open(device))
  39. }
  40. pub fn find<T: AsRef<str>>(name: Option<T>) -> Option<fn(Option<MixerConfig>) -> Box<dyn Mixer>> {
  41. match name.as_ref().map(AsRef::as_ref) {
  42. None | Some("softvol") => Some(mk_sink::<SoftMixer>),
  43. #[cfg(feature = "alsa-backend")]
  44. Some("alsa") => Some(mk_sink::<AlsaMixer>),
  45. _ => None,
  46. }
  47. }