public int MillisToTicks(int time)
{
var ticks = 0;
var bpm = 120.0;
var lastChange = 0;
// find start and bpm of last tempo change before time
for (int i = 0; i < _tempoChanges.Count; i++)
{
var c = _tempoChanges[i];
if (time < c.Time)
{
break;
}
ticks = c.Ticks;
bpm = c.Bpm;
lastChange = c.Time;
}
// add the missing ticks
time -= lastChange;
ticks += (int)(time / (60000.0 / (bpm * _division)));
return ticks;
}