fireworks-js icon indicating copy to clipboard operation
fireworks-js copied to clipboard

allow users to use their own sound class

Open aaron13100 opened this issue 1 year ago • 0 comments

Checklist

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);

aaron13100 avatar Nov 28 '23 13:11 aaron13100