private static long MetricTimeSpanToTicks(MetricTimeSpan time, short ticksPerQuarterNote, TempoMap tempoMap)
        {
            var timeMicroseconds = time.TotalMicroseconds;

            if (timeMicroseconds == 0)
            {
                return(0);
            }

            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));
        }
Example #2
0
        internal static ParsingResult TryParse(string input, out MetricTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            int hours;

            if (!ParsingUtilities.ParseInt(match, HoursGroupName, 0, out hours))
            {
                return(new ParsingResult(HoursIsOutOfRange));
            }

            int minutes;

            if (!ParsingUtilities.ParseInt(match, MinutesGroupName, 0, out minutes))
            {
                return(new ParsingResult(MinutesIsOutOfRange));
            }

            int seconds;

            if (!ParsingUtilities.ParseInt(match, SecondsGroupName, 0, out seconds))
            {
                return(new ParsingResult(SecondsIsOutOfRange));
            }

            int milliseconds;

            if (!ParsingUtilities.ParseInt(match, MillisecondsGroupName, 0, out milliseconds))
            {
                return(new ParsingResult(MillisecondsIsOutOfRange));
            }

            timeSpan = new MetricTimeSpan(hours, minutes, seconds, milliseconds);
            return(ParsingResult.Parsed);
        }
Example #3
0
        private static long MetricTimeSpanToTicks(MetricTimeSpan timeSpan, TempoMap tempoMap)
        {
            var timeMicroseconds = timeSpan.TotalMicroseconds;

            if (timeMicroseconds == 0)
            {
                return(0);
            }

            var valuesCache             = tempoMap.GetValuesCache <MetricTempoMapValuesCache>();
            var accumulatedMicroseconds = valuesCache.Microseconds.TakeWhile(m => m.Microseconds < timeMicroseconds).LastOrDefault();

            var lastAccumulatedMicroseconds = accumulatedMicroseconds?.Microseconds ?? 0;
            var lastTime = accumulatedMicroseconds?.Time ?? 0;
            var lastTicksPerMicrosecond = accumulatedMicroseconds?.TicksPerMicrosecond ?? valuesCache.DefaultTicksPerMicrosecond;

            return(RoundMicroseconds(lastTime + (timeMicroseconds - lastAccumulatedMicroseconds) * lastTicksPerMicrosecond));
        }
Example #4
0
 /// <summary>
 /// Converts the string representation of a metric time span to its <see cref="MetricTimeSpan"/>
 /// equivalent. A return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="input">A string containing a time span to convert.</param>
 /// <param name="timeSpan">When this method returns, contains the <see cref="MetricTimeSpan"/>
 /// equivalent of the time span contained in <paramref name="input"/>, if the conversion succeeded, or
 /// null if the conversion failed. The conversion fails if the <paramref name="input"/> is null or
 /// <see cref="String.Empty"/>, is not of the correct format. This parameter is passed uninitialized;
 /// any value originally supplied in result will be overwritten.</param>
 /// <returns>true if <paramref name="input"/> was converted successfully; otherwise, false.</returns>
 public static bool TryParse(string input, out MetricTimeSpan timeSpan)
 {
     return(MetricTimeSpanParser.TryParse(input, out timeSpan).Status == ParsingStatus.Parsed);
 }