Example #1
0
        /// <summary>
        /// Seeks the specified position in the track.
        /// </summary>
        /// <param name="position">
        /// The position in ticks to seek.
        /// </param>
        public void Seek(int position)
        {
            // Enforce preconditions.
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException("position", position,
                                                      "Position out of range.");
            }

            enumerator = trk.GetEnumerator();
            enumerator.MoveNext();
            currentEvent = (MidiEvent)enumerator.Current;

            endReached = false;

            // Tick accumulator.
            int ticks = currentEvent.Ticks;

            // Create MIDI chaser to chase MIDI messages so that the sequence
            // is updated correctly at the specified position.
            MidiChaser chaser = new MidiChaser(this._midiSender, tickGen);

            if (ticks <= position)
            {
                // Add first message.
                chaser.Add(currentEvent.Message);
            }

            // While the position being sought has not been reached.
            while (ticks < position)
            {
                // Move to the next event in the track.
                endReached = !enumerator.MoveNext();

                // If the position being sought lies beyond the end of the
                // track, trigger event to notify listeners and return.
                if (endReached)
                {
                    if (EndOfTrackReached != null)
                    {
                        EndOfTrackReached(this, EventArgs.Empty);
                    }

                    // Chase MIDI messages so that the sequence sounds correctly from
                    // the specified position.
                    chaser.Chase();

                    return;
                }
                // Else the position has not yet been reached.
                else
                {
                    // Get the current MIDI event.
                    currentEvent = (MidiEvent)enumerator.Current;

                    // Accumulate ticks.
                    ticks += currentEvent.Ticks;

                    // If we haven't gone beyond the specified position.
                    if (ticks <= position)
                    {
                        // Add message to chaser.
                        chaser.Add(currentEvent.Message);
                    }
                }
            }

            // Initialize the current MIDI event ticks to the number of ticks
            // remaining until the specified position is reached.
            currentEvent.Ticks = ticks - position;

            // Chase MIDI messages so that the sequence sounds correctly from
            // the specified position.
            chaser.Chase();
        }
Example #2
0
        /// <summary>
        /// Seeks the specified position in the track.
        /// </summary>
        /// <param name="position">
        /// The position in ticks to seek.
        /// </param>
        public void Seek(int position)
        {
            // Enforce preconditions.
            if(position < 0)
                throw new ArgumentOutOfRangeException("position", position,
                    "Position out of range.");

            enumerator = trk.GetEnumerator();
            enumerator.MoveNext();
            currentEvent = (MidiEvent)enumerator.Current;

            endReached = false;

            // Tick accumulator.
            int ticks = currentEvent.Ticks;

            // Create MIDI chaser to chase MIDI messages so that the sequence
            // is updated correctly at the specified position.
            MidiChaser chaser = new MidiChaser(this._midiSender, tickGen);

            if(ticks <= position)
            {
                // Add first message.
                chaser.Add(currentEvent.Message);
            }

            // While the position being sought has not been reached.
            while(ticks < position)
            {
                // Move to the next event in the track.
                endReached = !enumerator.MoveNext();

                // If the position being sought lies beyond the end of the
                // track, trigger event to notify listeners and return.
                if(endReached)
                {
                    if(EndOfTrackReached != null)
                        EndOfTrackReached(this, EventArgs.Empty);

                    // Chase MIDI messages so that the sequence sounds correctly from
                    // the specified position.
                    chaser.Chase();

                    return;
                }
                    // Else the position has not yet been reached.
                else
                {
                    // Get the current MIDI event.
                    currentEvent = (MidiEvent)enumerator.Current;

                    // Accumulate ticks.
                    ticks += currentEvent.Ticks;

                    // If we haven't gone beyond the specified position.
                    if(ticks <= position)
                    {
                        // Add message to chaser.
                        chaser.Add(currentEvent.Message);
                    }
                }
            }

            // Initialize the current MIDI event ticks to the number of ticks
            // remaining until the specified position is reached.
            currentEvent.Ticks = ticks - position;

            // Chase MIDI messages so that the sequence sounds correctly from
            // the specified position.
            chaser.Chase();
        }