Browse Source

Move callbacks and box them inside `register_` method

fixes
Andreas Linz 5 years ago
parent
commit
d015abe7dd
4 changed files with 17 additions and 17 deletions
  1. +6
    -6
      examples/sine.rs
  2. +2
    -2
      src/lib.rs
  3. +6
    -6
      src/stream.rs
  4. +3
    -3
      tests/stream.rs

+ 6
- 6
examples/sine.rs View File

@ -30,7 +30,7 @@ fn main() {
println!("Output format: {}", out.format().unwrap());
// register callbacks
out.register_write_callback(Box::new(|out: rsoundio::OutStream,
out.register_write_callback(|out: rsoundio::OutStream,
min_frame_count: u32,
max_frame_count: u32| {
let l: Vec<f32> = samples.iter()
@ -43,13 +43,13 @@ fn main() {
let r = l.clone();
let frames = vec![l, r];
out.write_stream_f32(min_frame_count, &frames).unwrap();
}));
out.register_underflow_callback(Box::new(|out: rsoundio::OutStream| {
});
out.register_underflow_callback(|out: rsoundio::OutStream| {
println!("Underflow in {} occured!", out.name().unwrap())
}));
out.register_error_callback(Box::new(|out: rsoundio::OutStream, err: rsoundio::SioError| {
});
out.register_error_callback(|out: rsoundio::OutStream, err: rsoundio::SioError| {
println!("{} error: {}", out.name().unwrap(), err)
}));
});
// open output stream
out.open().unwrap();

+ 2
- 2
src/lib.rs View File

@ -13,12 +13,12 @@
//! let dev = sio.default_output_device().unwrap();
//! let mut out = dev.create_outstream().unwrap();
//! // register write_callback
//! out.register_write_callback(Box::new(|out: rsoundio::OutStream, min_frame_count: u32,
//! out.register_write_callback(|out: rsoundio::OutStream, min_frame_count: u32,
//! max_frame_count: u32| {
//! let frames = vec![vec![], vec![]];
//! // frames must contain audio data for each channel
//! out.write_stream_f32(min_frame_count, &frames).unwrap();
//! }));
//! });
//! out.open().unwrap();
//! // start the audio stream and wait for events
//! // out.start().unwrap();

+ 6
- 6
src/stream.rs View File

@ -174,11 +174,11 @@ impl<'a> OutStream<'a> {
/// for a long time. This includes all I/O functions (disk, TTY, network),
/// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
/// pthread_join, pthread_cond_wait, etc.
pub fn register_write_callback<W>(&mut self, callback: Box<W>)
pub fn register_write_callback<W>(&mut self, callback: W)
where W: FnMut(OutStream, u32, u32) + 'a
{
// stored box reference to callback closure
self.callbacks.write = Some(callback);
self.callbacks.write = Some(Box::new(callback));
unsafe {
// register wrapper for write_callback
(*self.stream).write_callback = Some(write_wrapper::<W>);
@ -192,10 +192,10 @@ impl<'a> OutStream<'a> {
/// This *optional* callback happens when the sound device runs out of buffered audio data to play.
/// After this occurs, the outstream waits until the buffer is full to resume playback.
/// This is called from the `OutStream::write_callback` thread context.
pub fn register_underflow_callback<U>(&mut self, callback: Box<U>)
pub fn register_underflow_callback<U>(&mut self, callback: U)
where U: FnMut(OutStream) + 'a
{
self.callbacks.underflow = Some(callback);
self.callbacks.underflow = Some(Box::new(callback));
unsafe {
// register wrapper for write_callback
(*self.stream).underflow_callback = Some(underflow_wrapper::<U>);
@ -211,10 +211,10 @@ impl<'a> OutStream<'a> {
/// If you do not supply `error_callback`, the default callback will print
/// a message to stderr and then call `abort`.
/// This is called from the `OutStream::write_callback` thread context.
pub fn register_error_callback<E>(&mut self, callback: Box<E>)
pub fn register_error_callback<E>(&mut self, callback: E)
where E: FnMut(OutStream, ffi::enums::SioError) + 'a
{
self.callbacks.error = Some(callback);
self.callbacks.error = Some(Box::new(callback));
unsafe {
// register wrapper for write_callback
(*self.stream).error_callback = Some(error_wrapper::<E>);

+ 3
- 3
tests/stream.rs View File

@ -38,11 +38,11 @@ fn test_outstream() {
assert!(frames_written > 0);
assert!(out.latency().is_ok());
};
stream.register_write_callback(Box::new(cb));
stream.register_write_callback(cb);
let ucb = |_: rsoundio::OutStream| println!("Underflow!");
stream.register_underflow_callback(Box::new(ucb));
stream.register_underflow_callback(ucb);
let ecb = |_: rsoundio::OutStream, err: rsoundio::SioError| println!("Error: {}", err);
stream.register_error_callback(Box::new(ecb));
stream.register_error_callback(ecb);
stream.start().unwrap();
thread::sleep(Duration::new(1, 0));
assert!(stream.pause().is_none());

Loading…
Cancel
Save