Plugin Sound Packs - BaryoDev/dopaminejs GitHub Wiki
Plugin: Sound Packs
dopaminejs-plugin-sound-packs 1.0.0.
npm install dopaminejs-plugin-sound-packs
Four sets of tone definitions. This package is data only. It is not a kernel plugin and it plays nothing by itself.
import { getSoundPack, listSoundPacks, SoundPacks } from 'dopaminejs-plugin-sound-packs';
listSoundPacks(); // ['retro', 'modern', 'cute', 'scifi']
const pack = getSoundPack('retro');
getSoundPack with an unknown name returns the modern pack.
What a pack holds
Each pack has five keys: xp, levelUp, achievement, coin, error. Each entry is one of two shapes.
A single tone:
{ type: 'tone', frequency: 440, duration: 0.1, waveform: 'square', volume: 0.3 }
A run of notes:
{
type: 'sequence',
notes: [
{ frequency: 523, duration: 0.1 },
{ frequency: 659, duration: 0.1 },
{ frequency: 784, duration: 0.2 }
],
waveform: 'square',
volume: 0.4
}
frequency is in Hz and duration in seconds.
Playing a pack
These are synth definitions, not audio file URLs. Do not pass a pack as customSounds to SoundManager, which expects { key: url }. There is no setSoundPack method.
Play an entry with SoundManager.playTone:
import { SoundManager } from 'dopaminejs';
import { getSoundPack } from 'dopaminejs-plugin-sound-packs';
const sound = new SoundManager();
const pack = getSoundPack('retro');
// Returns a function that cancels the notes not played yet.
function playPackSound(sound, def) {
if (def.type === 'tone') {
sound.playTone(def.frequency, def.duration, def.waveform, def.volume);
return () => {};
}
const timers = [];
let delay = 0;
for (const note of def.notes) {
timers.push(setTimeout(() => sound.playTone(note.frequency, note.duration, def.waveform, def.volume), delay * 1000));
delay += note.duration;
}
return () => timers.forEach(clearTimeout);
}
rewards.on('xp_gained', () => playPackSound(sound, pack.xp));
rewards.on('level_up', () => playPackSound(sound, pack.levelUp));
rewards.on('achievement_unlocked', () => playPackSound(sound, pack.achievement));
playTone respects the mute flag and the master volume.
sound.destroy() does not know about the helper's timers. Call the returned function before it, or a late note calls playTone and opens a new audio context.