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;
}
Available on Web and crate feature audio-worklet only.
Expand description

Extension for BaseAudioContext.

Required Methods§

source

fn register_thread<F>( self, stack_size: Option<usize>, f: F, ) -> RegisterThreadFuture
where F: 'static + FnOnce() + Send,

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
§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();
source

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,

Available on crate feature 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
§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();
source

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
§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();

Object Safety§

This trait is not object safe.

Implementors§