fireworks-js
fireworks-js copied to clipboard
allow users to use their own sound class
Checklist
- [ ] run
pnpm format
- [ ] documentation is changed or added
- [X] commit message and code follows the Developer's Certification of Origin and the Code of conduct
This is untested so feel free to reject it. This allows users to override the Sound class, as in the following example.
class CustomFireworksSound extends Sound {
play() {
if (this.isEnabled) {
var soundFiles = this.options.sound.files;
var randomIndex = Math.floor(Math.random() * soundFiles.length);
var gainDifference = Math.abs(this.options.sound.volume.min - this.options.sound.volume.max);
var gain = Math.random() * gainDifference;
var fileToPlay = soundFiles[randomIndex];
var audioElement = document.createElement('audio');
audioElement.src = fileToPlay;
audioElement.setAttribute('data-gain', gain);
audioElement.setAttribute('data-allow-simultaneous', 'true');
playSoundWithEventTracking(audioElement);
console.log("custom play function");
}
}
}
fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);
or if we've wrapped all the Fireworks stuff like this..
function fireworksModule() {
// ... existing code ...
return {
Fireworks: Fireworks,
Sound: Sound
}
}
Then we can use our custom class like this...
const fireworksModuleInstance = fireworksModule();
const FireworksSoundClass = fireworksModuleInstance.Sound;
class CustomFireworksSound extends FireworksSoundClass {
play() {
// ...etc ...
}
}
fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);