public void OpenFile(string path) { Stop(); if (ActiveStream != null) { SelectionBegin = TimeSpan.Zero; SelectionEnd = TimeSpan.Zero; ChannelPosition = 0; } StopAndCloseStream(); if (System.IO.File.Exists(path)) { try { waveOutDevice = new WaveOut() { DesiredLatency = 100 }; var extension = Path.GetExtension(path); if (extension == ".mp3") { ActiveStream = new Mp3FileReader(path); } else if (extension == ".wav") { ActiveStream = new WaveFileReader(path); } else { throw new ArgumentException("Wrong file extension."); } inputStream = new WaveChannel32(ActiveStream); effectStream = new EffectStream(inputStream); outputStream = new WaveChannel32(effectStream); waveFormStream = new WaveChannel32(effectStream); sampleAggregator = new SampleAggregator(fftDataSize); outputStream.Sample += outputStream_Sample; waveOutDevice.Init(outputStream); ChannelLength = outputStream.TotalTime.TotalSeconds; FileTag = TagLib.File.Create(path); GenerateWaveformData(waveFormStream); CanPlay = true; } catch { ActiveStream = null; CanPlay = false; } } }
private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e) { WaveformGenerationParams waveformParams = e.Argument as WaveformGenerationParams; WaveChannel32 waveformInputStream = waveformParams.EffectWave; 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(); 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; }