Ejemplo n.º 1
0
        /// <summary>
        /// Checks if the specified <see cref="NoteOnEvent"/> corresponds to the specified
        /// <see cref="NoteOffEvent"/>.
        /// </summary>
        /// <remarks>
        /// Note On event corresponds to Note Off one if it has the same note's number and channel,
        /// i.e. those events make up a note.
        /// </remarks>
        /// <param name="noteOnEvent"><see cref="NoteOnEvent"/> to check <see cref="NoteOffEvent"/> for.</param>
        /// <param name="noteOffEvent"><see cref="NoteOffEvent"/> to check <see cref="NoteOnEvent"/> for.</param>
        /// <returns>true if <paramref name="noteOnEvent"/> corresponds to <paramref name="noteOffEvent"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="noteOnEvent"/> is null. -or-
        /// <paramref name="noteOffEvent"/> is null.</exception>
        public static bool IsNoteOnCorrespondToNoteOff(NoteOnEvent noteOnEvent, NoteOffEvent noteOffEvent)
        {
            ThrowIfArgument.IsNull(nameof(noteOnEvent), noteOnEvent);
            ThrowIfArgument.IsNull(nameof(noteOffEvent), noteOffEvent);

            return(noteOnEvent.Channel == noteOffEvent.Channel &&
                   noteOnEvent.NoteNumber == noteOffEvent.NoteNumber);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets octave of the note presented by an instance of <see cref="NoteOffEvent"/>.
        /// </summary>
        /// <param name="noteOffEvent">Note Off event to get note octave of.</param>
        /// <returns>Note octave of the <paramref name="noteOffEvent"/> event.</returns>
        /// <remarks>
        /// Octave number will be returned in scientific pitch notation which means
        /// that 4 will be returned for 60 note number.
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="noteOffEvent"/> is null.</exception>
        public static int GetNoteOctave(this NoteOffEvent noteOffEvent)
        {
            if (noteOffEvent == null)
            {
                throw new ArgumentNullException(nameof(noteOffEvent));
            }

            return(GetNoteOctave(noteOffEvent.NoteNumber));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets the note number of the <see cref="NoteOffEvent"/> with the specified note name and octave.
        /// </summary>
        /// <param name="noteOffEvent">Note Off event to set the note number of.</param>
        /// <param name="noteName">Name of the note.</param>
        /// <param name="octave">Number of the octave.</param>
        /// <remarks>
        /// Octave number is specified in scientific pitch notation which means that 4 must be
        /// passed to get the number of the middle C.
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="noteOffEvent"/> is null.</exception>
        /// <exception cref="InvalidEnumArgumentException"><paramref name="noteName"/> specified an
        /// invalid value.</exception>
        /// <exception cref="ArgumentException">Note number is out of range for the specified note
        /// name and octave.</exception>
        public static void SetNoteNumber(this NoteOffEvent noteOffEvent, NoteName noteName, int octave)
        {
            if (noteOffEvent == null)
            {
                throw new ArgumentNullException(nameof(noteOffEvent));
            }

            noteOffEvent.NoteNumber = GetNoteNumber(noteName, octave);
        }
Ejemplo n.º 5
0
        // Token: 0x06003234 RID: 12852 RVA: 0x001470DC File Offset: 0x001452DC
        private void ProcessEvents(WritingSettings settings, Action <IEventWriter, MidiEvent, bool> eventHandler)
        {
            byte?b     = null;
            bool flag  = true;
            bool flag2 = true;
            bool flag3 = true;

            foreach (MidiEvent midiEvent in this.Events.Concat(new EndOfTrackEvent[]
            {
                new EndOfTrackEvent()
            }))
            {
                if (!(midiEvent is UnknownMetaEvent) || !settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteUnknownMetaEvents))
                {
                    if (settings.CompressionPolicy.HasFlag(CompressionPolicy.NoteOffAsSilentNoteOn))
                    {
                        NoteOffEvent noteOffEvent = midiEvent as NoteOffEvent;
                        if (noteOffEvent != null)
                        {
                            midiEvent = new NoteOnEvent
                            {
                                DeltaTime  = noteOffEvent.DeltaTime,
                                Channel    = noteOffEvent.Channel,
                                NoteNumber = noteOffEvent.NoteNumber
                            };
                        }
                    }
                    if ((!settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultSetTempo) || !TrackChunk.TrySkipDefaultSetTempo(midiEvent, ref flag)) && (!settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultKeySignature) || !TrackChunk.TrySkipDefaultKeySignature(midiEvent, ref flag2)) && (!settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultTimeSignature) || !TrackChunk.TrySkipDefaultTimeSignature(midiEvent, ref flag3)))
                    {
                        IEventWriter writer = EventWriterFactory.GetWriter(midiEvent);
                        bool         arg;
                        if (midiEvent is ChannelEvent)
                        {
                            byte statusByte = writer.GetStatusByte(midiEvent);
                            byte?b2         = b;
                            int? num        = (b2 != null) ? new int?((int)b2.GetValueOrDefault()) : null;
                            int  num2       = (int)statusByte;
                            arg = (!(num.GetValueOrDefault() == num2 & num != null) || !settings.CompressionPolicy.HasFlag(CompressionPolicy.UseRunningStatus));
                            b   = new byte?(statusByte);
                        }
                        else
                        {
                            b   = null;
                            arg = true;
                        }
                        eventHandler(writer, midiEvent, arg);
                    }
                }
            }
        }
Ejemplo n.º 6
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);
        }