Skip to content

Instantly share code, notes, and snippets.

@designbyadrian
Created April 13, 2021 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save designbyadrian/947d4090bea34b4aff498cf09f0d7711 to your computer and use it in GitHub Desktop.
Save designbyadrian/947d4090bea34b4aff498cf09f0d7711 to your computer and use it in GitHub Desktop.
attempt at creating an instanciable sound class that can be triggered with play()
import {
SuperpoweredTrackLoader,
SuperpoweredWebAudio,
} from './SuperpoweredWebAudio.js';
class SoundscapeProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
onReady() {
this.player = new this.Superpowered.AdvancedAudioPlayer(
this.samplerate,
2,
2,
0,
0.501,
2,
false
);
}
onMessageFromMainScope(message) {
console.log('onMessageFromMainScope', message);
if (message.SuperpoweredLoaded) {
console.log('loaded', message.SuperpoweredLoaded);
this.player.openMemory(
this.Superpowered.arrayBufferToWASM(message.SuperpoweredLoaded.buffer),
false,
false
);
this.sendMessageToMainScope({ loaded: true });
}
if (message.load) {
console.log('load', message.load);
SuperpoweredTrackLoader.downloadAndDecode(message.load, this);
}
if (message.play) {
console.log('Duration: ', this.player.getDurationSeconds());
this.player.play();
}
if (message.pitchShift)
this.player.pitchShiftCents = parseInt(message.pitchShift) * 100;
}
}
if (typeof AudioWorkletProcessor === 'function')
registerProcessor('SoundscapeProcessor', SoundscapeProcessor);
export default SoundscapeProcessor;
import config from './Config';
import { createPromise } from './utils';
import {
RUNNING,
SUSPENDED,
INTERRUPTED,
INITIALIZING,
} from './constants/states';
class Sound {
static audioContext;
static audioContextReady = createPromise();
static state = SUSPENDED;
static superpowered;
static webaudioManager;
constructor(url) {
this.srcUrl = url;
this._messageHandler = this._messageHandler.bind(this);
}
/**
* @returns {Boolean}
*/
static get isSupported() {
return (
typeof window.AudioContext !== 'undefined' ||
typeof window.webkitAudioContext !== 'undefined'
);
}
static async setup() {
try {
this.superpowered = await SuperpoweredGlue.fetch(
`${config.superpoweredLocation}/superpowered.wasm`
);
this.superpowered.Initialize({
licenseKey: 'ExampleLicenseKey-WillExpire-OnNextUpdate',
enableAudioAnalysis: false,
enableFFTAndFrequencyDomain: false,
enableAudioTimeStretching: true,
enableAudioEffects: true,
enableAudioPlayerAndDecoder: true,
enableCryptographics: false,
enableNetworking: false,
});
this.webaudioManager = new SuperpoweredWebAudio(44100, this.superpowered);
this.audioContext = this.webaudioManager.audioContext;
} catch (e) {
console.error('Setup error', e);
}
}
/**
* @returns {Promise}
*/
static ready() {
return this.audioContextReady;
}
static async resume() {
// await this.audioContext.resume();
this.audioContextReady.resolve();
}
static suspend() {
// console.log('suspend!');
// audioContext.close();
// audioContext = audioNode = null;
// state = SUSPENDED;
}
preload() {
this.loaderPromise = createPromise();
Sound.webaudioManager.createAudioNode(
`${config.superpoweredLocation}/Processor.js`,
'SoundscapeProcessor',
(node) => {
this.audioNode = node;
Sound.audioContext.suspend();
this.audioNode.connect(Sound.audioContext.destination);
this.sendMessage({ load: this.srcUrl });
},
this._messageHandler
);
return this.loaderPromise;
}
_messageHandler(message) {
if (message.loaded) {
this.loaderPromise.resolve();
}
}
sendMessage(message = {}) {
this.audioNode.sendMessageToAudioScope(message);
}
play() {
this.sendMessage({ play: true });
}
pause() {}
stop() {}
}
Sound.setup();
export default Sound;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment