Exemple #1
0
        // Token: 0x060033BA RID: 13242 RVA: 0x001486D0 File Offset: 0x001468D0
        public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
        {
            if (writeStatusByte)
            {
                writer.WriteByte(byte.MaxValue);
            }
            UnknownMetaEvent unknownMetaEvent = midiEvent as UnknownMetaEvent;
            byte             statusByte;

            if (unknownMetaEvent != null)
            {
                statusByte = unknownMetaEvent.StatusByte;
            }
            else
            {
                Type type = midiEvent.GetType();
                if (!StandardEventTypes.Meta.TryGetStatusByte(type, out statusByte))
                {
                    EventTypesCollection customMetaEventTypes = settings.CustomMetaEventTypes;
                    if (customMetaEventTypes != null)
                    {
                        bool flag = !customMetaEventTypes.TryGetStatusByte(type, out statusByte);
                    }
                }
            }
            writer.WriteByte(statusByte);
            int size = midiEvent.GetSize(settings);

            writer.WriteVlqNumber(size);
            midiEvent.Write(writer, settings);
        }
 /// <summary>
 /// Writes content of a MIDI event.
 /// </summary>
 /// <param name="writer">Writer to write the content with.</param>
 /// <param name="settings">Settings according to which the event's content must be written.</param>
 internal sealed override void Write(MidiWriter writer, WritingSettings settings)
 {
     foreach (var parameter in _parameters)
     {
         writer.WriteByte(parameter);
     }
 }
Exemple #3
0
        public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
        {
            VerifyEvent(midiEvent);

            //

            if (writeStatusByte)
            {
                var eventType = midiEvent.GetType();

                byte statusByte;
                if (!StandardEventTypes.SysEx.TryGetStatusByte(eventType, out statusByte))
                {
                    Debug.Fail($"Unable to write the {eventType} event.");
                }

                writer.WriteByte(statusByte);
            }

            //

            var contentSize = midiEvent.GetSize(settings);

            writer.WriteVlqNumber(contentSize);
            midiEvent.Write(writer, settings);
        }
Exemple #4
0
        public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
        {
            if (writeStatusByte)
            {
                writer.WriteByte(EventStatusBytes.Global.Meta);
            }

            //

            byte statusByte;

            var unknownMetaEvent = midiEvent as UnknownMetaEvent;

            if (unknownMetaEvent != null)
            {
                statusByte = unknownMetaEvent.StatusByte;
            }
            else
            {
                var eventType = midiEvent.GetType();
                if (!StandardEventTypes.Meta.TryGetStatusByte(eventType, out statusByte) && settings.CustomMetaEventTypes?.TryGetStatusByte(eventType, out statusByte) != true)
                {
                    Debug.Fail($"Unable to write the {eventType} event.");
                }
            }

            writer.WriteByte(statusByte);

            //

            var contentSize = midiEvent.GetSize(settings);

            writer.WriteVlqNumber(contentSize);
            midiEvent.Write(writer, settings);
        }
Exemple #5
0
        public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
        {
            VerifyEvent(midiEvent);

            //

            if (writeStatusByte)
            {
                var eventType = midiEvent.GetType();

                byte statusByte;
                if (!StandardEventTypes.Channel.TryGetStatusByte(eventType, out statusByte))
                {
                    Debug.Fail($"Unable to write the {eventType} event.");
                }

                var channel = ((ChannelEvent)midiEvent).Channel;

                var totalStatusByte = DataTypesUtilities.Combine((FourBitNumber)statusByte, channel);
                writer.WriteByte(totalStatusByte);
            }

            //

            midiEvent.Write(writer, settings);
        }
 // Token: 0x060032AC RID: 12972 RVA: 0x00147ACC File Offset: 0x00145CCC
 internal sealed override void Write(MidiWriter writer, WritingSettings settings)
 {
     foreach (SevenBitNumber number in this._parameters)
     {
         writer.WriteByte(number);
     }
 }
 /// <summary>
 /// Writes content of a MIDI meta event.
 /// </summary>
 /// <param name="writer">Writer to write the content with.</param>
 /// <param name="settings">Settings according to which the event's content must be written.</param>
 protected override void WriteContent(MidiWriter writer, WritingSettings settings)
 {
     writer.WriteByte(Numerator);
     writer.WriteByte((byte)Math.Log(Denominator, 2));
     writer.WriteByte(ClocksPerClick);
     writer.WriteByte(NumberOf32ndNotesPerBeat);
 }
