Dispose() protected method

Disposes this WaveStream
protected Dispose ( bool disposing ) : void
disposing bool
return void
 public void PlaySound(string name, Action done = null)
 {
     FileStream ms = File.OpenRead(_soundLibrary[name]);
     var rdr = new Mp3FileReader(ms);
     WaveStream wavStream = WaveFormatConversionStream.CreatePcmStream(rdr);
     var baStream = new BlockAlignReductionStream(wavStream);
     var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
     waveOut.Init(baStream);
     waveOut.Play();
     var bw = new BackgroundWorker();
     bw.DoWork += (s, o) =>
                      {
                          while (waveOut.PlaybackState == PlaybackState.Playing)
                          {
                              Thread.Sleep(100);
                          }
                          waveOut.Dispose();
                          baStream.Dispose();
                          wavStream.Dispose();
                          rdr.Dispose();
                          ms.Dispose();
                          if (done != null) done();
                      };
     bw.RunWorkerAsync();
 }
        private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WaveformGenerationParams waveformParams = e.Argument as WaveformGenerationParams;
            Mp3FileReader waveformMp3Stream = new Mp3FileReader(waveformParams.Path);
            WaveChannel32 waveformInputStream = new WaveChannel32(waveformMp3Stream);
            waveformInputStream.Sample += waveStream_Sample;

            int frameLength = fftDataSize;
            int frameCount = (int)((double)waveformInputStream.Length / (double)frameLength);
            int waveformLength = frameCount * 2;
            byte[] readBuffer = new byte[frameLength];
            waveformAggregator = new SampleAggregator(frameLength);

            float maxLeftPointLevel = float.MinValue;
            float maxRightPointLevel = float.MinValue;
            int currentPointIndex = 0;
            float[] waveformCompressedPoints = new float[waveformParams.Points];
            List<float> waveformData = new List<float>();
            List<int> waveMaxPointIndexes = new List<int>();

            for (int i = 1; i <= waveformParams.Points; i++)
            {
                waveMaxPointIndexes.Add((int)Math.Round(waveformLength * ((double)i / (double)waveformParams.Points), 0));
            }
            int readCount = 0;
            while (currentPointIndex * 2 < waveformParams.Points && waveformInputStream.Position < (waveformInputStream.Length - 1024))
            {
                waveformInputStream.Read(readBuffer, 0, readBuffer.Length);

                waveformData.Add(waveformAggregator.LeftMaxVolume);
                waveformData.Add(waveformAggregator.RightMaxVolume);

                if (waveformAggregator.LeftMaxVolume > maxLeftPointLevel)
                    maxLeftPointLevel = waveformAggregator.LeftMaxVolume;
                if (waveformAggregator.RightMaxVolume > maxRightPointLevel)
                    maxRightPointLevel = waveformAggregator.RightMaxVolume;

                if (readCount > waveMaxPointIndexes[currentPointIndex])
                {
                    waveformCompressedPoints[(currentPointIndex * 2)] = maxLeftPointLevel;
                    waveformCompressedPoints[(currentPointIndex * 2) + 1] = maxRightPointLevel;
                    maxLeftPointLevel = float.MinValue;
                    maxRightPointLevel = float.MinValue;
                    currentPointIndex++;
                }
                if (readCount % 3000 == 0)
                {
                    float[] clonedData = (float[])waveformCompressedPoints.Clone();
                    App.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        WaveformData = clonedData;
                    }));
                }

                if (waveformGenerateWorker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                readCount++;
            }

            float[] finalClonedData = (float[])waveformCompressedPoints.Clone();
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                fullLevelData = waveformData.ToArray();
                WaveformData = finalClonedData;
            }));
            waveformInputStream.Close();
            waveformInputStream.Dispose();
            waveformInputStream = null;
            waveformMp3Stream.Close();
            waveformMp3Stream.Dispose();
            waveformMp3Stream = null;
        }
