Guest User

Untitled

a guest
Sep 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. pub struct Server {
  2. pub(crate) handle: libaudioverse_sys::LavHandle,
  3. block_callback: Option<Box<Box<'static + FnMut(f64)>>>,
  4.  
  5. impl Server {
  6. /// Set the callback to be called per block of synthesized audio
  7. pub fn set_block_callback<F>(&mut self, callback: F) -> Result<()>
  8. where
  9. F: 'static + FnMut(f64),
  10. {
  11. let cb: Box<Box<'static + FnMut(f64)>> = Box::new(Box::new(callback));
  12. let cb_ptr = Box::into_raw(cb);
  13. self.block_callback = Some(unsafe { Box::from_raw(cb_ptr) });
  14. check(unsafe {
  15. libaudioverse_sys::Lav_serverSetBlockCallback(
  16. self.handle,
  17. Some(callback_handler),
  18. cb_ptr as *mut _,
  19. )
  20. })?;
  21. Ok(())
  22. }
  23.  
  24. // actual usage:
  25.  
  26. let server = ...; // construct a server
  27. ...
  28. let environment = ...;
  29. let source = SourceNode::new(&server, &environment);
  30.  
  31. let cb = |_: f64| {
  32. let (x, y) = ...; // compute the next position of the source, referencing other variables created after server is constructed
  33. source.position().set(x, y);
  34. };
  35.  
  36. // trying to set this callback leads to lifetime errors
  37. server.set_block_callback(cb)
Add Comment
Please, Sign In to add comment