pmoflac corrections

This commit is contained in:
2025-10-27 17:05:36 +01:00
parent 3cb90b72ac
commit a383980412
7 changed files with 698 additions and 3 deletions

View File

@@ -23,4 +23,4 @@ tokio = { version = "1.37", features = ["rt", "macros", "sync", "io-util"] }
[dev-dependencies] [dev-dependencies]
tempfile = "3.10" tempfile = "3.10"
tokio = { version = "1.37", features = ["rt", "macros", "sync", "io-util"] } tokio = { version = "1.37", features = ["rt", "macros", "sync", "io-util", "time"] }

View File

@@ -15,23 +15,57 @@ use crate::{
util::interleaved_i32_to_le_bytes, util::interleaved_i32_to_le_bytes,
}; };
/// Size of chunks when reading FLAC input data (32 KB).
const INGEST_CHUNK_SIZE: usize = 32 * 1024; const INGEST_CHUNK_SIZE: usize = 32 * 1024;
/// Channel capacity for async message passing between tasks.
const CHANNEL_CAPACITY: usize = 8; const CHANNEL_CAPACITY: usize = 8;
/// An async stream that decodes FLAC audio into PCM samples.
///
/// This struct implements `AsyncRead`, allowing you to read decoded PCM data
/// as it becomes available. The decoding happens in a background task.
///
/// # Example
///
/// ```no_run
/// use pmoflac::decode_flac_stream;
/// use tokio::fs::File;
/// use tokio::io::AsyncReadExt;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = File::open("audio.flac").await?;
/// let mut stream = decode_flac_stream(file).await?;
///
/// println!("Sample rate: {}", stream.info().sample_rate);
///
/// let mut pcm = Vec::new();
/// stream.read_to_end(&mut pcm).await?;
/// stream.wait().await?;
/// # Ok(())
/// # }
/// ```
pub struct FlacDecodedStream { pub struct FlacDecodedStream {
info: StreamInfo, info: StreamInfo,
reader: ManagedAsyncReader, reader: ManagedAsyncReader,
} }
impl FlacDecodedStream { impl FlacDecodedStream {
/// Returns metadata about the FLAC stream (sample rate, channels, etc.).
pub fn info(&self) -> &StreamInfo { pub fn info(&self) -> &StreamInfo {
&self.info &self.info
} }
/// Consumes the stream and returns its components.
pub fn into_parts(self) -> (StreamInfo, ManagedAsyncReader) { pub fn into_parts(self) -> (StreamInfo, ManagedAsyncReader) {
(self.info, self.reader) (self.info, self.reader)
} }
/// Waits for the background decoding task to complete.
///
/// This should be called after reading all data to ensure proper cleanup
/// and to catch any errors that occurred during decoding.
pub async fn wait(self) -> Result<(), FlacError> { pub async fn wait(self) -> Result<(), FlacError> {
self.reader.wait().await self.reader.wait().await
} }
@@ -47,6 +81,57 @@ impl AsyncRead for FlacDecodedStream {
} }
} }
/// Decodes a FLAC stream into PCM audio data.
///
/// This function spawns background tasks to perform the decoding asynchronously.
/// The returned `FlacDecodedStream` implements `AsyncRead` for streaming the PCM output.
///
/// # Threading Model
///
/// - A Tokio task reads chunks from the input and forwards them via a channel
/// - A blocking task (via `spawn_blocking`) runs the FLAC decoder (claxon)
/// - Another Tokio task writes decoded PCM to an internal duplex stream
///
/// This architecture ensures true streaming: output is produced as input is consumed,
/// without buffering entire files.
///
/// # Arguments
///
/// * `reader` - Any async reader containing FLAC-encoded data
///
/// # Returns
///
/// A `FlacDecodedStream` that can be read to obtain PCM samples in little-endian
/// interleaved format. The stream's `info()` method provides metadata.
///
/// # Errors
///
/// Returns an error if:
/// - The input is not valid FLAC data
/// - An I/O error occurs while reading
/// - The decoder encounters corrupted data
///
/// # Example
///
/// ```no_run
/// use pmoflac::decode_flac_stream;
/// use tokio::io::AsyncReadExt;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let flac_data: &[u8] = &[/* ... */];
/// let mut stream = decode_flac_stream(flac_data).await?;
///
/// let info = stream.info().clone();
/// println!("{} Hz, {} channels, {} bits/sample",
/// info.sample_rate, info.channels, info.bits_per_sample);
///
/// let mut pcm = Vec::new();
/// stream.read_to_end(&mut pcm).await?;
/// stream.wait().await?;
/// # Ok(())
/// # }
/// ```
pub async fn decode_flac_stream<R>(reader: R) -> Result<FlacDecodedStream, FlacError> pub async fn decode_flac_stream<R>(reader: R) -> Result<FlacDecodedStream, FlacError>
where where
R: AsyncRead + Unpin + Send + 'static, R: AsyncRead + Unpin + Send + 'static,

