public void StopTrack(int trackIndex) { if (sourceGroup1) { if (backgroundAudioSources1[trackIndex].isPlaying) { GameObject decayingSourceObj = Instantiate(decayingAudioSource) as GameObject; DecayingAudioSource decAudioSource = decayingSourceObj.GetComponent <DecayingAudioSource>(); decAudioSource.sourceClip = backgroundAudioSources1[trackIndex].clip; decAudioSource.fadeOutSpeed = currentPhase.crossFadeSpeed; decAudioSource.startTime = backgroundAudioSources1[trackIndex].time; decAudioSource.fadeVolume = true; decAudioSource.volume = backgroundAudioSources1[trackIndex].volume; decAudioSource.GetComponent <AudioSource>().outputAudioMixerGroup = backgroundMusicMixer; backgroundAudioSources1[trackIndex].Stop(); } } else { if (backgroundAudioSources2[trackIndex].isPlaying) { GameObject decayingSourceObj = Instantiate(decayingAudioSource) as GameObject; DecayingAudioSource decAudioSource = decayingSourceObj.GetComponent <DecayingAudioSource>(); decAudioSource.sourceClip = backgroundAudioSources2[trackIndex].clip; decAudioSource.fadeOutSpeed = currentPhase.crossFadeSpeed; decAudioSource.startTime = backgroundAudioSources2[trackIndex].time; decAudioSource.fadeVolume = true; decAudioSource.volume = backgroundAudioSources2[trackIndex].volume; decAudioSource.GetComponent <AudioSource>().outputAudioMixerGroup = backgroundMusicMixer; backgroundAudioSources2[trackIndex].Stop(); } } trackPlayStates[trackIndex] = false; }
/// <summary> /// Loads the details of a new phase (scriptable Object) into the current working song phase. /// </summary> private void LoadNewPhase(int phaseIndex) { // Load the details of this phase into the currentPhase currentPhase.name = BeatTimingManager.btmInstance.currentSong.songPhases[phaseIndex].name; currentPhase.nextPhases = BeatTimingManager.btmInstance.currentSong.songPhases[phaseIndex].nextPhases; currentPhase.autoTransition = BeatTimingManager.btmInstance.currentSong.songPhases[phaseIndex].autoTransition; currentPhase.phaseTracks = BeatTimingManager.btmInstance.currentSong.songPhases[phaseIndex].phaseTracks; // Update the current tracks of our audiosources and phase out the previous ones. for (int i = 0; i < backgroundAudioSources.Length; i++) { // Create decaying audio sources to fade out the currently playing sounds if (backgroundAudioSources[i].clip != null) { GameObject decayingSourceObj = Instantiate(decayingAudioSource) as GameObject; DecayingAudioSource decAudioSource = decayingSourceObj.GetComponent <DecayingAudioSource>(); decAudioSource.sourceClip = backgroundAudioSources[i].clip; decAudioSource.fadeOutSpeed = currentPhase.crossFadeSpeed; decAudioSource.startTime = backgroundAudioSources[i].time; decAudioSource.fadeVolume = true; } // Update the permanent audio source tracks to play the new sounds if (i < currentPhase.phaseTracks.Length) { if (currentPhase.phaseTracks[i] != null) { backgroundAudioSources[i].clip = currentPhase.phaseTracks[i]; } else { backgroundAudioSources[i].clip = null; } } // We set the volume of the permanent AudioSources to 0 so they can fade in anew. backgroundAudioSources[i].volume = 0; backgroundAudioSources[i].time = (float)BeatTimingManager.btmInstance.seekerTime; fadeInAudiosources = true; } currentPhase.crossFadeSpeed = BeatTimingManager.btmInstance.currentSong.songPhases[phaseIndex].crossFadeSpeed; if (autoReactivateTracksOnPhaseSwitch) { PlayAllTracks(); } else { // Only replay those that have true PlayStates PlayActiveTracks(); } BeatTimingManager.btmInstance.LoadSongPhase(); }
/// <summary> /// Creates a decaying audio source with all the properties of our current composition source. /// This source is setup such that it will become perpetually softer until it destroys itself. /// </summary> private void FadeOutCompositionSource(bool fadeOutVolume, float fadeSpeed) { // Instantiate decaying source - while keeping a reference to it GameObject decayingSourceObj = Instantiate(decayingAudioSource) as GameObject; DecayingAudioSource decayingSource = decayingSourceObj.GetComponent <DecayingAudioSource>(); // Update the decaying source's setting to match those of our composition source. decayingSource.sourceClip = compositionAudioSource.clip; decayingSource.volume = compositionAudioSource.volume; decayingSource.startTime = compositionAudioSource.time; decayingSource.fadeVolume = fadeOutVolume; decayingSource.fadeOutSpeed = fadeSpeed; }
void SpawnDecayingSource() { GameObject decayingSourceObj = Instantiate(decayingAudioSource) as GameObject; DecayingAudioSource decAudioSource = decayingSourceObj.GetComponent <DecayingAudioSource>(); decAudioSource.sourceClip = backgroundSource.clip; decAudioSource.fadeOutSpeed = fadeSpeed; decAudioSource.startTime = backgroundSource.time; decAudioSource.fadeVolume = true; backgroundSource.clip = newClip; backgroundSource.volume = 0; }