From 155ee7eb790239b58a4a420088edf71ef495a740 Mon Sep 17 00:00:00 2001 From: Muffin Date: Sun, 10 Mar 2024 16:26:31 -0500 Subject: [PATCH] Remove audio-context dependency audio-context just just a tiny wrapper for making an AudioContext. We can just vendor our own two line version. audio-context notably caches audio contexts, which causes problems like https://github.com/ScratchAddons/ScratchAddons/issues/6847 when contexts are suspended and resumed --- package-lock.json | 35 ++--------------------------------- package.json | 3 +-- src/AudioEngine.js | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 26542ab..8545305 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,7 @@ "license": "BSD-3-Clause", "dependencies": { "@turbowarp/nanolog": "^0.2.0", - "@turbowarp/startaudiocontext": "^1.0.0", - "audio-context": "1.0.1" + "@turbowarp/startaudiocontext": "^1.0.0" }, "devDependencies": { "babel-core": "6.26.3", @@ -1185,15 +1184,6 @@ "node": ">= 4.5.0" } }, - "node_modules/audio-context": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/audio-context/-/audio-context-1.0.1.tgz", - "integrity": "sha512-b7ZmHGyjcAI3KmshPa4YSPxPbjglZQtKfZtzqhUfE3VeoANTsNffywkmujI32Sj31Aku/aB9s3BW4r3nmTF88g==", - "deprecated": "Depends on Web-Audio-API implementation. Use either web-audio-api, web-audio-js or web-audio-engine package.", - "dependencies": { - "global": "^4.3.1" - } - }, "node_modules/available-typed-arrays": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz", @@ -3087,11 +3077,6 @@ "node": ">=6.0.0" } }, - "node_modules/dom-walk": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz", - "integrity": "sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==" - }, "node_modules/domain-browser": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/domain-browser/-/domain-browser-1.2.0.tgz", @@ -4381,15 +4366,6 @@ "node": ">=10.13.0" } }, - "node_modules/global": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/global/-/global-4.4.0.tgz", - "integrity": "sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w==", - "dependencies": { - "min-document": "^2.19.0", - "process": "^0.11.10" - } - }, "node_modules/global-modules": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-2.0.0.tgz", @@ -6066,14 +6042,6 @@ "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==", "dev": true }, - "node_modules/min-document": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/min-document/-/min-document-2.19.0.tgz", - "integrity": "sha512-9Wy1B3m3f66bPPmU5hdA4DR4PB2OfDU/+GS3yAB7IQozE3tqXaVv2zOjgla7MEGSRv95+ILmOuvhLkOK6wJtCQ==", - "dependencies": { - "dom-walk": "^0.1.0" - } - }, "node_modules/minimalistic-assert": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz", @@ -7118,6 +7086,7 @@ "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", "integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==", + "dev": true, "engines": { "node": ">= 0.6.0" } diff --git a/package.json b/package.json index a210359..3296230 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,7 @@ }, "dependencies": { "@turbowarp/nanolog": "^0.2.0", - "@turbowarp/startaudiocontext": "^1.0.0", - "audio-context": "1.0.1" + "@turbowarp/startaudiocontext": "^1.0.0" }, "devDependencies": { "babel-core": "6.26.3", diff --git a/src/AudioEngine.js b/src/AudioEngine.js index 172c43d..fb219ab 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -1,5 +1,4 @@ const StartAudioContext = require('./StartAudioContext'); -const AudioContext = require('audio-context'); const log = require('./log'); const uid = require('./uid'); @@ -35,13 +34,24 @@ const decodeAudioData = function (audioContext, buffer) { }); }; +/** + * @returns {AudioContext} A new audio context. + */ +const makeAudioContext = () => { + const AudioContext = window.AudioContext || window.webkitAudioContext; + if (!AudioContext) { + throw new Error('Browser does not support AudioContext'); + } + return new AudioContext(); +}; + /** * There is a single instance of the AudioEngine. It handles global audio * properties and effects, loads all the audio buffers for sounds belonging to * sprites. */ class AudioEngine { - constructor (audioContext = new AudioContext()) { + constructor (audioContext = makeAudioContext()) { /** * AudioContext to play and manipulate sounds with a graph of source * and effect nodes.