private void CrossFadeNow(AudioSource audioClip) { audioClip.volume = 0f; isCrossFading = true; duckingMode = AudioDuckingMode.NotDucking; crossFadeStartTime = Time.realtimeSinceStartup; }
void Awake() { this.useGUILayout = false; duckingMode = AudioDuckingMode.NotDucking; currentSong = null; var audios = this.GetComponents <AudioSource>(); if (audios.Length < 2) { Debug.LogError("This prefab should have exactly two Audio Source components. Please revert it."); return; } AudioSource audio1 = audios[0]; AudioSource audio2 = audios[1]; audio1.clip = null; audio2.clip = null; activeAudio = audio1; transitioningAudio = audio2; go = this.gameObject; curFadeMode = FadeMode.None; fadeCompleteCallback = null; }
private void AudioDucking() { switch (duckingMode) { case AudioDuckingMode.NotDucking: break; case AudioDuckingMode.SetToDuck: activeAudio.volume = initialDuckVolume; duckingMode = AudioDuckingMode.Ducked; break; case AudioDuckingMode.Ducked: if (Time.time >= timeToFinishUnducking) { activeAudio.volume = originalMusicVolume; duckingMode = AudioDuckingMode.NotDucking; } else if (Time.time >= timeToStartUnducking) { activeAudio.volume = initialDuckVolume + (Time.time - timeToStartUnducking) / (timeToFinishUnducking - timeToStartUnducking) * duckRange; } break; } }
void Awake() { this.useGUILayout = false; duckingMode = AudioDuckingMode.NotDucking; currentSong = null; var audios = this.GetComponents<AudioSource>(); if (audios.Length < 2) { Debug.LogError("This prefab should have exactly two Audio Source components. Please revert it."); return; } AudioSource audio1 = audios[0]; AudioSource audio2 = audios[1]; audio1.clip = null; audio2.clip = null; activeAudio = audio1; transitioningAudio = audio2; go = this.gameObject; curFadeMode = FadeMode.None; fadeCompleteCallback = null; }
private void SetDuckProperties() { originalMusicVolume = activeAudio.volume; if (currentSong != null) { originalMusicVolume = currentSong.volume * PlaylistVolume; } initialDuckVolume = MasterAudio.Instance.DuckedVolumeMultiplier * originalMusicVolume; duckRange = originalMusicVolume - MasterAudio.Instance.DuckedVolumeMultiplier; duckingMode = AudioDuckingMode.NotDucking; // cancel any ducking }
public void DuckMusicForTime(float duckLength, float pitch, float duckedTimePercentage) { if (isCrossFading) { return; // no ducking during cross-fading, it screws up calculations. } var rangedDuck = duckLength / pitch; duckingMode = AudioDuckingMode.SetToDuck; timeToStartUnducking = Time.time + (rangedDuck * duckedTimePercentage); timeToFinishUnducking = Math.Max(Time.time + rangedDuck, timeToStartUnducking); }
void Awake() { // check for "extra" Playlist Controllers of the same name. var controllers = (PlaylistController[])GameObject.FindObjectsOfType(typeof(PlaylistController)); var sameNameCount = 0; for (var i = 0; i < controllers.Length; i++) { if (controllers[i].gameObject.name == gameObject.name) { sameNameCount++; } } if (sameNameCount > 1) { Destroy(gameObject); Debug.Log("More than one Playlist Controller prefab exists in this Scene with the same name. Destroying the one called '" + this.name + "'. You may wish to set up a Bootstrapper Scene so this does not occur."); return; } // end check this.useGUILayout = false; duckingMode = AudioDuckingMode.NotDucking; currentSong = null; songsPlayedFromPlaylist = 0; var audios = this.GetComponents <AudioSource>(); if (audios.Length < 2) { Debug.LogError("This prefab should have exactly two Audio Source components. Please revert it."); return; } AudioSource audio1 = audios[0]; AudioSource audio2 = audios[1]; audio1.clip = null; audio2.clip = null; activeAudio = audio1; transitioningAudio = audio2; go = this.gameObject; curFadeMode = FadeMode.None; fadeCompleteCallback = null; lostFocus = false; }
private void PlayPlaylistSong(MusicSetting setting) { AudioSource audioClip; AudioSource transClip; if (activeAudio == null) { Debug.LogError("PlaylistController prefab is not in your scene. Cannot play a song."); return; } if (activeAudio.clip == null) { audioClip = activeAudio; transClip = transitioningAudio; } else if (transitioningAudio.clip == null) { audioClip = transitioningAudio; transClip = activeAudio; } else { // both are busy! //Debug.LogWarning("Both audio sources are busy cross-fading. You may want to shorten your cross-fade time in Playlist Inspector."); audioClip = transitioningAudio; transClip = activeAudio; } if (setting.clip != null) { audioClip.clip = setting.clip; audioClip.pitch = setting.pitch; } audioClip.loop = SongShouldLoop(setting); audioClip.clip = setting.clip; audioClip.pitch = setting.pitch; // set last know time for current song. if (currentSong != null) { currentSong.lastKnownTimePoint = activeAudio.timeSamples; } if (MasterAudio.Instance.CrossFadeTime == 0 || transClip.clip == null) { CeaseAudioSource(transClip); audioClip.volume = setting.volume * PlaylistVolume; } else { audioClip.volume = 0f; isCrossFading = true; duckingMode = AudioDuckingMode.NotDucking; crossFadeStartTime = Time.time; } SetDuckProperties(); if (currentPlaylist != null) { switch (currentPlaylist.songTransitionType) { case MasterAudio.SongFadeInPosition.SynchronizeClips: transitioningAudio.timeSamples = activeAudio.timeSamples; break; case MasterAudio.SongFadeInPosition.NewClipFromLastKnownPosition: var thisSongInPlaylist = currentPlaylist.MusicSettings.Find(delegate(MusicSetting obj) { return obj == setting; }); if (thisSongInPlaylist != null) { transitioningAudio.timeSamples = thisSongInPlaylist.lastKnownTimePoint; } break; } } if (SongChanged != null) { var clipName = String.Empty; if (audioClip != null) { clipName = audioClip.clip.name; } SongChanged(clipName); } audioClip.Play(); activeAudio = audioClip; transitioningAudio = transClip; activeAudioEndVolume = setting.volume * PlaylistVolume; transitioningAudioStartVolume = transitioningAudio.volume * PlaylistVolume; currentSong = setting; }
void Awake() { // check for "extra" Playlist Controllers of the same name. var controllers = (PlaylistController[]) GameObject.FindObjectsOfType(typeof(PlaylistController)); var sameNameCount = 0; for (var i = 0; i < controllers.Length; i++) { if (controllers[i].gameObject.name == gameObject.name) { sameNameCount++; } } if (sameNameCount > 1) { Destroy(gameObject); Debug.Log("More than one Playlist Controller prefab exists in this Scene with the same name. Destroying the one called '" + this.name + "'. You may wish to set up a Bootstrapper Scene so this does not occur."); return; } // end check this.useGUILayout = false; duckingMode = AudioDuckingMode.NotDucking; currentSong = null; songsPlayedFromPlaylist = 0; var audios = this.GetComponents<AudioSource>(); if (audios.Length < 2) { Debug.LogError("This prefab should have exactly two Audio Source components. Please revert it."); return; } AudioSource audio1 = audios[0]; AudioSource audio2 = audios[1]; audio1.clip = null; audio2.clip = null; activeAudio = audio1; transitioningAudio = audio2; go = this.gameObject; curFadeMode = FadeMode.None; fadeCompleteCallback = null; }
public void DuckMusicForTime(float duckLength, float pitch, float duckedTimePercentage) { if (isCrossFading) { return; // no ducking during cross-fading, it screws up calculations. } var rangedDuck = duckLength / pitch; duckingMode = AudioDuckingMode.SetToDuck; timeToStartUnducking = Time.time + (rangedDuck * duckedTimePercentage); timeToFinishUnducking = Math.Max(Time.time + rangedDuck, timeToStartUnducking); }
/*! \endcond */ #region Monobehavior events // ReSharper disable once UnusedMember.Local private void Awake() { useGUILayout = false; if (ControllerIsReady) { // already called by lazy load. return; } ControllerIsReady = false; // check for "extra" Playlist Controllers of the same name. // ReSharper disable once ArrangeStaticMemberQualifier var controllers = (PlaylistController[])GameObject.FindObjectsOfType(typeof(PlaylistController)); var sameNameCount = 0; // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < controllers.Length; i++) { if (controllers[i].ControllerName == ControllerName) { sameNameCount++; } } if (sameNameCount > 1) { Destroy(gameObject); Debug.Log( "More than one Playlist Controller prefab exists in this Scene with the same name. Destroying the one called '" + ControllerName + "'. You may wish to set up a Bootstrapper Scene so this does not occur."); return; } // end check _duckingMode = AudioDuckingMode.NotDucking; _currentSong = null; _songsPlayedFromPlaylist = 0; var audios = GetComponents<AudioSource>(); if (audios.Length < 2) { Debug.LogError("This prefab should have exactly two Audio Source components. Please revert it."); return; } var ma = MasterAudio.SafeInstance; _willPersist = ma != null && ma.persistBetweenScenes; _audio1 = audios[0]; _audio2 = audios[1]; _audio1.clip = null; _audio2.clip = null; if (_audio1.playOnAwake || _audio2.playOnAwake) { Debug.LogWarning( "One or more 'Play on Awake' checkboxes in the Audio Sources on Playlist Controller '" + name + "' are checked. These are not used in Master Audio. Make sure to uncheck them before hitting Play next time. For Playlist Controllers, use the similarly named checkbox 'Start Playlist on Awake' in the Playlist Controller's Inspector."); } _activeAudio = _audio1; _transitioningAudio = _audio2; #if UNITY_5 _audio1.outputAudioMixerGroup = mixerChannel; _audio2.outputAudioMixerGroup = mixerChannel; SetSpatialBlend(); #endif _curFadeMode = FadeMode.None; _fadeCompleteCallback = null; _lostFocus = false; }
private void PlayPlaylistSong(MusicSetting setting) { AudioSource audioClip; AudioSource transClip; if (activeAudio == null) { Debug.LogError("PlaylistController prefab is not in your scene. Cannot play a song."); return; } if (activeAudio.clip != null) { var newClipName = string.Empty; if (setting.clip != null) { newClipName = setting.clip.name; } if (activeAudio.clip.name.Equals(newClipName)) { // ignore, it's the same clip! Playing the same causes it to stop. return; } } if (activeAudio.clip == null) { audioClip = activeAudio; transClip = transitioningAudio; } else if (transitioningAudio.clip == null) { audioClip = transitioningAudio; transClip = activeAudio; } else { // both are busy! audioClip = transitioningAudio; transClip = activeAudio; } if (setting.clip != null) { audioClip.clip = setting.clip; audioClip.pitch = setting.pitch; } audioClip.loop = SongShouldLoop(setting); AudioClip clipToPlay = null; switch (setting.audLocation) { case MasterAudio.AudioLocation.Clip: if (setting.clip == null) { MasterAudio.LogWarning("MasterAudio will not play empty Playlist clip for PlaylistController '" + this.transform.name + "'."); return; } clipToPlay = setting.clip; break; case MasterAudio.AudioLocation.ResourceFile: clipToPlay = AudioResourceOptimizer.PopulateResourceSongToPlaylistController(setting.resourceFileName, this.currentPlaylist.playlistName); if (clipToPlay == null) { return; } break; } audioClip.clip = clipToPlay; audioClip.pitch = setting.pitch; // set last known time for current song. if (currentSong != null) { currentSong.lastKnownTimePoint = activeAudio.timeSamples; } if (MasterAudio.Instance.CrossFadeTime == 0 || transClip.clip == null) { CeaseAudioSource(transClip); audioClip.volume = setting.volume * PlaylistVolume; } else { audioClip.volume = 0f; isCrossFading = true; duckingMode = AudioDuckingMode.NotDucking; crossFadeStartTime = Time.realtimeSinceStartup; } SetDuckProperties(); audioClip.Play(); // need to play before setting time or it sometimes resets back to zero. if (syncGroupNum > 0) { var firstMatchingGroupController = PlaylistController.Instances.Find(delegate(PlaylistController obj) { return obj != this && obj.syncGroupNum == syncGroupNum && obj.ActiveAudioSource.isPlaying; }); if (firstMatchingGroupController != null) { audioClip.timeSamples = firstMatchingGroupController.activeAudio.timeSamples; } } if (currentPlaylist != null) { switch (currentPlaylist.songTransitionType) { case MasterAudio.SongFadeInPosition.SynchronizeClips: transitioningAudio.timeSamples = activeAudio.timeSamples; break; case MasterAudio.SongFadeInPosition.NewClipFromLastKnownPosition: var thisSongInPlaylist = currentPlaylist.MusicSettings.Find(delegate(MusicSetting obj) { return obj == setting; }); if (thisSongInPlaylist != null) { transitioningAudio.timeSamples = thisSongInPlaylist.lastKnownTimePoint; } break; } } if (SongChanged != null) { var clipName = String.Empty; if (audioClip != null) { clipName = audioClip.clip.name; } SongChanged(clipName); } activeAudio = audioClip; transitioningAudio = transClip; activeAudioEndVolume = setting.volume * PlaylistVolume; var transStartVol = transitioningAudio.volume; if (currentSong != null) { transStartVol = currentSong.volume; } transitioningAudioStartVolume = transStartVol * PlaylistVolume; currentSong = setting; }
/*! \cond PRIVATE */ public void DuckMusicForTime(float duckLength, float pitch, float duckedTimePercentage) { if (MasterAudio.IsWarming) { return; } if (!ControllerIsReady) { Debug.LogError(NotReadyMessage); return; } if (IsCrossFading) { return; // no ducking during cross-fading, it screws up calculations. } var rangedDuck = duckLength / pitch; _duckingMode = AudioDuckingMode.SetToDuck; _timeToStartUnducking = Time.realtimeSinceStartup + (rangedDuck * duckedTimePercentage); _timeToFinishUnducking = Math.Max(Time.realtimeSinceStartup + rangedDuck, _timeToStartUnducking); }
private void SetDuckProperties() { _originalMusicVolume = _activeAudio == null ? 1 : _activeAudio.volume; if (_currentSong != null) { _originalMusicVolume = _currentSong.volume * PlaylistVolume; } _initialDuckVolume = MasterAudio.Instance.DuckedVolumeMultiplier * _originalMusicVolume; _duckRange = _originalMusicVolume - MasterAudio.Instance.DuckedVolumeMultiplier; _duckingMode = AudioDuckingMode.NotDucking; // cancel any ducking }
/*! \cond PRIVATE */ public void DuckMusicForTime(float duckLength, float unduckTime, float pitch, float duckedTimePercentage, float duckedVolMultiplier) { if (MasterAudio.IsWarming) { return; } if (!ControllerIsReady) { Debug.LogError(NotReadyMessage); return; } if (IsCrossFading) { return; // no ducking during cross-fading, it screws up calculations. } var rangedDuck = duckLength / pitch; _initialDuckVolume = duckedVolMultiplier * _originalMusicVolume; _duckRange = _originalMusicVolume - duckedVolMultiplier; _duckingMode = AudioDuckingMode.SetToDuck; _timeToStartUnducking = Time.realtimeSinceStartup + (rangedDuck * duckedTimePercentage); var duckFinishTime = _timeToStartUnducking + unduckTime; if (duckFinishTime > Time.realtimeSinceStartup + rangedDuck) { duckFinishTime = Time.realtimeSinceStartup + rangedDuck; } _timeToFinishUnducking = duckFinishTime; }
private void SetDuckProperties() { originalMusicVolume = activeAudio.volume; if (currentSong != null) { originalMusicVolume = currentSong.volume * PlaylistVolume; } initialDuckVolume = MasterAudio.Instance.DuckedVolumeMultiplier * originalMusicVolume; duckRange = originalMusicVolume - MasterAudio.Instance.DuckedVolumeMultiplier; duckingMode = AudioDuckingMode.NotDucking; // cancel any ducking }
private void SetDuckProperties() { _originalMusicVolume = _activeAudio == null ? 1 : _activeAudio.volume; if (_currentSong != null) { _originalMusicVolume = _currentSong.volume * PlaylistVolume; } var origDb = AudioUtil.GetDbFromFloatVolume(_originalMusicVolume); var targetVolumeDb = origDb - _currentDuckVolCut; var targetVolumeNormal = AudioUtil.GetFloatVolumeFromDb(targetVolumeDb); _duckRange = _originalMusicVolume - targetVolumeNormal; _initialDuckVolume = targetVolumeNormal; _duckingMode = AudioDuckingMode.NotDucking; // cancel any ducking }
private void AudioDucking() { switch (duckingMode) { case AudioDuckingMode.NotDucking: break; case AudioDuckingMode.SetToDuck: activeAudio.volume = initialDuckVolume; duckingMode = AudioDuckingMode.Ducked; break; case AudioDuckingMode.Ducked: if (Time.time >= timeToFinishUnducking) { activeAudio.volume = originalMusicVolume; duckingMode = AudioDuckingMode.NotDucking; } else if (Time.time >= timeToStartUnducking) { activeAudio.volume = initialDuckVolume + (Time.time - timeToStartUnducking) / (timeToFinishUnducking - timeToStartUnducking) * duckRange; } break; } }
private void AudioDucking() { switch (_duckingMode) { case AudioDuckingMode.NotDucking: break; case AudioDuckingMode.SetToDuck: _activeAudio.volume = _initialDuckVolume; _duckingMode = AudioDuckingMode.Ducked; break; case AudioDuckingMode.Ducked: if (Time.realtimeSinceStartup >= _timeToFinishUnducking) { _activeAudio.volume = _originalMusicVolume; _duckingMode = AudioDuckingMode.NotDucking; } else if (Time.realtimeSinceStartup >= _timeToStartUnducking) { _activeAudio.volume = _initialDuckVolume + (Time.realtimeSinceStartup - _timeToStartUnducking) / (_timeToFinishUnducking - _timeToStartUnducking) * _duckRange; } break; } }
private void CrossFadeNow(AudioSource audioClip) { audioClip.volume = 0f; isCrossFading = true; duckingMode = AudioDuckingMode.NotDucking; crossFadeStartTime = Time.realtimeSinceStartup; }
/*! \cond PRIVATE */ public void DuckMusicForTime(float duckLength, float unduckTime, float pitch, float duckedTimePercentage, float duckedVolCut) { if (MasterAudio.IsWarming) { return; } if (!ControllerIsReady) { Debug.LogError(NotReadyMessage); return; } if (IsCrossFading) { return; // no ducking during cross-fading, it screws up calculations. } var rangedDuck = duckLength / pitch; _currentDuckVolCut = duckedVolCut; // store for later usage var origDb = AudioUtil.GetDbFromFloatVolume(_originalMusicVolume); var targetVolumeDb = origDb + duckedVolCut; var targetVolumeNormal = AudioUtil.GetFloatVolumeFromDb(targetVolumeDb); _initialDuckVolume = targetVolumeNormal; _duckRange = _originalMusicVolume - targetVolumeNormal; _duckingMode = AudioDuckingMode.SetToDuck; _timeToStartUnducking = Time.realtimeSinceStartup + (rangedDuck * duckedTimePercentage); var duckFinishTime = _timeToStartUnducking + unduckTime; if (duckFinishTime > Time.realtimeSinceStartup + rangedDuck) { duckFinishTime = Time.realtimeSinceStartup + rangedDuck; } _timeToFinishUnducking = duckFinishTime; }