2012-04-09 172 views
1

我试图让sine wave example在AsioOut上运行,但它听起来更像是一个扭曲的方波。 AsioOut可能只支持PCM格式吗? asio的.wav文件播放工作得很好...NAudio Asio和IeeeFloat格式

如果是这样,我如何填充我的缓冲区与Ieee浮游物并转换为PCM?或什么是最好的方式来回溯Ieee ASIO?我很想避免不必要的样本转换..我试图生成一个适合缓冲区大小的正弦波,以确保我有连续的值,我用采样率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; 
    } 
} 

回答

3

好吧,我发现的bug。阿西欧在某种程度上是立体声设计。所以此代码工作:

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

}