Exemple #1
0
        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 WasapiOut(new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia), AudioClientShareMode.Shared, false, 2);
                    ActiveStream        = new Mp3FileReader(path);
                    inputStream         = new WaveChannel32(ActiveStream);
                    sampleAggregator    = new SampleAggregator(fftDataSize);
                    inputStream.Sample += inputStream_Sample;
                    waveOutDevice.Init(inputStream);
                    ChannelLength = inputStream.TotalTime.TotalSeconds;
                    FileTag       = File.Create(path);
                    GenerateWaveformData(path);
                    CanPlay = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"{ex.GetType()}: {ex.Message}");

                    ActiveStream = null;
                    CanPlay      = false;
                }
            }
        }
Exemple #2
0
        private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var waveformParams      = e.Argument as WaveformGenerationParams;
            var waveformMp3Stream   = new Mp3FileReader(waveformParams.Path);
            var waveformInputStream = new WaveChannel32(waveformMp3Stream);

            waveformInputStream.Sample += waveStream_Sample;

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

            waveformAggregator = new SampleAggregator(frameLength);

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

            for (var i = 1; i <= waveformParams.Points; i++)
            {
                waveMaxPointIndexes.Add((int)Math.Round(waveformLength * (i / (double)waveformParams.Points), 0));
            }
            var 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)
                {
                    var clonedData = (float[])waveformCompressedPoints.Clone();
                    Application.Current.Dispatcher.Invoke(() => { WaveformData = clonedData; });
                }

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

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

            Application.Current.Dispatcher.Invoke(() =>
            {
                fullLevelData = waveformData.ToArray();
                WaveformData  = finalClonedData;
            });
            waveformInputStream.Close();
            waveformInputStream.Dispose();
            waveformInputStream = null;
            waveformMp3Stream.Close();
            waveformMp3Stream.Dispose();
            waveformMp3Stream = null;
        }