コード例 #1
0
ファイル: Midi.cs プロジェクト: santerinogelainen/midify
        /// <summary>
        /// reads all tracks in filestream into three lists
        /// </summary>
        /// <returns>true if successful</returns>
        private bool ReadAllTracks()
        {
            // number of tracks in the file
            int numberOfTracks = ByteConverter.ToInt(this.Header.Tracks);

            // loop each track
            for (int i = 0; i < numberOfTracks; i++)
            {
                // add new track
                this.Tracks.Add(new TrackChunk());
                TrackChunk track = this.Tracks[this.Tracks.Count - 1];

                // try reading the track info into the new track
                if (!track.Read(this.Stream, this.TempoChanges, this.TimeSignatureChanges))
                {
                    Console.WriteLine("Error reading track at index {0}.", i);
                    return(false);
                }

#if DEBUG
#endif
            }

            // order tempo and timesignature changes by absolute timing
            this.TempoChanges         = this.TempoChanges.OrderBy(x => x.AbsoluteTiming).ToList();
            this.TimeSignatureChanges = this.TimeSignatureChanges.OrderBy(x => x.AbsoluteTiming).ToList();

            // order all trackevents and remove useless tracks
            this.Tracks.RemoveAll(x => x.Events.Count == 0);
            foreach (TrackChunk track in this.Tracks)
            {
                track.Events = track.Events.OrderBy(x => x.AbsoluteTiming).ToList();
            }
            return(true);
        }
コード例 #2
0
        public void ReadData(Stream data)
        {
            trackChunks.Clear();
            if (!data.CanRead)
            {
                throw new NotSupportedException("Stream does not support reading.");
            }

            long fileSize = data.Length;

            headerChunk.Read(data);

            int trackCount = 0;

            while (data.Position < fileSize)
            {
                string str         = data.ReadString(size: 4);
                UInt32 chunkLength = data.ReadUInt32BE();


                if (chunkLength > fileSize)
                {
                    // TODO - Turn this into an exception?
                    Console.WriteLine("WARNING: Chunk size beyond end of stream, ignoring chunk.");
                    break;
                }

                if (str != "MTrk")
                {
                    data.Position += chunkLength;
                }

                TrackChunk chunk = new TrackChunk(noteEventMatching);
                chunk.Read(data, chunkLength);
                trackChunks.Add(chunk);
                trackCount++;
                //Console.WriteLine(chunkCount);
            }
        }