Beispiel #3
0
        private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var waveformParams = (WaveformGenerationParams)e.Argument;

            float[] waveformDataFromCache;
            if (waveformDataCache.TryGet(waveformParams.Path, out waveformDataFromCache))
            {
                Application.Current.Dispatcher.Invoke(() => WaveformData = waveformDataFromCache);
                return;
            }

            Mp3FileReader waveformMp3Stream = new Mp3FileReader(waveformParams.Path);
            WaveChannel32 waveformInputStream = new WaveChannel32(waveformMp3Stream);
            waveformInputStream.Sample += waveStream_Sample;

            int frameLength = fftDataSize;
            int frameCount = (int)((double)waveformInputStream.Length / (double)frameLength);
            int waveformLength = frameCount * 2;
            byte[] readBuffer = new byte[frameLength];
            waveformAggregator = new SampleAggregator(frameLength);

            float maxLeftPointLevel = float.MinValue;
            float maxRightPointLevel = float.MinValue;
            int currentPointIndex = 0;
            float[] waveformCompressedPoints = new float[waveformParams.Points];
            List<float> waveformData = new List<float>();
            List<int> waveMaxPointIndexes = new List<int>();

            for (int i = 1; i <= waveformParams.Points; i++)
            {
                waveMaxPointIndexes.Add((int)Math.Round(waveformLength * ((double)i / (double)waveformParams.Points), 0));
            }
            int readCount = 0;
            while (currentPointIndex * 2 < waveformParams.Points)
            {
                waveformInputStream.Read(readBuffer, 0, readBuffer.Length);

                waveformData.Add(waveformAggregator.LeftMaxVolume);
                waveformData.Add(waveformAggregator.RightMaxVolume);

                if (waveformAggregator.LeftMaxVolume > maxLeftPointLevel)
                    maxLeftPointLevel = waveformAggregator.LeftMaxVolume;
                if (waveformAggregator.RightMaxVolume > maxRightPointLevel)
                    maxRightPointLevel = waveformAggregator.RightMaxVolume;

                if (readCount > waveMaxPointIndexes[currentPointIndex])
                {
                    waveformCompressedPoints[(currentPointIndex * 2)] = maxLeftPointLevel;
                    waveformCompressedPoints[(currentPointIndex * 2) + 1] = maxRightPointLevel;
                    maxLeftPointLevel = float.MinValue;
                    maxRightPointLevel = float.MinValue;
                    currentPointIndex++;
                }
                if (readCount % 3000 == 0)
                {
                    float[] clonedData = (float[])waveformCompressedPoints.Clone();

                    try
                    {
                        App.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            WaveformData = clonedData;
                        }));
                    }
                    catch (TaskCanceledException)
                    {
                        e.Cancel = true;
                        return;
                    }
                    
                }

                if (waveformGenerateWorker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                readCount++;
            }

            float[] finalClonedData = (float[])waveformCompressedPoints.Clone();

            // Don't cache half-finished waveforms (my CDJ-1000MK3s do this...)
            if (!waveformGenerateWorker.CancellationPending)
                waveformDataCache.Add(waveformParams.Path, finalClonedData);

            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                fullLevelData = waveformData.ToArray();
                WaveformData = finalClonedData;
            }));
            waveformInputStream.Close();
            waveformInputStream.Dispose();
            waveformInputStream = null;
            waveformMp3Stream.Close();
            waveformMp3Stream.Dispose();
            waveformMp3Stream = null;
        }
Beispiel #4
0
 /// <summary>
 /// Creates a WzSoundProperty with the specified name from a file
 /// </summary>
 /// <param name="name">The name of the property</param>
 /// <param name="file">The path to the sound file</param>
 public WzSoundProperty(string name, string file)
 {
     this.name = name;
     Mp3FileReader reader = new Mp3FileReader(file);
     this.len_ms = (int)((double)reader.Length * 1000d / (double)reader.WaveFormat.AverageBytesPerSecond);
     this.frequency = reader.WaveFormat.SampleRate;
     //this.bps = (byte)reader.WaveFormat.BitsPerSample;
     this.header = CreateHeader(this.frequency);
     reader.Dispose();
     this.mp3bytes = File.ReadAllBytes(file);
 }