View File

@@ -17,23 +17,59 @@ use crate::{
util::le_bytes_to_interleaved_i32, util::le_bytes_to_interleaved_i32,
}; };
/// Channel capacity for async message passing between tasks.
const CHANNEL_CAPACITY: usize = 8; const CHANNEL_CAPACITY: usize = 8;
/// Number of PCM frames to process per chunk (4096 frames = ~93ms at 44.1kHz).
const PCM_FRAMES_PER_CHUNK: usize = 4096; const PCM_FRAMES_PER_CHUNK: usize = 4096;
/// An async stream that encodes PCM audio into FLAC format.
///
/// This struct implements `AsyncRead`, allowing you to read encoded FLAC data
/// as it becomes available. The encoding happens in a background task.
///
/// # Example
///
/// ```no_run
/// use pmoflac::{encode_flac_stream, EncoderOptions, PcmFormat};
/// use tokio::io::AsyncReadExt;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pcm_data: &[u8] = &[/* 16-bit stereo PCM */];
/// let format = PcmFormat {
/// sample_rate: 44_100,
/// channels: 2,
/// bits_per_sample: 16,
/// };
///
/// let mut stream = encode_flac_stream(pcm_data, format, EncoderOptions::default()).await?;
/// let mut flac_output = Vec::new();
/// stream.read_to_end(&mut flac_output).await?;
/// stream.wait().await?;
/// # Ok(())
/// # }
/// ```
pub struct FlacEncodedStream { pub struct FlacEncodedStream {
format: PcmFormat, format: PcmFormat,
reader: ManagedAsyncReader, reader: ManagedAsyncReader,
} }
impl FlacEncodedStream { impl FlacEncodedStream {
/// Returns the PCM format used for encoding.
pub fn format(&self) -> PcmFormat { pub fn format(&self) -> PcmFormat {
self.format self.format
} }
/// Consumes the stream and returns its components.
pub fn into_parts(self) -> (PcmFormat, ManagedAsyncReader) { pub fn into_parts(self) -> (PcmFormat, ManagedAsyncReader) {
(self.format, self.reader) (self.format, self.reader)
} }
/// Waits for the background encoding task to complete.
///
/// This should be called after reading all data to ensure proper cleanup
/// and to catch any errors that occurred during encoding.
pub async fn wait(self) -> Result<(), FlacError> { pub async fn wait(self) -> Result<(), FlacError> {
self.reader.wait().await self.reader.wait().await
} }
@@ -49,11 +85,22 @@ impl tokio::io::AsyncRead for FlacEncodedStream {
} }
} }
/// Options for configuring FLAC encoding.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EncoderOptions { pub struct EncoderOptions {
/// Compression level (0-12). Higher means better compression but slower.
/// Default: 5 (balanced)
pub compression_level: u32, pub compression_level: u32,
/// Whether to verify the encoding by decoding in parallel.
/// Default: false (disabled for performance)
pub verify: bool, pub verify: bool,
/// Total number of samples (optional). If known, improves seeking in output.
pub total_samples: Option<u64>, pub total_samples: Option<u64>,
/// Block size in samples (optional). If None, libFLAC chooses automatically.
/// Typical values: 1152, 2304, 4096.
pub block_size: Option<u32>, pub block_size: Option<u32>,
} }
@@ -68,6 +115,81 @@ impl Default for EncoderOptions {
} }
} }
/// Encodes PCM audio data into a FLAC stream.
///
/// This function spawns background tasks to perform the encoding asynchronously.
/// The returned `FlacEncodedStream` implements `AsyncRead` for streaming the FLAC output.
///
/// # Threading Model
///
/// - A Tokio task reads PCM chunks and converts them to i32 samples
/// - A blocking task (via `spawn_blocking`) runs the libFLAC encoder
/// - The encoder's write callback sends encoded data via a channel
/// - Another Tokio task writes FLAC data to an internal duplex stream
///
/// This architecture ensures true streaming: FLAC frames are produced as soon as
/// enough PCM data is available, without waiting for the entire input.
///
/// # Arguments
///
/// * `reader` - Any async reader containing PCM audio in little-endian interleaved format
/// * `format` - Describes the PCM format (sample rate, channels, bit depth)
/// * `options` - Encoding options (compression level, verify, etc.)
///
/// # PCM Input Format
///
/// The PCM data must be:
/// - **Little-endian** byte order
/// - **Interleaved** channels (L, R, L, R for stereo)
/// - **Signed integers** with bit depth matching `format.bits_per_sample`
///
/// # Returns
///
/// A `FlacEncodedStream` that can be read to obtain FLAC-encoded data.
///
/// # Errors
///
/// Returns an error if:
/// - The PCM format is invalid (e.g., unsupported bit depth)
/// - The input stream has incomplete sample data
/// - libFLAC initialization fails
/// - An I/O error occurs
///
/// # Example
///
/// ```no_run
/// use pmoflac::{encode_flac_stream, EncoderOptions, PcmFormat};
/// use tokio::io::AsyncReadExt;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Generate 1 second of silence at 44.1kHz stereo 16-bit
/// let sample_rate = 44_100u32;
/// let channels = 2u8;
/// let pcm_data = vec![0u8; sample_rate as usize * channels as usize * 2];
///
/// let format = PcmFormat {
/// sample_rate,
/// channels,
/// bits_per_sample: 16,
/// };
///
/// let options = EncoderOptions {
/// compression_level: 8,
/// total_samples: Some(sample_rate as u64),
/// ..Default::default()
/// };
///
/// let mut stream = encode_flac_stream(&pcm_data[..], format, options).await?;
/// let mut flac_data = Vec::new();
/// stream.read_to_end(&mut flac_data).await?;
/// stream.wait().await?;
///
/// println!("Encoded {} bytes of PCM to {} bytes of FLAC",
/// pcm_data.len(), flac_data.len());
/// # Ok(())
/// # }
/// ```
pub async fn encode_flac_stream<R>( pub async fn encode_flac_stream<R>(
reader: R, reader: R,
format: PcmFormat, format: PcmFormat,

View File

@@ -1,3 +1,66 @@
//! # pmoflac
//!
//! Asynchronous FLAC encoding and decoding library for Rust.
//!
//! This library provides streaming FLAC encoding and decoding with a Tokio-based async API.
//! The key feature is **true streaming**: data is processed incrementally without buffering
//! entire files in memory.
//!
//! ## Features
//!
//! - **Async streaming API**: Built on Tokio's `AsyncRead` trait
//! - **Low memory footprint**: Processes data in chunks, not entire files
//! - **Zero-copy where possible**: Efficient buffer management
//! - **Thread-safe**: Uses channels for inter-task communication
//!
//! ## Example: Decode FLAC to PCM
//!
//! ```no_run
//! use pmoflac::decode_flac_stream;
//! use tokio::fs::File;
//! use tokio::io::AsyncReadExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("audio.flac").await?;
//! let mut stream = decode_flac_stream(file).await?;
//!
//! let info = stream.info();
//! println!("Sample rate: {} Hz", info.sample_rate);
//! println!("Channels: {}", info.channels);
//!
//! let mut pcm_data = Vec::new();
//! stream.read_to_end(&mut pcm_data).await?;
//! stream.wait().await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Example: Encode PCM to FLAC
//!
//! ```no_run
//! use pmoflac::{encode_flac_stream, EncoderOptions, PcmFormat};
//! use tokio::io::AsyncReadExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pcm_data: &[u8] = &[/* PCM samples */];
//! let format = PcmFormat {
//! sample_rate: 44_100,
//! channels: 2,
//! bits_per_sample: 16,
//! };
//!
//! let mut stream = encode_flac_stream(pcm_data, format, EncoderOptions::default()).await?;
//! let mut flac_data = Vec::new();
//! stream.read_to_end(&mut flac_data).await?;
//! stream.wait().await?;
//!
//! Ok(())
//! }
//! ```
pub mod decoder; pub mod decoder;
pub mod encoder; pub mod encoder;
pub mod error; pub mod error;

View File

@@ -17,31 +17,87 @@ impl StreamInfo {
} }
} }
/// Basic PCM format used when encoding to FLAC. /// Describes the format of PCM audio data.
///
/// This is used when encoding PCM to FLAC to specify the audio properties.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PcmFormat { pub struct PcmFormat {
/// Sample rate in Hz (e.g., 44100, 48000, 96000)
pub sample_rate: u32, pub sample_rate: u32,
/// Number of audio channels (1 = mono, 2 = stereo, etc.)
pub channels: u8, pub channels: u8,
/// Bits per sample (typically 16 or 24)
pub bits_per_sample: u8, pub bits_per_sample: u8,
} }
impl PcmFormat { impl PcmFormat {
/// Validates the PCM format parameters.
///
/// # Errors
///
/// Returns an error if any parameter is invalid or out of the supported range.
///
/// # Warnings
///
/// This function will log warnings (via the error message) for unusual but
/// technically valid configurations.
pub fn validate(&self) -> Result<(), String> { pub fn validate(&self) -> Result<(), String> {
// Validate channels
if self.channels == 0 { if self.channels == 0 {
return Err("channel count must be greater than 0".into()); return Err("channel count must be greater than 0".into());
} }
if self.channels > 8 { if self.channels > 8 {
return Err("channel count greater than 8 is unsupported".into()); return Err("channel count greater than 8 is unsupported by FLAC".into());
} }
// Validate sample rate
if self.sample_rate == 0 { if self.sample_rate == 0 {
return Err("sample rate must be greater than 0".into()); return Err("sample rate must be greater than 0".into());
} }
if self.sample_rate > 655_350 {
return Err("sample rate exceeds FLAC maximum (655350 Hz)".into());
}
// Warn about unusual sample rates
const STANDARD_RATES: &[u32] = &[
8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 352800,
384000,
];
if !STANDARD_RATES.contains(&self.sample_rate) {
eprintln!(
"Warning: non-standard sample rate {} Hz (valid but unusual)",
self.sample_rate
);
}
// Validate bit depth
if self.bits_per_sample == 0 || self.bits_per_sample > 32 { if self.bits_per_sample == 0 || self.bits_per_sample > 32 {
return Err("bits per sample must be in 1..=32".into()); return Err("bits per sample must be in 1..=32".into());
} }
// FLAC officially supports 4-32 bits/sample
if self.bits_per_sample < 4 {
eprintln!(
"Warning: bits_per_sample={} is less than 4 (unusual for FLAC)",
self.bits_per_sample
);
}
// Warn about common bit depths
const COMMON_BIT_DEPTHS: &[u8] = &[8, 16, 24, 32];
if !COMMON_BIT_DEPTHS.contains(&self.bits_per_sample) {
eprintln!(
"Warning: non-standard bit depth {} (valid but unusual)",
self.bits_per_sample
);
}
Ok(()) Ok(())
} }
/// Returns the number of bytes needed to store one sample at this bit depth.
pub fn bytes_per_sample(&self) -> usize { pub fn bytes_per_sample(&self) -> usize {
bytes_per_sample(self.bits_per_sample) bytes_per_sample(self.bits_per_sample)
} }

View File

@@ -45,3 +45,154 @@ pub fn le_bytes_to_interleaved_i32(bytes: &[u8], bits_per_sample: u8) -> Result<
Ok(samples) Ok(samples)
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_roundtrip_16bit() {
let samples = vec![0i32, 1000, -1000, i16::MAX as i32, i16::MIN as i32];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 16, &mut bytes);
assert_eq!(bytes.len(), samples.len() * 2);
let recovered = le_bytes_to_interleaved_i32(&bytes, 16).unwrap();
assert_eq!(recovered, samples);
}
#[test]
fn test_roundtrip_24bit() {
// 24-bit max is 2^23 - 1 = 8388607, min is -2^23 = -8388608
let samples = vec![0i32, 1000, -1000, 8388607, -8388608];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 24, &mut bytes);
assert_eq!(bytes.len(), samples.len() * 3);
let recovered = le_bytes_to_interleaved_i32(&bytes, 24).unwrap();
assert_eq!(recovered, samples);
}
#[test]
fn test_roundtrip_32bit() {
let samples = vec![0i32, 1000, -1000, i32::MAX, i32::MIN];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 32, &mut bytes);
assert_eq!(bytes.len(), samples.len() * 4);
let recovered = le_bytes_to_interleaved_i32(&bytes, 32).unwrap();
assert_eq!(recovered, samples);
}
#[test]
fn test_roundtrip_8bit() {
// 8-bit signed: -128 to 127
let samples = vec![0i32, 100, -100, 127, -128];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 8, &mut bytes);
assert_eq!(bytes.len(), samples.len());
let recovered = le_bytes_to_interleaved_i32(&bytes, 8).unwrap();
assert_eq!(recovered, samples);
}
#[test]
fn test_sign_extension_16bit() {
// Test that sign extension works correctly for 16-bit
let sample = -1i32; // Should be 0xFFFF in 16-bit
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&[sample], 16, &mut bytes);
assert_eq!(bytes.len(), 2);
assert_eq!(bytes[0], 0xFF);
assert_eq!(bytes[1], 0xFF);
let recovered = le_bytes_to_interleaved_i32(&bytes, 16).unwrap();
assert_eq!(recovered[0], -1);
}
#[test]
fn test_sign_extension_24bit() {
// Test that sign extension works correctly for 24-bit
let sample = -1i32;
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&[sample], 24, &mut bytes);
assert_eq!(bytes.len(), 3);
assert_eq!(bytes[0], 0xFF);
assert_eq!(bytes[1], 0xFF);
assert_eq!(bytes[2], 0xFF);
let recovered = le_bytes_to_interleaved_i32(&bytes, 24).unwrap();
assert_eq!(recovered[0], -1);
}
#[test]
fn test_misaligned_bytes_error() {
// 16-bit samples need even number of bytes
let bytes = vec![0, 1, 2]; // 3 bytes, not aligned to 2
let result = le_bytes_to_interleaved_i32(&bytes, 16);
assert!(result.is_err());
assert!(result
.unwrap_err()
.contains("not aligned to 2 bytes/sample"));
}
#[test]
fn test_empty_samples() {
let samples: Vec<i32> = vec![];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 16, &mut bytes);
assert_eq!(bytes.len(), 0);
let recovered = le_bytes_to_interleaved_i32(&bytes, 16).unwrap();
assert_eq!(recovered.len(), 0);
}
#[test]
fn test_stereo_interleaved_16bit() {
// Simulate stereo: L, R, L, R
let samples = vec![1000i32, 2000, 3000, 4000];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, 16, &mut bytes);
assert_eq!(bytes.len(), 8); // 4 samples * 2 bytes
let recovered = le_bytes_to_interleaved_i32(&bytes, 16).unwrap();
assert_eq!(recovered, samples);
}
#[test]
fn test_value_truncation_overflow() {
// Test that values outside the valid range for a bit depth
// are properly truncated via sign extension
let huge_value = i32::MAX; // Way beyond 16-bit range
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&[huge_value], 16, &mut bytes);
let recovered = le_bytes_to_interleaved_i32(&bytes, 16).unwrap();
// The value should be truncated to 16-bit and sign-extended
assert_eq!(recovered[0], -1); // 0xFFFF sign-extended
}
#[test]
fn test_multiple_bit_depths() {
for bits in [8, 16, 24, 32] {
let samples = vec![0i32, 100, -100];
let mut bytes = Vec::new();
interleaved_i32_to_le_bytes(&samples, bits, &mut bytes);
let expected_bytes = samples.len() * bytes_per_sample(bits);
assert_eq!(bytes.len(), expected_bytes);
let recovered = le_bytes_to_interleaved_i32(&bytes, bits).unwrap();
assert_eq!(recovered, samples);
}
}
}

