/// <summary> /// Scrubs the currently-playing track to a specific point in its timeline. /// </summary> /// <param name="time">How far along to scrub the track, in seconds</param> public void Scrub(float time) { if (PlayingTrack != null) { time = Mathf.Min(time, PlayingTrack.LengthInSeconds); CurrentSpeaker.SetPosition(PlayingTrack, PlayingTrack.SecondsToSamples(time)); } }
private void Update() { for (int i = 0; i < playingTracks.Count; ++i) { PlayingTrack pt = playingTracks[i]; if (pt.audioSource.volume != pt.targetVolume) { if (!pt.fadeStarted) { if (pt.targetVolume == 0) { float timeDiff = baseTrack.loopTrack.clip.length - baseTrack.audioSource.time; pt.fadeStarted = timeDiff <= fadeTime; } else { pt.fadeStarted = baseTrack.audioSource.time <= fadeTime; } } if (pt.fadeStarted == true) { float volumeFactor = 0; if (pt.targetVolume == 0) { float timeDiff = baseTrack.loopTrack.clip.length - baseTrack.audioSource.time; volumeFactor = Mathf.Clamp01(timeDiff / fadeTime); if (volumeFactor * pt.loopTrack.volume > pt.audioSource.volume) //Wrapping { volumeFactor = 0; } } else { volumeFactor = Mathf.Clamp01(baseTrack.audioSource.time / fadeTime); } pt.audioSource.volume = pt.loopTrack.volume * volumeFactor; } if (pt.audioSource.volume == pt.targetVolume) { pt.fadeStarted = false; if (pt.targetVolume > 0) { onLoopStarted.Invoke(pt.loopTrack); } else { onLoopStopped.Invoke(pt.loopTrack); } } } } }
public void AddTrack(LoopTrack aTrack) { PlayingTrack pt = new PlayingTrack(); pt.audioSource = GetNewAudioSource(aTrack); pt.loopTrack = aTrack; playingTracks.Add(pt); }
/// <summary> /// Plays the given track starting at the given play position. /// </summary> /// <param name="track">The track to play</param> /// <param name="time">The time to play the track at, in seconds</param> /// <param name="numberOfLoops">The number of times to loop the track. Set to 0 for endless play.</param> public void PlayAtPoint(MusicTrack track, float time, int numberOfLoops) { RemovePauseState(); Stop(); SetTrack(track); IntroSource.clip = PlayingTrack.IntroClip; LoopSource.clip = PlayingTrack.LoopClip; PlayAtPosition(PlayingTrack.SecondsToSamples(time), numberOfLoops); }
public void StopTrack(LoopTrack aTrack) { PlayingTrack pt = playingTracks.Find(x => x.loopTrack == aTrack); if (pt == null) { Debug.LogError("Trying to stop track that hasn't been added: " + aTrack.ToString()); return; } pt.targetVolume = 0; }
public void SetBaseTrack(LoopTrack aTrack) { PlayingTrack pt = playingTracks.Find(x => x.loopTrack == aTrack); if (pt == null) { Debug.LogError("Trying to set base track that hasn't been added: " + aTrack.ToString()); return; } baseTrack = pt; }
internal void SetPosition(MusicTrack track, int position) { SetTrack(track); if (IsPaused) { pausePosition = Math.Min(position, CurrentLengthInSamples); } else if (IsPlaying) { PlayAtPoint(PlayingTrack.SamplesToSeconds(position), numberOfLoopsLeft); } }
public void RemoveTrack(LoopTrack aTrack) { PlayingTrack pt = playingTracks.Find(x => x.loopTrack == aTrack); if (pt == null) { Debug.LogError("Trying to remove track that hasn't been added: " + aTrack.ToString()); return; } if (pt == baseTrack) { Debug.LogError("Trying to remove base track [" + aTrack.ToString() + "], replace with another track first!"); return; } Destroy(pt.audioSource); playingTracks.Remove(pt); }
/// <summary> /// Plays the current track starting at the given play position. /// </summary> /// <param name="samplePosition">The play position, in samples</param> /// <param name="numberOfLoops">The number of times to loop the track. Set to 0 for endless play.</param> private void PlayAtPosition(int samplePosition, int numberOfLoops) { if (PlayingTrack != null) { if (samplePosition <= IntroSource.clip.samples) { IntroSource.timeSamples = samplePosition; LoopSource.timeSamples = 0; IntroSource.Play(); LoopSource.PlayDelayed(PlayingTrack.SamplesToSeconds(IntroSource.clip.samples - samplePosition)); } else { LoopSource.timeSamples = samplePosition - IntroSource.clip.samples; LoopSource.Play(); } numberOfLoopsLeft = numberOfLoops; InvokeRepeating("OnTrackEndOrLoop", PlayingTrack.SamplesToSeconds(PlayingTrack.LengthInSamples - samplePosition), PlayingTrack.SamplesToSeconds(PlayingTrack.LoopLength)); } }