Example #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);
        }
Example #2
0
        /// <summary>
        /// Gets octave of the note presented by an instance of <see cref="NoteOnEvent"/>.
        /// </summary>
        /// <param name="noteOnEvent">Note On event to get note octave of.</param>
        /// <returns>Note octave of the <paramref name="noteOnEvent"/> 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="noteOnEvent"/> is null.</exception>
        public static int GetNoteOctave(this NoteOnEvent noteOnEvent)
        {
            if (noteOnEvent == null)
            {
                throw new ArgumentNullException(nameof(noteOnEvent));
            }

            return(GetNoteOctave(noteOnEvent.NoteNumber));
        }
Example #3
0
        /// <summary>
        /// Sets the note number of the <see cref="NoteOnEvent"/> with the specified note name and octave.
        /// </summary>
        /// <param name="noteOnEvent">Note On 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="noteOnEvent"/> 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 NoteOnEvent noteOnEvent, NoteName noteName, int octave)
        {
            if (noteOnEvent == null)
            {
                throw new ArgumentNullException(nameof(noteOnEvent));
            }

            noteOnEvent.NoteNumber = GetNoteNumber(noteName, octave);
        }
Example #4
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);
                    }
                }
            }
        }
Example #5
0
        private void ProcessEvents(WritingSettings settings, Action <IEventWriter, MidiEvent, bool> eventHandler)
        {
            byte?runningStatus   = null;
            var  writeStatusByte = true;

            var skipSetTempo      = true;
            var skipKeySignature  = true;
            var skipTimeSignature = true;

            foreach (var midiEvent in Events.Concat(new[] { new EndOfTrackEvent() }))
            {
                var eventToWrite = midiEvent;
                if (eventToWrite is UnknownMetaEvent && settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteUnknownMetaEvents))
                {
                    continue;
                }

                //

                if (settings.CompressionPolicy.HasFlag(CompressionPolicy.NoteOffAsSilentNoteOn))
                {
                    var noteOffEvent = eventToWrite as NoteOffEvent;
                    if (noteOffEvent != null)
                    {
                        eventToWrite = new NoteOnEvent
                        {
                            DeltaTime  = noteOffEvent.DeltaTime,
                            Channel    = noteOffEvent.Channel,
                            NoteNumber = noteOffEvent.NoteNumber
                        }
                    }
                    ;
                }

                //

                if (settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultSetTempo) &&
                    TrySkipDefaultSetTempo(eventToWrite, ref skipSetTempo))
                {
                    continue;
                }

                if (settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultKeySignature) &&
                    TrySkipDefaultKeySignature(eventToWrite, ref skipKeySignature))
                {
                    continue;
                }

                if (settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteDefaultTimeSignature) &&
                    TrySkipDefaultTimeSignature(eventToWrite, ref skipTimeSignature))
                {
                    continue;
                }

                //

                IEventWriter eventWriter = EventWriterFactory.GetWriter(eventToWrite);

                if (eventToWrite is ChannelEvent)
                {
                    var statusByte = eventWriter.GetStatusByte(eventToWrite);
                    writeStatusByte = runningStatus != statusByte || !settings.CompressionPolicy.HasFlag(CompressionPolicy.UseRunningStatus);
                    runningStatus   = statusByte;
                }
                else
                {
                    runningStatus   = null;
                    writeStatusByte = true;
                }

                //

                eventHandler(eventWriter, eventToWrite, writeStatusByte);
            }
        }
Example #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);
        }