Exemple #1
0
        public MidiEvent Read(MidiReader reader, ReadingSettings settings, byte currentStatusByte)
        {
            var statusByte = currentStatusByte.GetHead();
            var channel    = currentStatusByte.GetTail();

            ChannelEvent channelEvent = null;

            switch (statusByte)
            {
            case EventStatusBytes.Channel.NoteOff:
                channelEvent = new NoteOffEvent();
                break;

            case EventStatusBytes.Channel.NoteOn:
                channelEvent = new NoteOnEvent();
                break;

            case EventStatusBytes.Channel.ControlChange:
                channelEvent = new ControlChangeEvent();
                break;

            case EventStatusBytes.Channel.PitchBend:
                channelEvent = new PitchBendEvent();
                break;

            case EventStatusBytes.Channel.ChannelAftertouch:
                channelEvent = new ChannelAftertouchEvent();
                break;

            case EventStatusBytes.Channel.ProgramChange:
                channelEvent = new ProgramChangeEvent();
                break;

            case EventStatusBytes.Channel.NoteAftertouch:
                channelEvent = new NoteAftertouchEvent();
                break;

            default:
                ReactOnUnknownChannelEvent(statusByte, channel, reader, settings);
                return(null);
            }

            channelEvent.Read(reader, settings, MidiEvent.UnknownContentSize);
            channelEvent.Channel = channel;

            var noteOnEvent = channelEvent as NoteOnEvent;

            if (noteOnEvent != null && settings.SilentNoteOnPolicy == SilentNoteOnPolicy.NoteOff && noteOnEvent.Velocity == 0)
            {
                channelEvent = new NoteOffEvent
                {
                    DeltaTime  = noteOnEvent.DeltaTime,
                    Channel    = noteOnEvent.Channel,
                    NoteNumber = noteOnEvent.NoteNumber
                }
            }
            ;

            return(channelEvent);
        }
        /// <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);
        }
Exemple #3
0
        private void ProcessEvents(WritingSettings settings, Action <IEventWriter, MidiEvent, bool> eventHandler)
        {
            byte?runningStatus = null;

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

            foreach (var midiEvent in Events)
            {
                var eventToWrite = midiEvent;
                if (eventToWrite is SystemCommonEvent || eventToWrite is SystemRealTimeEvent)
                {
                    continue;
                }

                if (eventToWrite.EventType == MidiEventType.UnknownMeta && settings.DeleteUnknownMetaEvents)
                {
                    continue;
                }

                //

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

                //

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

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

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

                //

                IEventWriter eventWriter = EventWriterFactory.GetWriter(eventToWrite);

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

                //

                eventHandler(eventWriter, eventToWrite, writeStatusByte);
            }

            var endOfTrackEvent       = new EndOfTrackEvent();
            var endOfTrackEventWriter = EventWriterFactory.GetWriter(endOfTrackEvent);

            eventHandler(endOfTrackEventWriter, endOfTrackEvent, true);
        }
        private void ProcessEvents(WritingSettings settings, Action <IEventWriter, MidiEvent, bool> eventHandler)
        {
            byte?runningStatus = null;

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

            foreach (var midiEvent in GetEventsToWrite())
            {
                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);

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

                //

                eventHandler(eventWriter, eventToWrite, writeStatusByte);
            }
        }