diff --git a/examples/tone.js b/examples/tone.js new file mode 100644 index 0000000..a8edd11 --- /dev/null +++ b/examples/tone.js @@ -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(); diff --git a/package.json b/package.json index ef088a5..d31147f 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/register-global.mjs b/register-global.mjs new file mode 100644 index 0000000..5589c40 --- /dev/null +++ b/register-global.mjs @@ -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);