Exemple #8
0
 // Token: 0x06003231 RID: 12849 RVA: 0x00146E38 File Offset: 0x00145038
 protected override void WriteContent(MidiWriter writer, WritingSettings settings)
 {
     this.ProcessEvents(settings, delegate(IEventWriter eventWriter, MidiEvent midiEvent, bool writeStatusByte)
     {
         writer.WriteVlqNumber(midiEvent.DeltaTime);
         eventWriter.Write(midiEvent, writer, settings, writeStatusByte);
     });
 }
Exemple #9
0
 /// <summary>
 /// Writes content of a <see cref="TrackChunk"/>.
 /// </summary>
 /// <remarks>
 /// Content of a <see cref="TrackChunk"/> is collection of MIDI events.
 /// </remarks>
 /// <param name="writer">Writer to write the chunk's content with.</param>
 /// <param name="settings">Settings according to which the chunk's content must be written.</param>
 /// <exception cref="ObjectDisposedException">Method was called after the writer's underlying stream was disposed.</exception>
 /// <exception cref="IOException">An I/O error occurred on the writer's underlying stream.</exception>
 protected override void WriteContent(MidiWriter writer, WritingSettings settings)
 {
     ProcessEvents(settings, (eventWriter, midiEvent, writeStatusByte) =>
     {
         writer.WriteVlqNumber(midiEvent.DeltaTime);
         eventWriter.Write(midiEvent, writer, settings, writeStatusByte);
     });
 }
Exemple #10
0
 // Token: 0x060032C4 RID: 12996 RVA: 0x00147BB0 File Offset: 0x00145DB0
 internal sealed override void Write(MidiWriter writer, WritingSettings settings)
 {
     byte[] data = this.Data;
     if (data != null)
     {
         writer.WriteBytes(data);
     }
 }
Exemple #11
0
 // Token: 0x06003379 RID: 13177 RVA: 0x00017522 File Offset: 0x00015722
 protected override void WriteContent(MidiWriter writer, WritingSettings settings)
 {
     writer.WriteByte(this.GetFormatAndHours());
     writer.WriteByte(this.Minutes);
     writer.WriteByte(this.Seconds);
     writer.WriteByte(this.Frames);
     writer.WriteByte(this.SubFrames);
 }
Exemple #12
0
 // Token: 0x06003355 RID: 13141 RVA: 0x00147EB0 File Offset: 0x001460B0
 protected override void WriteContent(MidiWriter writer, WritingSettings settings)
 {
     byte[] data = this.Data;
     if (data != null)
     {
         writer.WriteBytes(data);
     }
 }
Exemple #13
0
        public int CalculateSize(MidiEvent midiEvent, WritingSettings settings, bool writeStatusByte)
        {
            VerifyEvent(midiEvent);

            //

            return((writeStatusByte ? 1 : 0) + midiEvent.GetSize(settings));
        }
 // Token: 0x06003306 RID: 13062 RVA: 0x00016FF4 File Offset: 0x000151F4
 protected sealed override int GetContentSize(WritingSettings settings)
 {
     if (string.IsNullOrEmpty(this.Text))
     {
         return(0);
     }
     return((settings.TextEncoding ?? SmfUtilities.DefaultEncoding).GetByteCount(this.Text));
 }
Exemple #15
0
        // Token: 0x06003228 RID: 12840 RVA: 0x00146D90 File Offset: 0x00144F90
        internal void Write(MidiWriter writer, WritingSettings settings)
        {
            writer.WriteString(this.ChunkId);
            uint contentSize = this.GetContentSize(settings);

            writer.WriteDword(contentSize);
            this.WriteContent(writer, settings);
        }
Exemple #16
0
 // Token: 0x06003356 RID: 13142 RVA: 0x00017358 File Offset: 0x00015558
 protected override int GetContentSize(WritingSettings settings)
 {
     byte[] data = this.Data;
     if (data == null)
     {
         return(0);
     }
     return(data.Length);
 }
