soundjs

persistent and controllable sounds in soundJS


I'm considering using SoundJS for an online sound interface and I'm a bit confused by the the different examples.

The simplicity of the Sound Grid example is appealing. Push a button, get a sound. but I'd like to be able to control the volume and pan of the individual sounds. In Test Suite, this appears to be possible but in this example you must first create an instance of the sound before you can control it's volume/pan.

This is confusing to me. I'd like the reference to the sounds to be loaded with the interface and stored until the page is closed. I'm not using this for a game so destroying unused or seldom used resources isn't important. Basically if you see the interface element that will be used to activate the sound, I want it to be ready and be controllable. I envision about 15 sounds per page. a few longer looping elements and mostly one shots.

From the docs:

Once a AbstractSoundInstance is created, a reference can be stored that can be used to control the audio directly through the AbstractSoundInstance. If the reference is not stored, the AbstractSoundInstance will play out its audio (and any loops), and is then de-referenced from the Sound class so that it can be cleaned up

Is this always a two step process? I guess what I'm looking for is a version of soundGrid with simple, per-pad volume/pan/loop controls.


Solution

  • What you are looking for is Sound.play. Specifically, you want to set the parameters on the play call. You could also use Sound.createInstance and SoundInstance.play to achieve the same result.

    var SoundInstance = createjs.Sound.play("myAudioID", {loop: 2, volume: 0.75, pan: 0.5});
    

    In answer to your comment, this is how you could store of reusable sound instances in an object hash. var mySounds = {}; mySounds["id1"] = createjs.Sound.createInstance("id1); ... var currentSI = mySounds["id1]; currentSI.volume = 0.5;

    Hope the helps.