NAudio Asio和IeeeFloat格式

时间:2012-04-09 01:26:54

标签: audio asio pcm naudio sine

我试图让sine wave example在AsioOut上运行,但听起来更像是一个扭曲的方波。是否有可能AsioOut仅支持PCM格式? asio .wav文件播放效果很好......

如果是这样,我如何用Ieee浮点数填充缓冲区并转换为PCM?或者什么是在ASIO上发布Ieee的最好方法?我希望避免不必要的样本转换..

到目前为止,在我的代码中,我试图生成一个符合缓冲区大小的正弦波,以确保我有连续的值,我用采样率44100和1个通道初始化它。然后我将类的实例传递给我的AsioOut的Init():

public class SineWaveProvider32 : IWaveProvider
{
    private WaveFormat waveFormat;
    public WaveFormat WaveFormat
    {
        get
        {
            return this.waveFormat;
        }
    }

    public SineWaveProvider32() : this(44100, 1)
    {
    }

    public SineWaveProvider32(int sampleRate, int channels)
    {
        this.SetWaveFormat(sampleRate, channels);
    }

    public void SetWaveFormat(int sampleRate, int channels)
    {
        this.waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
    }

            public unsafe int Read(byte[] buffer, int offset, int count)
    {
        var samples = count/4;
        fixed(byte* buff = buffer)
        {
            for (int n = 0; n < samples; n++)
            {
                var num = (float)(Math.Sin( (2 * Math.PI * n)/ samples ));
                ((float*)buff)[n] = num;
            }
        }

        return count;
    }
}

1 个答案:

答案 0 :(得分:3)

好的,我发现了这个错误。 Asio以某种方式设计立体声。所以这段代码有效:

public class SineWaveProvider32 : IWaveProvider
{
    private WaveFormat waveFormat;
    public WaveFormat WaveFormat
    {
        get
        {
            return this.waveFormat;
        }
    }

    public SineWaveProvider32() : this(44100, 2)
    {
    }

    public SineWaveProvider32(int sampleRate, int channels)
    {
        this.SetWaveFormat(sampleRate, channels);
    }

    public void SetWaveFormat(int sampleRate, int channels)
    {
        this.waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
    }

    public unsafe int Read(byte[] buffer, int offset, int count)
    {
        var samples = count/4;
        fixed(byte* buff = buffer)
        {
            for (int n = 0; n < samples; n+=2)
            {
                var num = (float)(Math.Sin( (2 * Math.PI * n * 3)/ samples ));
                ((float*)buff)[n] = 0;
                ((float*)buff)[n+1] = num;
            }
        }

        return count;
    }

}