Ejemplo n.º 1
0
        private static IEnumerable <TrackChunk> ConvertTrackChunks(IEnumerable <TrackChunk> trackChunks, MidiFileFormat format)
        {
            var chunksConverter = ChunksConverterFactory.GetConverter(format);

            return(chunksConverter.Convert(trackChunks)
                   .OfType <TrackChunk>());
        }
Ejemplo n.º 2
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);
                }
            }
        }
        // Token: 0x06003246 RID: 12870 RVA: 0x00147440 File Offset: 0x00145640
        public IEnumerable <MidiChunk> Convert(IEnumerable <MidiChunk> chunks)
        {
            TrackChunk[] array = chunks.OfType <TrackChunk>().ToArray <TrackChunk>();
            if (array.Length == 0)
            {
                return(chunks);
            }
            IEnumerable <Class0 <TrackChunk, int> > source = array.Select((TrackChunk c, int i) => new Class0 <TrackChunk, int>(c, ((int)MultiSequenceChunksConverter.GetSequenceNumber(c)) ?? i)).ToArray <Class0 <TrackChunk, int> >();
            IChunksConverter singleTrackChunksConverter    = ChunksConverterFactory.GetConverter(MidiFileFormat.SingleTrack);

            return((from n in source
                    group n by n.Prop_1).SelectMany((IGrouping <int, Class0 <TrackChunk, int> > g) => singleTrackChunksConverter.Convert(from n in g
                                                                                                                                         select n.Prop_0)).Concat(from c in chunks
                                                                                                                                                                  where !(c is TrackChunk)
                                                                                                                                                                  select c));
        }
Ejemplo n.º 4
0
        public IEnumerable <MidiChunk> Convert(IEnumerable <MidiChunk> chunks)
        {
            ThrowIfArgument.IsNull(nameof(chunks), chunks);

            var trackChunks = chunks.OfType <TrackChunk>().ToArray();

            if (trackChunks.Length == 0)
            {
                return(chunks);
            }

            var sequenceNumbers = trackChunks.Select((c, i) => new { Chunk = c, Number = GetSequenceNumber(c) ?? i })
                                  .ToArray();

            var singleTrackChunksConverter = ChunksConverterFactory.GetConverter(MidiFileFormat.SingleTrack);

            return(sequenceNumbers.GroupBy(n => n.Number)
                   .SelectMany(g => singleTrackChunksConverter.Convert(g.Select(n => n.Chunk)))
                   .Concat(chunks.Where(c => !(c is TrackChunk))));
        }
Ejemplo n.º 5
0
 // Token: 0x0600340F RID: 13327 RVA: 0x00148AE4 File Offset: 0x00146CE4
 public void Write(Stream stream, MidiFileFormat format = MidiFileFormat.MultiTrack, WritingSettings settings = null)
 {
     if (this.TimeDivision == null)
     {
         throw new InvalidOperationException("Time division is null.");
     }
     if (!stream.CanWrite)
     {
         throw new ArgumentException("Stream doesn't support writing.", "stream");
     }
     if (settings == null)
     {
         settings = new WritingSettings();
     }
     using (MidiWriter midiWriter = new MidiWriter(stream))
     {
         IEnumerable <MidiChunk> enumerable = ChunksConverterFactory.GetConverter(format).Convert(this.Chunks);
         int num = enumerable.Count((MidiChunk c) => c is TrackChunk);
         if (num > 65535)
         {
             throw new TooManyTrackChunksException(string.Format("Count of track chunks to be written ({0}) is greater than the valid maximum ({1}).", num, ushort.MaxValue), num);
         }
         new HeaderChunk
         {
             FileFormat   = (ushort)format,
             TimeDivision = this.TimeDivision,
             TracksNumber = (ushort)num
         }.Write(midiWriter, settings);
         foreach (MidiChunk midiChunk in enumerable)
         {
             if (!settings.CompressionPolicy.HasFlag(CompressionPolicy.DeleteUnknownChunks) || !(midiChunk is UnknownChunk))
             {
                 midiChunk.Write(midiWriter, settings);
             }
         }
     }
 }