NAudio Asio录制和播放 [英] NAudio Asio Record and Playback

查看:348
本文介绍了NAudio Asio录制和播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写自己的VST主机,为此,我需要录制和播放来自Asio驱动程序的音频(在我的情况下为音频接口).这就是为什么我要使用NAudio的AsioOut.

I'm trying to write my own VST Host and for that i need to record and play audio from an Asio Driver (in my case for an audio interface). That's why i'm trying to use NAudio's AsioOut.

出于测试目的,我目前正试图记录输入,将其复制并播放到输出中.

For testing purposes i'm currently just trying to record the input, copy and play it to the output.

我的代码如下:

var asioout = new AsioOut();
BufferedWaveProvider wavprov = new BufferedWaveProvider(new WaveFormat(44100, 2));
asioout.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(asio_DataAvailable);
asioout.InitRecordAndPlayback(wavprov, 2, 25);
asioout.Play();

...

void asio_DataAvailable(object sender, AsioAudioAvailableEventArgs e)
{
    Array.Copy(e.InputBuffers, e.OutputBuffers, e.InputBuffers.Length);
    e.WrittenToOutputBuffers = true;
}

这样,我听不到任何输出.我也这样尝试过:

This way i can't hear any output. I also tried it this way:

void asio_DataAvailable(object sender, AsioAudioAvailableEventArgs e)
{
    byte[] buf = new byte[e.SamplesPerBuffer];
    for (int i = 0; i < e.InputBuffers.Length; i++)
    {
        //Marshal.Copy(e.InputBuffers[i], e.OutputBuffers, 0, e.InputBuffers.Length);
        //also tried to upper one but this way i also couldn't hear anything
        Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
        Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer);
    }
    e.WrittenToOutputBuffers = true;
}

这样,我可以听到输入音量的声音,但声音失真很大.

This way i can hear sound in the volume of my input but it's very distorded.

我在这里做错什么了?

PS:我知道如何记录和播放. .. 存在,但我无法真正从该线程中获得完整的答案,只是想与Marshall.Copy ...一起尝试的想法.

PS: I know how to record and playback.... exists but i couldn't really get a complete answer from this thread, just the idea to try it with Marshall.Copy....

推荐答案

第二次尝试比第一次尝试更正确:每个输入缓冲区必须分别复制.但是,复制方法的最后一个参数应该是字节数,而不是缓冲区中的样本数.每个样本通常为3或4个字节,具体取决于您的ASIO位深度.

Your second attempt is more correct than the first: each input buffer must be copied separately. However the final parameter of the copy method should be the number of bytes, not the number of samples in the buffer. This will typically be 3 or 4 bytes per sample, depending on your ASIO bit depth.

这篇关于NAudio Asio录制和播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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