问题
I try to write an application that records the sound from the microphone and send it directly to the speakers. For testing I use a headset to avoid an acoustic feedback. I found this tutorial https://markheath.net/post/how-to-record-and-play-audio-at-same . Since I had a problem in my final application with this, I created a small test app to make sure that the cause of my problem isn't some side effect. I create a small test program with 2 buttons (start&stop) to test it. But for some reason the recorded sound in my bigger finale app and in this app has periodic silence parts which I can't find why.
This is my code:
public partial class MainWindow : Window
{
private AudioLive MyLive;
public MainWindow()
{
InitializeComponent();
MyLive = new AudioLive();
MyLive.Init();
}
private void ButtonStart(object sender, RoutedEventArgs e)
{
MyLive.StartLive();
}
private void ButtonEnd(object sender, RoutedEventArgs e)
{
MyLive.EndLive();
}
}
class AudioLive
{
private WaveIn m_Recorder;
private BufferedWaveProvider m_BufferedWaveProvider;
private SavingWaveProvider m_SavingWaveProvider;
private WaveOut m_Player;
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (m_Recorder != null)
{
m_Recorder.Dispose();
}
m_Recorder = null;
if (m_SavingWaveProvider != null)
{
m_SavingWaveProvider.Dispose();
}
m_SavingWaveProvider = null;
}
private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
m_BufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
public bool Init()
{
m_Recorder = new WaveIn();
m_Recorder.WaveFormat = new WaveFormat(44100, 2);
m_Recorder.DataAvailable += RecorderOnDataAvailable;
// set up our signal chain
m_BufferedWaveProvider = new BufferedWaveProvider(m_Recorder.WaveFormat);
return true;
}
public void StartLive()
{
m_SavingWaveProvider = new SavingWaveProvider(m_BufferedWaveProvider, "live.wav");
// set up playback
m_Player = new WaveOut();
m_Player.Init(m_SavingWaveProvider);
// begin playback & record
m_Player.Play();
m_Recorder.StartRecording();
}
public void EndLive()
{
// stop recording
m_Recorder.StopRecording();
// stop playback
m_Player.Stop();
m_Player.Dispose();
// finalise the WAV file
m_SavingWaveProvider.Dispose();
}
}
My test application doesn't containing anything else than these 2 buttons. Has anybody an idea why I get these periodical silence parts during recording?
回答1:
It's because BufferedWaveProvider
produces silence if it doesn't have any audio buffered, which can happen when live streaming audio. It would be better for you simply to write received audio into a WaveFileWriter
as well as putting it into BufferedWaveProvider
in
RecorderOnDataAvailable
instead of using SavingWaveProvider
.
来源:https://stackoverflow.com/questions/53847245/unwanted-silence-parts-in-naudio-recording