private static long ConvertFromByTicksPerQuarterNote(MetricTime time, short ticksPerQuarterNote, TempoMap tempoMap) { ThrowIfArgument.IsNull(nameof(time), time); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); // var timeMicroseconds = time.TotalMicroseconds; var accumulatedMicroseconds = 0d; var lastTime = 0L; var lastTempo = Tempo.Default; foreach (var tempoChange in tempoMap.Tempo.Values) { var tempoChangeTime = tempoChange.Time; var microseconds = GetMicroseconds(tempoChangeTime - lastTime, lastTempo, ticksPerQuarterNote); if (IsGreaterOrEqual(accumulatedMicroseconds + microseconds, timeMicroseconds)) { break; } accumulatedMicroseconds += microseconds; lastTempo = tempoChange.Value; lastTime = tempoChangeTime; } return(RoundMicroseconds(lastTime + (timeMicroseconds - accumulatedMicroseconds) * ticksPerQuarterNote / lastTempo.MicrosecondsPerQuarterNote)); }
public long ConvertFrom(ILength length, long time, TempoMap tempoMap) { ThrowIfArgument.IsNull(nameof(length), length); ThrowIfTimeArgument.IsNegative(nameof(time), time); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); var metricLength = length as MetricLength; if (metricLength == null) { throw new ArgumentException($"Length is not an instance of the {nameof(MetricLength)}.", nameof(length)); } var startTime = TimeConverter.ConvertTo <MetricTime>(time, tempoMap); var endTime = new MetricTime(startTime.TotalMicroseconds + metricLength.TotalMicroseconds); return(TimeConverter.ConvertFrom(endTime, tempoMap) - time); }
/// <summary> /// Initializes a new instance of the <see cref="MetricLength"/> with the specified /// numbers of hours, minutes, seconds and milliseconds. /// </summary> /// <param name="hours">Number of hours.</param> /// <param name="minutes">Number of minutes.</param> /// <param name="seconds">Number of seconds.</param> /// <param name="milliseconds">Number of milliseconds.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="hours"/> is negative. -or- /// <paramref name="minutes"/> is negative. -or- <paramref name="seconds"/> is negative. -or- /// <paramref name="milliseconds"/> is negative.</exception> public MetricLength(int hours, int minutes, int seconds, int milliseconds) { _time = new MetricTime(hours, minutes, seconds, milliseconds); }
/// <summary> /// Initializes a new instance of the <see cref="MetricLength"/> with the specified /// metric time using its total number of microseconds as the length. /// </summary> /// <param name="time">Metric time to initialize the <see cref="MetricLength"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="time"/> is null.</exception> public MetricLength(MetricTime time) { ThrowIfArgument.IsNull(nameof(time), time); _time = new MetricTime(time.TotalMicroseconds); }
/// <summary> /// Initializes a new instance of the <see cref="MetricLength"/> with the specified /// number of microseconds. /// </summary> /// <param name="totalMicroseconds">Number of microseconds which represents metric length.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="totalMicroseconds"/> is negative.</exception> public MetricLength(long totalMicroseconds) { _time = new MetricTime(totalMicroseconds); }
/// <summary> /// Determines whether the specified object is equal to the current object. /// </summary> /// <param name="time">The object to compare with the current object.</param> /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns> public bool Equals(MetricTime time) { return(this == time); }