1
0
Fork 0

Add an audio thread for the standalone target

This commit is contained in:
Robbert van der Helm 2022-04-22 20:58:24 +02:00
parent 81e56dd018
commit 8cb763246b

View file

@ -1,8 +1,11 @@
use atomic_refcell::AtomicRefCell;
use baseview::{EventStatus, Window, WindowHandler, WindowOpenOptions}; use baseview::{EventStatus, Window, WindowHandler, WindowOpenOptions};
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use raw_window_handle::HasRawWindowHandle; use raw_window_handle::HasRawWindowHandle;
use std::any::Any; use std::any::Any;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::thread;
use super::backend::Backend; use super::backend::Backend;
use super::context::{WrapperGuiContext, WrapperProcessContext}; use super::context::{WrapperGuiContext, WrapperProcessContext};
@ -36,7 +39,7 @@ pub struct WrapperConfig {
} }
pub struct Wrapper<P: Plugin, B: Backend> { pub struct Wrapper<P: Plugin, B: Backend> {
backend: B, backend: AtomicRefCell<B>,
/// The wrapped plugin instance. /// The wrapped plugin instance.
plugin: RwLock<P>, plugin: RwLock<P>,
@ -98,7 +101,7 @@ impl<P: Plugin, B: Backend> Wrapper<P, B> {
let editor = plugin.editor().map(Arc::from); let editor = plugin.editor().map(Arc::from);
let wrapper = Arc::new(Wrapper { let wrapper = Arc::new(Wrapper {
backend, backend: AtomicRefCell::new(backend),
plugin: RwLock::new(plugin), plugin: RwLock::new(plugin),
editor, editor,
@ -140,7 +143,24 @@ impl<P: Plugin, B: Backend> Wrapper<P, B> {
/// Will return an error if the plugin threw an error during audio processing or if the editor /// Will return an error if the plugin threw an error during audio processing or if the editor
/// could not be opened. /// could not be opened.
pub fn run(self: Arc<Self>) -> Result<(), WrapperError> { pub fn run(self: Arc<Self>) -> Result<(), WrapperError> {
// TODO: Do IO things, kinda important // We'll spawn a separate thread to handle IO and to process audio. This audio thread should
// terminate together with this function.
let terminate_audio_thread = Arc::new(AtomicBool::new(false));
let audio_thread = {
let terminate_audio_thread = terminate_audio_thread.clone();
let this = self.clone();
thread::spawn(move || {
this.backend.borrow_mut().run(move |buffer| {
if terminate_audio_thread.load(Ordering::SeqCst) {
return false;
}
// TODO: Process audio
true
});
})
};
match self.editor.clone() { match self.editor.clone() {
Some(editor) => { Some(editor) => {
@ -195,6 +215,9 @@ impl<P: Plugin, B: Backend> Wrapper<P, B> {
} }
} }
terminate_audio_thread.store(true, Ordering::SeqCst);
audio_thread.join();
Ok(()) Ok(())
} }