From fbd47fa3459d47d05a4a62d68d7440935d4ce909 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 10 Jun 2024 20:21:34 +0200 Subject: [PATCH] Store sample rate of block FLAC can have a different sample rate for every frame, therefore it should be stored in `Block` and have a function that exposes the value of it. Ref: https://github.com/ietf-wg-cellar/flac-test-files/blob/main/uncommon/01%20-%20changing%20samplerate.flac --- src/frame.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/frame.rs b/src/frame.rs index 9c06852..1e6d476 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -406,15 +406,18 @@ pub struct Block { block_size: u32, /// The number of channels in the block. channels: u32, + /// The sample rate of this block. + sample_rate: u32, /// The decoded samples, the channels stored consecutively. buffer: Vec, } impl Block { - fn new(time: u64, bs: u32, buffer: Vec) -> Block { + fn new(time: u64, bs: u32, sr: u32, buffer: Vec) -> Block { Block { first_sample_number: time, block_size: bs, + sample_rate: sr, channels: buffer.len() as u32 / bs, buffer: buffer, } @@ -426,6 +429,7 @@ impl Block { first_sample_number: 0, block_size: 0, channels: 0, + sample_rate: 0, buffer: Vec::with_capacity(0), } } @@ -504,6 +508,12 @@ impl Block { return self.buffer; } + /// Returns the sample rate of this block. + #[inline(always)] + pub fn sample_rate(&self) -> u32 { + self.sample_rate + } + /// Returns an iterator that produces left and right channel samples. /// /// This iterator can be more efficient than requesting a sample directly, @@ -773,7 +783,7 @@ impl FrameReader { BlockTime::SampleNumber(snr) => snr, }; - let block = Block::new(time, header.block_size as u32, buffer); + let block = Block::new(time, header.block_size as u32, header.sample_rate.unwrap_or_default(), buffer); Ok(Some(block)) }