/// <summary> /// Applies 3D settings to a cue. /// </summary> void Update3DSettings(Cue3D cue3D) { for (int i = 0; i < listeners.Count; i++) { cue3D.Cues[i].Apply3D(listeners[i], cue3D.Emitter); } }
/// <summary> /// Plays a sound in 3D to all the AudioListeners listed in Listeners. /// </summary> /// <param name="cueName">Name of the sound to be played.</param> /// <param name="emitter">The emitter of the sound.</param> /// <remarks>If there are no listeners, then the sound is just played normally.</remarks> /// <param name="variables">Variables to be applied to the cue.</param> public void Play(string cueName, AudioEmitter emitter, params CueVariable[] variables) { // Play the cue normally if there are no listeners for 3D sound. if (listeners.Count == 0) { Play(cueName); return; } if (!isInitialized) { throw new Exception("Audio system must be initialized."); } // Generate a cue instance for each listener List <Cue> cues = new List <Cue>(listeners.Count); for (int i = 0; i < listeners.Count; i++) { Cue cue = soundBank.GetCue(cueName); for (int v = 0; v < variables.Length; v++) { variables[v].Apply(cue); } cues.Add(soundBank.GetCue(cueName)); } // Get an inactive instance if available Cue3D cue3D; if (inactiveCue3Ds.Count > 0) { cue3D = inactiveCue3Ds.Dequeue(); cue3D.Cues = cues; cue3D.Emitter = emitter; } else { // we need to create a new one cue3D = new Cue3D(cues, emitter); } // apply the 3D settings for this listener Update3DSettings(cue3D); // play the cue cue3D.Play(); // add to the activeCue3Ds list. activeCue3Ds.Add(cue3D); }
/// <summary> /// Updates the AudioSystem. /// </summary> protected override void UpdateCore(GameTime gameTime) { if (!isInitialized) { return; } // Remove cues from the activeCues list which have stopped. for (int i = activeCues.Count - 1; i >= 0; i--) { if (activeCues[i].IsStopped) { activeCues.RemoveAt(i); } } // Update 3D cues. for (int i = activeCue3Ds.Count - 1; i >= 0; i--) { Cue3D cue3D = activeCue3Ds[i]; if (cue3D.Cues[0].IsStopped) { // If the cue has stopped playing, dispose it. cue3D.DisposeCues(); // Store the Cue3D instance for future reuse. inactiveCue3Ds.Enqueue(cue3D); // Remove it from the active list. activeCue3Ds.RemoveAt(i); } else { // If the cue is still playing, update its 3D settings. Update3DSettings(cue3D); } } // Update the audio engine. audioEngine.Update(); }