1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! Audio worklet example.

#![allow(clippy::unwrap_used)]

#[cfg(not(target_family = "wasm"))]
fn main() {
	panic!("This example is supposed to only be run with the `wasm32-unknown-unknown` target.")
}

#[cfg(target_family = "wasm")]
fn main() {
	web::main();
}

/// Implementation for the Web platform.
#[cfg(target_family = "wasm")]
mod web {
	use std::cell::Cell;
	use std::iter;
	use std::rc::Rc;
	use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8, Ordering};
	use std::sync::Arc;

	use itertools::Itertools;
	use js_sys::{Array, Float32Array, Object, Promise, Reflect};
	use wasm_bindgen::closure::Closure;
	use wasm_bindgen::{JsCast, JsValue};
	use wasm_bindgen_futures::JsFuture;
	use web_sys::{
		console, AudioContext, AudioWorkletGlobalScope, AudioWorkletNodeOptions,
		AudioWorkletProcessor, Blob, BlobPropertyBag, Document, HtmlButtonElement, HtmlElement,
		HtmlInputElement, HtmlTableElement, HtmlTableRowElement, Url,
	};
	use web_thread::web::audio_worklet::{
		AudioWorkletGlobalScopeExt, BaseAudioContextExt, ExtendAudioWorkletProcessor,
	};
	use web_thread::web::{self, YieldTime};

	/// `fn main` implementation.
	pub(crate) fn main() {
		console_error_panic_hook::set_once();

		let document = web_sys::window().unwrap().document().unwrap();
		let body = document.body().unwrap();

		// Create a centered container.
		let container = create_centered_container(&document, &body);

		// Create start/stop button.
		let button: HtmlButtonElement = document.create_element("button").unwrap().unchecked_into();
		button.set_inner_text("Start");
		container.append_child(&button).unwrap();

		// Let button start the audio worklet because an [`AudioContext`] can only start
		// after a user-interaction
		button.clone().set_onclick(Some(
			Closure::once_into_js(|| {
				// Disable button during starting.
				button.set_disabled(true);
				button.set_inner_text("Starting ...");
				button.set_onclick(None);

				wasm_bindgen_futures::future_to_promise(async {
					start(document, container, button).await;
					Ok(JsValue::UNDEFINED)
				})
			})
			.as_ref()
			.unchecked_ref(),
		));
	}

	/// Start the example.
	#[allow(clippy::too_many_lines)]
	async fn start(
		document: Document,
		container: HtmlElement,
		start_stop_button: HtmlButtonElement,
	) {
		// Create audio context.
		let context = AudioContext::new().unwrap();

		// Some browsers don't support `TextDecoder`/`TextEncoder` in audio worklets:
		// <https://bugzilla.mozilla.org/show_bug.cgi?id=1826432>
		JsFuture::from(
			context
				.audio_worklet()
				.unwrap()
				.add_module(&url(&wasm_bindgen::link_to!(
					module = "/examples/polyfill.min.js"
				)))
				.unwrap(),
		)
		.await
		.unwrap();

		// Register thread.
		let (sender, receiver) = async_channel::bounded(1);
		context
			.clone()
			.register_thread(None, move || {
				console::log_1(&"Hello from audio worklet!".into());

				let global: AudioWorkletGlobalScope = js_sys::global().unchecked_into();
				// Register `ExampleProcessor`.
				global
					.register_processor_ext::<ExampleProcessor>("example")
					.unwrap();
				sender.try_send(()).unwrap();
			})
			.await
			.unwrap();

		// Wait until processor is registered.
		receiver.recv().await.unwrap();
		web::yield_now_async(YieldTime::UserBlocking).await;

		// Remove start button in preparation of adding new content.
		start_stop_button.remove();

		// Get output channel count.
		let channel_count = context.destination().channel_count();

		// Create table to present slider for each channel.
		let volume_table = VolumeControlTable::new(document.clone(), &container);

		// Create master volume control elements.
		let (master_builder, master_mute_callback) = volume_table.volume_control("Master");

		// Create volume control elements for every channel.
		let volumes: Rc<Vec<_>> = Rc::new(
			(0..channel_count)
				.map(|index| {
					// Create control elements.
					let (builder, mute_callback) =
						volume_table.volume_control(&format!("Channel {index}"));

					// Create callback for controlling volume.
					let slider_callback = Closure::<dyn Fn()>::new({
						let master_builder = master_builder.clone();
						let builder = builder.clone();
						move || {
							let value_string = builder.slider.value();
							builder.label.set_inner_text(&value_string);
							let value = value_string.parse().unwrap();
							builder.shared.volume.store(value, Ordering::Relaxed);

							// If the master volume is lower, we increase it, otherwise its weird
							// that master volume is lower then the highest volume.
							if master_builder.shared.volume.load(Ordering::Relaxed) < value {
								master_builder.shared.volume.store(value, Ordering::Relaxed);
								master_builder.slider.set_value(&value_string);
								master_builder.label.set_inner_text(&value_string);
							}
						}
					});
					builder
						.slider
						.set_oninput(Some(slider_callback.as_ref().unchecked_ref()));

					VolumeControl {
						slider: builder.slider,
						_slider_callback: slider_callback,
						label: builder.label,
						_mute_callback: mute_callback,
						shared: builder.shared,
					}
				})
				.collect(),
		);

		// Setup master slider callback.
		let master_slider_callback = Closure::<dyn FnMut()>::new({
			let builder = master_builder.clone();
			let volumes = Rc::clone(&volumes);
			move || {
				let value_string = builder.slider.value();
				builder.label.set_inner_text(&value_string);
				let value = value_string.parse().unwrap();
				builder.shared.volume.store(value, Ordering::Relaxed);

				for VolumeControl {
					slider,
					label,
					shared,
					..
				} in volumes.iter()
				{
					// Update values for all channels (even if we are muted).
					slider.set_value(&value_string);
					label.set_inner_text(&value_string);
					shared.volume.store(value, Ordering::Relaxed);
				}
			}
		});
		master_builder
			.slider
			.set_oninput(Some(master_slider_callback.as_ref().unchecked_ref()));

		// Setup space before frequency control.
		let br_1 = document.create_element("br").unwrap();
		container.append_child(&br_1).unwrap();

		// Frequency control elements.
		let piano = PianoControl::new(&document, &container);

		// Initialize `ExampleProcessor`.
		let data = Data {
			master: master_builder.shared,
			channels: volumes
				.iter()
				.map(|volume| Arc::clone(&volume.shared))
				.collect(),
			piano: Arc::clone(&piano.value),
		};
		let options = AudioWorkletNodeOptions::new();
		options.set_output_channel_count(&Array::of1(&channel_count.into()));
		let worklet = context
			.audio_worklet_node::<ExampleProcessor>("example", data, Some(&options))
			.unwrap();
		worklet
			.connect_with_audio_node(&context.destination())
			.unwrap();

		// Setup space before control buttons.
		let br_2 = document.create_element("br").unwrap();
		container.append_child(&br_2).unwrap();

		// Setup suspend/resume button.
		let suspend_resume_button: HtmlButtonElement =
			document.create_element("button").unwrap().unchecked_into();
		suspend_resume_button.set_inner_text("Suspend");
		let suspended = Rc::new(Cell::new(false));
		let suspend_resume_callback = Closure::<dyn Fn() -> Promise>::new({
			let button = suspend_resume_button.clone();
			let context = context.clone();
			move || {
				// Disable button during suspending or resuming.
				button.set_disabled(true);

				let button = button.clone();
				let context = context.clone();
				let suspended = Rc::clone(&suspended);
				wasm_bindgen_futures::future_to_promise(async move {
					// If context is suspended, resume.
					if suspended.get() {
						button.set_inner_text("Resuming ...");
						JsFuture::from(context.resume().unwrap()).await.unwrap();
						button.set_inner_text("Suspend");
						suspended.set(false);
					}
					// If context is running, suspend.
					else {
						button.set_inner_text("Suspending ...");
						JsFuture::from(context.suspend().unwrap()).await.unwrap();
						button.set_inner_text("Resume");
						suspended.set(true);
					}

					// Re-enable button after we finished suspending or resuming.
					button.set_disabled(false);

					Ok(JsValue::UNDEFINED)
				})
			}
		});
		suspend_resume_button.set_onclick(Some(suspend_resume_callback.as_ref().unchecked_ref()));
		container.append_child(&suspend_resume_button).unwrap();

		// Setup stop button.
		start_stop_button.set_inner_text("Stop");
		start_stop_button.set_onclick(Some(
			Closure::once_into_js({
				let container = container.clone();
				let start_stop_button = start_stop_button.clone();
				move || {
					// Disable button during stopping.
					start_stop_button.set_disabled(true);
					start_stop_button.set_inner_text("Stopping ...");
					// Disable resume button as well.
					suspend_resume_button.set_disabled(true);
					drop(suspend_resume_callback);

					wasm_bindgen_futures::future_to_promise(async move {
						// Closure audio context.
						JsFuture::from(context.close().unwrap()).await.unwrap();

						// Remove all control elements.
						volume_table.remove();
						br_1.remove();
						piano.table.remove();
						drop(piano);
						br_2.remove();
						suspend_resume_button.remove();
						drop(master_slider_callback);
						drop(master_mute_callback);
						drop(Rc::into_inner(volumes).unwrap());

						// Setup restart button.
						start_stop_button.set_onclick({
							let start_stop_button = start_stop_button.clone();
							Some(
								Closure::once_into_js(move || {
									// Disable button during restarting.
									start_stop_button.set_disabled(true);
									start_stop_button.set_inner_text("Starting ...");
									start_stop_button.set_onclick(None);

									wasm_bindgen_futures::future_to_promise(async {
										start(document, container, start_stop_button).await;
										Ok(JsValue::UNDEFINED)
									})
								})
								.as_ref()
								.unchecked_ref(),
							)
						});
						// Re-enable button after restarting.
						start_stop_button.set_disabled(false);
						start_stop_button.set_inner_text("Start");

						Ok(JsValue::UNDEFINED)
					})
				}
			})
			.as_ref()
			.unchecked_ref(),
		));
		start_stop_button.set_disabled(false);
		container.append_child(&start_stop_button).unwrap();
	}

	/// Data shared between the window and [`ExampleProcessor`].
	struct Data {
		/// Master shared data..
		master: Arc<SharedData>,
		/// Shared data for each channel.
		channels: Vec<Arc<SharedData>>,
		/// Piano key.
		piano: Arc<AtomicI8>,
	}

	/// Data shared between the window and [`ExampleProcessor`].
	struct SharedData {
		/// Volume for this channel.
		volume: AtomicU8,
		/// If this channel is muted.
		mute: AtomicBool,
	}

	/// Example [`AudioWorkletProcessor`].
	struct ExampleProcessor {
		/// Buffer used to calculate each sample.
		samples: Vec<f32>,
		/// Buffer used to adjust output for each channel.
		buffer: Vec<f32>,
		/// Data shared between the window and [`ExampleProcessor`].
		shared: Data,
		/// Current volume of each channel.
		volumes: Vec<f32>,
		/// Current frequency.
		frequency: f32,
	}

	impl ExampleProcessor {
		/// <https://en.wikipedia.org/wiki/A440_(pitch_standard)>
		const BASE_FREQUENCY: f32 = 440.;
		/// Transform frequency into an oscillating frequency.
		#[allow(clippy::absolute_paths)]
		const TRANSFORM: f32 = 2. * std::f32::consts::PI;
	}

	impl ExtendAudioWorkletProcessor for ExampleProcessor {
		type Data = Data;

		fn new(
			_: AudioWorkletProcessor,
			data: Option<Self::Data>,
			options: AudioWorkletNodeOptions,
		) -> Self {
			console::log_1(&"`ExampleProcessor` initialized!".into());
			let output_channel_count: Array = Reflect::get(&options, &"outputChannelCount".into())
				.unwrap()
				.unchecked_into();
			#[allow(
				clippy::as_conversions,
				clippy::cast_possible_truncation,
				clippy::cast_sign_loss
			)]
			let channel_count = output_channel_count.get(0).as_f64().unwrap() as usize;
			Self {
				samples: Vec::new(),
				buffer: Vec::new(),
				shared: data.unwrap(),
				volumes: vec![0.01; channel_count],
				frequency: Self::BASE_FREQUENCY,
			}
		}

		#[allow(
			clippy::as_conversions,
			clippy::cast_possible_truncation,
			clippy::cast_precision_loss,
			clippy::cast_sign_loss
		)]
		fn process(&mut self, _: Array, outputs: Array, _: Object) -> bool {
			let master_muted = self.shared.master.mute.load(Ordering::Relaxed);

			// Do nothing if master is muted and all channels have reached zero volume.
			if master_muted && self.volumes.iter().all(|volume| *volume == 0.) {
				return true;
			}

			let global: AudioWorkletGlobalScope = js_sys::global().unchecked_into();

			let sample_rate = global.sample_rate();
			let time = global.current_time() as f32;

			let output = outputs.into_iter().exactly_one().unwrap();
			let output: Array = output.unchecked_into();
			let mut output = output.into_iter();

			let first_channel: Float32Array = output.next().unwrap().unchecked_into();
			let sample_size = first_channel.length() as usize;
			self.samples.reserve_exact(sample_size);
			self.buffer.reserve_exact(sample_size);

			let mut sampled = false;

			for ((current, shared), channel) in self
				.volumes
				.iter_mut()
				.zip(&self.shared.channels)
				.zip(iter::once(first_channel).chain(output.map(JsValue::unchecked_into)))
			{
				// If we are muted always set target volume to zero.
				let target = if master_muted || shared.mute.load(Ordering::Relaxed) {
					0.
				} else {
					f32::from(shared.volume.load(Ordering::Relaxed)) / 1000.
				};

				// If this channels target volume is zero and we reached it do nothing.
				#[allow(clippy::float_cmp)]
				if target == 0. && *current == target {
					continue;
				}

				// Calculate base samples for all channels only if we plan to do actual work.
				if !sampled {
					// <https://en.wikipedia.org/wiki/Piano_key_frequencies>.
					let key = f32::from(self.shared.piano.load(Ordering::Relaxed));
					let target = ((key - 49.) / 12.).exp2() * Self::BASE_FREQUENCY;

					// TODO: Should implement exponential ramp up like described in the spec:
					// <https://webaudio.github.io/web-audio-api/#dom-audioparam-exponentialramptovalueattime>
					for index in 0..sample_size {
						#[allow(clippy::float_cmp)]
						if self.frequency != target {
							if (self.frequency.abs() - target.abs()).abs() > 0.5 {
								if self.frequency < target {
									self.frequency += 0.5;
								} else {
									self.frequency -= 0.5;
								}
							} else {
								self.frequency = target;
							}
						}

						let sample = f32::sin(
							self.frequency * Self::TRANSFORM * (time + index as f32 / sample_rate),
						);

						if let Some(entry) = self.samples.get_mut(index) {
							*entry = sample;
						} else {
							self.samples.push(sample);
						}
					}

					sampled = true;
				};

				// Copy data into each channel after adjusting for volume.
				for (index, base_sample) in self.samples.iter().enumerate() {
					#[allow(clippy::float_cmp)]
					if *current != target {
						if (current.abs() - target.abs()).abs() > 0.0001 {
							if *current < target {
								*current += 0.0001;
							} else {
								*current -= 0.0001;
							}
						} else {
							*current = target;
						}
					}

					let sample = *base_sample * *current;

					if let Some(entry) = self.buffer.get_mut(index) {
						*entry = sample;
					} else {
						self.buffer.push(sample);
					}
				}

				channel.copy_from(&self.buffer);
			}

			true
		}
	}

	/// Create centered container by making the body a CSS grid.
	fn create_centered_container(document: &Document, body: &HtmlElement) -> HtmlElement {
		document
			.document_element()
			.unwrap()
			.unchecked_into::<HtmlElement>()
			.style()
			.set_property("height", "100%")
			.unwrap();
		let style = body.style();
		style.set_property("height", "100%").unwrap();
		style.set_property("display", "grid").unwrap();

		// Create centered container.
		let container: HtmlElement = document.create_element("div").unwrap().unchecked_into();
		let style = container.style();
		style.set_property("margin", "auto").unwrap();
		style.set_property("text-align", "center").unwrap();
		body.append_child(&container).unwrap();

		container
	}

	/// Table for all volume control elements.
	struct VolumeControlTable {
		/// Hold [`Document`] to create columns.
		document: Document,
		/// The table itself.
		table: HtmlTableElement,
		/// Name of each channel.
		name: HtmlTableRowElement,
		/// Volume slider.
		slider: HtmlTableRowElement,
		/// Volume value label.
		value: HtmlTableRowElement,
		/// Mute button.
		mute: HtmlTableRowElement,
	}

	impl VolumeControlTable {
		/// Creates a new [`VolumeControlTable`].
		fn new(document: Document, container: &HtmlElement) -> Self {
			let table: HtmlTableElement =
				document.create_element("table").unwrap().unchecked_into();
			container.append_child(&table).unwrap();
			let style = table.style();
			style.set_property("border", "1px solid").unwrap();
			style.set_property("border-collapse", "collapse").unwrap();
			style.set_property("margin", "auto").unwrap();
			let name: HtmlTableRowElement = table.insert_row().unwrap().unchecked_into();
			let slider: HtmlTableRowElement = table.insert_row().unwrap().unchecked_into();
			let value: HtmlTableRowElement = table.insert_row().unwrap().unchecked_into();
			let mute: HtmlTableRowElement = table.insert_row().unwrap().unchecked_into();

			Self {
				document,
				table,
				name,
				slider,
				value,
				mute,
			}
		}

		/// Create table column for volume control elements.
		fn volume_control(&self, name: &str) -> (VolumeControlBuilder, Closure<dyn Fn()>) {
			// Name.
			let cell = self.name.insert_cell().unwrap();
			cell.set_inner_text(name);
			cell.style().set_property("border", "1px solid").unwrap();
			// Slider.
			let slider: HtmlInputElement = self
				.document
				.create_element("input")
				.unwrap()
				.unchecked_into();
			slider.set_value("10"); // Default value.
			{
				// Make slider vertical.
				let style = slider.style();
				// Chrome.
				style
					.set_property("-webkit-writing-mode", "vertical-lr")
					.unwrap();
				// Firefox.
				slider.set_attribute("orient", "vertical").unwrap();
				// Safari.
				style
					.set_property("-webkit-appearance", "slider-vertical")
					.unwrap();
			}
			slider.set_type("range");
			let cell = self.slider.insert_cell().unwrap();
			cell.style()
				.set_property("border-right", "1px solid")
				.unwrap();
			cell.append_child(&slider).unwrap();
			// Value label.
			let label = self.value.insert_cell().unwrap();
			label
				.style()
				.set_property("border-right", "1px solid")
				.unwrap();
			label.set_inner_text("10");
			// Mute button.
			let mute: HtmlButtonElement = self
				.document
				.create_element("button")
				.unwrap()
				.unchecked_into();
			#[allow(clippy::non_ascii_literal)]
			mute.set_inner_text("🔊");
			let cell = self.mute.insert_cell().unwrap();
			let style = cell.style();
			style.set_property("border-top", "1px solid").unwrap();
			style.set_property("border-right", "1px solid").unwrap();
			cell.append_child(&mute).unwrap();

			let shared = Arc::new(SharedData {
				volume: AtomicU8::new(10),
				mute: AtomicBool::new(false),
			});

			// Create callback for mute button.
			let mute_callback = Closure::<dyn Fn()>::new({
				let mute = mute.clone();
				let shared = Arc::clone(&shared);
				move || {
					// If we are muted, unmute.
					if shared.mute.load(Ordering::Relaxed) {
						#[allow(clippy::non_ascii_literal)]
						mute.set_inner_text("🔊");
						shared.mute.store(false, Ordering::Relaxed);
					}
					// If we are not muted, mute.
					else {
						#[allow(clippy::non_ascii_literal)]
						mute.set_inner_text("🔇");
						shared.mute.store(true, Ordering::Relaxed);
					}
				}
			});
			mute.set_onclick(Some(mute_callback.as_ref().unchecked_ref()));

			(
				VolumeControlBuilder {
					slider,
					label,
					shared,
				},
				mute_callback,
			)
		}

		/// Remove the table from the document.
		fn remove(self) {
			self.table.remove();
		}
	}

	/// Elements to build a [`VolumeControl`].
	#[derive(Clone)]
	struct VolumeControlBuilder {
		/// The volume slider.
		slider: HtmlInputElement,
		/// Label showing the current value.
		label: HtmlElement,
		/// Data shared between the window and [`ExampleProcessor`].
		shared: Arc<SharedData>,
	}

	/// Stores volume control elements.
	struct VolumeControl {
		/// The volume slider.
		slider: HtmlInputElement,
		/// Callback handling slider input.
		_slider_callback: Closure<dyn Fn()>,
		/// Label showing the current value.
		label: HtmlElement,
		/// Callback handling mute button.
		_mute_callback: Closure<dyn Fn()>,
		/// Data shared with [`ExampleProcessor`].
		shared: Arc<SharedData>,
	}

	/// Stores piano key control elements.
	struct PianoControl {
		/// Table holding all elements.
		table: HtmlTableElement,
		/// Shared value with [`ExampleProcessor`].
		value: Arc<AtomicI8>,
		/// Callback updating shared value.
		_callback: Closure<dyn Fn()>,
	}

	impl PianoControl {
		/// Creates a new [`PianoControl`].
		fn new(document: &Document, container: &HtmlElement) -> Self {
			/// Name of each paino note in an octave.
			const NOTE: [&str; 12] = [
				"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B",
			];

			// Table.
			let table: HtmlTableElement =
				document.create_element("table").unwrap().unchecked_into();
			let style = table.style();
			style.set_property("border", "1px solid").unwrap();
			style.set_property("border-collapse", "collapse").unwrap();
			style.set_property("margin", "auto").unwrap();

			// Header.
			let cell = table.insert_row().unwrap();
			cell.set_text_content(Some("Piano Key"));
			cell.style()
				.set_property("border-bottom", "1px solid")
				.unwrap();

			// Piano key slider.
			let slider: HtmlInputElement =
				document.create_element("input").unwrap().unchecked_into();
			slider.set_value("49"); // Default value.
			slider.set_min("-8");
			slider.set_max("99");
			slider.set_type("range");
			table.insert_row().unwrap().append_child(&slider).unwrap();

			// Piano key label.
			let label = table.insert_row().unwrap();
			label.set_text_content(Some("A4"));

			// Insert table.
			container.append_child(&table).unwrap();

			// Setup callback.
			let value = Arc::new(AtomicI8::new(49));
			let callback = Closure::new({
				let slider = slider.clone();
				let value = Arc::clone(&value);
				move || {
					let key = slider.value().parse().unwrap();
					value.store(key, Ordering::Relaxed);
					let octave = (key + 8) / 12;
					let note = (key + 8) % 12;
					#[allow(clippy::indexing_slicing)]
					label.set_text_content(Some(&format!(
						"{}{octave}",
						NOTE[usize::try_from(note).unwrap()]
					)));
				}
			});
			slider.set_oninput(Some(callback.as_ref().unchecked_ref()));

			Self {
				table,
				value,
				_callback: callback,
			}
		}
	}

	/// Create an object URL from a JS script.
	fn url(script: &str) -> String {
		let sequence = Array::of1(&script.into());
		let property = BlobPropertyBag::new();
		property.set_type("text/javascript");
		let blob = Blob::new_with_str_sequence_and_options(&sequence, &property)
			.expect("`new Blob()` should never throw");

		Url::create_object_url_with_blob(&blob).expect("`URL.createObjectURL()` should never throw")
	}
}