Release 1.0.4-alpha
This commit is contained in:
commit
b4e5469b51
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,42 @@
|
|||
[package]
|
||||
name = "lightningbeam-core"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
wasm-bindgen = "0.2.45"
|
||||
cpal = { version = "0.15", features = ["wasm-bindgen"] }
|
||||
anyhow = "1.0"
|
||||
wasm-logger = "0.2"
|
||||
log = "0.4"
|
||||
rubato = "0.14.0"
|
||||
symphonia = { version = "0.5", features = ["all"] }
|
||||
crossbeam-channel = "0.5.4"
|
||||
atomic_refcell = "0.1.13" # WASM-compatible atomic refcell
|
||||
parking_lot = "0.12"
|
||||
|
||||
[dependencies.web-sys]
|
||||
version = "0.3.22"
|
||||
features = ["console", "AudioContext", "Window", "Performance", "PerformanceTiming"]
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
wasm-bindgen-futures = "0.4"
|
||||
js-sys = "0.3"
|
||||
web-time = "0.2" # WASM-compatible timing
|
||||
gloo-timers = { version = "0.2", features = ["futures"] }
|
||||
|
||||
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||
# logging them with `console.error`. This is great for development, but requires
|
||||
# all the `std::fmt` and `std::panicking` infrastructure, so it's only enabled
|
||||
# in debug mode.
|
||||
[target."cfg(debug_assertions)".dependencies]
|
||||
console_error_panic_hook = "0.1.5"
|
||||
|
||||
[features]
|
||||
default = ["native"]
|
||||
native = []
|
||||
wasm = []
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
echo "Building native..."
|
||||
cargo build
|
||||
echo
|
||||
echo "Building wasm..."
|
||||
wasm-pack build --target web --out-dir ../src/pkg --features wasm
|
||||
|
|
@ -0,0 +1,380 @@
|
|||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{Sample};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::{TrackManager, Timestamp, Duration, SampleCount, AudioOutput, PlaybackState};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_time::{Instant, Duration as StdDuration};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::time::{Instant, Duration as StdDuration};
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
use std::cell::Cell;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
enum AudioState {
|
||||
Suspended,
|
||||
Running,
|
||||
}
|
||||
|
||||
const DELAY_HISTORY_SIZE: usize = 5;
|
||||
|
||||
#[derive(Default)]
|
||||
struct StutterDetector {
|
||||
delay_history: Mutex<VecDeque<StdDuration>>,
|
||||
desired_buffer_size: AtomicU32,
|
||||
current_buffer_size: AtomicU32,
|
||||
max_buffer_size: AtomicU32,
|
||||
stutter_count: AtomicU32,
|
||||
max_stutter_count: AtomicU32,
|
||||
last_callback_time: Cell<Option<Instant>>,
|
||||
scheduling_threshold: AtomicU32,
|
||||
}
|
||||
|
||||
pub struct CpalAudioOutput {
|
||||
track_manager: Option<Arc<Mutex<TrackManager>>>,
|
||||
_stream: Option<cpal::Stream>,
|
||||
playback_state: PlaybackState,
|
||||
audio_state: AudioState,
|
||||
timestamp: Arc<Mutex<Timestamp>>,
|
||||
chunk_size: usize,
|
||||
sample_rate: u32,
|
||||
stutter_detector: Arc<Mutex<StutterDetector>>,
|
||||
resize_sender: crossbeam_channel::Sender<()>, // Or other channel implementation
|
||||
resize_receiver: crossbeam_channel::Receiver<()>,
|
||||
}
|
||||
|
||||
impl StutterDetector {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
delay_history: Mutex::new(VecDeque::with_capacity(DELAY_HISTORY_SIZE)),
|
||||
desired_buffer_size: AtomicU32::new(256),
|
||||
current_buffer_size: AtomicU32::new(256),
|
||||
max_buffer_size: AtomicU32::new(8192),
|
||||
stutter_count: AtomicU32::new(0),
|
||||
max_stutter_count: AtomicU32::new(3),
|
||||
last_callback_time: Cell::new(None),
|
||||
scheduling_threshold: AtomicU32::new(1200), // 1.2 stored in fixed point
|
||||
}
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
*self = Self::new();
|
||||
}
|
||||
fn get_scheduling_threshold(&self) -> f32 {
|
||||
self.scheduling_threshold.load(Ordering::Relaxed) as f32 / 1000.0
|
||||
}
|
||||
}
|
||||
|
||||
impl CpalAudioOutput {
|
||||
pub fn new() -> Self {
|
||||
let (tx, rx) = crossbeam_channel::bounded(1);
|
||||
Self {
|
||||
track_manager: None,
|
||||
_stream: None,
|
||||
playback_state: PlaybackState::Stopped,
|
||||
audio_state: AudioState::Suspended,
|
||||
timestamp: Arc::new(Mutex::new(Timestamp::from_seconds(0.0))),
|
||||
chunk_size: 0,
|
||||
sample_rate: 44100,
|
||||
stutter_detector: Arc::new(Mutex::new(StutterDetector::new())),
|
||||
resize_sender: tx,
|
||||
resize_receiver: rx,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_stream<T>(
|
||||
&mut self,
|
||||
device: &cpal::Device,
|
||||
config: cpal::SupportedStreamConfig,
|
||||
) -> Result<cpal::Stream, anyhow::Error>
|
||||
where
|
||||
T: Sample + From<f32> + cpal::SizedSample,
|
||||
{
|
||||
let supported_config = config.config();
|
||||
self.sample_rate = supported_config.sample_rate.0;
|
||||
let num_channels = supported_config.channels as usize;
|
||||
|
||||
let stutter_detector = self.stutter_detector.clone();
|
||||
let resize_sender = self.resize_sender.clone();
|
||||
let sample_rate = self.sample_rate;
|
||||
|
||||
let buffer_size_range = match config.buffer_size() {
|
||||
cpal::SupportedBufferSize::Range { min, max } => (*min, *max),
|
||||
cpal::SupportedBufferSize::Unknown => (256, 4096),
|
||||
};
|
||||
|
||||
let detector_guard = self.stutter_detector.lock().unwrap();
|
||||
let desired_buffer_size = detector_guard.desired_buffer_size.load(Ordering::Relaxed);
|
||||
drop(detector_guard);
|
||||
|
||||
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);
|
||||
|
||||
log::info!("Starting stream with buffer size {}", clamped_buffer_size);
|
||||
|
||||
let track_manager = self.track_manager.clone();
|
||||
let timestamp = self.timestamp.clone();
|
||||
|
||||
let err_fn = |err| log::error!("Audio stream error: {:?}", err);
|
||||
|
||||
let stream = device.build_output_stream(
|
||||
&stream_config,
|
||||
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
|
||||
// Timing measurement
|
||||
let processing_start = if cfg!(target_arch = "wasm32") {
|
||||
let perf = web_sys::window()
|
||||
.and_then(|w| w.performance())
|
||||
.expect("performance should be available");
|
||||
let now_ms = perf.now();
|
||||
Instant::now() + StdDuration::from_secs_f64(now_ms / 1000.0)
|
||||
} else {
|
||||
Instant::now()
|
||||
};
|
||||
|
||||
// Initialize resize flag outside of lock scope
|
||||
let mut should_resize = false;
|
||||
let current_size;
|
||||
let buffer_duration;
|
||||
let scheduling_threshold;
|
||||
|
||||
{
|
||||
let detector = stutter_detector.lock().unwrap();
|
||||
|
||||
// Update detector state
|
||||
current_size = detector.current_buffer_size.load(Ordering::Relaxed);
|
||||
buffer_duration = StdDuration::from_secs_f64(current_size as f64 / sample_rate as f64);
|
||||
scheduling_threshold = detector.get_scheduling_threshold();
|
||||
|
||||
// Calculate scheduling delay
|
||||
let last_time = detector.last_callback_time.get();
|
||||
// log::info!("Current size: {}", current_size);
|
||||
|
||||
// Audio processing
|
||||
if let Some(track_manager) = &track_manager {
|
||||
let num_frames = data.len() / num_channels; // Stereo: divide by 2
|
||||
let sample_count = SampleCount::new(num_frames);
|
||||
let chunk_duration = Duration::new(num_frames as f64 / sample_rate as f64);
|
||||
|
||||
let mut track_manager = track_manager.lock().unwrap();
|
||||
|
||||
let mut timestamp_guard = timestamp.lock().unwrap();
|
||||
let timestamp = &mut *timestamp_guard;
|
||||
|
||||
let chunk = track_manager.update_audio(
|
||||
timestamp.clone(),
|
||||
sample_count,
|
||||
sample_rate,
|
||||
);
|
||||
|
||||
// Write samples (interleaved stereo)
|
||||
for (i, frame) in chunk.iter().enumerate() {
|
||||
let sample = T::from(*frame);
|
||||
for channel in 0..num_channels {
|
||||
let index = i * num_channels + channel;
|
||||
if index < data.len() {
|
||||
data[index] = sample;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*timestamp_guard += chunk_duration;
|
||||
|
||||
// Stutter detection logic
|
||||
let processing_time = processing_start.elapsed();
|
||||
let processing_overrun = processing_time > buffer_duration;
|
||||
|
||||
// Update delay history
|
||||
if let Some(last) = last_time {
|
||||
let interval = processing_start.duration_since(last);
|
||||
let mut history = detector.delay_history.lock().unwrap();
|
||||
if history.len() >= 5 {
|
||||
history.pop_front();
|
||||
}
|
||||
history.push_back(interval);
|
||||
// log::info!("Interval: {:?}", interval);
|
||||
}
|
||||
|
||||
// Calculate average delay
|
||||
let avg_delay = {
|
||||
let history = detector.delay_history.lock().unwrap();
|
||||
if history.is_empty() {
|
||||
StdDuration::ZERO
|
||||
} else {
|
||||
history.iter().sum::<StdDuration>() / history.len() as u32
|
||||
}
|
||||
};
|
||||
|
||||
// log::info!("Average delay: {:?}", avg_delay);
|
||||
|
||||
// Determine stutter
|
||||
let stutter_detected = avg_delay > buffer_duration.mul_f32(scheduling_threshold)
|
||||
|| processing_overrun;
|
||||
|
||||
// Update stutter count with hysteresis
|
||||
let current_count = detector.stutter_count.load(Ordering::Relaxed);
|
||||
if stutter_detected {
|
||||
detector.stutter_count.store(
|
||||
(current_count + 1).min(detector.max_stutter_count.load(Ordering::Relaxed)),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
} else {
|
||||
detector.stutter_count.store(
|
||||
current_count.saturating_sub(1),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
}
|
||||
|
||||
// Check for resize
|
||||
if detector.stutter_count.load(Ordering::Relaxed) >= detector.max_stutter_count.load(Ordering::Relaxed) {
|
||||
let desired_size = detector.desired_buffer_size.load(Ordering::Relaxed);
|
||||
let new_size = (desired_size * 2).min(detector.max_buffer_size.load(Ordering::Relaxed));
|
||||
|
||||
if new_size != desired_size {
|
||||
detector.desired_buffer_size.store(new_size, Ordering::Relaxed);
|
||||
detector.stutter_count.store(0, Ordering::Relaxed);
|
||||
should_resize = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
detector.last_callback_time.set(Some(processing_start));
|
||||
}
|
||||
|
||||
// Send resize request outside of lock
|
||||
if should_resize {
|
||||
let _ = resize_sender.try_send(());
|
||||
}
|
||||
},
|
||||
err_fn,
|
||||
None,
|
||||
)?;
|
||||
|
||||
// Update current buffer size after stream creation
|
||||
let detector = self.stutter_detector.lock().unwrap();
|
||||
detector.current_buffer_size.store(clamped_buffer_size, Ordering::Relaxed);
|
||||
|
||||
Ok(stream)
|
||||
}
|
||||
|
||||
fn recreate_stream(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Stop and destroy old stream first
|
||||
if let Some(old_stream) = self._stream.take() {
|
||||
old_stream.pause()?;
|
||||
// Explicitly drop the stream
|
||||
drop(old_stream);
|
||||
}
|
||||
|
||||
// Add a small delay to ensure resources are freed (especially important in WASM)
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use gloo_timers::future::sleep;
|
||||
spawn_local(async {
|
||||
sleep(std::time::Duration::from_millis(50)).await;
|
||||
});
|
||||
}
|
||||
|
||||
// Recreate stream with current configuration
|
||||
let host = cpal::default_host();
|
||||
let device = host.default_output_device()
|
||||
.ok_or_else(|| "No output device available")?;
|
||||
let supported_config = device.default_output_config()?;
|
||||
|
||||
{
|
||||
let mut detector = self.stutter_detector.lock().unwrap();
|
||||
let desired_buffer_size = detector.desired_buffer_size.load(Ordering::Relaxed);
|
||||
detector.reset();
|
||||
detector.desired_buffer_size.store(desired_buffer_size, Ordering::Relaxed);
|
||||
}
|
||||
// let mut history = detector.delay_history.lock().unwrap();
|
||||
|
||||
self._stream = Some(self.build_stream::<f32>(&device, supported_config)?);
|
||||
|
||||
// Restart playback if needed
|
||||
if self.audio_state == AudioState::Running {
|
||||
self._stream.as_ref().unwrap().play()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl AudioOutput for CpalAudioOutput {
|
||||
fn start(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let host = cpal::default_host();
|
||||
let device = host
|
||||
.default_output_device()
|
||||
.ok_or_else(|| "No output device available")?;
|
||||
let supported_config = device.default_output_config()?;
|
||||
self._stream = Some(self.build_stream::<f32>(&device, supported_config)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn play(&mut self, start_timestamp: Timestamp) {
|
||||
self.timestamp.lock().unwrap().set(start_timestamp);
|
||||
self.playback_state = PlaybackState::Playing;
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
self.playback_state = PlaybackState::Stopped;
|
||||
}
|
||||
|
||||
fn resume(&mut self) -> Result<(), anyhow::Error> {
|
||||
if self.audio_state == AudioState::Suspended {
|
||||
if let Some(stream) = &self._stream {
|
||||
stream.play()?;
|
||||
self.audio_state = AudioState::Running;
|
||||
log::info!("Audio resumed");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_track_manager(&mut self, track_manager: Arc<Mutex<TrackManager>>) {
|
||||
self.track_manager = Some(track_manager);
|
||||
}
|
||||
|
||||
fn get_timestamp(&mut self) -> Timestamp {
|
||||
*self.timestamp.lock().unwrap()
|
||||
}
|
||||
fn set_chunk_size(&mut self, chunk_size: usize) {
|
||||
self.chunk_size = chunk_size
|
||||
}
|
||||
fn check_resize(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Process resize requests with timeout
|
||||
let timeout = StdDuration::from_millis(10);
|
||||
while let Ok(()) = self.resize_receiver.try_recv() {
|
||||
let start = Instant::now();
|
||||
|
||||
// Try to lock, non-blocking
|
||||
{
|
||||
let detector = match self.stutter_detector.try_lock() {
|
||||
Ok(d) => d,
|
||||
Err(_) => {
|
||||
// Couldn't acquire lock immediately, skip this iteration
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
// Quick check before heavy operation
|
||||
if detector.desired_buffer_size.load(Ordering::Relaxed) == detector.current_buffer_size.load(Ordering::Relaxed) {
|
||||
continue;
|
||||
}
|
||||
detector.current_buffer_size.store(detector.desired_buffer_size.load(Ordering::Relaxed), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
// Actual stream recreation
|
||||
log::info!("Restarting stream");
|
||||
let _ = self.recreate_stream()?;
|
||||
|
||||
if Instant::now().duration_since(start) > timeout {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,679 @@
|
|||
mod time;
|
||||
use time::{Timestamp, Duration, Frame, SampleCount};
|
||||
mod audio;
|
||||
use audio::{CpalAudioOutput};
|
||||
use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
|
||||
use rubato::{FftFixedIn, Resampler};
|
||||
use std::io::Cursor;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::error::Error;
|
||||
|
||||
use symphonia::core::{
|
||||
audio::AudioBufferRef,
|
||||
audio::Signal,
|
||||
codecs::{DecoderOptions},
|
||||
formats::{FormatOptions},
|
||||
io::MediaSourceStream,
|
||||
meta::MetadataOptions,
|
||||
probe::Hint,
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::io;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::io::Write;
|
||||
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use std::fmt;
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
mod wasm_imports {
|
||||
pub use wasm_bindgen::prelude::*;
|
||||
pub use web_sys::console;
|
||||
}
|
||||
#[cfg(feature = "wasm")]
|
||||
use wasm_imports::*;
|
||||
|
||||
|
||||
pub trait Track: Send {
|
||||
fn get_name(&self) -> &str {
|
||||
"Unnamed Track"
|
||||
}
|
||||
|
||||
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<Vec<f32>> {
|
||||
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<Frame> {
|
||||
None
|
||||
}
|
||||
|
||||
// Set the sample rate of any audio this track might contain
|
||||
fn set_sample_rate(&mut self, _sample_rate: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TrackManager {
|
||||
tracks: Vec<Box<dyn Track>>,
|
||||
timestamp: Timestamp,
|
||||
playback_state: PlaybackState,
|
||||
}
|
||||
|
||||
impl TrackManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tracks: Vec::new(),
|
||||
timestamp: Timestamp::from_seconds(0.0),
|
||||
playback_state: PlaybackState::Stopped,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_track(&mut self, track: Box<dyn Track>) {
|
||||
self.tracks.push(track);
|
||||
}
|
||||
|
||||
pub fn update_audio(&mut self, timestamp: Timestamp, chunk_size: SampleCount, sample_rate: u32) -> Vec<f32> {
|
||||
|
||||
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<Frame> {
|
||||
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;
|
||||
}
|
||||
pub fn get_tracks(&self) -> &Vec<Box<dyn Track>> {
|
||||
&self.tracks
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AudioOutput {
|
||||
fn start(&mut self) -> Result<(), Box<dyn std::error::Error>>;
|
||||
fn play(&mut self, start_timestamp: Timestamp);
|
||||
fn stop(&mut self);
|
||||
fn resume(&mut self) -> Result<(), anyhow::Error>;
|
||||
fn register_track_manager(&mut self, track_manager: Arc<Mutex<TrackManager>>);
|
||||
fn get_timestamp(&mut self) -> Timestamp;
|
||||
fn set_chunk_size(&mut self, chunk_size: usize);
|
||||
fn check_resize(&mut self) -> Result<(), Box<dyn std::error::Error>>;
|
||||
}
|
||||
|
||||
pub trait FrameTarget {
|
||||
fn draw(&mut self, frame: &[u8], width: u32, height: u32);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
||||
enum PlaybackState {
|
||||
Playing,
|
||||
Stopped,
|
||||
}
|
||||
|
||||
pub struct SineWaveTrack {
|
||||
frequency: f32,
|
||||
phase: f32,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl SineWaveTrack {
|
||||
pub fn new(frequency: f32) -> Self {
|
||||
Self {
|
||||
frequency,
|
||||
phase: 0.0,
|
||||
name: "Sine Wave Track".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<Vec<f32>> {
|
||||
let mut chunk = Vec::with_capacity(chunk_size.as_usize());
|
||||
let phase_increment = (2.0 * std::f32::consts::PI * self.frequency) / sample_rate as f32;
|
||||
|
||||
for _ in 0..chunk_size.as_usize() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Some(chunk)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct AudioBuffer {
|
||||
original_data: Vec<f32>,
|
||||
original_sample_rate: u32,
|
||||
resampled_data: Vec<f32>,
|
||||
start_time: Timestamp,
|
||||
}
|
||||
|
||||
impl AudioBuffer {
|
||||
fn duration(&self) -> Duration {
|
||||
if self.resampled_data.is_empty() {
|
||||
Duration::from_seconds(0.0)
|
||||
} else {
|
||||
Duration::from_seconds(
|
||||
self.resampled_data.len() as f64 /
|
||||
self.original_sample_rate as f64
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct RecordedAudioTrack {
|
||||
name: String,
|
||||
buffers: Vec<AudioBuffer>,
|
||||
target_sample_rate: Option<u32>,
|
||||
}
|
||||
|
||||
|
||||
impl RecordedAudioTrack {
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
buffers: Vec::new(),
|
||||
target_sample_rate: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_buffer(&mut self, start_time: Timestamp, sample_rate: u32, data: Vec<f32>) {
|
||||
let resampled_data = match self.target_sample_rate {
|
||||
Some(target_rate) if sample_rate != target_rate =>
|
||||
self::resample(&data, sample_rate, target_rate),
|
||||
Some(_target_rate) =>
|
||||
data.clone(), // Already at target rate
|
||||
None =>
|
||||
Vec::new(), // Will be resampled later
|
||||
};
|
||||
|
||||
self.buffers.push(AudioBuffer {
|
||||
original_data: data,
|
||||
original_sample_rate: sample_rate,
|
||||
resampled_data,
|
||||
start_time,
|
||||
});
|
||||
|
||||
// Keep buffers sorted by start time
|
||||
self.buffers.sort_by(|a, b| a.start_time.partial_cmp(&b.start_time).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
impl Track for RecordedAudioTrack {
|
||||
fn get_name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
fn set_sample_rate(&mut self, target_rate: u32) {
|
||||
self.target_sample_rate = Some(target_rate);
|
||||
|
||||
for buffer in &mut self.buffers {
|
||||
if buffer.original_sample_rate == target_rate {
|
||||
buffer.resampled_data = buffer.original_data.clone();
|
||||
} else {
|
||||
buffer.resampled_data = self::resample(
|
||||
&buffer.original_data,
|
||||
buffer.original_sample_rate,
|
||||
target_rate
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_audio(
|
||||
&mut self,
|
||||
timestamp: Timestamp,
|
||||
duration: SampleCount,
|
||||
sample_rate: u32,
|
||||
playing: bool,
|
||||
) -> Option<Vec<f32>> {
|
||||
if !playing || self.target_sample_rate != Some(sample_rate) {
|
||||
return Some(vec![0.0; duration.as_usize()]);
|
||||
}
|
||||
|
||||
// let chunk_samples = duration.as_usize();
|
||||
let mut output = vec![0.0; duration.as_usize()];
|
||||
let mut remaining_samples = duration;
|
||||
let mut current_time = timestamp;
|
||||
|
||||
// Find the first buffer that overlaps with the requested time
|
||||
let mut buffer_index = match self.buffers.binary_search_by(|b| {
|
||||
b.start_time.partial_cmp(¤t_time).unwrap()
|
||||
}) {
|
||||
Ok(i) => i,
|
||||
Err(i) if i > 0 => i - 1, // Check previous buffer if timestamp is between buffers
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
while remaining_samples.as_usize() > 0 && buffer_index < self.buffers.len() {
|
||||
let buffer = &self.buffers[buffer_index];
|
||||
|
||||
// Calculate overlap with current buffer
|
||||
let buffer_start = buffer.start_time;
|
||||
let buffer_end = buffer_start + buffer.duration();
|
||||
|
||||
if current_time >= buffer_end {
|
||||
// Move to next buffer
|
||||
buffer_index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate how many samples we can take from this buffer
|
||||
let buffer_offset = ((current_time - buffer_start).as_seconds() * sample_rate as f64) as usize;
|
||||
let available_samples = SampleCount::new(buffer.resampled_data.len().saturating_sub(buffer_offset));
|
||||
let samples_to_take = remaining_samples.min(available_samples);
|
||||
|
||||
if samples_to_take == 0 {
|
||||
// No more samples in this buffer
|
||||
buffer_index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Copy samples from buffer to output
|
||||
let output_offset = duration - remaining_samples;
|
||||
output[output_offset.as_usize()..(output_offset + samples_to_take).as_usize()]
|
||||
.copy_from_slice(&buffer.resampled_data[buffer_offset..buffer_offset + samples_to_take.as_usize()]);
|
||||
|
||||
// Update state
|
||||
remaining_samples -= samples_to_take;
|
||||
current_time += samples_to_take.to_duration(sample_rate);
|
||||
}
|
||||
|
||||
Some(output)
|
||||
}
|
||||
}
|
||||
|
||||
fn resample(input: &[f32], input_rate: u32, output_rate: u32) -> Vec<f32> {
|
||||
if input_rate == output_rate {
|
||||
return input.to_vec();
|
||||
}
|
||||
|
||||
let input_rate = input_rate.try_into().unwrap();
|
||||
let output_rate = output_rate.try_into().unwrap();
|
||||
let chunk_size = input.len();
|
||||
|
||||
let mut resampler = FftFixedIn::new(
|
||||
output_rate,
|
||||
input_rate,
|
||||
chunk_size,
|
||||
1, // channel count
|
||||
2, // fft size
|
||||
).unwrap();
|
||||
|
||||
let output = resampler.process(&[input], None).unwrap();
|
||||
output[0].clone()
|
||||
}
|
||||
|
||||
pub trait AudioLoader {
|
||||
fn load_audio(
|
||||
&self,
|
||||
track: &mut RecordedAudioTrack,
|
||||
start_time: Timestamp,
|
||||
audio_data: &[u8],
|
||||
) -> Result<(), Box<dyn Error>>;
|
||||
}
|
||||
|
||||
pub struct GenericAudioLoader;
|
||||
|
||||
impl AudioLoader for GenericAudioLoader {
|
||||
fn load_audio(
|
||||
&self,
|
||||
track: &mut RecordedAudioTrack,
|
||||
start_time: Timestamp,
|
||||
audio_data: &[u8],
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
decode_audio(track, start_time, audio_data)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_audio(
|
||||
track: &mut RecordedAudioTrack,
|
||||
start_time: Timestamp,
|
||||
audio_data: &[u8],
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
// Create a media source from the byte slice
|
||||
let mss = MediaSourceStream::new(
|
||||
Box::new(Cursor::new(audio_data.to_vec())),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
// Use a fresh hint (no extension specified) for format detection
|
||||
let hint = Hint::new();
|
||||
|
||||
// Probe the media source for a supported format
|
||||
let probed = symphonia::default::get_probe()
|
||||
.format(&hint, mss, &FormatOptions::default(), &MetadataOptions::default())?;
|
||||
|
||||
// Get the format reader
|
||||
let mut format = probed.format;
|
||||
|
||||
// Find the first supported audio track
|
||||
let default_track = format
|
||||
.tracks()
|
||||
.iter()
|
||||
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)
|
||||
.ok_or("No supported audio track found")?;
|
||||
|
||||
// Create a decoder for the track
|
||||
let mut decoder = symphonia::default::get_codecs()
|
||||
.make(&default_track.codec_params, &DecoderOptions::default())?;
|
||||
|
||||
// Get the sample rate from the track
|
||||
let sample_rate = default_track.codec_params.sample_rate.ok_or("Unknown sample rate")?;
|
||||
let mut decoded_samples = Vec::new();
|
||||
|
||||
// Decode loop
|
||||
loop {
|
||||
let packet = match format.next_packet() {
|
||||
Ok(packet) => packet,
|
||||
Err(_) => break, // End of stream
|
||||
};
|
||||
|
||||
match decoder.decode(&packet)? {
|
||||
AudioBufferRef::F32(buf) => {
|
||||
for i in 0..buf.frames() {
|
||||
for c in 0..buf.spec().channels.count() {
|
||||
decoded_samples.push(buf.chan(c)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
AudioBufferRef::S16(buf) => {
|
||||
for i in 0..buf.frames() {
|
||||
for c in 0..buf.spec().channels.count() {
|
||||
decoded_samples.push(buf.chan(c)[i] as f32 / 32768.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err("Unsupported audio format".into()),
|
||||
}
|
||||
}
|
||||
|
||||
// Add the decoded audio to the track
|
||||
track.add_buffer(start_time, sample_rate, decoded_samples);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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]
|
||||
pub struct CoreInterface {
|
||||
#[wasm_bindgen(skip)]
|
||||
track_manager: Arc<Mutex<TrackManager>>,
|
||||
#[wasm_bindgen(skip)]
|
||||
cpal_audio_output: Arc<Mutex<dyn AudioOutput>>,
|
||||
#[wasm_bindgen(skip)]
|
||||
resize_interval_id: Option<i32>,
|
||||
#[wasm_bindgen(skip)]
|
||||
resize_closure: Option<Closure<dyn FnMut()>>,
|
||||
}
|
||||
|
||||
#[cfg(feature="wasm")]
|
||||
#[wasm_bindgen]
|
||||
impl CoreInterface {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
track_manager: Arc::new(Mutex::new(TrackManager::new())),
|
||||
cpal_audio_output: Arc::new(Mutex::new(CpalAudioOutput::new())),
|
||||
resize_interval_id: None,
|
||||
resize_closure: None,
|
||||
}
|
||||
}
|
||||
pub fn init(&mut self) {
|
||||
println!("Init CoreInterface");
|
||||
{
|
||||
let track_manager_clone = self.track_manager.clone();
|
||||
let mut cpal_audio_output = self.cpal_audio_output.lock().unwrap();
|
||||
cpal_audio_output.register_track_manager(track_manager_clone);
|
||||
let _ = cpal_audio_output.start();
|
||||
}
|
||||
|
||||
self.start_resize_polling();
|
||||
}
|
||||
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 resume_audio(&mut self) -> Result<(), JsValue> {
|
||||
// Call this on user gestures if audio gets suspended
|
||||
self.cpal_audio_output.lock().unwrap().resume()
|
||||
.map_err(|e| JsValue::from_str(&format!("Failed to resume audio: {}", e)))
|
||||
}
|
||||
// In CoreInterface
|
||||
fn start_resize_polling(&mut self) {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
use wasm_bindgen::{prelude::*, JsCast};
|
||||
use js_sys::Array;
|
||||
|
||||
let window = web_sys::window().unwrap();
|
||||
let audio_output = Arc::clone(&self.cpal_audio_output);
|
||||
|
||||
// Use weak reference to break cycle
|
||||
let weak_audio = Arc::downgrade(&audio_output);
|
||||
let closure = Closure::<dyn FnMut()>::new(move || {
|
||||
if let Some(audio) = weak_audio.upgrade() {
|
||||
// NON-BLOCKING lock attempt
|
||||
if let Ok(mut audio) = audio.try_lock() {
|
||||
let _ = audio.check_resize();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let args = Array::new();
|
||||
let interval_id = window
|
||||
.set_interval_with_callback_and_timeout_and_arguments(
|
||||
closure.as_ref().unchecked_ref(),
|
||||
50,
|
||||
&args
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
self.resize_interval_id = Some(interval_id);
|
||||
self.resize_closure = Some(closure);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let mut audio = self.cpal_audio_output.clone();
|
||||
std::thread::spawn(move || loop {
|
||||
let _ = audio.check_resize();
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
});
|
||||
}
|
||||
}
|
||||
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.lock().unwrap().get_timestamp().as_seconds()
|
||||
}
|
||||
pub fn get_tracks(&mut self) -> Vec<JsTrack> {
|
||||
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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Cleanup implementation
|
||||
#[cfg(feature = "wasm")]
|
||||
impl Drop for CoreInterface {
|
||||
fn drop(&mut self) {
|
||||
if let Some(interval_id) = self.resize_interval_id {
|
||||
web_sys::window()
|
||||
.unwrap()
|
||||
.clear_interval_with_handle(interval_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PlainTextLogger;
|
||||
|
||||
impl Log for PlainTextLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= Level::Info
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// WASM: Log to the JS console
|
||||
console::log_1(
|
||||
&format!(
|
||||
"{} [{}:{}] {}",
|
||||
record.level(),
|
||||
record.file().unwrap_or("unknown"),
|
||||
record.line().unwrap_or(0),
|
||||
record.args()
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
// Native: Log to stderr
|
||||
let _ = writeln!(
|
||||
io::stderr(),
|
||||
"{} [{}:{}] {}",
|
||||
record.level(),
|
||||
record.file().unwrap_or("unknown"),
|
||||
record.line().unwrap_or(0),
|
||||
record.args()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let _ = io::stderr().flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_plain_text_logger() -> Result<(), SetLoggerError> {
|
||||
log::set_boxed_logger(Box::new(PlainTextLogger))?;
|
||||
log::set_max_level(LevelFilter::Info);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn it_works() {
|
||||
// let result = add(2, 2);
|
||||
// assert_eq!(result, 4);
|
||||
// }
|
||||
}
|
||||
|
||||
// This is like the `main` function, except for JavaScript.
|
||||
#[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(())
|
||||
}
|
||||
|
|
@ -0,0 +1,231 @@
|
|||
/// A strongly-typed representation of a timestamp (seconds).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Timestamp(f64);
|
||||
|
||||
/// A strongly-typed representation of a duration (seconds).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Duration(f64);
|
||||
|
||||
/// A strongly-typed representation of a number of samples.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
|
||||
pub struct SampleCount(usize);
|
||||
|
||||
impl Timestamp {
|
||||
/// Create a new timestamp in seconds.
|
||||
pub fn new(seconds: f64) -> Self {
|
||||
Timestamp(seconds)
|
||||
}
|
||||
|
||||
/// Create a new timestamp from seconds. (dummy method)
|
||||
pub fn from_seconds(seconds: f64) -> Self {
|
||||
Timestamp(seconds)
|
||||
}
|
||||
|
||||
/// Create a new timestamp from milliseconds.
|
||||
pub fn from_millis(milliseconds: u64) -> Self {
|
||||
Timestamp(milliseconds as f64 / 1000.0)
|
||||
}
|
||||
|
||||
/// Get the value in seconds.
|
||||
pub fn as_seconds(&self) -> f64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Get the value in milliseconds.
|
||||
pub fn as_millis(&self) -> u64 {
|
||||
(self.0 * 1000.0).round() as u64
|
||||
}
|
||||
|
||||
/// Add a duration to a timestamp, producing a new timestamp.
|
||||
pub fn add_duration(&self, duration: Duration) -> Timestamp {
|
||||
Timestamp(self.0 + duration.0)
|
||||
}
|
||||
|
||||
/// Subtract a duration from a timestamp, producing a new timestamp.
|
||||
pub fn subtract_duration(&self, duration: Duration) -> Timestamp {
|
||||
Timestamp(self.0 - duration.0)
|
||||
}
|
||||
|
||||
/// Subtract another timestamp, producing a duration.
|
||||
pub fn subtract_timestamp(&self, other: Timestamp) -> Duration {
|
||||
Duration(self.0 - other.0)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, other: Timestamp) {
|
||||
self.0 = other.as_seconds();
|
||||
}
|
||||
|
||||
pub fn max(&self, other: Timestamp) -> Timestamp {
|
||||
Timestamp(self.0.max(other.0))
|
||||
}
|
||||
|
||||
pub fn min(&self, other: Timestamp) -> Timestamp {
|
||||
Timestamp(self.0.min(other.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Duration {
|
||||
/// Create a new duration in seconds.
|
||||
pub fn new(seconds: f64) -> Self {
|
||||
Duration(seconds)
|
||||
}
|
||||
|
||||
/// Create a new duration from seconds. (dummy method)
|
||||
pub fn from_seconds(seconds: f64) -> Self {
|
||||
Duration(seconds)
|
||||
}
|
||||
|
||||
/// Create a new duration from milliseconds.
|
||||
pub fn from_millis(milliseconds: u64) -> Self {
|
||||
Duration(milliseconds as f64 / 1000.0)
|
||||
}
|
||||
|
||||
/// Create a new duration from frames, given a frame rate.
|
||||
pub fn from_frames(frames: u64, frame_rate: f64) -> Self {
|
||||
Duration(frames as f64 / frame_rate)
|
||||
}
|
||||
|
||||
/// Get the value in seconds.
|
||||
pub fn as_seconds(&self) -> f64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Get the value in milliseconds.
|
||||
pub fn as_millis(&self) -> u64 {
|
||||
(self.0 * 1000.0).round() as u64
|
||||
}
|
||||
|
||||
/// Get the number of frames for this duration, given a frame rate.
|
||||
pub fn to_frames(&self, frame_rate: f64) -> u64 {
|
||||
(self.0 * frame_rate).round() as u64
|
||||
}
|
||||
|
||||
/// Get the number of samples in this duration at a given sample rate
|
||||
pub fn to_samples(&self, sample_rate: u32) -> u64 {
|
||||
(self.0 * sample_rate as f64).round() as u64
|
||||
}
|
||||
|
||||
pub fn to_std(&self) -> std::time::Duration {
|
||||
std::time::Duration::from_nanos((self.0/1_000_000_000.0).round() as u64)
|
||||
}
|
||||
|
||||
/// Add two durations together.
|
||||
pub fn add(&self, other: Duration) -> Duration {
|
||||
Duration(self.0 + other.0)
|
||||
}
|
||||
|
||||
/// Subtract one duration from another.
|
||||
pub fn subtract(&self, other: Duration) -> Duration {
|
||||
Duration(self.0 - other.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl SampleCount {
|
||||
/// Create a new count of samples.
|
||||
pub fn new(samples: usize) -> Self {
|
||||
SampleCount(samples)
|
||||
}
|
||||
|
||||
pub fn as_usize(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn to_duration(&self, sample_rate: u32) -> Duration {
|
||||
Duration((self.0 as f64) / (sample_rate as f64))
|
||||
}
|
||||
|
||||
pub fn max(&self, other: SampleCount) -> SampleCount {
|
||||
SampleCount(self.0.max(other.0))
|
||||
}
|
||||
|
||||
pub fn min(&self, other: SampleCount) -> SampleCount {
|
||||
SampleCount(self.0.min(other.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<usize> for SampleCount{
|
||||
fn eq(&self, other: &usize) -> bool {
|
||||
self.0 == *other
|
||||
}
|
||||
}
|
||||
|
||||
// Overloading operators for more natural usage
|
||||
use std::ops::{Add, Sub, AddAssign, SubAssign};
|
||||
|
||||
impl Add<Duration> for Timestamp {
|
||||
type Output = Timestamp;
|
||||
|
||||
fn add(self, duration: Duration) -> Timestamp {
|
||||
self.add_duration(duration)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Duration> for Timestamp {
|
||||
type Output = Timestamp;
|
||||
|
||||
fn sub(self, duration: Duration) -> Timestamp {
|
||||
self.subtract_duration(duration)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Timestamp> for Timestamp {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, other: Timestamp) -> Duration {
|
||||
self.subtract_timestamp(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<Duration> for Timestamp {
|
||||
fn add_assign(&mut self, duration: Duration) {
|
||||
self.0 += duration.0;
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<Duration> for Timestamp {
|
||||
fn sub_assign(&mut self, duration: Duration) {
|
||||
self.0 -= duration.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Add for SampleCount {
|
||||
type Output = SampleCount;
|
||||
fn add(self, other: SampleCount) -> SampleCount {
|
||||
SampleCount(self.0 + other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for SampleCount {
|
||||
type Output = SampleCount;
|
||||
fn sub(self, other: SampleCount) -> SampleCount {
|
||||
SampleCount(self.0 - other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<SampleCount> for SampleCount {
|
||||
fn add_assign(&mut self, other: SampleCount) {
|
||||
self.0 += other.0;
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<SampleCount> for SampleCount {
|
||||
fn sub_assign(&mut self, other: SampleCount) {
|
||||
self.0 -= other.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a video frame.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Frame {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub data: Vec<u8>, // RGBA pixel data
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn new(width: u32, height: u32, data: Vec<u8>) -> Self {
|
||||
Frame { width, height, data }
|
||||
}
|
||||
}
|
||||
34
src/main.js
34
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,35 @@ 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)
|
||||
|
||||
let audioStarted = false;
|
||||
const startCoreInterfaceAudio = () => {
|
||||
if (!audioStarted) {
|
||||
try {
|
||||
coreInterface.resume_audio();
|
||||
audioStarted = true;
|
||||
console.log("Started CoreInterface Audio!")
|
||||
} catch (err) {
|
||||
console.error("Audio resume failed:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the event listeners to prevent them from firing again
|
||||
document.removeEventListener("click", startCoreInterfaceAudio);
|
||||
document.removeEventListener("keydown", startCoreInterfaceAudio);
|
||||
};
|
||||
|
||||
// Add event listeners for mouse click and key press
|
||||
document.addEventListener("click", startCoreInterfaceAudio);
|
||||
document.addEventListener("keydown", startCoreInterfaceAudio);
|
||||
}
|
||||
testAudio()
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
export function main_js(): void;
|
||||
export class CoreInterface {
|
||||
free(): void;
|
||||
constructor();
|
||||
init(): void;
|
||||
play(timestamp: number): void;
|
||||
stop(): void;
|
||||
resume_audio(): 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_resume_audio: (a: number, b: 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__h078d17eff0b70a99: (a: number, b: number) => void;
|
||||
readonly _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h26b4819ece79f796: (a: number, b: number) => void;
|
||||
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb1f2a4138d993283: (a: number, b: number, c: 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<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
||||
*
|
||||
* @returns {Promise<InitOutput>}
|
||||
*/
|
||||
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
||||
|
|
@ -0,0 +1,742 @@
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_20(arg0, arg1) {
|
||||
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h078d17eff0b70a99(arg0, arg1);
|
||||
}
|
||||
|
||||
function __wbg_adapter_23(arg0, arg1) {
|
||||
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h26b4819ece79f796(arg0, arg1);
|
||||
}
|
||||
|
||||
function __wbg_adapter_26(arg0, arg1, arg2) {
|
||||
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb1f2a4138d993283(arg0, arg1, addHeapObject(arg2));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
resume_audio() {
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
wasm.coreinterface_resume_audio(retptr, this.__wbg_ptr);
|
||||
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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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_clearInterval_ad2594253cc39c4b = function(arg0, arg1) {
|
||||
getObject(arg0).clearInterval(arg1);
|
||||
};
|
||||
imports.wbg.__wbg_clearTimeout_96804de0ab838f26 = function(arg0) {
|
||||
const ret = clearTimeout(takeObject(arg0));
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
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_new_78feb108b6472713 = function() {
|
||||
const ret = new Array();
|
||||
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_now_6f91d421b96ea22a = function(arg0) {
|
||||
const ret = getObject(arg0).now();
|
||||
return ret;
|
||||
};
|
||||
imports.wbg.__wbg_now_d18023d54d4e5500 = function(arg0) {
|
||||
const ret = getObject(arg0).now();
|
||||
return ret;
|
||||
};
|
||||
imports.wbg.__wbg_performance_c185c0cdc2766575 = function(arg0) {
|
||||
const ret = getObject(arg0).performance;
|
||||
return isLikeNone(ret) ? 0 : addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbg_performance_f71bd4abe0370171 = function(arg0) {
|
||||
const ret = getObject(arg0).performance;
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbg_queueMicrotask_97d92b4fcc8a61c5 = function(arg0) {
|
||||
queueMicrotask(getObject(arg0));
|
||||
};
|
||||
imports.wbg.__wbg_queueMicrotask_d3219def82552485 = function(arg0) {
|
||||
const ret = getObject(arg0).queueMicrotask;
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbg_resolve_4851785c9c5f573d = function(arg0) {
|
||||
const ret = Promise.resolve(getObject(arg0));
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbg_resume_35efdc4ffe13bf18 = function() { return handleError(function (arg0) {
|
||||
const ret = getObject(arg0).resume();
|
||||
return addHeapObject(ret);
|
||||
}, arguments) };
|
||||
imports.wbg.__wbg_setInterval_83d54331ceeda644 = function() { return handleError(function (arg0, arg1, arg2, arg3) {
|
||||
const ret = getObject(arg0).setInterval(getObject(arg1), arg2, ...getObject(arg3));
|
||||
return ret;
|
||||
}, arguments) };
|
||||
imports.wbg.__wbg_setTimeout_eefe7f4c234b0c6b = function() { return handleError(function (arg0, arg1) {
|
||||
const ret = setTimeout(getObject(arg0), arg1);
|
||||
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.__wbg_suspend_6011a41599f07de4 = function() { return handleError(function (arg0) {
|
||||
const ret = getObject(arg0).suspend();
|
||||
return addHeapObject(ret);
|
||||
}, arguments) };
|
||||
imports.wbg.__wbg_then_44b73946d2fb3e7d = function(arg0, arg1) {
|
||||
const ret = getObject(arg0).then(getObject(arg1));
|
||||
return 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_wrapper208 = function(arg0, arg1, arg2) {
|
||||
const ret = makeMutClosure(arg0, arg1, 82, __wbg_adapter_20);
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbindgen_closure_wrapper265 = function(arg0, arg1, arg2) {
|
||||
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_23);
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
imports.wbg.__wbindgen_closure_wrapper283 = function(arg0, arg1, arg2) {
|
||||
const ret = makeMutClosure(arg0, arg1, 116, __wbg_adapter_26);
|
||||
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_function = function(arg0) {
|
||||
const ret = typeof(getObject(arg0)) === 'function';
|
||||
return ret;
|
||||
};
|
||||
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;
|
||||
Binary file not shown.
|
|
@ -0,0 +1,26 @@
|
|||
/* 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_resume_audio: (a: number, b: 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__h078d17eff0b70a99: (a: number, b: number) => void;
|
||||
export const _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h26b4819ece79f796: (a: number, b: number) => void;
|
||||
export const _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb1f2a4138d993283: (a: number, b: number, c: number) => void;
|
||||
export const __wbindgen_start: () => void;
|
||||
|
|
@ -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/*"
|
||||
]
|
||||
}
|
||||
Loading…
Reference in New Issue