コード例 #1
0
        // TODO: improve performance
        /// <summary>
        /// Converts an instance of the <see cref="MidiEvent"/> to bytes array using the specified
        /// minimum size of resulting array.
        /// </summary>
        /// <param name="midiEvent">MIDI event to convert.</param>
        /// <param name="minSize">Minimum size of bytes array representing <paramref name="midiEvent"/>.</param>
        /// <returns>Array of bytes representing <paramref name="midiEvent"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="midiEvent"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="minSize"/> is negative.</exception>
        public byte[] Convert(MidiEvent midiEvent, int minSize)
        {
            ThrowIfArgument.IsNull(nameof(midiEvent), midiEvent);
            ThrowIfArgument.IsNegative(nameof(minSize), minSize, "Min size is negative.");

            _dataBytesStream.Seek(0, SeekOrigin.Begin);

            var eventWriter = EventWriterFactory.GetWriter(midiEvent);

            eventWriter.Write(midiEvent, _midiWriter, WritingSettings, true);

            var buffer   = _dataBytesStream.GetBuffer();
            var dataSize = _dataBytesStream.Position;
            var result   = new byte[Math.Max(dataSize, minSize)];

            Array.Copy(buffer, 0, result, 0, dataSize);

            return(result);
        }
コード例 #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);
        }
コード例 #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 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);
            }
        }