soundscenemanager
v1.0.6
Published
A framework for managing multiple sounds that need to change based on the 'scene' on an application.
Downloads
3
Maintainers
Readme
SoundSceneManager
Simple scene manager for fading between various sets of sounds..
Usage
npm install soundscenemanager
var soundscenemanager = require('soundscenemanager');
var oceanBgSound = context.createBufferSource();
oceanBgSound.buffer = downloadedOceanBuffer; //some downloaded AudioBuffer
var options = {
scenes:[{
name:"ocean",
sounds:[{
name:"background",
node: oceanBgSound,
}]
}, {
name:"city",
sounds:[{
name:"kitchen",
node: kitchenSound,
}]
}],
fadeDuration: 1,
startingScene: "ocean",
context: context
}
var s = soundscenemanager(options);
// Transition to the next scene (city).
s.transitionToNextScene(context.currentTime+10, 2);
// Fades out all audio.
s.mute(context.currentTime+20, 2);
API
Constructor
eg :
var oceanBgSound = context.createBufferSource();
oceanBgSound.buffer = downloadedOceanBuffer;
var options ={
scenes:[{
name:"ocean",
maxVolume : 0.7,
sounds:[{
name:"background",
node: oceanBgSound,
},{
name:"diver",
node: diverSound,
}]
},{
name:"city",
sounds:[{
name:"kitchen",
node: kitchenSound,
},{
name:"club",
node: clubSound,
}]
}],
fadeDuration: 1,
startingScene: "ocean",
fadeInAtStart: true,
fadeInAtStartDuration : 5,
context: context
}
var s = soundscenemanager(options);
options
object attribute contains the data about the various scenes in the following format:scenes
: Array - Array of scene objects which are being managed by this SoundSceneManager. Each scenes object should have the following properties:name
: String - The name of the scene.maxVolume
: Number - The maximum volume of this scene [0,1];fadeInAtStart
: Boolean - If the starting scene should fade in.fadeInAtStartDuration
: Number - Time taken (sec) for the starting scene to fade in.- sounds : Array - Array of sound objects which are a part of this specific scene. Each sound object should have the following properties:
name
: String - A name for identifying the sound object.node
: AudioNode/SoundModel/Audio - An AudioNode which will be connected to the SoundSceneManager and faded in/out. It's preferable that this AudioNode isn't connected to any other AudioNodes, as it will be disconnected in the process of initializing the SoundSceneManager. A Sonoport SoundModel object is also accepted here and so is an HTML5 Audio object. Note: If using an HTML5 Audio element with a cross-origin source, ensure that the CORS header is set and thecrossOrigin
property is set on the Audio Object before callingplay
on the Audio Object.
fadeDuration
: Number - Default duration, in seconds, of all fadesin/fadeouts.startingScene
: String - The name of the first scene to fade in at startup.context
: AudioContext - AudioContext within which all the AudioNodes are created.
Methods
transitionToScene
: Transition to a given scene. Fades out the audio from the current scene and fades in the audio from the given scene. If the nextScene is the same as current scene, this method will not do anything.eg :
s.transitionToScene('nextSceneName', context.currentTime+10, 2);
arguments :
nextSceneName
: String - Thename
of the scene to transition to. Ifnull
, the current scene will fade out.startTime
: Number - Start time for the transtion in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds of the transtion.
transitionToNextScene
: Similar totransitionToScene
except it transtions to the next scene based on the order of the scenes as defined in thescenes
array in the constructor options. If the current scene is the last scene in the array, this will transition to the first scene in the array.eg :
s.transitionToNextScene(context.currentTime+10, 2);
arguments :
startTime
: Number - Start time for the transtion in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds of the transtion.
transitionToPrevScene
: Similar totransitionToScene
except it transtions to the previous scene based on the order of the scenes as defined in thescenes
array in the constructor options. If the current scene is the first scene in the array, this will transition to the last scene in the array.eg :
s.transitionToPrevScene(context.currentTime+10, 2);
arguments :
startTime
: Number - Start time for the transtion in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds of the transtion.
mute
: Fades out all audio from the SoundSceneManager.eg :
s.mute(context.currentTime+10, 2);
arguments :
startTime
: Number - Start time for begining of the fadeout in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds taken for the fadeout.
unMute
: Fades in all audio from the SoundSceneManager if faded out.eg :
s.unMute(context.currentTime+10, 2);
arguments :
startTime
: Number - Start time for begining of the fadein in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds taken for the fadein.
toggleMute
: Toggles betweenmute
andunMute
APIs based on the current state of the SoundSceneManager.eg :
s.toggle(context.currentTime+10, 2);
arguments :
startTime
: Number - Start time for begining of the fadein/fadeout in the same time coordinate system as the AudioContext currentTime attribute.fadeDuration
: Number - Duration in seconds taken for the fadein/fadeout.
addScene
: Adding a new scene for the SoundSceneManager to manage.eg :
var newScene = { name:"ocean", sounds:[{ name:"background", node: oceanBgSound, },{ name:"diver", node: diverSound, }] } s.addScene(newScene);
arguments :
newScene
: Object - A new scene object that should have the following properties :name
: String - The name of the scene.maxVolume
: Number - The maximum volume of this scene [0,1];`- sounds : Array - Array of sound objects which are a part of this specific scene. Each sound object should have the following properties:
name
: String - A name for identifying the sound object.node
: AudioNode - An AudioNode which will be connected to the SoundSceneManager and faded in/out. It's preferable that this AudioNode isn't connected to any other AudioNodes, as it will be disconnected in the process of initializing the SoundSceneManager. A Sonoport SoundModel object is also accepted here and so is an HTML5 Audio object. Note: If using an HTML5 Audio element with a cross-origin source, ensure that the CORS header is set and thecrossOrigin
property is set on the Audio Object before callingplay
on the Audio Object.
addSoundToScene
: Add a new sound to ax existing scene.eg :
var newSound = { name:"background", node: oceanBgSound, } s.addSoundToScene(newSound,sceneName);
arguments :
newSound
: Object - A new sound object that should have the following properties :name
: String - A name for identifying the sound object.node
: AudioNode - An AudioNode which will be connected to the SoundSceneManager and faded in/out. It's preferable that this AudioNode isn't connected to any other AudioNodes, as it will be disconnected in the process of initializing the SoundSceneManager. A Sonoport SoundModel object is also accepted here and so is an HTML5 Audio object. Note: If using an HTML5 Audio element with a cross-origin source, ensure that the CORS header is set and thecrossOrigin
property is set on the Audio Object before callingplay
on the Audio Object.
sceneName
: String - The name of the scene we want to add this Sound to.
Properties
fadeDuration
: Number - Default duration, in seconds, of all fadein/fadeouts.currentSound
: Object - The current scene object that's playing or being faded in.isMuted
: Boolean - Indicates if the SoundSceneManager is muted.
License
Apache-2.0
See License file