using System; using System.Collections.Generic; using System.Text; namespace SalsaModel { public class Timing : SalsaModel.ITiming { //public double BeatLength = 300; //public double BeatLength = 1000; const bool c_onOne = false; double _beatLength = 300; public double BeatLength { get { return _beatLength; } set { double beatsSoFar = ((double)Tick) / _beatLength; _beatLength = value; initialCount = System.Environment.TickCount - (int)(beatsSoFar * _beatLength); } } public int initialCount = 0; bool paused; public int Tick { get { if (initialCount == 0) { initialCount = System.Environment.TickCount; } if (paused) { return pauseTime - initialCount; } else { return System.Environment.TickCount - initialCount; } } } /// /// The current beat, from 1 to 8. NOTE: not 0-7! /// /// public int Beat() { return ((int)(Tick / BeatLength) % 8) + 1; } public int GlobalBeat() { return (int)(Tick / BeatLength) + 1; } public double Percent() { return (Tick % BeatLength) / BeatLength; } /// /// Returns the current step number, 0-5. For on-1, step 0 is beat 1; /// for on-2, the steps are not aligned directly with the bars. The /// beats 4 and 8 where "nothing happens" are returned as -1. /// public int StepNumber() { int beat = Beat(); if (c_onOne) { switch (beat) { case 1: case 2: case 3: return beat - 1; case 5: case 6: case 7: return beat - 2; case 4: case 8: return -1; default: throw new InvalidOperationException("bad beat"); } } else { switch (beat) { // case 5: return 0; case 6: return 0; case 7: return 1; case 1: return 2; case 2: return 3; case 3: return 4; case 5: return 5; case 8: case 4: return -1; default: throw new InvalidOperationException("bad beat"); } } } int pauseTime; public void Pause() { pauseTime = System.Environment.TickCount; paused = true; } public void Unpause() { initialCount += System.Environment.TickCount - pauseTime; paused = false; } } }