diff --git a/package-lock.json b/package-lock.json index dd82da2..62d72cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "LGPL-2.1", "dependencies": { "lodash": "^4.17.21", - "tone": "^15.0.2", + "tone": "^15.1.22", "yuidocjs": "^0.10.2" }, "devDependencies": { @@ -5236,9 +5236,10 @@ } }, "node_modules/tone": { - "version": "15.0.2", - "resolved": "https://registry.npmjs.org/tone/-/tone-15.0.2.tgz", - "integrity": "sha512-i/Z8ve4h5I+A5s7ZQYP/o4+d0gKnIxHKphy7+DE6ZXV3PSpqqUxHL1oDaVf81AF9K65tpfC3IxvRAtR0wmIkyw==", + "version": "15.1.22", + "resolved": "https://registry.npmjs.org/tone/-/tone-15.1.22.tgz", + "integrity": "sha512-TCScAGD4sLsama5DjvTUXlLDXSqPealhL64nsdV1hhr6frPWve0DeSo63AKnSJwgfg55fhvxj0iPPRwPN5o0ag==", + "license": "MIT", "dependencies": { "standardized-audio-context": "^25.3.70", "tslib": "^2.3.1" diff --git a/package.json b/package.json index 8e70c96..570c14e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ ], "dependencies": { "lodash": "^4.17.21", - "tone": "^15.0.2", + "tone": "^15.1.22", "yuidocjs": "^0.10.2" }, "devDependencies": { diff --git a/rollup.config.mjs b/rollup.config.mjs index 336a268..1b5c432 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -2,6 +2,29 @@ import path from 'path'; import resolve from '@rollup/plugin-node-resolve'; import { terser } from 'rollup-plugin-terser'; +const toneGlobalShim = path.resolve('src', 'vendor', 'toneGlobal.js'); + +function aliasToneGlobal() { + return { + name: 'alias-tone-global', + resolveId(source, importer) { + if (source === 'tone/build/esm/core/Global.js') { + return toneGlobalShim; + } + + if ( + source === '../Global.js' && + importer && + importer.includes(`${path.sep}node_modules${path.sep}tone${path.sep}build${path.sep}esm${path.sep}core${path.sep}`) + ) { + return toneGlobalShim; + } + + return null; + } + }; +} + export default [ // Unminified version { @@ -11,6 +34,7 @@ export default [ format: 'iife', // Change to IIFE format }, plugins: [ + aliasToneGlobal(), resolve(), terser({ compress: { @@ -50,6 +74,7 @@ export default [ format: 'iife', // Change to IIFE format }, plugins: [ + aliasToneGlobal(), resolve(), // Resolves node_modules terser(), // Minify the output ], diff --git a/src/app.js b/src/app.js index f8145ca..0fc6ae2 100644 --- a/src/app.js +++ b/src/app.js @@ -1,4 +1,6 @@ +import { toneLoggingSilenced } from './bootstrap'; import { getAudioContext, setAudioContext, userStartAudio, userStopAudio } from './core/Utils'; +void toneLoggingSilenced; p5.prototype.getAudioContext = getAudioContext; p5.prototype.setAudioContext = setAudioContext; p5.prototype.userStartAudio = userStartAudio; @@ -84,4 +86,4 @@ p5.Score = Score; p5.SoundLoop = SoundLoop; //import Recorder from './Recorder'; -//p5.prototype.Recorder = Recorder; \ No newline at end of file +//p5.prototype.Recorder = Recorder; diff --git a/src/bootstrap.js b/src/bootstrap.js new file mode 100644 index 0000000..9624634 --- /dev/null +++ b/src/bootstrap.js @@ -0,0 +1,26 @@ +export const toneLoggingSilenced = (() => { + const root = + typeof globalThis !== 'undefined' + ? globalThis + : typeof window !== 'undefined' + ? window + : undefined; + + if (root) { + Object.defineProperty(root, 'TONE_SILENCE_LOGGING', { + configurable: true, + value: true, + writable: true + }); + + if (root.window) { + Object.defineProperty(root.window, 'TONE_SILENCE_LOGGING', { + configurable: true, + value: true, + writable: true + }); + } + } + + return true; +})(); diff --git a/src/core/p5soundNode.js b/src/core/p5soundNode.js index cd885b5..843597e 100644 --- a/src/core/p5soundNode.js +++ b/src/core/p5soundNode.js @@ -6,7 +6,6 @@ import { getAudioContext } from "./Utils"; import { gainToDb as ToneGainToDb } from "tone/build/esm/core/type/Conversions.js"; - /** * This is the primary or "base" class for p5.sound.js nodes. * diff --git a/src/sources/Oscillator.js b/src/sources/Oscillator.js index a30551e..c072fc1 100644 --- a/src/sources/Oscillator.js +++ b/src/sources/Oscillator.js @@ -73,7 +73,6 @@ class Oscillator extends p5soundSource { type = f; frequency = t; } - this.frequency = frequency; this.type = type; this.node = new ToneOscillator().connect(this.output); diff --git a/src/vendor/toneGlobal.js b/src/vendor/toneGlobal.js new file mode 100644 index 0000000..88f2488 --- /dev/null +++ b/src/vendor/toneGlobal.js @@ -0,0 +1,42 @@ +import { hasAudioContext } from "tone/build/esm/core/context/AudioContext.js"; +import { Context } from "tone/build/esm/core/context/Context.js"; +import { DummyContext } from "tone/build/esm/core/context/DummyContext.js"; +import { + isAudioContext, + isOfflineAudioContext +} from "tone/build/esm/core/util/AdvancedTypeCheck.js"; + +const dummyContext = new DummyContext(); +let globalContext = dummyContext; + +function wrapContext(context) { + if (context instanceof Context || context instanceof DummyContext) { + return context; + } + + if (isAudioContext(context) || isOfflineAudioContext(context)) { + return new Context(context); + } + + return context; +} + +export function getContext() { + if (globalContext === dummyContext && hasAudioContext) { + setContext(new Context()); + } + + return globalContext; +} + +export function setContext(context, disposeOld = false) { + if (disposeOld && globalContext && typeof globalContext.dispose === "function") { + globalContext.dispose(); + } + + globalContext = wrapContext(context); +} + +export function start() { + return globalContext.resume(); +}