Skip to content

Commit

Permalink
feature gate opus
Browse files Browse the repository at this point in the history
  • Loading branch information
haileys committed Feb 14, 2024
1 parent b6a91a9 commit dcd5962
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions bark-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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" }
8 changes: 8 additions & 0 deletions bark-core/src/decode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "opus")]
pub mod opus;

pub mod pcm;

use core::fmt::Display;
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -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)) }
};
Expand All @@ -64,6 +69,7 @@ trait Decode: Display {
enum DecodeFormat {
S16LE(pcm::S16LEDecoder),
F32LE(pcm::F32LEDecoder),
#[cfg(feature = "opus")]
Opus(opus::OpusDecoder),
}

Expand All @@ -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),
}
}
Expand All @@ -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),
}
}
Expand Down
4 changes: 4 additions & 0 deletions bark-core/src/encode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "opus")]
pub mod opus;

pub mod pcm;

use core::fmt::Display;
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand Down
4 changes: 3 additions & 1 deletion bark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 3 additions & 0 deletions bark/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Source {
pub enum Format {
S16LE,
F32LE,
#[cfg(feature = "opus")]
Opus,
}

Expand All @@ -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),
}
Expand All @@ -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"),
}
}
Expand Down
5 changes: 4 additions & 1 deletion bark/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?),
};

Expand Down

0 comments on commit dcd5962

Please sign in to comment.