public void TriggerMusicVariation() { if (currentChange != null || currentTransition.state != Transition.State.Done) { return; } List <LoopChange> potentialChanges = new List <LoopChange>(); float highestChangeWeight = 0; int highestChangeIndex = -1; List <MusicLooper.PlayingTrack> playingTracks = musicLooper.activeTracks; for (int i = 0; i < playingTracks.Count; ++i) { MusicLooper.PlayingTrack playingTrack = playingTracks[i]; float random = Random.Range(0.0f, 1.0f); float weight = currentLoopCollection.loopTracks.Find(x => x.loopTrack == playingTrack.loopTrack).weight; bool shouldPlay = random <= weight; if (playingTrack.isPlaying != shouldPlay) { potentialChanges.Add(new LoopChange(playingTrack.loopTrack, shouldPlay)); } else //Guarantee a change { float weightToChange = playingTrack.isPlaying ? 1 - weight : weight; if (weightToChange > highestChangeWeight) { highestChangeWeight = weightToChange; highestChangeIndex = i; } } } if (potentialChanges.Count > 0) { int index = Random.Range(0, potentialChanges.Count); LoopChange change = potentialChanges[index]; AddTrackChange(change.track, change.play); } else if (highestChangeIndex != -1) { MusicLooper.PlayingTrack track = playingTracks[highestChangeIndex]; AddTrackChange(track.loopTrack, !track.isPlaying); } }
public float GetCurrentSequenceTime() { if (currentLoopSequence == null) { return(0); } MusicLooper.PlayingTrack playingTrack = musicLooper.activeTracks.Find(x => x.loopTrack == currentLoopSequence.baseLoop); if (playingTrack == null) { return(0); } return(playingTrack.audioSource.time); }
public void TriggerMusicSequence(LoopSequenceTrigger aTrigger) { LoopSequence loopSequence = loopSequences.Find(x => x.sequences.Exists(y => y.trigger == aTrigger)); if (loopSequences == null) { return; } List <MusicLooper.PlayingTrack> playingTracks = musicLooper.activeTracks; currentTracksForSequence.Clear(); currentTracksToRemove.Clear(); AddSequenceTracks(loopSequence, aTrigger); musicLooper.SetBaseTrack(loopSequence.baseLoop); currentLoopSequence = loopSequence; for (int i = playingTracks.Count - 1; i >= 0; --i) { MusicLooper.PlayingTrack playingTrack = playingTracks[i]; if (!currentTracksForSequence.Exists(x => x == playingTrack.loopTrack)) { if (playingTrack.isPlaying && !playingTrack.isChanging) { currentTracksToRemove.Add(playingTrack.loopTrack); musicLooper.StopTrack(playingTrack.loopTrack); } else { musicLooper.RemoveTrack(playingTrack.loopTrack); } } } if (currentTracksToRemove.Count == 0) { PlaySequenceTracks(); } else { musicLooper.onLoopStopped += OnLoopStopped; } }
public void SetLoopCollection(LoopCollection aLoopCollection) { if (currentLoopCollection == aLoopCollection || currentTransition.state != Transition.State.Done) { return; } Debug.Log("GameMusicHandler: Setting loop collection: " + aLoopCollection.ToString()); List <MusicLooper.PlayingTrack> playingTracks = musicLooper.activeTracks; if (playingTracks.Count == 0) { currentLoopCollection = aLoopCollection; Debug.Log("GameMusicHandler: No previous loop collection"); foreach (LoopCollection.Track track in aLoopCollection.loopTracks) { AddTrack(track.loopTrack); if (track.weight == 1) { musicLooper.PlayTrack(track.loopTrack); } } musicLooper.SetBaseTrack(aLoopCollection.loopBase); } else { currentTransition.tracksToRemove.Clear(); currentChange = null; currentTransition.nextCollection = aLoopCollection; currentTransition.transitionBaseTrack = aLoopCollection.loopBase; if (currentLoopCollection != null && currentLoopCollection.loopBase.clip.length < aLoopCollection.loopBase.clip.length) { currentTransition.transitionBaseTrack = currentLoopCollection.loopBase; } Debug.Log("GameMusicHandler: Setting transition base track: " + currentTransition.transitionBaseTrack.ToString()); bool hasTransitionTrack = false; foreach (LoopCollection.Track track in aLoopCollection.loopTracks) { MusicLooper.PlayingTrack playingTrack = playingTracks.Find(x => x.loopTrack == track.loopTrack); if (playingTrack == null) { AddTrack(track.loopTrack); } else if (!playingTrack.isPlaying) { musicLooper.PlayTrack(track.loopTrack); hasTransitionTrack = true; Debug.Log("GameMusicHandler: Found transition track: " + track.ToString()); } } if (hasTransitionTrack) { currentTransition.state = Transition.State.StartingTransitionTracks; Debug.Log("GameMusicHandler: State: " + Transition.State.StartingTransitionTracks.ToString()); } else { StopTracksToRemove(); } } }