Beispiel #5
0
 /// <summary>
 /// Creates a WzSoundProperty with the specified name from a file
 /// </summary>
 /// <param name="name">The name of the property</param>
 /// <param name="file">The path to the sound file</param>
 public WzSoundProperty(string name, string file)
 {
     this.name = name;
     Mp3FileReader reader = new Mp3FileReader(file);
     this.len_ms = (int)((double)reader.Length * 1000d / (double)reader.WaveFormat.AverageBytesPerSecond);
     this.channels = (short)reader.Mp3WaveFormat.Channels;
     this.frequency = reader.Mp3WaveFormat.SampleRate;
     this.byterate = reader.Mp3WaveFormat.AverageBytesPerSecond;
     this.extraSize = (short)reader.Mp3WaveFormat.ExtraSize;
     this.blockSize = (short)reader.Mp3WaveFormat.blockSize;
     RebuildHeader();
     reader.Dispose();
     this.mp3bytes = File.ReadAllBytes(file);
 }
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Mp3FileReader reader = new Mp3FileReader(FileName);
            WaveChannel32 channel = new WaveChannel32(reader);
            channel.Sample += new EventHandler<SampleEventArgs>(channel_Sample);

            int points = 2000;

            int frameLength = (int)FFTDataSize.FFT2048;
            int frameCount = (int)((double)channel.Length / (double)frameLength);
            int waveformLength = frameCount * 2;
            byte[] readBuffer = new byte[frameLength];

            float maxLeftPointLevel = float.MinValue;
            float maxRightPointLevel = float.MinValue;
            int currentPointIndex = 0;
            float[] waveformCompressedPoints = new float[points];
            List<float> waveformData = new List<float>();
            List<int> waveMaxPointIndexes = new List<int>();

            for (int i = 1; i <= points; i++)
            {
                waveMaxPointIndexes.Add((int)Math.Round(waveformLength * ((double)i / (double)points), 0));
            }
            int readCount = 0;
            while (currentPointIndex * 2 < points)
            {
                channel.Read(readBuffer, 0, readBuffer.Length);

                waveformData.Add(InputSampler.LeftMax);
                waveformData.Add(InputSampler.RightMax);

                if (InputSampler.LeftMax > maxLeftPointLevel)
                    maxLeftPointLevel = InputSampler.LeftMax;
                if (InputSampler.RightMax > maxRightPointLevel)
                    maxRightPointLevel = InputSampler.RightMax;

                if (readCount > waveMaxPointIndexes[currentPointIndex])
                {
                    waveformCompressedPoints[(currentPointIndex * 2)] = maxLeftPointLevel;
                    waveformCompressedPoints[(currentPointIndex * 2) + 1] = maxRightPointLevel;
                    maxLeftPointLevel = float.MinValue;
                    maxRightPointLevel = float.MinValue;
                    currentPointIndex++;
                }
                if (readCount % 3000 == 0)
                {
                    WaveformData = (float[])waveformCompressedPoints.Clone();
                }

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                readCount++;
            }

            FullLevelData = waveformData.ToArray();
            WaveformData = (float[])waveformCompressedPoints.Clone();

            // Cleanup
            channel.Close();
            channel.Dispose();
            channel = null;
            reader.Close();
            reader.Dispose();
            reader = null;
        }
Beispiel #7
0
 public static void Mp3ToWav(string mp3File, string outputFile)
 {
     using (Mp3FileReader reader = new Mp3FileReader(mp3File))
     {
         CreateWaveFile2(outputFile, reader);
         reader.Dispose();
     }
 }
Beispiel #8
0
        protected override void playSong()
        {
            try {

                currentSong = nextSong;
                if (currentSong == null) {

                    currentSong = getNextSong();
                    if (currentSong == null) {
                        MessageBox.Show("No songs to play.");
                        return;
                    }
                }

                if (fileReader != null)
                    fileReader.Dispose();
                fileReader = new Mp3FileReader(currentSong.filepath);

            } catch (Exception) {
                MessageBox.Show("Error reading " + currentSong + ".\n" +
                    "File does not exist or cannot be read.");
                if (fileReader != null)
                    fileReader.Dispose();
                loadNextSong(null, null);
                return;
            }

            startGUIThread();

            waveChannel = new WaveChannel32(fileReader, 1, panSlider.Pan);
            waveChannel.PadWithZeroes = false;

            SampleChannel sampleChannel = new SampleChannel(waveChannel);
            sampleChannel.PreVolumeMeter += onPreVolumeMeter;
            volumeDelegate = (vol) => sampleChannel.Volume = vol;
            volumeDelegate(absoluteVolume * mixedVolume);

            //Debug.WriteLine("absoluteVolume: " + absoluteVolume + " mixedVolume: " + mixedVolume);
            //Debug.WriteLine("absoluteVolume * mixedVolume = " + absoluteVolume * mixedVolume);

            postVolumeMeter = new MeteringSampleProvider(sampleChannel);
            postVolumeMeter.StreamVolume += onPostVolumeMeter;

            deviceOut.Init(postVolumeMeter);
            deviceOut.Play();

            label_EndTime.Text = String.Format("{0:00}:{1:00}",
                        (int)fileReader.TotalTime.TotalMinutes,
                        fileReader.TotalTime.Seconds);

            label_SongTitle.Text = currentSong.ToString();
        }