Пример #1
0
        /// <summary>
        /// Loads a MIDI file into the Sequence.
        /// </summary>
        /// <param name="fileName">
        /// The MIDI file's name.
        /// </param>
        public void Load(string fileName)
        {
            #region Require

            if (disposed)
            {
                throw new ObjectDisposedException("Sequence");
            }
            else if (IsBusy)
            {
                throw new InvalidOperationException();
            }
            else if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            #endregion

            FileStream stream = new FileStream(fileName, FileMode.Open,
                                               FileAccess.Read, FileShare.Read);

            using (stream)
            {
                MidiFileProperties newProperties = new MidiFileProperties();
                TrackReader        reader        = new TrackReader();
                List <Track>       newTracks     = new List <Track>();

                newProperties.Read(stream);

                for (int i = 0; i < newProperties.TrackCount; i++)
                {
                    reader.Read(stream);
                    newTracks.Add(reader.Track);
                }

                properties = newProperties;
                tracks     = newTracks;
            }

            #region Ensure

            Debug.Assert(Count == properties.TrackCount);

            #endregion
        }
Пример #2
0
        private void LoadDoWork(object sender, DoWorkEventArgs e)
        {
            string fileName = (string)e.Argument;

            FileStream stream = new FileStream(fileName, FileMode.Open,
                                               FileAccess.Read, FileShare.Read);

            using (stream)
            {
                MidiFileProperties newProperties = new MidiFileProperties();
                TrackReader        reader        = new TrackReader();
                List <Track>       newTracks     = new List <Track>();

                newProperties.Read(stream);

                float percentage;

                for (int i = 0; i < newProperties.TrackCount && !loadWorker.CancellationPending; i++)
                {
                    reader.Read(stream);
                    newTracks.Add(reader.Track);

                    percentage = (i + 1f) / newProperties.TrackCount;

                    loadWorker.ReportProgress((int)(100 * percentage));
                }

                if (loadWorker.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                {
                    properties = newProperties;
                    tracks     = newTracks;
                }
            }
        }