Exemple #17
0
 // Token: 0x060032C5 RID: 12997 RVA: 0x00016C9B File Offset: 0x00014E9B
 internal sealed override int GetSize(WritingSettings settings)
 {
     byte[] data = this.Data;
     if (data == null)
     {
         return(0);
     }
     return(data.Length);
 }
Exemple #18
0
        /// <summary>
        /// Writes current <see cref="MidiFile"/> to the stream.
        /// </summary>
        /// <param name="stream">Stream to write file's data to.</param>
        /// <param name="format">Format of the file to be written.</param>
        /// <param name="settings">Settings according to which the file must be written.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="stream"/> does not support writing,
        /// or is already closed.</exception>
        /// <exception cref="InvalidEnumArgumentException"><paramref name="format"/> specified an invalid value.</exception>
        /// <exception cref="InvalidOperationException">Time division is null.</exception>
        /// <exception cref="TooManyTrackChunksException">Count of track chunks presented in the file
        /// exceeds maximum value allowed for MIDI file.</exception>
        private void Write(Stream stream, MidiFileFormat format = MidiFileFormat.MultiTrack, WritingSettings settings = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!Enum.IsDefined(typeof(MidiFileFormat), format))
            {
                throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(MidiFileFormat));
            }

            if (TimeDivision == null)
            {
                throw new InvalidOperationException("Time division is null.");
            }

            //

            if (settings == null)
            {
                settings = new WritingSettings();
            }

            using (var writer = new MidiWriter(stream))
            {
                var chunksConverter = ChunksConverterFactory.GetConverter(format);
                var chunks          = chunksConverter.Convert(Chunks);

                var trackChunksCount = chunks.Count(c => c is TrackChunk);
                if (trackChunksCount > ushort.MaxValue)
                {
                    throw new TooManyTrackChunksException(
                              $"Count of track chunks to be written ({trackChunksCount}) is greater than the valid maximum ({ushort.MaxValue}).",
                              trackChunksCount);
                }

                var headerChunk = new HeaderChunk
                {
                    FileFormat   = (ushort)format,
                    TimeDivision = TimeDivision,
                    TracksNumber = (ushort)trackChunksCount
                };
                headerChunk.Write(writer, settings);

                foreach (var chunk in chunks)
                {
                    if (settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteUnknownChunks) && chunk is UnknownChunk)
                    {
                        continue;
                    }

                    chunk.Write(writer, settings);
                }
            }
        }
Exemple #19
0
        /// <summary>
        /// Writes chunk to the <see cref="MidiWriter"/>'s underlying stream according to
        /// specified <see cref="WritingSettings"/>.
        /// </summary>
        /// <param name="writer">Writer to write the chunk's data with.</param>
        /// <param name="settings">Settings according to which the chunk's data must be written.</param>
        /// <exception cref="ObjectDisposedException">
        /// Method was called after <paramref name="writer"/> was disposed.
        /// </exception>
        /// <exception cref="IOException">
        /// An I/O error occurred on the <paramref name="writer"/>'s underlying stream.
        /// </exception>
        internal void Write(MidiWriter writer, WritingSettings settings)
        {
            writer.WriteString(ChunkId);

            var size = GetContentSize(settings);

            writer.WriteDword(size);

            WriteContent(writer, settings);
        }
Exemple #20
0
        // Token: 0x06003232 RID: 12850 RVA: 0x00146E74 File Offset: 0x00145074
        protected override uint GetContentSize(WritingSettings settings)
        {
            uint result = 0u;

            this.ProcessEvents(settings, delegate(IEventWriter eventWriter, MidiEvent midiEvent, bool writeStatusByte)
            {
                result += (uint)(midiEvent.DeltaTime.GetVlqLength() + eventWriter.CalculateSize(midiEvent, settings, writeStatusByte));
            });
            return(result);
        }
