Skip to content

Commit

Permalink
Remove audio-context dependency
Browse files Browse the repository at this point in the history
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 ScratchAddons/ScratchAddons#6847
when contexts are suspended and resumed
  • Loading branch information
GarboMuffin committed Mar 10, 2024
1 parent b1e5e39 commit 155ee7e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
35 changes: 2 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 12 additions & 2 deletions src/AudioEngine.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const StartAudioContext = require('./StartAudioContext');
const AudioContext = require('audio-context');

const log = require('./log');
const uid = require('./uid');
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 155ee7e

Please sign in to comment.