diff --git a/lightningbeam-core/src/audio.rs b/lightningbeam-core/src/audio.rs index 4166a88..9dc806f 100644 --- a/lightningbeam-core/src/audio.rs +++ b/lightningbeam-core/src/audio.rs @@ -1,5 +1,5 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use cpal::{Sample, SampleFormat, StreamConfig, SupportedBufferSize, SampleRate, BufferSize}; +use cpal::{Sample}; use std::sync::{Arc, Mutex}; use crate::{TrackManager, Timestamp, Duration, SampleCount, AudioOutput, PlaybackState}; @@ -44,15 +44,29 @@ where self.sample_rate = supported_config.sample_rate.0; let num_channels = supported_config.channels as usize; // Get channel count + let buffer_size_range = match config.buffer_size() { + cpal::SupportedBufferSize::Range { min, max } => (*min, *max), + cpal::SupportedBufferSize::Unknown => { + // Use a reasonable default range if the device doesn't specify + (256, 4096) + } + }; + + // Define the desired buffer size and clamp it to the supported range + let desired_buffer_size = 2048; + let clamped_buffer_size = desired_buffer_size.clamp(buffer_size_range.0, buffer_size_range.1); + + let mut stream_config = supported_config.clone(); + stream_config.buffer_size = cpal::BufferSize::Fixed(clamped_buffer_size); + let track_manager = self.track_manager.clone(); - let playback_state = self.playback_state.clone(); let timestamp = self.timestamp.clone(); let sample_rate = self.sample_rate; let err_fn = |err| eprintln!("Audio stream error: {:?}", err); let stream = device.build_output_stream( - &supported_config, + &stream_config, move |data: &mut [T], _: &cpal::OutputCallbackInfo| { if let Some(track_manager) = &track_manager { let num_frames = data.len() / num_channels; // Stereo: divide by 2 @@ -60,14 +74,12 @@ where let chunk_duration = Duration::new(num_frames as f64 / sample_rate as f64); let mut track_manager = track_manager.lock().unwrap(); - let playing = matches!(playback_state, PlaybackState::Playing); let mut timestamp_guard = timestamp.lock().unwrap(); let timestamp = &mut *timestamp_guard; let chunk = track_manager.update_audio( timestamp.clone(), - playing, sample_count, sample_rate, ); diff --git a/lightningbeam-core/src/lib.rs b/lightningbeam-core/src/lib.rs index 0912adc..b99a592 100644 --- a/lightningbeam-core/src/lib.rs +++ b/lightningbeam-core/src/lib.rs @@ -5,112 +5,89 @@ use audio::{CpalAudioOutput}; use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError}; use std::sync::{Arc, Mutex}; +use std::fmt; #[cfg(feature = "wasm")] mod wasm_imports { - pub use wasm_bindgen::prelude::*; - pub use web_sys::console; - use wasm_logger; + pub use wasm_bindgen::prelude::*; + pub use web_sys::console; } #[cfg(feature = "wasm")] use wasm_imports::*; -pub trait AudioTrack: Send { - /// Render a chunk of audio for the given timestamp and duration. - fn render_chunk(&mut self, timestamp: Timestamp, duration: SampleCount) -> Vec; +pub trait Track: Send { + fn get_name(&self) -> &str { + "Unnamed Track" + } - /// Get the sample rate of the audio track. - fn sample_rate(&self) -> u32; -} - -pub trait VideoTrack: Send { - /// Render a frame for the given timestamp. - fn render_frame(&self, timestamp: Timestamp) -> Frame; - - /// Get the frame rate of the video track. - fn frame_rate(&self) -> f64; + fn set_name(&mut self, _name: String) { + } + /// Render audio for the given timestamp and duration. + /// Returns `None` if this track doesn't produce audio. + fn render_audio(&mut self, _timestamp: Timestamp, _duration: SampleCount, _sample_rate: u32, _playing: bool) -> Option> { + None + } + + /// Render a video frame for the given timestamp. + /// Returns `None` if this track doesn't produce video. + fn render_video(&self, _timestamp: Timestamp, _playing: bool) -> Option { + None + } } pub struct TrackManager { - audio_tracks: Vec>, - video_tracks: Vec>, - // sample_rate: u32, - // frame_duration: Duration, // Duration of each frame in seconds (e.g., 1/60 for 60 FPS) + tracks: Vec>, timestamp: Timestamp, playback_state: PlaybackState, } impl TrackManager { - pub fn new(sample_rate: u32, frame_duration: f64) -> Self { + pub fn new() -> Self { Self { - audio_tracks: Vec::new(), - video_tracks: Vec::new(), - // sample_rate, - // frame_duration: Duration::new(frame_duration), - playback_state: PlaybackState::Stopped, + tracks: Vec::new(), timestamp: Timestamp::from_seconds(0.0), + playback_state: PlaybackState::Stopped, } } - pub fn add_audio_track(&mut self, track: Box) { - self.audio_tracks.push(track); + pub fn add_track(&mut self, track: Box) { + self.tracks.push(track); } - pub fn add_video_track(&mut self, track: Box) { - self.video_tracks.push(track); + pub fn update_audio(&mut self, timestamp: Timestamp, chunk_size: SampleCount, sample_rate: u32) -> Vec { + + let mut mixed = vec![0.0; chunk_size.as_usize()]; + let playing = matches!(self.playback_state, PlaybackState::Playing); + + for track in &mut self.tracks { + if let Some(samples) = track.render_audio(timestamp, chunk_size, sample_rate, playing) { + for (i, sample) in samples.iter().enumerate() { + mixed[i] += *sample; + } + } + } + + mixed } - + + pub fn update_video(&self, timestamp: Timestamp) -> Vec { + let playing = matches!(self.playback_state, PlaybackState::Playing); + self.tracks + .iter() + .filter_map(|track| track.render_video(timestamp, playing)) + .collect() + } + pub fn play(&mut self, start_timestamp: Timestamp) { self.timestamp = start_timestamp; self.playback_state = PlaybackState::Playing; } - pub fn stop(&mut self) { - self.playback_state = PlaybackState::Stopped; + self.playback_state = PlaybackState::Stopped; } - - // pub fn play(&mut self, timestamp: Timestamp, audio_output: &mut dyn AudioOutput, video_output: &mut dyn FrameTarget) { - // let mut timestamp = timestamp.clone(); - - // // Main playback loop - // loop { - // // Render and play audio chunks - // let mut audio_mix: Vec = vec![0.0; self.frame_duration.to_samples(self.sample_rate) as usize]; - // for track in &mut self.audio_tracks { - // let chunk = track.render_chunk(timestamp, self.frame_duration); - // for (i, sample) in chunk.iter().enumerate() { - // audio_mix[i] += sample; // Simple mixing (sum of samples) - // } - // } - // audio_output.play_chunk(audio_mix); - - // // Render video frames - // for track in &self.video_tracks { - // let track_frame = track.render_frame(timestamp); - // } - - // // Update timestamp - // timestamp += self.frame_duration; - - // // Break condition (e.g., end of tracks) - // if self.audio_tracks.iter().all(|t| t.render_chunk(timestamp, self.frame_duration).is_empty()) { - // break; - // } - // } - // } - pub fn update_audio(&mut self, timestamp: Timestamp, playing: bool, chunk_size: SampleCount, sample_rate: u32) -> Vec { - let mut mixed_audio = vec![0.0; chunk_size.as_usize()]; - - // TODO: render video - for track in &mut self.audio_tracks { - let track_audio = track.render_chunk(timestamp, chunk_size); - for (i, sample) in track_audio.iter().enumerate() { - mixed_audio[i] += *sample; // Simple mixing, add samples together - } - } - - mixed_audio + pub fn get_tracks(&self) -> &Vec> { + &self.tracks } } @@ -136,39 +113,75 @@ enum PlaybackState { pub struct SineWaveTrack { frequency: f32, phase: f32, - sample_rate: u32, + name: String, } impl SineWaveTrack { - pub fn new(frequency: f32, sample_rate: u32) -> Self { + pub fn new(frequency: f32) -> Self { Self { frequency, phase: 0.0, - sample_rate, + name: "Sine Wave Track".to_string(), } } } -impl AudioTrack for SineWaveTrack { - fn render_chunk(&mut self, timestamp: Timestamp, chunk_size: SampleCount) -> Vec { +impl Track for SineWaveTrack { + fn get_name(&self) -> &str { + &self.name + } + fn set_name(&mut self, name: String) { + self.name = name; + } + fn render_audio(&mut self, _timestamp: Timestamp, chunk_size: SampleCount, sample_rate: u32, playing: bool) -> Option> { let mut chunk = Vec::with_capacity(chunk_size.as_usize()); - let phase_increment = (2.0 * std::f32::consts::PI * self.frequency) / self.sample_rate as f32; - + let phase_increment = (2.0 * std::f32::consts::PI * self.frequency) / sample_rate as f32; + for _ in 0..chunk_size.as_usize() { - chunk.push((self.phase).sin()); - self.phase += phase_increment; - if self.phase > 2.0 * std::f32::consts::PI { - self.phase -= 2.0 * std::f32::consts::PI; - } + if playing { + chunk.push((self.phase).sin()*0.25); + } else { + chunk.push(0.0); + } + self.phase += phase_increment; + if self.phase > 2.0 * std::f32::consts::PI { + self.phase -= 2.0 * std::f32::consts::PI; + } } - - chunk - } - fn sample_rate(&self) -> u32 { - self.sample_rate + + Some(chunk) } } +#[cfg(feature="wasm")] +#[wasm_bindgen] +pub struct JsTrack { + name: String, +} + +#[cfg(feature="wasm")] +#[wasm_bindgen] +impl JsTrack { + #[wasm_bindgen(getter)] + pub fn name(&self) -> String { + self.name.clone() + } +} +#[cfg(feature="wasm")] +impl fmt::Display for JsTrack { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "JsTrack {{ name: {} }}", self.name) + } +} + +#[cfg(feature="wasm")] +#[wasm_bindgen] +impl JsTrack { + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + format!("{}", self) // Calls the Display implementation + } +} #[cfg(feature="wasm")] #[wasm_bindgen] @@ -183,9 +196,9 @@ pub struct CoreInterface { #[wasm_bindgen] impl CoreInterface { #[wasm_bindgen(constructor)] - pub fn new(sample_rate: u32, frame_duration: f64) -> Self { + pub fn new() -> Self { Self { - track_manager: Arc::new(Mutex::new(TrackManager::new(sample_rate, frame_duration))), + track_manager: Arc::new(Mutex::new(TrackManager::new())), cpal_audio_output: Box::new(CpalAudioOutput::new()) } } @@ -193,45 +206,72 @@ impl CoreInterface { println!("Init CoreInterface"); let track_manager_clone = self.track_manager.clone(); self.cpal_audio_output.register_track_manager(track_manager_clone); - self.cpal_audio_output.start(); + let _ = self.cpal_audio_output.start(); } pub fn play(&mut self, timestamp: f64) { // Lock the Mutex to get access to TrackManager let mut track_manager = self.track_manager.lock().unwrap(); track_manager.play(Timestamp::new(timestamp)); } + pub fn stop(&mut self) { + // Lock the Mutex to get access to TrackManager + let mut track_manager = self.track_manager.lock().unwrap(); + track_manager.stop(); + } + pub fn add_sine_track(&mut self, frequency: f32) -> Result<(), String> { + if frequency.is_nan() || frequency.is_infinite() || frequency <= 0.0 { + return Err(format!("Invalid frequency: {}", frequency)); + } + log::info!("Freq: {}", frequency); + let mut track_manager = self.track_manager.lock().unwrap(); + let sine_track = SineWaveTrack::new(frequency); + track_manager.add_track(Box::new(sine_track)); + + Ok(()) + } + pub fn get_timestamp(&mut self) -> f64 { self.cpal_audio_output.get_timestamp().as_seconds() } + pub fn get_tracks(&mut self) -> Vec { + let track_manager = self.track_manager.lock().unwrap(); + let tracks = track_manager.get_tracks(); + tracks + .iter() + .map(|track| JsTrack { + name: track.get_name().to_string(), + }) + .collect() + } } struct PlainTextLogger; impl Log for PlainTextLogger { - fn enabled(&self, metadata: &Metadata) -> bool { - metadata.level() <= Level::Info + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= Level::Info + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + console::log_1(&format!( + "{} [{}:{}] {}", + record.level(), + record.file().unwrap_or("unknown"), + record.line().unwrap_or(0), + record.args() + ).into()); } - - fn log(&self, record: &Record) { - if self.enabled(record.metadata()) { - console::log_1(&format!( - "{} [{}:{}] {}", - record.level(), - record.file().unwrap_or("unknown"), - record.line().unwrap_or(0), - record.args() - ).into()); - } - } - - fn flush(&self) {} + } + + fn flush(&self) {} } pub fn init_plain_text_logger() -> Result<(), SetLoggerError> { - log::set_boxed_logger(Box::new(PlainTextLogger))?; - log::set_max_level(LevelFilter::Info); - Ok(()) + log::set_boxed_logger(Box::new(PlainTextLogger))?; + log::set_max_level(LevelFilter::Info); + Ok(()) } #[cfg(test)] @@ -249,14 +289,14 @@ mod tests { #[cfg(feature="wasm")] #[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(); - init_plain_text_logger().expect("Failed to initialize plain text logger"); - - - log::info!("Logger initialized!"); - - Ok(()) + // 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(); + init_plain_text_logger().expect("Failed to initialize plain text logger"); + + + log::info!("Logger initialized!"); + + Ok(()) } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 475b157..bb0bd79 100644 --- a/src/main.js +++ b/src/main.js @@ -78,6 +78,8 @@ const { Menu, MenuItem, PredefinedMenuItem, Submenu } = window.__TAURI__.menu; const { getCurrentWindow } = window.__TAURI__.window; const { getVersion } = window.__TAURI__.app; +import init, { CoreInterface } from './pkg/lightningbeam_core.js'; + window.onerror = (message, source, lineno, colno, error) => { invoke("error", { msg: `${message} at ${source}:${lineno}:${colno}\n${error?.stack || ''}` }); }; @@ -8408,3 +8410,14 @@ if (window.openedFiles?.length>0) { newWindow(window.openedFiles[i]) } } + +async function testAudio() { + console.log("Starting rust") + await init(); + console.log("Rust started") + const coreInterface = new CoreInterface(100, 100) + coreInterface.init() + coreInterface.play(0.0) + console.log(coreInterface) +} +testAudio() \ No newline at end of file diff --git a/src/pkg/lightningbeam_core.d.ts b/src/pkg/lightningbeam_core.d.ts new file mode 100644 index 0000000..87d6b82 --- /dev/null +++ b/src/pkg/lightningbeam_core.d.ts @@ -0,0 +1,66 @@ +/* tslint:disable */ +/* eslint-disable */ +export function main_js(): void; +export class CoreInterface { + free(): void; + constructor(); + init(): void; + play(timestamp: number): void; + stop(): void; + add_sine_track(frequency: number): void; + get_timestamp(): number; + get_tracks(): JsTrack[]; +} +export class JsTrack { + private constructor(); + free(): void; + toString(): string; + readonly name: string; +} + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly __wbg_jstrack_free: (a: number, b: number) => void; + readonly jstrack_name: (a: number, b: number) => void; + readonly jstrack_toString: (a: number, b: number) => void; + readonly __wbg_coreinterface_free: (a: number, b: number) => void; + readonly coreinterface_new: () => number; + readonly coreinterface_init: (a: number) => void; + readonly coreinterface_play: (a: number, b: number) => void; + readonly coreinterface_stop: (a: number) => void; + readonly coreinterface_add_sine_track: (a: number, b: number, c: number) => void; + readonly coreinterface_get_timestamp: (a: number) => number; + readonly coreinterface_get_tracks: (a: number, b: number) => void; + readonly main_js: () => void; + readonly __wbindgen_exn_store: (a: number) => void; + readonly __wbindgen_export_1: WebAssembly.Table; + readonly __wbindgen_malloc: (a: number, b: number) => number; + readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_add_to_stack_pointer: (a: number) => number; + readonly __wbindgen_free: (a: number, b: number, c: number) => void; + readonly _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h03a328ab39659ec3: (a: number, b: number) => void; + readonly __wbindgen_start: () => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ +export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/src/pkg/lightningbeam_core.js b/src/pkg/lightningbeam_core.js new file mode 100644 index 0000000..0ef6268 --- /dev/null +++ b/src/pkg/lightningbeam_core.js @@ -0,0 +1,655 @@ +const lAudioContext = (typeof AudioContext !== 'undefined' ? AudioContext : (typeof webkitAudioContext !== 'undefined' ? webkitAudioContext : undefined)); +let wasm; + +const heap = new Array(128).fill(undefined); + +heap.push(undefined, null, true, false); + +function getObject(idx) { return heap[idx]; } + +let heap_next = heap.length; + +function addHeapObject(obj) { + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; + + heap[idx] = obj; + return idx; +} + +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + wasm.__wbindgen_exn_store(addHeapObject(e)); + } +} + +let cachedFloat32ArrayMemory0 = null; + +function getFloat32ArrayMemory0() { + if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) { + cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer); + } + return cachedFloat32ArrayMemory0; +} + +function getArrayF32FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getFloat32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len); +} + +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); + +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +function isLikeNone(x) { + return x === undefined || x === null; +} + +function dropObject(idx) { + if (idx < 132) return; + heap[idx] = heap_next; + heap_next = idx; +} + +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; +} + +const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(state => { + wasm.__wbindgen_export_1.get(state.dtor)(state.a, state.b) +}); + +function makeMutClosure(arg0, arg1, dtor, f) { + const state = { a: arg0, b: arg1, cnt: 1, dtor }; + const real = (...args) => { + // First up with a closure we increment the internal reference + // count. This ensures that the Rust closure environment won't + // be deallocated while we're invoking it. + state.cnt++; + const a = state.a; + state.a = 0; + try { + return f(a, state.b, ...args); + } finally { + if (--state.cnt === 0) { + wasm.__wbindgen_export_1.get(state.dtor)(a, state.b); + CLOSURE_DTORS.unregister(state); + } else { + state.a = a; + } + } + }; + real.original = state; + CLOSURE_DTORS.register(real, state, state); + return real; +} + +function debugString(val) { + // primitive types + const type = typeof val; + if (type == 'number' || type == 'boolean' || val == null) { + return `${val}`; + } + if (type == 'string') { + return `"${val}"`; + } + if (type == 'symbol') { + const description = val.description; + if (description == null) { + return 'Symbol'; + } else { + return `Symbol(${description})`; + } + } + if (type == 'function') { + const name = val.name; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } + } + // objects + if (Array.isArray(val)) { + const length = val.length; + let debug = '['; + if (length > 0) { + debug += debugString(val[0]); + } + for(let i = 1; i < length; i++) { + debug += ', ' + debugString(val[i]); + } + debug += ']'; + return debug; + } + // Test for built-in + const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); + let className; + if (builtInMatches && builtInMatches.length > 1) { + className = builtInMatches[1]; + } else { + // Failed to match the standard '[object ClassName]' + return toString.call(val); + } + if (className == 'Object') { + // we're a user defined class or Object + // JSON.stringify avoids problems with cycles, and is generally much + // easier than looping through ownProperties of `val`. + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } + } + // errors + if (val instanceof Error) { + return `${val.name}: ${val.message}\n${val.stack}`; + } + // TODO we could test for more things here, like `Set`s and `Map`s. + return className; +} + +let WASM_VECTOR_LEN = 0; + +const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8ArrayMemory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +let cachedDataViewMemory0 = null; + +function getDataViewMemory0() { + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); + } + return cachedDataViewMemory0; +} + +function getArrayJsValueFromWasm0(ptr, len) { + ptr = ptr >>> 0; + const mem = getDataViewMemory0(); + const result = []; + for (let i = ptr; i < ptr + 4 * len; i += 4) { + result.push(takeObject(mem.getUint32(i, true))); + } + return result; +} + +export function main_js() { + wasm.main_js(); +} + +function __wbg_adapter_18(arg0, arg1) { + wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h03a328ab39659ec3(arg0, arg1); +} + +const CoreInterfaceFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_coreinterface_free(ptr >>> 0, 1)); + +export class CoreInterface { + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + CoreInterfaceFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_coreinterface_free(ptr, 0); + } + constructor() { + const ret = wasm.coreinterface_new(); + this.__wbg_ptr = ret >>> 0; + CoreInterfaceFinalization.register(this, this.__wbg_ptr, this); + return this; + } + init() { + wasm.coreinterface_init(this.__wbg_ptr); + } + /** + * @param {number} timestamp + */ + play(timestamp) { + wasm.coreinterface_play(this.__wbg_ptr, timestamp); + } + stop() { + wasm.coreinterface_stop(this.__wbg_ptr); + } + /** + * @param {number} frequency + */ + add_sine_track(frequency) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.coreinterface_add_sine_track(retptr, this.__wbg_ptr, frequency); + var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); + var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); + if (r1) { + throw takeObject(r0); + } + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } + } + /** + * @returns {number} + */ + get_timestamp() { + const ret = wasm.coreinterface_get_timestamp(this.__wbg_ptr); + return ret; + } + /** + * @returns {JsTrack[]} + */ + get_tracks() { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.coreinterface_get_tracks(retptr, this.__wbg_ptr); + var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); + var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4, 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } + } +} + +const JsTrackFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_jstrack_free(ptr >>> 0, 1)); + +export class JsTrack { + + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JsTrack.prototype); + obj.__wbg_ptr = ptr; + JsTrackFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JsTrackFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jstrack_free(ptr, 0); + } + /** + * @returns {string} + */ + get name() { + let deferred1_0; + let deferred1_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jstrack_name(retptr, this.__wbg_ptr); + var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); + var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); + deferred1_0 = r0; + deferred1_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * @returns {string} + */ + toString() { + let deferred1_0; + let deferred1_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jstrack_toString(retptr, this.__wbg_ptr); + var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); + var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); + deferred1_0 = r0; + deferred1_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_call_672a4d21634d4a24 = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).call(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_close_5a97ef05b337f8ce = function() { return handleError(function (arg0) { + const ret = getObject(arg0).close(); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_connect_b22945d106632a36 = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).connect(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_copyToChannel_b81ecf19fd54e146 = function() { return handleError(function (arg0, arg1, arg2, arg3) { + getObject(arg0).copyToChannel(getArrayF32FromWasm0(arg1, arg2), arg3); + }, arguments) }; + imports.wbg.__wbg_createBufferSource_f7860a96f709acbd = function() { return handleError(function (arg0) { + const ret = getObject(arg0).createBufferSource(); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_createBuffer_926beeec3ff39b5a = function() { return handleError(function (arg0, arg1, arg2, arg3) { + const ret = getObject(arg0).createBuffer(arg1 >>> 0, arg2 >>> 0, arg3); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_currentTime_adef4d803f58eb66 = function(arg0) { + const ret = getObject(arg0).currentTime; + return ret; + }; + imports.wbg.__wbg_destination_6400091abd6f01b3 = function(arg0) { + const ret = getObject(arg0).destination; + return addHeapObject(ret); + }; + imports.wbg.__wbg_eval_e10dc02e9547f640 = function() { return handleError(function (arg0, arg1) { + const ret = eval(getStringFromWasm0(arg0, arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_instanceof_Window_def73ea0955fc569 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof Window; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_jstrack_new = function(arg0) { + const ret = JsTrack.__wrap(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbg_log_c222819a41e063d3 = function(arg0) { + console.log(getObject(arg0)); + }; + imports.wbg.__wbg_maxChannelCount_a06f8ca4190698ed = function(arg0) { + const ret = getObject(arg0).maxChannelCount; + return ret; + }; + imports.wbg.__wbg_new_405e22f390576ce2 = function() { + const ret = new Object(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) { + const ret = new Function(getStringFromWasm0(arg0, arg1)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_newwithcontextoptions_b62c06fed7900366 = function() { return handleError(function (arg0) { + const ret = new lAudioContext(getObject(arg0)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_resume_35efdc4ffe13bf18 = function() { return handleError(function (arg0) { + const ret = getObject(arg0).resume(); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_setTimeout_f2fe5af8e3debeb3 = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).setTimeout(getObject(arg1), arg2); + return ret; + }, arguments) }; + imports.wbg.__wbg_setbuffer_10a9ee2a05c73896 = function(arg0, arg1) { + getObject(arg0).buffer = getObject(arg1); + }; + imports.wbg.__wbg_setchannelCount_876fcf5798895180 = function(arg0, arg1) { + getObject(arg0).channelCount = arg1 >>> 0; + }; + imports.wbg.__wbg_setonended_00ff85c70a4f819f = function(arg0, arg1) { + getObject(arg0).onended = getObject(arg1); + }; + imports.wbg.__wbg_setsamplerate_8bc3fd769a6db02b = function(arg0, arg1) { + getObject(arg0).sampleRate = arg1; + }; + imports.wbg.__wbg_start_e81f89e130c3c86e = function() { return handleError(function (arg0, arg1) { + getObject(arg0).start(arg1); + }, arguments) }; + imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { + const ret = typeof global === 'undefined' ? null : global; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { + const ret = typeof globalThis === 'undefined' ? null : globalThis; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { + const ret = typeof self === 'undefined' ? null : self; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { + const ret = typeof window === 'undefined' ? null : window; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbindgen_boolean_get = function(arg0) { + const v = getObject(arg0); + const ret = typeof(v) === 'boolean' ? (v ? 1 : 0) : 2; + return ret; + }; + imports.wbg.__wbindgen_cb_drop = function(arg0) { + const obj = takeObject(arg0).original; + if (obj.cnt-- == 1) { + obj.a = 0; + return true; + } + const ret = false; + return ret; + }; + imports.wbg.__wbindgen_closure_wrapper99 = function(arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 40, __wbg_adapter_18); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { + const ret = debugString(getObject(arg1)); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbindgen_is_undefined = function(arg0) { + const ret = getObject(arg0) === undefined; + return ret; + }; + imports.wbg.__wbindgen_object_clone_ref = function(arg0) { + const ret = getObject(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); + }; + imports.wbg.__wbindgen_string_new = function(arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + + return imports; +} + +function __wbg_init_memory(imports, memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedDataViewMemory0 = null; + cachedFloat32ArrayMemory0 = null; + cachedUint8ArrayMemory0 = null; + + + wasm.__wbindgen_start(); + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('lightningbeam_core_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync }; +export default __wbg_init; diff --git a/src/pkg/lightningbeam_core_bg.wasm b/src/pkg/lightningbeam_core_bg.wasm new file mode 100644 index 0000000..720d434 Binary files /dev/null and b/src/pkg/lightningbeam_core_bg.wasm differ diff --git a/src/pkg/lightningbeam_core_bg.wasm.d.ts b/src/pkg/lightningbeam_core_bg.wasm.d.ts new file mode 100644 index 0000000..3d84001 --- /dev/null +++ b/src/pkg/lightningbeam_core_bg.wasm.d.ts @@ -0,0 +1,23 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export const __wbg_jstrack_free: (a: number, b: number) => void; +export const jstrack_name: (a: number, b: number) => void; +export const jstrack_toString: (a: number, b: number) => void; +export const __wbg_coreinterface_free: (a: number, b: number) => void; +export const coreinterface_new: () => number; +export const coreinterface_init: (a: number) => void; +export const coreinterface_play: (a: number, b: number) => void; +export const coreinterface_stop: (a: number) => void; +export const coreinterface_add_sine_track: (a: number, b: number, c: number) => void; +export const coreinterface_get_timestamp: (a: number) => number; +export const coreinterface_get_tracks: (a: number, b: number) => void; +export const main_js: () => void; +export const __wbindgen_exn_store: (a: number) => void; +export const __wbindgen_export_1: WebAssembly.Table; +export const __wbindgen_malloc: (a: number, b: number) => number; +export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; +export const __wbindgen_add_to_stack_pointer: (a: number) => number; +export const __wbindgen_free: (a: number, b: number, c: number) => void; +export const _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h03a328ab39659ec3: (a: number, b: number) => void; +export const __wbindgen_start: () => void; diff --git a/src/pkg/package.json b/src/pkg/package.json new file mode 100644 index 0000000..b23063e --- /dev/null +++ b/src/pkg/package.json @@ -0,0 +1,14 @@ +{ + "name": "lightningbeam-core", + "version": "0.1.0", + "files": [ + "lightningbeam_core_bg.wasm", + "lightningbeam_core.js", + "lightningbeam_core.d.ts" + ], + "module": "lightningbeam_core.js", + "types": "lightningbeam_core.d.ts", + "sideEffects": [ + "./snippets/*" + ] +} \ No newline at end of file