Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
use firewheel::{FirewheelConfig, FirewheelContext};
use std::sync::mpsc;
use super::{AudioThreadState, LocalStore};
/// A thread-safe wrapper around the underlying Firewheel audio context.
#[derive(Debug)]
pub struct InnerContext(mpsc::Sender<ThreadLocalCall>);
type ThreadLocalCall = Box<dyn FnOnce(&mut AudioThreadState) + Send + 'static>;
impl InnerContext {
// Spawn the audio process and control thread.
#[inline(always)]
pub fn new(settings: FirewheelConfig) -> Self {
let (bev_to_audio_tx, bev_to_audio_rx) = mpsc::channel::<ThreadLocalCall>();
std::thread::spawn(move || {
let mut state = AudioThreadState::new(settings);
while let Ok(func) = bev_to_audio_rx.recv() {
(func)(&mut state);
}
});
InnerContext(bev_to_audio_tx)
}
// Send `f` to the underlying control thread to operate on the audio context.
//
// This call will block until `f` returns.
//
// This method takes a mutable reference to `self` to prevent trivial deadlocks.
// This API can't completely prevent them in the general case: calling
// [AudioContext::with] within itself will deadlock.
//
// This API is based on [this PR](https://github.com/bevyengine/bevy/pull/9122).
#[inline(always)]
pub fn with_store<F, O>(&mut self, f: F) -> O
where
F: FnOnce(&mut FirewheelContext, &mut LocalStore) -> O + Send,
O: Send + 'static,
{
let (send, receive) = mpsc::sync_channel(1);
let func: Box<dyn FnOnce(&mut AudioThreadState) + Send> = Box::new(move |state| {
let AudioThreadState { context, store } = state;
let result = f(context, store);
send.send(result).unwrap();
});
// # SAFETY
//
// This thread will block until the function returns,
// so we can pretend it has a static lifetime.
let func = unsafe {
core::mem::transmute::<
Box<dyn FnOnce(&mut AudioThreadState) + Send>,
Box<dyn FnOnce(&mut AudioThreadState) + Send + 'static>,
>(func)
};
// If the audio communication thread fails to send or receive
// messages, like in the event of a panic, a panic will be
// propagated to the calling thread .
self.0.send(func).unwrap();
receive.recv().unwrap()
}
}