Exemple #21
0
        public int CalculateSize(MidiEvent midiEvent, WritingSettings settings, bool writeStatusByte)
        {
            VerifyEvent(midiEvent);

            //

            var contentSize = midiEvent.GetSize(settings);

            return((writeStatusByte ? 1 : 0) + 1 + contentSize.GetVlqLength() + contentSize);
        }
        // Token: 0x06003305 RID: 13061 RVA: 0x00147E28 File Offset: 0x00146028
        protected sealed override void WriteContent(MidiWriter writer, WritingSettings settings)
        {
            string text = this.Text;

            if (string.IsNullOrEmpty(text))
            {
                return;
            }
            byte[] bytes = (settings.TextEncoding ?? SmfUtilities.DefaultEncoding).GetBytes(text);
            writer.WriteBytes(bytes);
        }
Exemple #23
0
        /// <summary>
        /// Gets size of <see cref="TrackChunk"/>'s content as number of bytes required to write it according
        /// to specified <see cref="WritingSettings"/>.
        /// </summary>
        /// <param name="settings">Settings according to which the chunk's content will be written.</param>
        /// <returns>Number of bytes required to write <see cref="TrackChunk"/>'s content.</returns>
        protected override uint GetContentSize(WritingSettings settings)
        {
            uint result = 0;

            ProcessEvents(settings, (eventWriter, midiEvent, writeStatusByte) =>
            {
                result += (uint)(midiEvent.DeltaTime.GetVlqLength() +
                                 eventWriter.CalculateSize(midiEvent, settings, writeStatusByte));
            });

            return(result);
        }
        internal override void Write(MidiWriter writer, WritingSettings settings)
        {
            var component = Component;

            var componentValueMask = ComponentValueMasks[component];
            var componentValue     = ComponentValue & componentValueMask;

            var data = DataTypesUtilities.Combine((FourBitNumber)(byte)component,
                                                  (FourBitNumber)(byte)componentValue);

            writer.WriteByte(data);
        }
        /// <summary>
        /// Gets the size of the content of a MIDI meta event.
        /// </summary>
        /// <param name="settings">Settings according to which the event's content must be written.</param>
        /// <returns>Size of the event's content.</returns>
        protected sealed override int GetContentSize(WritingSettings settings)
        {
            var text = Text;

            if (string.IsNullOrEmpty(text))
            {
                return(0);
            }

            var encoding = settings.TextEncoding ?? SmfConstants.DefaultTextEncoding;

            return(encoding.GetByteCount(Text));
        }
 // Token: 0x060033B0 RID: 13232 RVA: 0x00148648 File Offset: 0x00146848
 public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
 {
     if (writeStatusByte)
     {
         Type type = midiEvent.GetType();
         byte number;
         StandardEventTypes.Channel.TryGetStatusByte(type, out number);
         FourBitNumber channel = ((ChannelEvent)midiEvent).Channel;
         byte          value   = DataTypesUtilities.Combine((FourBitNumber)number, channel);
         writer.WriteByte(value);
     }
     midiEvent.Write(writer, settings);
 }
Exemple #27
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);
                    }
                }
            }
        }
Exemple #28
0
        // Token: 0x060033BF RID: 13247 RVA: 0x00148774 File Offset: 0x00146974
        public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte)
        {
            if (writeStatusByte)
            {
                Type type = midiEvent.GetType();
                byte value;
                StandardEventTypes.SysEx.TryGetStatusByte(type, out value);
                writer.WriteByte(value);
            }
            int size = midiEvent.GetSize(settings);

            writer.WriteVlqNumber(size);
            midiEvent.Write(writer, settings);
        }
        /// <summary>
        /// Writes content of a MIDI meta event.
        /// </summary>
        /// <param name="writer">Writer to write the content with.</param>
        /// <param name="settings">Settings according to which the event's content must be written.</param>
        protected sealed override void WriteContent(MidiWriter writer, WritingSettings settings)
        {
            var text = Text;

            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            var encoding = settings.TextEncoding ?? SmfConstants.DefaultTextEncoding;
            var bytes    = encoding.GetBytes(text);

            writer.WriteBytes(bytes);
        }
        public int CalculateSize(MidiEvent midiEvent, WritingSettings settings, bool writeStatusByte)
        {
            if (midiEvent == null)
            {
                throw new ArgumentNullException(nameof(midiEvent));
            }

            if (!(midiEvent is ChannelEvent))
            {
                throw new ArgumentException("Event is not Channel event.", nameof(midiEvent));
            }

            //

            return((writeStatusByte ? 1 : 0) + midiEvent.GetSize());
        }