/// <summary> /// Start playing an AudioEvent /// </summary> /// <param name="eventToPlay">The AudioEvent to play</param> /// <param name="emitter">The AudioSource component to play the event on</param> /// <returns>The reference for the runtime event that can be modified or stopped explicitly</returns> public static ActiveEvent PlayEvent(AudioEvent eventToPlay, AudioSource emitter) { ValidateManager(); if (eventToPlay.InstanceLimit > 0 && CountActiveInstances(eventToPlay) >= eventToPlay.InstanceLimit) { Debug.LogFormat("AudioManager: Instance limit reached for {0}.", eventToPlay.name); return(null); } if (eventToPlay.Group != 0) { StopGroupInstances(eventToPlay.Group); } ActiveEvent tempEvent = new ActiveEvent(eventToPlay, emitter); eventToPlay.SetActiveEventProperties(tempEvent); ActiveEvents.Add(tempEvent); if (AvailableSources.Contains(emitter)) { //Debug.LogFormat("Removing available source {0}", emitter.GetInstanceID()); AvailableSources.Remove(emitter); } return(tempEvent); }
/// <summary> /// Clear an ActiveEvent from the list of ActiveEvents /// </summary> /// <param name="stoppedEvent">The event that is no longer playing to remove from the ActiveEvent list</param> public static void RemoveActiveEvent(ActiveEvent stoppedEvent) { if (!AvailableSources.Contains(stoppedEvent.source)) { AvailableSources.Add(stoppedEvent.source); } ActiveEvents.Remove(stoppedEvent); stoppedEvent = null; }
public static void StopAll(AudioEvent eventsToStop) { for (int i = 0; i < ActiveEvents.Count; i++) { ActiveEvent tempEvent = ActiveEvents[i]; if (tempEvent.rootEvent == eventsToStop) { tempEvent.Stop(); } } }
private void Update() { for (int i = 0; i < ActiveEvents.Count; i++) { ActiveEvent tempEvent = ActiveEvents[i]; if (tempEvent != null && tempEvent.source != null) { tempEvent.Update(); } } }
/// <summary> /// Process the node on the current index and increase the index for next playback /// </summary> /// <param name="activeEvent">The existing runtime event to set properties on</param> public override void ProcessNode(ActiveEvent activeEvent) { ProcessConnectedNode(this.currentNode, activeEvent); this.currentNode++; if (this.currentNode >= this.input.ConnectedNodes.Length) { this.currentNode = 0; } }
/// <summary> /// Apply all modifications to the ActiveEvent before it gets played /// </summary> /// <param name="activeEvent">The runtime event being prepared for playback</param> public override void ProcessNode(ActiveEvent activeEvent) { if (file == null) { Debug.LogWarningFormat("Empty Voice File node in event {0}", activeEvent.rootEvent.name); } activeEvent.ModulateVolume(this.volumeOffset); activeEvent.ModulatePitch(this.pitchOffset); activeEvent.clip = this.file; activeEvent.text = this.text; }
/// <summary> /// Call an immediate stop on all active audio events of a particular group /// </summary> /// <param name="groupNum">The group number to stop</param> private static void StopGroupInstances(int groupNum) { for (int i = 0; i < ActiveEvents.Count; i++) { ActiveEvent tempEvent = ActiveEvents[i]; if (tempEvent.rootEvent.Group == groupNum) { Debug.LogFormat("Stopping: {0}", tempEvent.rootEvent.name); tempEvent.StopImmediate(); } } }
/// <summary> /// Apply all modifications to the ActiveEvent before it gets played /// </summary> /// <param name="activeEvent">The runtime event being prepared for playback</param> public override void ProcessNode(ActiveEvent activeEvent) { if (this.file == null) { Debug.LogWarningFormat("No file in node {0}", this.name); return; } activeEvent.ModulateVolume(this.volumeOffset); activeEvent.ModulatePitch(this.pitchOffset); activeEvent.clip = this.file; }
/// <summary> /// Randomly select a connected node /// </summary> /// <param name="activeEvent">The existing runtime audio event</param> public override void ProcessNode(ActiveEvent activeEvent) { if (this.input.ConnectedNodes == null || this.input.ConnectedNodes.Length == 0) { Debug.LogWarningFormat("No connected nodes for {0}", this.name); return; } int nodeNum = Random.Range(0, this.input.ConnectedNodes.Length); ProcessConnectedNode(nodeNum, activeEvent); }
/// <summary> /// Get the connected node of index nodeNum and process it on the ActiveEvent /// </summary> /// <param name="nodeNum">The index of the connected node to process</param> /// <param name="activeEvent">The existing runtime instance of an AudioEvent</param> protected void ProcessConnectedNode(int nodeNum, ActiveEvent activeEvent) { if (this.input == null) { Debug.LogWarningFormat(activeEvent.source, "{0} does not have an input on node {1}", activeEvent, this.name); return; } if (nodeNum >= this.input.ConnectedNodes.Length) { Debug.LogWarningFormat(activeEvent.source, "{0} tried to access invalid connected node {1}", this.name, nodeNum); return; } this.input.ConnectedNodes[nodeNum].ParentNode.ProcessNode(activeEvent); }
/// <summary> /// Start playing an AudioEvent /// </summary> /// <param name="eventToPlay">The AudioEvent to play</param> /// <param name="emitterObject">The GameObject to play the event on</param> /// <returns>The reference for the runtime event that can be modified or stopped explicitly</returns> public static ActiveEvent PlayEvent(AudioEvent eventToPlay, GameObject emitterObject) { if (eventToPlay == null) { return(null); } ValidateManager(); if (eventToPlay.InstanceLimit > 0 && CountActiveInstances(eventToPlay) >= eventToPlay.InstanceLimit) { //Debug.LogFormat("AudioManager: Instance limit reached for {0}.", eventToPlay.name); return(null); } if (eventToPlay.Group != 0) { StopGroupInstances(eventToPlay.Group); } AudioSource tempSource = GetUnusedSource(emitterObject); if (tempSource == null) { tempSource = emitterObject.AddComponent <AudioSource>(); tempSource.playOnAwake = false; } else { AvailableSources.Remove(tempSource); } ActiveEvent tempEvent = new ActiveEvent(eventToPlay, tempSource); tempEvent.Play(); if (AvailableSources.Contains(tempSource)) { AvailableSources.Remove(tempSource); } if (tempEvent.clip != null) { ActiveEvents.Add(tempEvent); } return(tempEvent); }
/// <summary> /// Play the currently-selected event in the scene /// </summary> private void PreviewEvent() { if (!Application.isPlaying) { EditorUtility.DisplayDialog("Can't Preview Audio Event", "Editor must be in play mode to preview events", "OK"); return; } if (this.previewEvent != null) { this.previewEvent.Stop(); } GameObject tempEmitter = new GameObject("Preview_" + this.selectedEvent.name); this.previewEvent = AudioManager.PlayEvent(this.selectedEvent, tempEmitter); Destroy(tempEmitter, this.previewEvent.EstimatedRemainingTime + 1); }
/// <summary> /// Get data for all ActiveEvents in the AudioManager /// </summary> private void CollectProfilerEvents() { List <ActiveEvent> activeEvents = AudioManager.GetActiveEvents(); ProfilerEvent[] currentEvents = new ProfilerEvent[activeEvents.Count]; for (int i = 0; i < currentEvents.Length; i++) { ActiveEvent tempActiveEvent = activeEvents[i]; ProfilerEvent tempProfilerEvent = new ProfilerEvent(); tempProfilerEvent.eventName = tempActiveEvent.rootEvent.name; tempProfilerEvent.clip = tempActiveEvent.source.clip; tempProfilerEvent.emitterObject = tempActiveEvent.source.gameObject; tempProfilerEvent.bus = tempActiveEvent.source.outputAudioMixerGroup; currentEvents[i] = tempProfilerEvent; } this.profilerFrames.Add(currentEvents); while (this.profilerFrames.Count > MaxFrames) { this.profilerFrames.RemoveAt(0); } }
/// <summary> /// Select a node with the current language in the AudioManager /// </summary> /// <param name="activeEvent">The existing runtime audio event</param> public override void ProcessNode(ActiveEvent activeEvent) { if (this.input.ConnectedNodes == null || this.input.ConnectedNodes.Length == 0) { Debug.LogWarningFormat("No connected nodes for {0}", this.name); return; } for (int i = 0; i < this.input.ConnectedNodes.Length; i++) { AudioNode tempNode = this.input.ConnectedNodes[i].ParentNode; if (tempNode.GetType() == typeof(AudioVoiceFile)) { AudioVoiceFile voiceNode = (AudioVoiceFile)tempNode; if (voiceNode.Language == AudioManager.CurrentLanguage) { ProcessConnectedNode(i, activeEvent); return; } } } Debug.LogErrorFormat(activeEvent.rootEvent, "AudioManager: Event \"{0}\" not localized for language: {1}", activeEvent.rootEvent.name, AudioManager.CurrentLanguage); }
/// <summary> /// Add the node's delay to the total delay of the event /// </summary> /// <param name="activeEvent"></param> public override void ProcessNode(ActiveEvent activeEvent) { activeEvent.initialDelay += this.delaySeconds; ProcessConnectedNode(0, activeEvent); }
/// <summary> /// Print out the message and move on to the next node /// </summary> /// <param name="activeEvent"></param> public override void ProcessNode(ActiveEvent activeEvent) { Debug.Log(this.message, activeEvent.source); ProcessConnectedNode(0, activeEvent); }
/// <summary> /// Base function for node functionality when the AudioEvent is played /// </summary> /// <param name="activeEvent">The existing runtime instance of an event</param> public virtual void ProcessNode(ActiveEvent activeEvent) { return; }
/// <summary> /// Internal AudioManager use: play the event using a pre-existing ActiveEvent /// </summary> /// <param name="activeEvent">The ActiveEvent for the AudioManager to update and track currently playing events</param> public void SetActiveEventProperties(ActiveEvent activeEvent) { this.output.ProcessNode(activeEvent); }