NAudio playing a sine wave for x milliseconds using C#

前端 未结 2 574
不知归路
不知归路 2020-12-18 02:27

I am using NAudio to play a sinewave of a given frequency as in the blog post Playback of Sine Wave in NAudio. I just want the sound to play() for x milliseconds an

相关标签:
2条回答
  • 2020-12-18 02:47

    The SineWaveProvider32 class doesn't need to indefinitely provide audio. If you want the beep to have a maximum duration of a second (say), then for mono 44.1 kHz, you need to provide 44,100 samples. The Read method should return 0 when it has no more data to supply.

    To make your GUI thread not block, you need to get rid of Thread.Sleep, waveOut.Stop and Dispose, and simply start playing the audio (you may find window callbacks more reliable than function).

    Then, when the audio has finished playing, you can close and clean up the WaveOut object.

    0 讨论(0)
  • 2020-12-18 03:03

    Check out the blog post Pass Variables to a New Thread in C# on how to pass variables to another thread.

    I think what you want to do is something like create a thread that plays your sound, create a timer, and start the thread. When the timer expires, kill the thread, and when the thread closes have it do all the cleanup.

    0 讨论(0)
提交回复
热议问题