librespot/audio/src/lib.rs

102 lines
2.7 KiB
Rust
Raw Normal View History

#![allow(clippy::unused_io_amount)]
2018-02-26 01:50:41 +00:00
#[macro_use]
extern crate log;
2017-08-03 19:37:04 +00:00
mod decrypt;
2018-02-26 01:50:41 +00:00
mod fetch;
2017-08-03 19:37:04 +00:00
2021-02-10 22:07:59 +00:00
use cfg_if::cfg_if;
#[cfg(any(
all(feature = "with-lewton", feature = "with-tremor"),
all(feature = "with-vorbis", feature = "with-tremor"),
all(feature = "with-lewton", feature = "with-vorbis")
))]
compile_error!("Cannot use two decoders at the same time.");
cfg_if! {
if #[cfg(feature = "with-lewton")] {
mod lewton_decoder;
pub use lewton_decoder::{VorbisDecoder, VorbisError};
} else if #[cfg(any(feature = "with-tremor", feature = "with-vorbis"))] {
mod libvorbis_decoder;
pub use crate::libvorbis_decoder::{VorbisDecoder, VorbisError};
} else {
compile_error!("Must choose a vorbis decoder.");
}
}
2021-01-07 06:42:38 +00:00
mod passthrough_decoder;
2021-02-10 22:07:59 +00:00
pub use passthrough_decoder::{PassthroughDecoder, PassthroughError};
2019-11-01 19:46:28 +00:00
mod range_set;
2017-08-03 19:37:04 +00:00
pub use decrypt::AudioDecrypt;
2021-01-21 21:12:35 +00:00
pub use fetch::{AudioFile, StreamLoaderController};
2019-11-11 07:22:41 +00:00
pub use fetch::{
READ_AHEAD_BEFORE_PLAYBACK_ROUNDTRIPS, READ_AHEAD_BEFORE_PLAYBACK_SECONDS,
READ_AHEAD_DURING_PLAYBACK_ROUNDTRIPS, READ_AHEAD_DURING_PLAYBACK_SECONDS,
};
2021-01-07 06:42:38 +00:00
use std::fmt;
pub enum AudioPacket {
Samples(Vec<i16>),
OggData(Vec<u8>),
}
impl AudioPacket {
pub fn samples(&self) -> &[i16] {
match self {
AudioPacket::Samples(s) => s,
AudioPacket::OggData(_) => panic!("can't return OggData on samples"),
}
}
pub fn oggdata(&self) -> &[u8] {
match self {
AudioPacket::Samples(_) => panic!("can't return samples on OggData"),
AudioPacket::OggData(d) => d,
}
}
pub fn is_empty(&self) -> bool {
match self {
AudioPacket::Samples(s) => s.is_empty(),
AudioPacket::OggData(d) => d.is_empty(),
}
}
}
2021-01-07 06:42:38 +00:00
#[derive(Debug)]
pub enum AudioError {
PassthroughError(PassthroughError),
VorbisError(VorbisError),
}
impl fmt::Display for AudioError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AudioError::PassthroughError(err) => write!(f, "PassthroughError({})", err),
AudioError::VorbisError(err) => write!(f, "VorbisError({})", err),
}
}
}
impl From<VorbisError> for AudioError {
fn from(err: VorbisError) -> AudioError {
AudioError::VorbisError(VorbisError::from(err))
}
}
impl From<PassthroughError> for AudioError {
fn from(err: PassthroughError) -> AudioError {
AudioError::PassthroughError(PassthroughError::from(err))
}
}
pub trait AudioDecoder {
fn seek(&mut self, ms: i64) -> Result<(), AudioError>;
fn next_packet(&mut self) -> Result<Option<AudioPacket>, AudioError>;
}