CreateJS / SoundJS

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.
http://createjs.com/
MIT License
4.42k stars 838 forks source link

Add ability to set volume to a function #274

Open JasXSL opened 7 years ago

JasXSL commented 7 years ago

I think it would be a good idea to add the ability to set sound volume to a function that returns a number. That would make it easier to add different channels of sound. Such as adding different volume functions for music and sound effects.

lannymcnie commented 7 years ago

Can you provide an example of what you mean?

JasXSL commented 7 years ago

For an instance

// Create two channels, one for music and one for SFX
MyApp.channelVolumes = { music: 0.5, sfx: 0.5 };

// Create functions to return the volumes. These could also have custom logic.
MyApp.getMusicVol = function(){ return MyApp.channelVolumes.music; };
MyApp.getSfxVol = function(){ return MyApp.channelVolumes.sfx; };

// Play a music piece or a few
createjs.Sound.play("musicPiece.ogg", { volume: MyApp.getMusicVol; });

// When a sound effect is received, duck the volume of the music channel
createjs.Sound.play("boom.ogg", { volume: MyApp.getSfxVol; });
MyApp.channelVolumes.music = 0.25;

If soundjs has something built in to create different audio channels like this, that's better. But I haven't seen anything in the docs about it. Grabbing the volume from a function seems like an easy solution.

Edit: Accidentally added (); to the functions