use wasm_bindgen::prelude::*; use web_sys::console; // When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global // allocator. // // If you don't want to use `wee_alloc`, you can safely delete this. #[cfg(feature = "wee_alloc")] #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; // This is like the `main` function, except for JavaScript. #[wasm_bindgen(start)] pub fn main_js() -> Result<(), JsValue> { // This provides better error messages in debug mode. // It's disabled in release mode, so it doesn't bloat up the file size. #[cfg(debug_assertions)] console_error_panic_hook::set_once(); Ok(()) } use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::Stream; #[wasm_bindgen] pub struct Handle(Stream); #[wasm_bindgen] pub fn beep() -> Handle { let host = cpal::default_host(); let device = host .default_output_device() .expect("failed to find a default output device"); let config = device.default_output_config().unwrap(); Handle(match config.sample_format() { cpal::SampleFormat::F32 => run::(&device, &config.into()), cpal::SampleFormat::I16 => run::(&device, &config.into()), cpal::SampleFormat::U16 => run::(&device, &config.into()), }) } fn run(device: &cpal::Device, config: &cpal::StreamConfig) -> Stream where T: cpal::Sample, { let sample_rate = config.sample_rate.0 as f32; let channels = config.channels as usize; // Produce a sinusoid of maximum amplitude. let mut sample_clock = 0f32; let mut next_value = move || { sample_clock = (sample_clock + 1.0) % sample_rate; (sample_clock * 440.0 * 2.0 * 3.141592 / sample_rate).sin() }; let err_fn = |err| console::error_1(&format!("an error occurred on stream: {}", err).into()); let stream = device .build_output_stream( config, move |data: &mut [T], _| write_data(data, channels, &mut next_value), err_fn, ) .unwrap(); stream.play().unwrap(); stream } fn write_data(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32) where T: cpal::Sample, { for frame in output.chunks_mut(channels) { let value: T = cpal::Sample::from::(&next_sample()); for sample in frame.iter_mut() { *sample = value; } } }