record-player 0.2.0

Shared Bitneedle transport, record physics, and acoustic scratch engine
Documentation
use crate::{PlayerConfig, PlayerEngine, PlayerEvent};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct WasmPlayerEngine {
    inner: PlayerEngine,
}

#[wasm_bindgen]
impl WasmPlayerEngine {
    #[wasm_bindgen(constructor)]
    pub fn new(config: JsValue) -> Result<WasmPlayerEngine, JsValue> {
        let config: PlayerConfig = if config.is_null() || config.is_undefined() {
            PlayerConfig::default()
        } else {
            serde_wasm_bindgen::from_value(config)?
        };
        Ok(Self {
            inner: PlayerEngine::new(config),
        })
    }
    pub fn dispatch(&mut self, event: JsValue) -> Result<(), JsValue> {
        let event: PlayerEvent = serde_wasm_bindgen::from_value(event)?;
        self.inner
            .dispatch(event)
            .map_err(|e| JsValue::from_str(&e.to_string()))
    }
    #[wasm_bindgen(js_name = drainCommands)]
    pub fn drain_commands(&mut self) -> Result<JsValue, JsValue> {
        serde_wasm_bindgen::to_value(&self.inner.drain_commands()).map_err(Into::into)
    }
    pub fn state(&self) -> Result<JsValue, JsValue> {
        serde_wasm_bindgen::to_value(self.inner.state()).map_err(Into::into)
    }
    pub fn view(&self) -> Result<JsValue, JsValue> {
        serde_wasm_bindgen::to_value(&self.inner.view()).map_err(Into::into)
    }
    pub fn revision(&self) -> u64 {
        self.inner.revision()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    

    

    

    fn streaming_programme(frame_count: usize, sample_rate_hz: f64) -> (Vec<f32>, Vec<f32>) {
        let left = (0..frame_count)
            .map(|frame| {
                let time = frame as f64 / sample_rate_hz;
                (0.63 * (std::f64::consts::TAU * 997.0 * time).sin()
                    + 0.11 * (std::f64::consts::TAU * 3_101.0 * time).cos()) as f32
            })
            .collect();
        let right = (0..frame_count)
            .map(|frame| {
                let time = frame as f64 / sample_rate_hz;
                (0.47 * (std::f64::consts::TAU * 1_503.0 * time).cos()
                    - 0.09 * (std::f64::consts::TAU * 5_011.0 * time).sin()) as f32
            })
            .collect();
        (left, right)
    }

    

    

}