private bool ProcessPairWaveForm(WaveformGenerationParams argument)
        {
            bool           isCancel      = false;
            WaveFileReader forwardStream = new WaveFileReader(new MemoryStream(argument.Path));
            WaveFileReader backStream    = new WaveFileReader(new MemoryStream(argument.BackPath));


            WaveChannel32 forwardChannel = new WaveChannel32(forwardStream);
            WaveChannel32 backChannel    = new WaveChannel32(backStream);

            backChannel.Sample    += waveStream_Sample;
            forwardChannel.Sample += waveStream_Sample;

            long frameLength = 2 * backChannel.Length / argument.Points;

            frameLength = frameLength - frameLength % backChannel.WaveFormat.BlockAlign;

            waveformAggregator = new SampleAggregator((int)(frameLength / backChannel.WaveFormat.BlockAlign));

            float[] numArray = new float[argument.Points];
            byte[]  buffer   = new byte[frameLength];

            int factPointsCount = argument.Points / 2;

            for (int i = 0; i < factPointsCount; i++)
            {
                backChannel.Read(buffer, 0, buffer.Length);
                numArray[i * 2] = waveformAggregator.LeftMaxVolume * verticalScale;

                forwardChannel.Read(buffer, 0, buffer.Length);
                numArray[i * 2 + 1] = waveformAggregator.LeftMaxVolume * verticalScale;

                if (this.waveformGenerateWorker.CancellationPending)
                {
                    isCancel = true;
                    break;
                }
            }

            float[] finalClonedData = (float[])numArray.Clone();
            Application.Current.Dispatcher.Invoke(new Action(() => this.WaveformData = finalClonedData));

            forwardChannel.Close();
            forwardChannel.Dispose();
            forwardChannel = null;

            backChannel.Close();
            backChannel.Dispose();
            backChannel = null;

            forwardStream.Close();
            forwardStream.Dispose();
            forwardStream = null;

            backStream.Close();
            backStream.Dispose();
            backStream = null;

            return(isCancel);
        }
Ejemplo n.º 2
0
        private NAudioEngine()
        {
            sampleAggregator = new SampleAggregator(fftDataSize);

            var deviceEnumerator = new MMDeviceEnumerator();
            var defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            this.capture = new WasapiLoopbackCapture(defaultDevice);
            capture.ShareMode = AudioClientShareMode.Shared;

            capture.DataAvailable += CaptureOnDataAvailable;

            capture.StartRecording();
        }
Ejemplo n.º 3
0
        public void OpenFile(string path, ref EqualizerSounds _equalizer)
        {
            Stop();

            if (ActiveStream != null)
            {
                SelectionBegin  = TimeSpan.Zero;
                SelectionEnd    = TimeSpan.Zero;
                ChannelPosition = 0;
            }

            StopAndCloseStream();

            if (System.IO.File.Exists(path))
            {
                try
                {
                    // _soundOut.Initialize(source);

                    waveOutDevice = new CSCore.SoundOut.WaveOut(100);
                    //{
                    //    DesiredLatency = 100
                    //};

                    ActiveStream        = new Mp3FileReader(path);
                    inputStream         = new WaveChannel32(ActiveStream);
                    sampleAggregator    = new SampleAggregator(fftDataSize);
                    inputStream.Sample += inputStream_Sample;
                    //// inputStream.WaveFormat

                    var source = CodecFactory.Instance.GetCodec(path)
                                 .Loop()
                                 .ChangeSampleRate(32000)
                                 .ToSampleSource()
                                 .AppendSource(EqualizerSounds.Create10BandEqualizer, out _equalizer)
                                 .ToWaveSource();

                    //waveOutDevice.Init(inputStream);
                    waveOutDevice.Initialize(source);
                    ChannelLength = inputStream.TotalTime.TotalSeconds;
                    FileTag       = TagLib.File.Create(path);
                    GenerateWaveformData(path);
                    CanPlay = true;
                }
                catch
                {
                    ActiveStream = null;
                    CanPlay      = false;
                }
            }
        }
Ejemplo n.º 4
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 WaveOut()
                    {
                        DesiredLatency = 100
                    };
                    ActiveStream        = new Mp3FileReader(path);
                    inputStream         = new WaveChannel32(ActiveStream);
                    sampleAggregator    = new SampleAggregator(fftDataSize);
                    inputStream.Sample += inputStream_Sample;
                    waveOutDevice.Init(inputStream);
                    ChannelLength = inputStream.TotalTime.TotalSeconds;
                    FileTag       = TagLib.File.Create(path);
                    GenerateWaveformData(path);
                    CanPlay = true;
                }
                catch
                {
                    ActiveStream = null;
                    CanPlay      = false;
                }
            }
        }
Ejemplo n.º 5
0
        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.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;
        }
Ejemplo n.º 6
0
        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.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;
        }
Ejemplo n.º 7
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 WaveOut()
                    {
                        DesiredLatency = 100
                    };
                    ActiveStream = new Mp3FileReader(path);
                    inputStream = new WaveChannel32(ActiveStream);
                    sampleAggregator = new SampleAggregator(fftDataSize);
                    inputStream.Sample += inputStream_Sample;
                    waveOutDevice.Init(inputStream);
                    ChannelLength = inputStream.TotalTime.TotalSeconds;
                    FileTag = TagLib.File.Create(path);
                    GenerateWaveformData(path);
                    CanPlay = true;
                }
                catch
                {
                    ActiveStream = null;
                    CanPlay = false;
                }
            }
        }