Beispiel #1
0
        public MidiEvent Read(MidiReader reader, ReadingSettings settings, byte currentStatusByte)
        {
            var statusByte = reader.ReadByte();
            var size       = reader.ReadVlqNumber();

            //

            MetaEvent metaEvent;

            switch (statusByte)
            {
            case EventStatusBytes.Meta.Lyric:
                metaEvent = new LyricEvent();
                break;

            case EventStatusBytes.Meta.SetTempo:
                metaEvent = new SetTempoEvent();
                break;

            case EventStatusBytes.Meta.Text:
                metaEvent = new TextEvent();
                break;

            case EventStatusBytes.Meta.SequenceTrackName:
                metaEvent = new SequenceTrackNameEvent();
                break;

            case EventStatusBytes.Meta.PortPrefix:
                metaEvent = new PortPrefixEvent();
                break;

            case EventStatusBytes.Meta.TimeSignature:
                metaEvent = new TimeSignatureEvent();
                break;

            case EventStatusBytes.Meta.SequencerSpecific:
                metaEvent = new SequencerSpecificEvent();
                break;

            case EventStatusBytes.Meta.KeySignature:
                metaEvent = new KeySignatureEvent();
                break;

            case EventStatusBytes.Meta.Marker:
                metaEvent = new MarkerEvent();
                break;

            case EventStatusBytes.Meta.ChannelPrefix:
                metaEvent = new ChannelPrefixEvent();
                break;

            case EventStatusBytes.Meta.InstrumentName:
                metaEvent = new InstrumentNameEvent();
                break;

            case EventStatusBytes.Meta.CopyrightNotice:
                metaEvent = new CopyrightNoticeEvent();
                break;

            case EventStatusBytes.Meta.SmpteOffset:
                metaEvent = new SmpteOffsetEvent();
                break;

            case EventStatusBytes.Meta.DeviceName:
                metaEvent = new DeviceNameEvent();
                break;

            case EventStatusBytes.Meta.CuePoint:
                metaEvent = new CuePointEvent();
                break;

            case EventStatusBytes.Meta.ProgramName:
                metaEvent = new ProgramNameEvent();
                break;

            case EventStatusBytes.Meta.SequenceNumber:
                metaEvent = new SequenceNumberEvent();
                break;

            case EventStatusBytes.Meta.EndOfTrack:
                metaEvent = new EndOfTrackEvent();
                break;

            default:
            {
                Type eventType = null;
                metaEvent = settings.CustomMetaEventTypes?.TryGetType(statusByte, out eventType) == true && IsMetaEventType(eventType)
                            ? (MetaEvent)Activator.CreateInstance(eventType)
                            : new UnknownMetaEvent(statusByte);
            }
            break;
            }

            //

            var readerPosition = reader.Position;

            metaEvent.Read(reader, settings, size);

            var bytesRead   = reader.Position - readerPosition;
            var bytesUnread = size - bytesRead;

            if (bytesUnread > 0)
            {
                reader.Position += bytesUnread;
            }

            return(metaEvent);
        }
Beispiel #2
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);
        }