NAudio:使用ASIO用吉他录制音频和输出 [英] NAudio: Using ASIO to record audio and output with a guitar

查看:270
本文介绍了NAudio:使用ASIO用吉他录制音频和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究SR.设计项目,这是一个Windows窗体应用程序,它将允许用户插入吉他并实时创建失真,并使用输入和输出进行播放,然后将其自动切换到ASCII标签中.目前,我正在尝试使实时侦听部分正常工作,仅在使用ASIO遇到一些问题时,我才能正确记录和实现失真.我看过这篇文章如何使用NAudio录制和播放使用AsioOut ,但是对于我的问题并没有太大帮助,这是我的代码:

private BufferedWaveProvider buffer;
private AsioOut input;
private AsioOut output;

private void listenBtn_Click(object sender, EventArgs e)
{
    input = new AsioOut(RecordInCbox.SelectedIndex);
    WaveFormat format = new WaveFormat();
    buffer = new BufferedWaveProvider(format);
    buffer.DiscardOnBufferOverflow = true;
    input.InitRecordAndPlayback(buffer, 1, 44100);
    input.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(AudioAvailable);

    //output = new AsioOut(RecordInCbox.SelectedIndex);
    //output.Init(buffer);

    input.Play();
    //output.Play();
}

public void AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
    byte[] buf = new byte[e.SamplesPerBuffer];
    e.WrittenToOutputBuffers = true;
    for (int i = 0; i < e.InputBuffers.Length; i++)
    {
        Array.Copy(e.InputBuffers, e.OutputBuffers, 1);
        Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
        buffer.AddSamples(buf, 0, buf.Length);
    }
}

当前它正在获取音频并将其推入缓冲区,但输出不起作用.如果将录音设置设为在窗口上收听",则可以弹奏吉他,但是觉得这是不必要的,因为我希望能够执行失真并听到输出.谢谢!

解决方案

不幸的是,我找不到使ASIO正常工作的方法,但是我想出了一种替代方法,它与延迟一样好我把时间降到了50毫秒,但一直在研究NAudio来源,看是否有办法将其降低到该水平以下. (大约20到30毫秒左右)以获得更好的实时播放效果.

    private BufferedWaveProvider buffer;
    private WaveOut waveOut;
    private WaveIn sourceStream = null;

    private bool listen = false;

    private void listenBtn_Click(object sender, EventArgs e)
    {
        listen = !listen;
        if (listen)
            listenBtn.Text = "Stop listening";
        else
        {
            listenBtn.Text = "Listen";
            sourceStream.StopRecording();
            return;
        }

        sourceStream = new WaveIn();
        sourceStream.WaveFormat = new WaveFormat(44100, 1);

        waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());

        sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);
        sourceStream.RecordingStopped += new EventHandler<StoppedEventArgs>(sourceStream_RecordingStopped);

        buffer = new BufferedWaveProvider(sourceStream.WaveFormat);
        buffer.DiscardOnBufferOverflow = true;
        waveOut.DesiredLatency = 51;
        waveOut.Volume = 1f;
        waveOut.Init(buffer);
        sourceStream.StartRecording();
    }

    private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
    {
        buffer.AddSamples(e.Buffer, 0, e.BytesRecorded);

        waveOut.Play();
    }

    private void sourceStream_RecordingStopped(object sender, StoppedEventArgs e)
    {
        sourceStream.Dispose();
        waveOut.Dispose();
    }

同样,我确实知道这不是在使用ASIO,但根据我现有的资源和文档,它是更好的选择.我不是使用ASIO,而是创建waveIn并模拟录音",而不是将其写入文件中,而是将流引入并将其推入waveOut缓冲区,这样在我进行一些声音处理后即可播放它. /p>

I am currently working on a SR. design project which is a windows forms app which will allow for users to plug in their guitar and create distortions in real time and play it with input and output, and auto tab it into ASCII tabs. Currently I am trying to get the real time listening portion working, I have the recording and implementation of distortions working just fine just having some issues using ASIO. I've looked at this post How to record and playback with NAudio using AsioOut but it was not of much help with my issue, here is my code:

private BufferedWaveProvider buffer;
private AsioOut input;
private AsioOut output;

private void listenBtn_Click(object sender, EventArgs e)
{
    input = new AsioOut(RecordInCbox.SelectedIndex);
    WaveFormat format = new WaveFormat();
    buffer = new BufferedWaveProvider(format);
    buffer.DiscardOnBufferOverflow = true;
    input.InitRecordAndPlayback(buffer, 1, 44100);
    input.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(AudioAvailable);

    //output = new AsioOut(RecordInCbox.SelectedIndex);
    //output.Init(buffer);

    input.Play();
    //output.Play();
}

public void AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
    byte[] buf = new byte[e.SamplesPerBuffer];
    e.WrittenToOutputBuffers = true;
    for (int i = 0; i < e.InputBuffers.Length; i++)
    {
        Array.Copy(e.InputBuffers, e.OutputBuffers, 1);
        Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
        buffer.AddSamples(buf, 0, buf.Length);
    }
}

Currently it is getting the audio and pushing it into the buffer but the output is not working. I am able to get it to play the guitar if I set the recording setting to Listen on windows but feel this is unnecessary as how I want to be able to perform my distortion and hear that as the output. Thanks!

解决方案

Unfortunately I could not find a way to get the ASIO to work, but I have come up with an alternative method which works just as well, as for the latency I got it down to 50 ms, but have been looking into the NAudio source to see if there might be a way to get it below that. (roughly around 20-30 ms) For a better realtime play.

    private BufferedWaveProvider buffer;
    private WaveOut waveOut;
    private WaveIn sourceStream = null;

    private bool listen = false;

    private void listenBtn_Click(object sender, EventArgs e)
    {
        listen = !listen;
        if (listen)
            listenBtn.Text = "Stop listening";
        else
        {
            listenBtn.Text = "Listen";
            sourceStream.StopRecording();
            return;
        }

        sourceStream = new WaveIn();
        sourceStream.WaveFormat = new WaveFormat(44100, 1);

        waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());

        sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);
        sourceStream.RecordingStopped += new EventHandler<StoppedEventArgs>(sourceStream_RecordingStopped);

        buffer = new BufferedWaveProvider(sourceStream.WaveFormat);
        buffer.DiscardOnBufferOverflow = true;
        waveOut.DesiredLatency = 51;
        waveOut.Volume = 1f;
        waveOut.Init(buffer);
        sourceStream.StartRecording();
    }

    private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
    {
        buffer.AddSamples(e.Buffer, 0, e.BytesRecorded);

        waveOut.Play();
    }

    private void sourceStream_RecordingStopped(object sender, StoppedEventArgs e)
    {
        sourceStream.Dispose();
        waveOut.Dispose();
    }

Again I do understand that this is not using ASIO but it was a better alternative based on the resources I had available and the documentation. Instead of using ASIO I am just creating the waveIn and mocking a "recording" but instead of writing that to a file I am taking the stream and pushing it into a waveOut buffer which will allow for it play after I do some sound manipulation.

这篇关于NAudio:使用ASIO用吉他录制音频和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