Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/tone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../register-global.mjs';
import * as Tone from 'tone';

const latencyHint = process.env.WEB_AUDIO_LATENCY === 'playback' ? 'playback' : 'interactive';
const audioContext = new AudioContext({ latencyHint });
Tone.setContext(audioContext);

const synth = new Tone.Synth().toDestination();
const now = Tone.now();
// trigger the attack immediately
synth.triggerAttack('C4', now);
// wait one second before triggering the release
synth.triggerRelease(now + 1);

await new Promise(resolve => setTimeout(resolve, 1500));
console.log('closing');
await audioContext.close();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"mocha": "^11.0.1",
"octokit": "^4.1.0",
"template-literal": "^1.0.4",
"tone": "^15.1.22",
"webidl2": "^24.2.0",
"wpt-runner": "^5.0.0"
},
Expand Down
8 changes: 8 additions & 0 deletions register-global.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as webaudio from './index.mjs';

// Expose webaudio globally
Object.assign(globalThis, webaudio);
// note that `standardized-audio-context` explicitly relies on window
// cf. https://github.com/chrisguttandin/standardized-audio-context/blob/master/src/factories/window.ts
globalThis.window = {};
Object.assign(globalThis.window, webaudio);