1

我试图让正弦波示例在 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;
    }
}
4

1 回答 1

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;
    }

}
于 2012-04-09T02:13:07.033 回答