Skip to content

Commit

Permalink
Merge pull request #100 from Manishearth/timer
Browse files Browse the repository at this point in the history
Various timing fixes
  • Loading branch information
Manishearth authored Jul 30, 2018
2 parents 6fed7a2 + bdd033e commit 06774e4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion audio/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl Div<f64> for Tick {

impl Tick {
pub fn from_time(time: f64, rate: f32) -> Tick {
Tick((time * rate as f64) as u64)
Tick((0.5 + time * rate as f64).floor() as u64)
}

pub fn advance(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions audio/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct RealTimeAudioContextOptions {
impl Default for RealTimeAudioContextOptions {
fn default() -> Self {
Self {
sample_rate: 48000.,
sample_rate: 44100.,
latency_hint: LatencyCategory::Interactive,
}
}
Expand All @@ -68,7 +68,7 @@ impl Default for OfflineAudioContextOptions {
Self {
channels: 1,
length: 0,
sample_rate: 48000.,
sample_rate: 44100.,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion audio/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct AudioDecoderOptions {
impl Default for AudioDecoderOptions {
fn default() -> Self {
AudioDecoderOptions {
sample_rate: 48000.,
sample_rate: 44100.,
channels: 1,
}
}
Expand Down
51 changes: 27 additions & 24 deletions audio/src/offline_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,40 @@ impl AudioSink for OfflineAudioSink {

fn has_enough_data(&self) -> bool {
self.has_enough_data.get()
|| (self.rendered_blocks.get() >= (self.length / FRAMES_PER_BLOCK_USIZE))
|| (self.rendered_blocks.get() * FRAMES_PER_BLOCK_USIZE >= self.length)
}

fn push_data(&self, mut chunk: Chunk) -> Result<(), ()> {
{
let offset = self.rendered_blocks.get() * FRAMES_PER_BLOCK_USIZE;
let mut buffer = self.buffer.borrow_mut();
if buffer.is_none() {
*buffer = Some(vec![0.; self.channel_count * self.length]);
}
if chunk.len() == 0 {
chunk.blocks.push(Default::default());
}
if chunk.blocks[0].is_empty() {
chunk.blocks[0].explicit_silence();
}
if let Some(ref mut buffer) = *buffer {
for channel_number in 0..self.channel_count {
let channel_offset = offset + (channel_number * self.length);
let mut channel_data =
&mut buffer[channel_offset..channel_offset + FRAMES_PER_BLOCK_USIZE];
channel_data.copy_from_slice(chunk.blocks[0].data_chan(channel_number as u8));
}
};
self.rendered_blocks.update(|blocks| blocks + 1);
let offset = self.rendered_blocks.get() * FRAMES_PER_BLOCK_USIZE;
let (last, copy_len) = if self.length - offset <= FRAMES_PER_BLOCK_USIZE {
(true, self.length - offset)
} else {
(false, FRAMES_PER_BLOCK_USIZE)
};
let mut buffer = self.buffer.borrow_mut();
if buffer.is_none() {
*buffer = Some(vec![0.; self.channel_count * self.length]);
}
if chunk.len() == 0 {
chunk.blocks.push(Default::default());
}
if chunk.blocks[0].is_empty() {
chunk.blocks[0].explicit_silence();
}
if let Some(ref mut buffer) = *buffer {
for channel_number in 0..self.channel_count {
let channel_offset = offset + (channel_number * self.length);
let mut channel_data =
&mut buffer[channel_offset..channel_offset + copy_len];
channel_data.copy_from_slice(&chunk.blocks[0].data_chan(channel_number as u8)[0..copy_len]);
}
};
self.rendered_blocks.update(|blocks| blocks + 1);

if self.rendered_blocks.get() >= (self.length / FRAMES_PER_BLOCK_USIZE) {
if last {
if let Some(callback) = self.eos_callback.borrow_mut().take() {
let processed_audio =
ProcessedAudio(self.buffer.borrow_mut().take().unwrap().into_boxed_slice());
ProcessedAudio(buffer.take().unwrap().into_boxed_slice());
callback(Box::new(processed_audio));
}
}
Expand Down

0 comments on commit 06774e4

Please sign in to comment.