mirror of
https://github.com/haileys/bark
synced 2025-03-16 22:57:00 +00:00
feature gate opus
This commit is contained in:
parent
b6a91a9949
commit
dcd5962b19
6 changed files with 25 additions and 4 deletions
|
@ -3,7 +3,8 @@ name = "bark-core"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
opus = ["dep:opus"]
|
||||
|
||||
[dependencies]
|
||||
bark-protocol = { workspace = true }
|
||||
|
@ -12,6 +13,6 @@ bytemuck = { workspace = true }
|
|||
derive_more = { workspace = true }
|
||||
heapless = { workspace = true }
|
||||
log = { workspace = true }
|
||||
opus = "0.3.0"
|
||||
opus = { version = "0.3", optional = true }
|
||||
thiserror = { workspace = true }
|
||||
soxr = { git = "https://github.com/haileys/soxr-rs" }
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#[cfg(feature = "opus")]
|
||||
pub mod opus;
|
||||
|
||||
pub mod pcm;
|
||||
|
||||
use core::fmt::Display;
|
||||
|
@ -15,6 +17,7 @@ use crate::audio::Frame;
|
|||
pub enum NewDecoderError {
|
||||
#[error("unknown format in audio header: {0:?}")]
|
||||
UnknownFormat(AudioPacketFormat),
|
||||
#[cfg(feature = "opus")]
|
||||
#[error("opus codec error: {0}")]
|
||||
Opus(#[from] ::opus::Error),
|
||||
}
|
||||
|
@ -25,6 +28,7 @@ pub enum DecodeError {
|
|||
WrongLength { length: usize, expected: usize },
|
||||
#[error("wrong frame count: {frames}, expected: {expected}")]
|
||||
WrongFrameCount { frames: usize, expected: usize },
|
||||
#[cfg(feature = "opus")]
|
||||
#[error("opus codec error: {0}")]
|
||||
Opus(#[from] ::opus::Error),
|
||||
}
|
||||
|
@ -40,6 +44,7 @@ impl Decoder {
|
|||
let decode = match header.format {
|
||||
AudioPacketFormat::S16LE => DecodeFormat::S16LE(pcm::S16LEDecoder),
|
||||
AudioPacketFormat::F32LE => DecodeFormat::F32LE(pcm::F32LEDecoder),
|
||||
#[cfg(feature = "opus")]
|
||||
AudioPacketFormat::OPUS => DecodeFormat::Opus(opus::OpusDecoder::new()?),
|
||||
format => { return Err(NewDecoderError::UnknownFormat(format)) }
|
||||
};
|
||||
|
@ -64,6 +69,7 @@ trait Decode: Display {
|
|||
enum DecodeFormat {
|
||||
S16LE(pcm::S16LEDecoder),
|
||||
F32LE(pcm::F32LEDecoder),
|
||||
#[cfg(feature = "opus")]
|
||||
Opus(opus::OpusDecoder),
|
||||
}
|
||||
|
||||
|
@ -72,6 +78,7 @@ impl Decode for DecodeFormat {
|
|||
match self {
|
||||
DecodeFormat::S16LE(dec) => dec.decode_packet(bytes, out),
|
||||
DecodeFormat::F32LE(dec) => dec.decode_packet(bytes, out),
|
||||
#[cfg(feature = "opus")]
|
||||
DecodeFormat::Opus(dec) => dec.decode_packet(bytes, out),
|
||||
}
|
||||
}
|
||||
|
@ -82,6 +89,7 @@ impl Display for DecodeFormat {
|
|||
match self {
|
||||
DecodeFormat::S16LE(dec) => dec.fmt(f),
|
||||
DecodeFormat::F32LE(dec) => dec.fmt(f),
|
||||
#[cfg(feature = "opus")]
|
||||
DecodeFormat::Opus(dec) => dec.fmt(f),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#[cfg(feature = "opus")]
|
||||
pub mod opus;
|
||||
|
||||
pub mod pcm;
|
||||
|
||||
use core::fmt::Display;
|
||||
|
@ -10,6 +12,7 @@ use crate::audio::Frame;
|
|||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum NewEncoderError {
|
||||
#[cfg(feature = "opus")]
|
||||
#[error("opus codec error: {0}")]
|
||||
Opus(#[from] ::opus::Error),
|
||||
}
|
||||
|
@ -18,6 +21,7 @@ pub enum NewEncoderError {
|
|||
pub enum EncodeError {
|
||||
#[error("output buffer too small, need at least {need} bytes")]
|
||||
OutputBufferTooSmall { need: usize },
|
||||
#[cfg(feature = "opus")]
|
||||
#[error("opus codec error: {0}")]
|
||||
Opus(#[from] ::opus::Error),
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ name = "bark"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = ["opus"]
|
||||
opus = ["bark-core/opus"]
|
||||
|
||||
[dependencies]
|
||||
bark-core = { workspace = true }
|
||||
|
|
|
@ -29,6 +29,7 @@ pub struct Source {
|
|||
pub enum Format {
|
||||
S16LE,
|
||||
F32LE,
|
||||
#[cfg(feature = "opus")]
|
||||
Opus,
|
||||
}
|
||||
|
||||
|
@ -43,6 +44,7 @@ impl FromStr for Format {
|
|||
match s {
|
||||
"s16le" => Ok(Format::S16LE),
|
||||
"f32le" => Ok(Format::F32LE),
|
||||
#[cfg(feature = "opus")]
|
||||
"opus" => Ok(Format::Opus),
|
||||
_ => Err(UnknownFormat),
|
||||
}
|
||||
|
@ -54,6 +56,7 @@ impl Display for Format {
|
|||
match self {
|
||||
Format::S16LE => write!(f, "s16le"),
|
||||
Format::F32LE => write!(f, "f32le"),
|
||||
#[cfg(feature = "opus")]
|
||||
Format::Opus => write!(f, "opus"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,14 @@ use std::time::Duration;
|
|||
|
||||
use bark_core::audio::Frame;
|
||||
use bark_core::encode::Encode;
|
||||
use bark_core::encode::opus::OpusEncoder;
|
||||
use bark_core::encode::pcm::{S16LEEncoder, F32LEEncoder};
|
||||
use bark_protocol::FRAMES_PER_PACKET;
|
||||
use bytemuck::Zeroable;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[cfg(feature = "opus")]
|
||||
use bark_core::encode::opus::OpusEncoder;
|
||||
|
||||
use bark_protocol::time::SampleDuration;
|
||||
use bark_protocol::packet::{self, Audio, StatsReply, PacketKind};
|
||||
use bark_protocol::types::{TimestampMicros, AudioPacketHeader, SessionId, ReceiverId, TimePhase};
|
||||
|
@ -75,6 +77,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
|
|||
let mut encoder: Box<dyn Encode> = match opt.format {
|
||||
config::Format::S16LE => Box::new(S16LEEncoder),
|
||||
config::Format::F32LE => Box::new(F32LEEncoder),
|
||||
#[cfg(feature = "opus")]
|
||||
config::Format::Opus => Box::new(OpusEncoder::new()?),
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue