audio/
audio.rs

1use jsbind::prelude::*;
2use webbind::*;
3
4fn main() {
5    let context = AudioContext::new();
6    println!("Got an AudioContext");
7
8    // Create oscillator
9    let mut oscillator = context.create_oscillator();
10    println!("Configuring oscillator");
11    oscillator.set_type_(&OscillatorType::TRIANGLE);
12    oscillator.frequency().set_value(261.63); // Middle C
13
14    let document = window().document();
15    let body = document.get_elements_by_tag_name(&"body".into()).item(0);
16    let mut button = document
17        .create_element(&"BUTTON".into())
18        .dyn_into::<HTMLButtonElement>()
19        .unwrap();
20
21    button.set_text_content(&"Click me".into());
22    button.add_event_listener(
23        &JsString::from("click"),
24        &EventListener::from_closure(move |_e: Event| {
25            println!("Playing");
26            oscillator.connect_with_destination_param(
27                context.destination().unchecked_ref::<AudioParam>(),
28            );
29            oscillator.start_with_when(0.0);
30            println!("All done!");
31            Undefined::VALUE
32        }),
33    );
34    body.append_child(button.dyn_ref::<Node>().unwrap());
35}