A game framework written with osu! in mind.
at master 99 lines 2.2 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using osu.Framework.Timing; 6 7namespace osu.Framework.Audio.Track 8{ 9 public sealed class TrackVirtual : Track 10 { 11 private readonly StopwatchClock clock = new StopwatchClock(); 12 13 private double seekOffset; 14 15 public TrackVirtual(double length) 16 { 17 Length = length; 18 } 19 20 public override bool Seek(double seek) 21 { 22 seekOffset = Math.Clamp(seek, 0, Length); 23 24 lock (clock) 25 { 26 if (IsRunning) 27 clock.Restart(); 28 else 29 clock.Reset(); 30 } 31 32 return seekOffset == seek; 33 } 34 35 public override void Start() 36 { 37 if (Length == 0) 38 return; 39 40 lock (clock) clock.Start(); 41 } 42 43 public override void Reset() 44 { 45 lock (clock) clock.Reset(); 46 seekOffset = 0; 47 48 base.Reset(); 49 } 50 51 public override void Stop() 52 { 53 lock (clock) clock.Stop(); 54 } 55 56 public override bool IsRunning 57 { 58 get 59 { 60 lock (clock) return clock.IsRunning; 61 } 62 } 63 64 public override double CurrentTime 65 { 66 get 67 { 68 lock (clock) return Math.Min(Length, seekOffset + clock.CurrentTime); 69 } 70 } 71 72 protected override void UpdateState() 73 { 74 base.UpdateState(); 75 76 lock (clock) 77 { 78 if (clock.IsRunning && CurrentTime >= Length) 79 { 80 if (Looping) 81 Restart(); 82 else 83 { 84 Stop(); 85 RaiseCompleted(); 86 } 87 } 88 } 89 } 90 91 internal override void OnStateChanged() 92 { 93 base.OnStateChanged(); 94 95 lock (clock) 96 clock.Rate = Rate; 97 } 98 } 99}