Example #1
0
        /// <summary>
        /// Writes the chunk object <paramref name="instance"/> to the stream.
        /// </summary>
        /// <param name="context">Must not be null.</param>
        /// <param name="instance">Must not be null.</param>
        public override void Write(ChunkFileContext context, object instance)
        {
            Check.IfArgumentNull(context, nameof(context));
            Check.IfArgumentNull(instance, nameof(instance));
            Check.IfArgumentNotOfType <MTrkChunk>(instance, nameof(instance));

            var trackChunk = (MTrkChunk)instance;
            var writer     = context.Services.GetService <FileChunkWriter>();
            var stream     = writer.CurrentStream;
            var midiWriter = new MidiFileStreamWriter(stream);

            foreach (var message in trackChunk.Events)
            {
                if (message.Message is MidiShortMessage shortMessage)
                {
                    midiWriter.WriteMidiEvent(message.DeltaTime, shortMessage.Data);
                }
                else
                {
                    if (message.Message is MidiMetaMessage metaMsg)
                    {
                        midiWriter.WriteMetaEvent(message.DeltaTime, metaMsg.MetaType, metaMsg.GetData());
                    }
                    if (message.Message is MidiSysExMessage sysexMsg)
                    {
                        midiWriter.WriteSysExEvent(message.DeltaTime, sysexMsg.GetData(), sysexMsg.IsContinuation);
                    }
                }
            }
        }
Example #2
0
        public override void Write(ChunkFileContext context, object instance)
        {
            Check.IfArgumentNull(context, "context");
            Check.IfArgumentNull(instance, "instance");
            Check.IfArgumentNotOfType <MTrkChunk>(instance, "instance");

            var trackChunk = (MTrkChunk)instance;
            var writer     = context.CompositionContainer.GetService <FileChunkWriter>();
            var stream     = writer.CurrentStream;
            var midiWriter = new MidiFileStreamWriter(stream);

            foreach (var message in trackChunk.Events)
            {
                if (message.Message is MidiShortMessage)
                {
                    midiWriter.WriteMidiEvent(message.DeltaTime, ((MidiShortMessage)message.Message).Data);
                }
                else
                {
                    var metaMsg  = message.Message as MidiMetaMessage;
                    var sysexMsg = message.Message as MidiSysExMessage;

                    if (metaMsg != null)
                    {
                        midiWriter.WriteMetaEvent(message.DeltaTime, metaMsg.MetaType, metaMsg.GetData());
                    }
                    if (sysexMsg != null)
                    {
                        midiWriter.WriteSysExEvent(message.DeltaTime, sysexMsg.GetData(), sysexMsg.IsContinuation);
                    }
                }
            }
        }