Code Monkey home page Code Monkey logo

Comments (5)

dwebb7 avatar dwebb7 commented on July 17, 2024

After looking into this more I have realized that WasapiOut is stopping not WasapiCapture. Is this intended?

from cscore.

filoe avatar filoe commented on July 17, 2024

The problem is that the event does not get fired on the ui thread. Accessing UI Elements from other threads is not allowed. In order to ensure that all components are getting released correctly, the exception gets catched and populated by the Stopped Event of the WasapiOut class. The correct solution for this problem is to use the InvokeAsync Method of any UI Element. It is not recommend to use the sync Invoke Method since it could cause deadlocks or performance critical operations.

from cscore.

dwebb7 avatar dwebb7 commented on July 17, 2024

Thanks! That did it. For sake of documentation I updated the code from above with your suggestion using InvokeAsync to change the label to the PeakValue every 100ms. Works great.

    public partial class MainWindow : Window
    {
        private WasapiCapture wasapiCapture;
        private WasapiOut wasapiOut;
        private SoundInSource wasapiCaptureSource;
        private ISampleSource sampleSource;
        private PeakMeter peakMeter;
        private IWaveSource waveSource;

        public MainWindow()
        {
            InitializeComponent();
            wasapiCapture = new WasapiCapture();
            wasapiCapture.Initialize();
            wasapiCaptureSource = new SoundInSource(wasapiCapture);
            sampleSource = wasapiCaptureSource.ToSampleSource();
            peakMeter = new PeakMeter(sampleSource);
            peakMeter.Interval = 100;
            peakMeter.PeakCalculated += peakMeter_PeakCalculated;
            waveSource = peakMeter.ToWaveSource();
            wasapiCapture.Start();
            wasapiOut = new WasapiOut();
            wasapiOut.Initialize(waveSource);
            Thread.Sleep(100);
            wasapiOut.Play();
        }

        void peakMeter_PeakCalculated(object sender, PeakEventArgs e)
        {
            Action peakAction = () => lbltest.Content = e.PeakValue.ToString();
            Dispatcher.InvokeAsync(peakAction);
        }
    }

from cscore.

dwebb7 avatar dwebb7 commented on July 17, 2024

Is this the same issue? I am trying to toggle the reverb mix with a button click and it stops WasapiOut also. I tried to add a InvokeAsync to the click method and it works sometimes and other times it stops WasapiOut. Any ideas? Here is my code.

    public partial class MainWindow : Window
    {
        private WasapiCapture wasapiCapture;
        private WasapiOut wasapiOut;
        private SoundInSource wasapiCaptureSource;
        private readonly DmoWavesReverbEffect reverbEffect;
        private ISampleSource sampleSource;
        private PeakMeter peakMeter;
        private IWaveSource waveSource;

        public MainWindow()
        {
            InitializeComponent();
            wasapiCapture = new WasapiCapture();
            wasapiCapture.Initialize();
            wasapiCaptureSource = new SoundInSource(wasapiCapture);
            sampleSource = wasapiCaptureSource.ToSampleSource();
            peakMeter = new PeakMeter(sampleSource);
            peakMeter.Interval = 100;
            peakMeter.PeakCalculated += peakMeter_PeakCalculated;
            waveSource = peakMeter.ToWaveSource();
            reverbEffect = new DmoWavesReverbEffect(waveSource);
            wasapiCapture.Start();
            wasapiOut = new WasapiOut();
            wasapiOut.Initialize(reverbEffect);
            Thread.Sleep(100);
            wasapiOut.Play();
        }

        void peakMeter_PeakCalculated(object sender, PeakEventArgs e)
        {
            Action peakAction = () => lbltest.Content = e.PeakValue.ToString();
            Dispatcher.InvokeAsync(peakAction);
        }

        private void TestButton_Click(object sender, RoutedEventArgs e)
        {
            Action reverbAction = () =>
            {
                if (reverbEffect.ReverbMix == 0f)
                {
                    reverbEffect.ReverbMix = -96;
                }
                else if (reverbEffect.ReverbMix == -96f)
                {
                    reverbEffect.ReverbMix = 0;
                }
            };
            Dispatcher.InvokeAsync(reverbAction);
        }
    }

from cscore.

filoe avatar filoe commented on July 17, 2024

This was a bug which got fixed a few days ago with commit 31bf8cd.
Since the TestButton_Click EventHandler runs on the GUI Thread, it is not necessary to invoke anything.

from cscore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.