Example #1
0
        /// <summary>
        /// Reads an event from the reader's underlying stream.
        /// </summary>
        /// <param name="reader">Reader to read an event.</param>
        /// <param name="settings">Settings according to which an event must be read.</param>
        /// <param name="channelEventStatusByte">Current channel event status byte used as running status.</param>
        /// <returns>Instance of the <see cref="MidiEvent"/> representing a MIDI event.</returns>
        /// <exception cref="ObjectDisposedException">Method was called after the writer's underlying stream
        /// was disposed.</exception>
        /// <exception cref="IOException">An I/O error occurred on the writer's underlying stream.</exception>
        /// <exception cref="UnexpectedRunningStatusException">Unexpected running status is encountered.</exception>
        /// <exception cref="UnknownChannelEventException">Reader has encountered an unknown channel event.</exception>
        /// <exception cref="NotEnoughBytesException">Not enough bytes to read an event.</exception>
        /// <exception cref="InvalidChannelEventParameterValueException">Value of a channel event's parameter just
        /// read is invalid.</exception>
        private MidiEvent ReadEvent(MidiReader reader, ReadingSettings settings, ref byte?channelEventStatusByte)
        {
            var deltaTime = reader.ReadVlqLongNumber();

            if (deltaTime < 0)
            {
                deltaTime = 0;
            }

            //

            var statusByte = reader.ReadByte();

            if (statusByte <= SevenBitNumber.MaxValue)
            {
                if (channelEventStatusByte == null)
                {
                    throw new UnexpectedRunningStatusException();
                }

                statusByte = channelEventStatusByte.Value;
                reader.Position--;
            }

            //

            var eventReader = EventReaderFactory.GetReader(statusByte);
            var midiEvent   = eventReader.Read(reader, settings, statusByte);

            //

            if (settings.SilentNoteOnPolicy == SilentNoteOnPolicy.NoteOff)
            {
                var noteOnEvent = midiEvent as NoteOnEvent;
                if (noteOnEvent?.Velocity == 0)
                {
                    midiEvent = new NoteOffEvent
                    {
                        DeltaTime  = noteOnEvent.DeltaTime,
                        Channel    = noteOnEvent.Channel,
                        NoteNumber = noteOnEvent.NoteNumber
                    };
                }
            }

            //

            if (midiEvent is ChannelEvent)
            {
                channelEventStatusByte = statusByte;
            }

            //

            midiEvent.DeltaTime = deltaTime;
            return(midiEvent);
        }
Example #2
0
        // Token: 0x06003233 RID: 12851 RVA: 0x00146EB4 File Offset: 0x001450B4
        private MidiEvent ReadEvent(MidiReader reader, ReadingSettings settings, ref byte?channelEventStatusByte)
        {
            long num = reader.ReadVlqLongNumber();

            if (num < 0L)
            {
                num = 0L;
            }
            this.absTime += num;
            byte b = reader.ReadByte();

            if (b <= SevenBitNumber.MaxValue)
            {
                if (channelEventStatusByte == null)
                {
                    throw new UnexpectedRunningStatusException();
                }
                b = channelEventStatusByte.Value;
                long position = reader.Position;
                reader.Position = position - 1L;
            }
            MidiEvent midiEvent = EventReaderFactory.GetReader(b).Read(reader, settings, b);

            if (settings.SilentNoteOnPolicy == SilentNoteOnPolicy.NoteOff)
            {
                NoteOnEvent    noteOnEvent    = midiEvent as NoteOnEvent;
                SevenBitNumber?sevenBitNumber = (noteOnEvent != null) ? new SevenBitNumber?(noteOnEvent.Velocity) : null;
                int?           num2           = (sevenBitNumber != null) ? new int?((int)sevenBitNumber.GetValueOrDefault()) : null;
                if (num2.GetValueOrDefault() == 0 & num2 != null)
                {
                    midiEvent = new NoteOffEvent
                    {
                        DeltaTime  = noteOnEvent.DeltaTime,
                        Channel    = noteOnEvent.Channel,
                        NoteNumber = noteOnEvent.NoteNumber
                    };
                }
            }
            if (midiEvent is ChannelEvent)
            {
                channelEventStatusByte = new byte?(b);
            }
            midiEvent.DeltaTime    = num;
            midiEvent.AbsoluteTime = this.absTime;
            if (midiEvent is NoteOnEvent)
            {
                this.onEvents.Add(this.Events.Count);
            }
            else if (midiEvent is NoteOffEvent)
            {
                for (int i = 0; i < this.onEvents.Count; i++)
                {
                    NoteOnEvent noteOnEvent2 = this.Events[this.onEvents[i]] as NoteOnEvent;
                    if (noteOnEvent2.Channel == ((NoteOffEvent)midiEvent).Channel && noteOnEvent2.NoteNumber == ((NoteOffEvent)midiEvent).NoteNumber)
                    {
                        ((NoteOnEvent)this.Events[this.onEvents[i]]).OffEvent = (NoteOffEvent)midiEvent;
                        this.onEvents.RemoveAt(i);
                        return(null);
                    }
                }
            }
            return(midiEvent);
        }