Skip to content

Commit

Permalink
Add a buffer callback to Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
zeozeozeo committed Dec 16, 2023
1 parent 59a7bb4 commit e3bac25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ impl Backend {
}
}
}
renderer_moved.guard().on_buffer(data);
},
move |err| {
// we got an error on stream, push it to the error queue
Expand Down
16 changes: 16 additions & 0 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ pub trait Renderer: Clone + Send + 'static {
///
/// Note: you can use a [`crate::Resampler`] to resample audio data.
fn next_frame(&mut self, sample_rate: u32) -> Frame;

/// This gets called when an audio buffer is done processing.
fn on_buffer<T>(&mut self, _buffer: &mut [T])
where
T: cpal::SizedSample + cpal::FromSample<f32>,
{
}
}

/// Default audio renderer.
#[derive(Debug, Clone, Default)]
pub struct DefaultRenderer {
/// All playing sounds.
pub sounds: Vec<SoundHandle>,
/// The last buffer size given by the [cpal] backend.
pub last_buffer_size: usize,
}

impl DefaultRenderer {
Expand Down Expand Up @@ -50,6 +59,13 @@ impl Renderer for DefaultRenderer {

out
}

fn on_buffer<T>(&mut self, buffer: &mut [T])
where
T: cpal::SizedSample + cpal::FromSample<f32>,
{
self.last_buffer_size = buffer.len();
}
}

/// Wraps [`Renderer`] so it can be shared between threads.
Expand Down

0 comments on commit e3bac25

Please sign in to comment.