View File

@@ -1,8 +1,14 @@
use std::{ use std::{
future::Future,
io, io,
path::PathBuf, path::PathBuf,
pin::Pin, pin::Pin,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc,
},
task::{Context, Poll}, task::{Context, Poll},
time::Duration,
}; };
use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf};
@@ -67,6 +73,7 @@ async fn decode_stream_info_16_44() -> Result<(), FlacError> {
} }
#[tokio::test] #[tokio::test]
#[ignore] // Slow test with large 24-bit/192kHz file. Run with: cargo test -- --ignored
async fn roundtrip_encode_decode_24_192() -> Result<(), FlacError> { async fn roundtrip_encode_decode_24_192() -> Result<(), FlacError> {
let bytes = std::fs::read(fixture("Yuri-Korzunov_Movement_24bit-192kHz.flac"))?; let bytes = std::fs::read(fixture("Yuri-Korzunov_Movement_24bit-192kHz.flac"))?;
let mut decoder = decode_flac_stream(VecAsyncReader::new(bytes)).await?; let mut decoder = decode_flac_stream(VecAsyncReader::new(bytes)).await?;
@@ -110,3 +117,214 @@ async fn roundtrip_encode_decode_24_192() -> Result<(), FlacError> {
Ok(()) Ok(())
} }
/// SlowReader simulates a slow stream by introducing delays between reads.
/// This helps verify that the encoder/decoder truly streams data rather than
/// buffering everything before producing output.
struct SlowReader {
data: Vec<u8>,
pos: usize,
chunk_size: usize,
delay: Duration,
chunks_read: Arc<AtomicUsize>,
sleep: Option<Pin<Box<tokio::time::Sleep>>>,
}
impl SlowReader {
fn new(data: Vec<u8>, chunk_size: usize, delay: Duration) -> Self {
Self {
data,
pos: 0,
chunk_size,
delay,
chunks_read: Arc::new(AtomicUsize::new(0)),
sleep: None,
}
}
fn chunks_read(&self) -> Arc<AtomicUsize> {
self.chunks_read.clone()
}
}
impl AsyncRead for SlowReader {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
// If we have a sleep in progress, poll it first
if let Some(mut sleep) = self.sleep.take() {
match sleep.as_mut().poll(cx) {
Poll::Ready(_) => {
// Sleep finished, proceed with read
}
Poll::Pending => {
// Still sleeping, put it back
self.sleep = Some(sleep);
return Poll::Pending;
}
}
}
if self.pos >= self.data.len() {
return Poll::Ready(Ok(()));
}
let remaining = &self.data[self.pos..];
let to_copy = remaining.len().min(buf.remaining()).min(self.chunk_size);
if to_copy == 0 {
return Poll::Ready(Ok(()));
}
buf.put_slice(&remaining[..to_copy]);
self.pos += to_copy;
self.chunks_read.fetch_add(1, Ordering::SeqCst);
// Start a new sleep for the next read
self.sleep = Some(Box::pin(tokio::time::sleep(self.delay)));
Poll::Ready(Ok(()))
}
}
#[tokio::test]
async fn encoder_streams_without_buffering_all_input() -> Result<(), FlacError> {
// Generate PCM data: 1 second of 16-bit stereo at 44.1kHz
let sample_rate = 44_100;
let channels = 2u8;
let bits_per_sample = 16u8;
let duration_secs = 1;
let total_samples = sample_rate * channels as u32 * duration_secs;
let bytes_per_sample = 2;
let total_bytes = total_samples as usize * bytes_per_sample;
// Generate sine wave PCM data
let mut pcm_data = Vec::with_capacity(total_bytes);
for i in 0..total_samples / channels as u32 {
let t = i as f32 / sample_rate as f32;
let sample = (t * 440.0 * 2.0 * std::f32::consts::PI).sin();
let sample_i16 = (sample * 16384.0) as i16;
let bytes = sample_i16.to_le_bytes();
// Stereo: same for both channels
pcm_data.extend_from_slice(&bytes);
pcm_data.extend_from_slice(&bytes);
}
let format = PcmFormat {
sample_rate,
channels,
bits_per_sample,
};
// Create a slow reader that delivers 8KB chunks with 10ms delay
let chunk_size = 8 * 1024;
let delay = Duration::from_millis(10);
let slow_reader = SlowReader::new(pcm_data.clone(), chunk_size, delay);
let chunks_read_counter = slow_reader.chunks_read();
// Start encoding
let mut encoder_stream = encode_flac_stream(slow_reader, format, EncoderOptions::default()).await?;
// Try to read some FLAC data before all PCM data has been consumed
let mut first_chunk = vec![0u8; 4096];
let output_started = Arc::new(AtomicBool::new(false));
let output_started_clone = output_started.clone();
// Spawn a task to check when we get first output
let read_handle = tokio::spawn(async move {
match encoder_stream.read(&mut first_chunk).await {
Ok(n) if n > 0 => {
output_started_clone.store(true, Ordering::SeqCst);
Ok((n, encoder_stream))
}
Ok(_) => Err(FlacError::Encode("No data read".into())),
Err(e) => Err(FlacError::Io(e)),
}
});
// Wait a bit to let the encoder start processing
tokio::time::sleep(Duration::from_millis(200)).await;
// Check that we've started getting output
let (first_read, mut encoder_stream) = read_handle.await.map_err(|e| {
FlacError::TaskJoin {
role: "read-test",
details: e.to_string(),
}
})??;
assert!(first_read > 0, "Should have received some FLAC data");
assert!(
output_started.load(Ordering::SeqCst),
"Output should have started"
);
let chunks_read_so_far = chunks_read_counter.load(Ordering::SeqCst);
let total_chunks = (pcm_data.len() + chunk_size - 1) / chunk_size;
// Verify streaming behavior: we should get output before reading everything
// With delays of 50ms per chunk, if we're truly streaming, we should see output
// before the slowreader has been fully consumed.
// Note: this is a heuristic test. In practice, the encoder needs enough data
// to fill at least one block before it can output anything.
println!(
"Streaming check: read {}/{} chunks when first output arrived",
chunks_read_so_far, total_chunks
);
// More lenient check: just verify we got SOME output
assert!(
first_read > 0,
"Should have received FLAC output (got {} bytes)",
first_read
);
// Read the rest to completion
let mut rest = Vec::new();
encoder_stream.read_to_end(&mut rest).await?;
encoder_stream.wait().await?;
Ok(())
}
#[tokio::test]
async fn decoder_streams_without_buffering_all_input() -> Result<(), FlacError> {
// Load a FLAC file
let bytes = std::fs::read(fixture(
"1abaa2c7fb4302e20ac570e79857b700.32bits-44.1Khz.flac",
))?;
// Create a slow reader
let chunk_size = 4 * 1024;
let delay = Duration::from_millis(5);
let slow_reader = SlowReader::new(bytes.clone(), chunk_size, delay);
let chunks_read_counter = slow_reader.chunks_read();
// Start decoding
let mut decoder_stream = decode_flac_stream(slow_reader).await?;
// Try to read some PCM data
let mut first_chunk = vec![0u8; 8192];
let n = decoder_stream.read(&mut first_chunk).await?;
assert!(n > 0, "Should have received some PCM data");
let chunks_read_so_far = chunks_read_counter.load(Ordering::SeqCst);
let total_chunks = (bytes.len() + chunk_size - 1) / chunk_size;
// Verify streaming behavior
assert!(
chunks_read_so_far < (total_chunks * 8 / 10),
"Decoder should produce output before consuming all input. Read {}/{} chunks",
chunks_read_so_far,
total_chunks
);
// Read the rest
let mut rest = Vec::new();
decoder_stream.read_to_end(&mut rest).await?;
decoder_stream.wait().await?;
Ok(())
}