录音波 NAUDIO LIBLARY - 音质差

本文关键字:LIBLARY NAUDIO 录音 | 更新日期: 2023-09-27 18:35:26

我正在使用NAUDIO进行音频编程(使用教程)。我的程序(C# .net winForms)我有记录和停止按钮。代码如下所示:

    NAudio.Wave.WaveIn sourceStream = null;
    NAudio.Wave.WaveFileWriter waveWriter = null;
    private void RecordButton_Click(object sender, EventArgs e)
    {
        int deviceNumber = 0;
        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.DeviceNumber = deviceNumber;
        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
        sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
        waveWriter = new NAudio.Wave.WaveFileWriter("d:''a.wav", sourceStream.WaveFormat);
        sourceStream.StartRecording();
    }
    private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
        if (waveWriter == null) return;
        waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();
    }
    private void StopButton_Click(object sender, EventArgs e)
    { 
            waveWriter.Dispose();
            waveWriter = null;       
    }

它可以工作,但波形文件质量不好 - 我录制的声音有小差距。我想要采样率 44100 Hz 和采样格式 16 位。

录音波 NAUDIO LIBLARY - 音质差

我无法完全复制您发现的内容,但我确实注意到DataAvailable事件没有为其他操作留下太多空间。一个简单的Thread.Sleep(100);会使应用程序停止。

假设您的连续写入(和刷新)可能会导致该问题,我实现了一个天真的Queue,它保留要写入的字节并使用线程池中的线程进行实际写入。

DataAvailable事件现在如下所示:

    Queue<byte[]> writebuffer = new Queue<byte[]>();
    private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
        if (waveWriter == null) return;
        byte[] realbytes = new byte[e.BytesRecorded];
        Array.Copy(e.Buffer, realbytes, e.BytesRecorded);
        writebuffer.Enqueue(realbytes);
    }

StartRecording调用之后,我将任务排队以读取队列并将数据写入流。如果我在那里添加延迟,应用程序将不再停止。

ThreadPool.QueueUserWorkItem((s) =>
{
    var keeprunning = true;
    sourceStream.RecordingStopped += (rss, rse) => { keeprunning = false; };
    while(keeprunning)
    {
        if (writebuffer.Count==0)
        {
                Thread.Sleep(0);
        }
        else
        { 
            var buf = writebuffer.Dequeue();
            waveWriter.Write(buf,0,buf.Length);
            // Thread.Sleep(100); // for testing 
        }
    }
    waveWriter.Dispose();
    waveWriter = null;
});

我也遇到了这个问题。解决方案是从使用WaveIn进行录制切换到WasapiCapture。设置和使用几乎相同,但质量要好得多。试试这个:

public class Recorder
{
    public string _outputFolder { get; set; }
    public string _outputFilePath { get; set; }
    public WasapiCapture _capture { get; set; }
    public WaveFileWriter _writer { get; set; }
    public Recorder()
    {
        _outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");
        Directory.CreateDirectory(_outputFolder);
        _outputFilePath = Path.Combine(_outputFolder, "recorded.wav");
    }
    public void StartRecording()
    {
        _capture = new WasapiCapture();
        _capture.ShareMode = AudioClientShareMode.Shared;
        _capture.WaveFormat = new WaveFormat(44100, 32, 1);
        _writer = new WaveFileWriter(_outputFilePath, _capture.WaveFormat);
        _capture.DataAvailable += _capture_DataAvailable;
        _capture.RecordingStopped += _capture_RecordingStopped; ;
        _capture.StartRecording();
    }
    public void StopRecording()
    {
        _capture.StopRecording();
    }
    private void _capture_DataAvailable(object sender, WaveInEventArgs e)
    {
        _writer.Write(e.Buffer, 0, e.BytesRecorded);
    }
    private void _capture_RecordingStopped(object sender, StoppedEventArgs e)
    {
        _writer?.Dispose();
        _writer = null;
        _capture.Dispose();
        _capture = null;
    }
}