style: apply cargo fmt across the crate (A20)
The repo never enforced rustfmt, so formatting had drifted broadly. This is a single mechanical `cargo fmt` pass over the whole crate (no behavioral change; lib suite green, 493 passed). Going forward fmt should be enforced (planned CI fmt --check step). Part of the 0.6.1 hygiene pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+63
-17
@@ -1,5 +1,5 @@
|
||||
use crate::codec::{AudioEncoder, AudioDecoder, CodecError};
|
||||
use opus::{Encoder, Decoder, Application, Channels};
|
||||
use crate::codec::{AudioDecoder, AudioEncoder, CodecError};
|
||||
use opus::{Application, Channels, Decoder, Encoder};
|
||||
|
||||
pub struct OpusEncoder {
|
||||
encoder: Encoder,
|
||||
@@ -8,7 +8,11 @@ pub struct OpusEncoder {
|
||||
impl OpusEncoder {
|
||||
/// Creates a new Opus encoder.
|
||||
/// Standard voice parameters: sample_rate = 48000, channels = Channels::Mono, application = Application::Voip
|
||||
pub fn new(sample_rate: u32, channels: Channels, application: Application) -> Result<Self, CodecError> {
|
||||
pub fn new(
|
||||
sample_rate: u32,
|
||||
channels: Channels,
|
||||
application: Application,
|
||||
) -> Result<Self, CodecError> {
|
||||
let encoder = Encoder::new(sample_rate, channels, application)
|
||||
.map_err(|e| CodecError::Init(format!("Failed to create Opus encoder: {}", e)))?;
|
||||
Ok(Self { encoder })
|
||||
@@ -20,9 +24,11 @@ impl AudioEncoder for OpusEncoder {
|
||||
// We allocate a buffer for the compressed output.
|
||||
// A maximum packet size of 4000 bytes is more than enough for a single voice frame.
|
||||
let mut compressed = vec![0u8; 4000];
|
||||
let len = self.encoder.encode(pcm, &mut compressed)
|
||||
let len = self
|
||||
.encoder
|
||||
.encode(pcm, &mut compressed)
|
||||
.map_err(|e| CodecError::Encode(format!("Opus encoding failed: {}", e)))?;
|
||||
|
||||
|
||||
compressed.truncate(len);
|
||||
Ok(compressed)
|
||||
}
|
||||
@@ -42,10 +48,18 @@ impl OpusDecoder {
|
||||
/// Creates a new Opus decoder.
|
||||
/// Standard voice parameters: sample_rate = 48000, channels = Channels::Mono.
|
||||
/// `frame_samples` is the per-channel length of one transmitted frame (e.g. 960).
|
||||
pub fn new(sample_rate: u32, channels: Channels, frame_samples: usize) -> Result<Self, CodecError> {
|
||||
pub fn new(
|
||||
sample_rate: u32,
|
||||
channels: Channels,
|
||||
frame_samples: usize,
|
||||
) -> Result<Self, CodecError> {
|
||||
let decoder = Decoder::new(sample_rate, channels)
|
||||
.map_err(|e| CodecError::Init(format!("Failed to create Opus decoder: {}", e)))?;
|
||||
Ok(Self { decoder, channels, frame_samples })
|
||||
Ok(Self {
|
||||
decoder,
|
||||
channels,
|
||||
frame_samples,
|
||||
})
|
||||
}
|
||||
|
||||
fn channels_count(&self) -> usize {
|
||||
@@ -73,7 +87,9 @@ impl AudioDecoder for OpusDecoder {
|
||||
}
|
||||
};
|
||||
|
||||
let decoded_per_channel = self.decoder.decode(input, &mut pcm, false)
|
||||
let decoded_per_channel = self
|
||||
.decoder
|
||||
.decode(input, &mut pcm, false)
|
||||
.map_err(|e| CodecError::Decode(format!("Opus decoding failed: {}", e)))?;
|
||||
|
||||
pcm.truncate(decoded_per_channel * channels_count);
|
||||
@@ -100,7 +116,10 @@ mod tests {
|
||||
|
||||
// encode it
|
||||
let compressed = encoder.encode(&pcm).unwrap();
|
||||
assert!(!compressed.is_empty(), "Compressed buffer should not be empty");
|
||||
assert!(
|
||||
!compressed.is_empty(),
|
||||
"Compressed buffer should not be empty"
|
||||
);
|
||||
assert!(
|
||||
compressed.len() < pcm.len() * std::mem::size_of::<i16>(),
|
||||
"Compressed size ({}) should be smaller than raw PCM size ({})",
|
||||
@@ -110,13 +129,21 @@ mod tests {
|
||||
|
||||
// decode it
|
||||
let decoded = decoder.decode(Some(&compressed)).unwrap();
|
||||
assert_eq!(decoded.len(), 960, "Decoded sample count should be exactly 960");
|
||||
assert_eq!(
|
||||
decoded.len(),
|
||||
960,
|
||||
"Decoded sample count should be exactly 960"
|
||||
);
|
||||
|
||||
// 2. Round-trip carries signal energy (not silence)
|
||||
let sum_sq: f64 = decoded.iter().map(|&x| (x as f64).powi(2)).sum();
|
||||
let rms = (sum_sq / decoded.len() as f64).sqrt();
|
||||
// Since input had amplitude ~10000, let's verify RMS is significantly above 0 (e.g. > 100.0)
|
||||
assert!(rms > 100.0, "Decoded signal should carry energy (RMS was {})", rms);
|
||||
assert!(
|
||||
rms > 100.0,
|
||||
"Decoded signal should carry energy (RMS was {})",
|
||||
rms
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -125,11 +152,19 @@ mod tests {
|
||||
|
||||
// decode(None) returns exactly frame_samples (960) samples
|
||||
let plc_none = decoder.decode(None).unwrap();
|
||||
assert_eq!(plc_none.len(), 960, "decode(None) should yield exactly 960 samples");
|
||||
assert_eq!(
|
||||
plc_none.len(),
|
||||
960,
|
||||
"decode(None) should yield exactly 960 samples"
|
||||
);
|
||||
|
||||
// decode(Some(&[])) (empty slice) does the same
|
||||
let plc_empty = decoder.decode(Some(&[])).unwrap();
|
||||
assert_eq!(plc_empty.len(), 960, "decode(Some(&[])) should yield exactly 960 samples");
|
||||
assert_eq!(
|
||||
plc_empty.len(),
|
||||
960,
|
||||
"decode(Some(&[])) should yield exactly 960 samples"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -140,7 +175,11 @@ mod tests {
|
||||
let pcm = vec![0i16; 960];
|
||||
let compressed = encoder.encode(&pcm).unwrap();
|
||||
let decoded = decoder.decode(Some(&compressed)).unwrap();
|
||||
assert_eq!(decoded.len(), 960, "Decoded sample count should match packet duration");
|
||||
assert_eq!(
|
||||
decoded.len(),
|
||||
960,
|
||||
"Decoded sample count should match packet duration"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -149,11 +188,18 @@ mod tests {
|
||||
|
||||
// decode(None) returns exactly frame_samples * 2 (1920) samples
|
||||
let plc_none = decoder.decode(None).unwrap();
|
||||
assert_eq!(plc_none.len(), 960 * 2, "Stereo decode(None) should yield exactly 1920 samples");
|
||||
assert_eq!(
|
||||
plc_none.len(),
|
||||
960 * 2,
|
||||
"Stereo decode(None) should yield exactly 1920 samples"
|
||||
);
|
||||
|
||||
// decode(Some(&[])) (empty slice) does the same
|
||||
let plc_empty = decoder.decode(Some(&[])).unwrap();
|
||||
assert_eq!(plc_empty.len(), 960 * 2, "Stereo decode(Some(&[])) should yield exactly 1920 samples");
|
||||
assert_eq!(
|
||||
plc_empty.len(),
|
||||
960 * 2,
|
||||
"Stereo decode(Some(&[])) should yield exactly 1920 samples"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user