//Remove a HDR audio source to the collection public void RemoveAudioSource(HDRAudioSource source) { int index = AudioSources.FindIndex(p => p.Audio == source); if (index != -1) { AudioSources.RemoveAt(index); } }
public AudioTuple(HDRAudioSource source) { Audio = source; //ActualLoudness = source.Decibel; //DynamicLoudness = ActualLoudness; }
private bool IsSourceActive(HDRAudioSource audio) { return(audio.enabled && audio.attachedTo.isPlaying && !audio.attachedTo.mute); }
//Add a HDR audio source to the collection, don't add the same twice public void AddAudioSource(HDRAudioSource source) { AudioSources.Add(new HDRAudio.AudioTuple(source)); }
//http://en.wikipedia.org/wiki/Decibel Returns the power ratio sum and finds the loudest source private float UpdateHDRLoudness(out HDRAudio.AudioTuple loudestSource) { float loudnessSum = 0.0f; Vector3 listenerPos = ActiveListener.transform.position; loudestSource = AudioSources[0]; for (int i = 0; i < AudioSources.Count; ++i) { HDRAudioSource current = AudioSources[i].Audio; var audio = current.attachedTo; AudioSources[i].ActualLoudness = 0; if (IsSourceActive(current)) { float listenerDistance = Vector3.Distance(current.transform.position, listenerPos); float distanceBasedVolume = 0.0f; if (listenerDistance > audio.maxDistance) { distanceBasedVolume = 0.0f; } else if (audio.rolloffMode == AudioRolloffMode.Logarithmic) { //volume = min distance / distance //Clamp volume to max 1 distanceBasedVolume = Mathf.Min(audio.minDistance / listenerDistance, 1); } else if (audio.rolloffMode == AudioRolloffMode.Linear) { //volume = linear falloff between min and max distance, 1 to 0 distanceBasedVolume = Mathf.Clamp(1 - (listenerDistance - audio.minDistance) / (audio.maxDistance - audio.minDistance), 0, 1); } else if (audio.rolloffMode == AudioRolloffMode.Custom) { distanceBasedVolume = current.CustomFalloffCurve.Evaluate(listenerDistance / audio.maxDistance); } AudioSources[i].ActualLoudness = distanceBasedVolume * current.Decibel; } float target = AudioSources[i].ActualLoudness; float from = AudioSources[i].DynamicLoudness; //Make sure it the volume doesn't transition too fast AudioSources[i].DynamicLoudness = LerpClamp(from, target, Time.deltaTime, DBReactionSpeed); if (AudioSources[i].DynamicLoudness > loudestSource.DynamicLoudness) { loudestSource = AudioSources[i]; } //As x^y, where y < 1 && y >= 0, the result would be one, we don't want inaudible sources to contribute loudness if (current.ContributeToEnviroment && AudioSources[i].DynamicLoudness > 0.0f) { loudnessSum += Mathf.Pow(10, AudioSources[i].DynamicLoudness / 10.0f); } } return(loudnessSum); }