Пример #1
0
        /// <summary>
        /// Finds the currently played beat given a list of tracks and the current time.
        /// </summary>
        /// <param name="tracks">The tracks in which to search the played beat for.</param>
        /// <param name="tick">The current time in midi ticks.</param>
        /// <returns>The information about the current beat or null if no beat could be found.</returns>
        public MidiTickLookupFindBeatResult FindBeat(Track[] tracks, int tick)
        {
            // get all beats within the masterbar
            var masterBar = FindMasterBar(tick);

            if (masterBar == null)
            {
                return(null);
            }

            var trackLookup = new FastDictionary <int, bool>();

            foreach (var track in tracks)
            {
                trackLookup[track.Index] = true;
            }

            BeatTickLookup beat  = null;
            int            index = 0;
            var            beats = masterBar.Beats;

            for (int b = 0; b < beats.Count; b++)
            {
                // is the current beat played on the given tick?
                var currentBeat = beats[b];
                // skip non relevant beats
                if (!trackLookup.ContainsKey(currentBeat.Beat.Voice.Bar.Staff.Track.Index))
                {
                    continue;
                }
                if (currentBeat.Start <= tick && tick < currentBeat.End)
                {
                    // take the latest played beat we can find. (most right)
                    if (beat == null || (beat.Start < currentBeat.Start))
                    {
                        beat  = beats[b];
                        index = b;
                    }
                }
                // if we are already past the tick, we can stop searching
                else if (currentBeat.End > tick)
                {
                    break;
                }
            }

            if (beat == null)
            {
                return(null);
            }

            // search for next relevant beat in masterbar
            BeatTickLookup nextBeat = null;

            for (int b = index + 1; b < beats.Count; b++)
            {
                var currentBeat = beats[b];
                if (currentBeat.Start > beat.Start && trackLookup.ContainsKey(currentBeat.Beat.Voice.Bar.Staff.Track.Index))
                {
                    nextBeat = currentBeat;
                    break;
                }
            }

            // first relevant beat in next bar
            if (nextBeat == null && masterBar.NextMasterBar != null)
            {
                var nextBar = masterBar.NextMasterBar;
                beats = nextBar.Beats;
                for (int b = 0; b < beats.Count; b++)
                {
                    var currentBeat = beats[b];
                    if (trackLookup.ContainsKey(currentBeat.Beat.Voice.Bar.Staff.Track.Index))
                    {
                        nextBeat = currentBeat;
                        break;
                    }
                }
            }

            var result = new MidiTickLookupFindBeatResult();

            result.CurrentBeat = beat.Beat;
            result.NextBeat    = nextBeat == null ? null : nextBeat.Beat;
            result.Duration    = MidiUtils.TicksToMillis(beat.End - beat.Start, masterBar.Tempo);
            return(result);
        }
Пример #2
0
 /// <summary>
 /// Adds the given <see cref="BeatTickLookup"/> to the current <see cref="MasterBarTickLookup"/>.
 /// </summary>
 /// <param name="beat">The lookup to add.</param>
 public void AddBeat(BeatTickLookup beat)
 {
     _currentMasterBar.AddBeat(beat);
 }
Пример #3
0
 /// <summary>
 /// Adds a new <see cref="BeatTickLookup"/> to the list of played beats during this MasterBar period.
 /// </summary>
 /// <param name="beat"></param>
 public void AddBeat(BeatTickLookup beat)
 {
     Beats.Add(beat);
 }