Trait web_thread::web::audio_worklet::BaseAudioContextExt
source · pub trait BaseAudioContextExt {
// Required methods
fn register_thread<F>(
self,
stack_size: Option<usize>,
f: F,
) -> RegisterThreadFuture ⓘ
where F: 'static + FnOnce() + Send;
fn register_thread_with_message<F, M>(
self,
stack_size: Option<usize>,
f: F,
message: M,
) -> RegisterThreadFuture ⓘ
where F: 'static + FnOnce(M) + Send,
M: 'static + MessageSend;
fn audio_worklet_node<P>(
&self,
name: &str,
data: P::Data,
options: Option<&AudioWorkletNodeOptions>,
) -> Result<AudioWorkletNode, AudioWorkletNodeError<P>>
where P: 'static + ExtendAudioWorkletProcessor;
}
Web
and crate feature audio-worklet
only.Expand description
Extension for BaseAudioContext
.
Required Methods§
sourcefn register_thread<F>(
self,
stack_size: Option<usize>,
f: F,
) -> RegisterThreadFuture ⓘ
fn register_thread<F>( self, stack_size: Option<usize>, f: F, ) -> RegisterThreadFuture ⓘ
Registers a thread at this BaseAudioContext
.
§Notes
Unfortunately there is currently no way to determine when the thread has
fully shutdown. So this will leak memory unless
AudioWorkletHandle::release()
is called.
§Errors
- If a thread was already registered at this
BaseAudioContext
. - If the
BaseAudioContext
isclosed
. - If the main thread does not support spawning threads, see
has_spawn_support()
.
§Example
use web_sys::AudioContext;
use web_thread::web::audio_worklet::BaseAudioContextExt;
let context = AudioContext::new().unwrap();
context.clone().register_thread(
None,
|| {
// Do work.
},
).await.unwrap();
sourcefn register_thread_with_message<F, M>(
self,
stack_size: Option<usize>,
f: F,
message: M,
) -> RegisterThreadFuture ⓘ
Available on crate feature message
only.
fn register_thread_with_message<F, M>( self, stack_size: Option<usize>, f: F, message: M, ) -> RegisterThreadFuture ⓘ
message
only.Registers a thread at this BaseAudioContext
.
§Notes
Unfortunately there is currently no way to determine when the thread has
fully shutdown. So this will leak memory unless
AudioWorkletHandle::release()
is called.
§Errors
- If a thread was already registered at this
BaseAudioContext
. - If the
BaseAudioContext
isclosed
. - If the main thread does not support spawning threads, see
has_spawn_support()
.
§Example
use js_sys::ArrayBuffer;
use web_sys::AudioContext;
use web_thread::web::audio_worklet::BaseAudioContextExt;
use web_thread::web::message::TransferableWrapper;
let context = AudioContext::new().unwrap();
let buffer = TransferableWrapper(ArrayBuffer::new(1024));
context
.clone()
.register_thread_with_message(
None,
|message| {
let buffer: ArrayBuffer = message.0;
// Do work.
},
buffer,
)
.await
.unwrap();
sourcefn audio_worklet_node<P>(
&self,
name: &str,
data: P::Data,
options: Option<&AudioWorkletNodeOptions>,
) -> Result<AudioWorkletNode, AudioWorkletNodeError<P>>where
P: 'static + ExtendAudioWorkletProcessor,
fn audio_worklet_node<P>(
&self,
name: &str,
data: P::Data,
options: Option<&AudioWorkletNodeOptions>,
) -> Result<AudioWorkletNode, AudioWorkletNodeError<P>>where
P: 'static + ExtendAudioWorkletProcessor,
Instantiates a AudioWorkletProcessor
. No data
will be delivered if
name
corresponds to a different type registered with
AudioWorkletGlobalScopeExt::register_processor_ext()
. If name
corresponds to a AudioWorkletProcessor
not registered through
AudioWorkletGlobalScopeExt::register_processor_ext()
, it will leak
data
.
§Errors
- If
Self::register_thread()
was not called on this context yet. - If
new AudioWorkletNode
throws an exception.
§Example
use web_sys::{AudioContext, AudioWorkletGlobalScope, AudioWorkletNodeOptions, AudioWorkletProcessor};
use web_thread::web::{self, YieldTime};
use web_thread::web::audio_worklet::{AudioWorkletGlobalScopeExt, BaseAudioContextExt, ExtendAudioWorkletProcessor};
/// Example [`AudioWorkletProcessor`].
struct TestProcessor;
impl ExtendAudioWorkletProcessor for TestProcessor {
type Data = String;
fn new(
_: AudioWorkletProcessor,
data: Option<Self::Data>,
_: AudioWorkletNodeOptions,
) -> Self {
assert_eq!(data.as_deref(), Some("test"));
Self
}
}
let context = AudioContext::new().unwrap();
let (sender, receiver) = async_channel::bounded(1);
context.clone().register_thread(
None,
move || {
let global: AudioWorkletGlobalScope = js_sys::global().unchecked_into();
global
.register_processor_ext::<TestProcessor>("test")
.unwrap();
sender.try_send(()).unwrap();
},
).await.unwrap();
// Wait until processor is registered.
receiver.recv().await.unwrap();
web::yield_now_async(YieldTime::UserBlocking).await;
let node = context.audio_worklet_node::<TestProcessor>("test", String::from("test"), None).unwrap();