Skip to content

Commit

Permalink
make callback run without crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 7, 2023
1 parent 4374dd9 commit 1ed0c50
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
14 changes: 8 additions & 6 deletions examples/offline.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { AudioContext, OfflineAudioContext } from '../index.mjs';

const offline = new OfflineAudioContext(1, 44100, 44100);

offline.suspend_and(0.5, function() {
console.log("hello", offline.length);
const osc = offline.createOscillator();
osc.connect(offline.destination);
osc.frequency.value = 220;
osc.start(0);
offline.suspend_and(0.1, function() {
console.log("hello there", offline.length);
const osc = offline.createOscillator();
osc.connect(offline.destination);
osc.frequency.value = 220;
osc.start(0);
});

const buffer = await offline.startRendering();
console.log("buffer duration s:", buffer.duration);
const channelData = buffer.getChannelData(0)
console.log("buffer", channelData[40000]);

const latencyHint = process.env.WEB_AUDIO_LATENCY === 'playback' ? 'playback' : 'interactive';
const online = new AudioContext({ latencyHint });
Expand Down
19 changes: 6 additions & 13 deletions generator/templates/audio_context.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,22 +489,15 @@ fn suspend_and(ctx: CallContext) -> Result<JsUndefined> {
};

println!("suspend_and {}", when);
let js_func_ref = ctx.env.create_reference(js_func)?;
let js_func: JsFunction = ctx.env.get_reference_value(&js_func_ref).unwrap();

/*
let tsfn =
ctx
.env
.create_threadsafe_function(&js_func, 0, |ctx: ThreadSafeCallContext<()>| {
Ok(vec![()])
})?;
*/

let tsfn = ctx.env.create_threadsafe_function(&js_func, 0, |ctx: ThreadSafeCallContext<()>| {
Ok(vec![()])
})?;

napi_obj.0.suspend_at(when, move |_| {
println!("callback runs now");
js_func.call_without_args(Some(&js_this)).unwrap();
//tsfn.call(Ok(()), napi::threadsafe_function::ThreadsafeFunctionCallMode::Blocking);
// js_func.call_without_args(Some(&js_this)).unwrap();
tsfn.call(Ok(()), napi::threadsafe_function::ThreadsafeFunctionCallMode::Blocking);
});

ctx.env.get_undefined()
Expand Down
25 changes: 14 additions & 11 deletions src/offline_audio_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,26 @@ fn suspend_and(ctx: CallContext) -> Result<JsUndefined> {
};

println!("suspend_and {}", when);
let js_func_ref = ctx.env.create_reference(js_func)?;
let js_func: JsFunction = ctx.env.get_reference_value(&js_func_ref).unwrap();

/*
let tsfn =
ctx
.env
.create_threadsafe_function(&js_func, 0, |ctx: ThreadSafeCallContext<()>| {
Ok(vec![()])
})?;
*/
ctx.env
.create_threadsafe_function(&js_func, 0, |ctx: ThreadSafeCallContext<()>| {
Ok(vec![()])
})?;

napi_obj.0.suspend_at(when, move |_| {
println!("callback runs now");
js_func.call_without_args(Some(&js_this)).unwrap();
//tsfn.call(Ok(()), napi::threadsafe_function::ThreadsafeFunctionCallMode::Blocking);
// js_func.call_without_args(Some(&js_this)).unwrap();
tsfn.call(
Ok(()),
napi::threadsafe_function::ThreadsafeFunctionCallMode::Blocking,
);
});

// let tsfn_cloned = tsfn.clone();

// let js_func_ref = ctx.env.create_reference(js_func)?;
// let js_func: JsFunction = ctx.env.get_reference_value(&js_func_ref).unwrap();

ctx.env.get_undefined()
}
2 changes: 1 addition & 1 deletion wpt
Submodule wpt updated 22 files
+44 −0 webaudio/resources/buffer-loader.js
+0 −94 webaudio/the-audio-api/the-audiocontext-interface/audiocontext-not-fully-active.html
+0 −215 webaudio/the-audio-api/the-audiocontext-interface/audiocontextoptions.html
+0 −25 webaudio/the-audio-api/the-audiocontext-interface/constructor-allowed-to-start.html
+0 −65 webaudio/the-audio-api/the-audiocontext-interface/suspend-with-navigation.html
+0 −221 webaudio/the-audio-api/the-audionode-interface/audionode-disconnect-audioparam.html
+0 −93 webaudio/the-audio-api/the-audionode-interface/audionode.html
+0 −184 webaudio/the-audio-api/the-delaynode-interface/no-dezippering.html
+0 −572 webaudio/the-audio-api/the-iirfilternode-interface/iirfilter.html
+0 −76 webaudio/the-audio-api/the-mediaelementaudiosourcenode-interface/cors-check.https.html
+0 −75 webaudio/the-audio-api/the-mediaelementaudiosourcenode-interface/no-cors.https.html
+0 −298 webaudio/the-audio-api/the-pannernode-interface/panner-automation-basic.html
+0 −47 webaudio/the-audio-api/the-pannernode-interface/panner-automation-equalpower-stereo.html
+0 −265 webaudio/the-audio-api/the-pannernode-interface/panner-automation-position.html
+0 −51 webaudio/the-audio-api/the-pannernode-interface/panner-azimuth.html
+0 −227 webaudio/the-audio-api/the-pannernode-interface/panner-distance-clamping.html
+0 −44 webaudio/the-audio-api/the-pannernode-interface/panner-equalpower-stereo.html
+0 −139 webaudio/the-audio-api/the-pannernode-interface/panner-equalpower.html
+0 −98 webaudio/the-audio-api/the-pannernode-interface/panner-rolloff-clamping.html
+0 −71 webaudio/the-audio-api/the-pannernode-interface/pannernode-basic.window.js
+0 −37 webaudio/the-audio-api/the-pannernode-interface/pannernode-setposition-throws.html
+0 −36 webaudio/the-audio-api/the-pannernode-interface/test-pannernode-automation.html

0 comments on commit 1ed0c50

Please sign in to comment.