Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store sample rate of block #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>,
}

impl Block {
fn new(time: u64, bs: u32, buffer: Vec<i32>) -> Block {
fn new(time: u64, bs: u32, sr: u32, buffer: Vec<i32>) -> Block {
Block {
first_sample_number: time,
block_size: bs,
sample_rate: sr,
channels: buffer.len() as u32 / bs,
buffer: buffer,
}
Expand All @@ -426,6 +429,7 @@ impl Block {
first_sample_number: 0,
block_size: 0,
channels: 0,
sample_rate: 0,
buffer: Vec::with_capacity(0),
}
}
Expand Down Expand Up @@ -504,6 +508,12 @@ impl Block {
return self.buffer;
}

/// Returns the sample rate of this block.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns the sample rate of this block.
/// Returns the sample rate of this block in Hz.

#[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,
Expand Down Expand Up @@ -773,7 +783,7 @@ impl<R: ReadBytes> FrameReader<R> {
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap_or_default would default to 0 when the header does not specify a sample rate. Instead, we need to get the value from the streaminfo:

0b0000 => sample_rate = None, // 0000 means 'get from streaminfo block'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem like streaminfo is available from this function's scope?


Ok(Some(block))
}
Expand Down