在使用 naudio 播放期间调整字节流

Adjust bytes stream during playback with naudio

我在 Visual studio 中将 Naudio 用于 C# 音频项目。我正在寻找一个简单的工作示例,说明如何在波形文件到达声卡之前调整其读取流。这是我想做的一个非工作示例:

        public static string CurrentFile;
    public WaveOut waveout;

    public WaveFileReader wavereader {
    get { byte[] bts = //somehow Get byte buffer from reader?????
            int i = 0;
            while (i < bts.Length) {
                // do some cool stuff to the stream here
                i++; }
            return bts;//give the adjusted stream back to waveout before playback????
        }  }


    public void go()
    {
        CurrentFile = "c:/Temp/track1 - 0.wav";
        wavereader = new WaveFileReader(CurrentFile);
        waveout.Init(wavereader);
        waveout.Play();
    }

您需要创建自己的 ISampleProvider 实现来执行音频操作。在对 Read 的每次调用中,您从源提供程序(这将是 wavefilereader 转换为样本提供程序)读取。然后执行 DSP。

因此播放代码将如下所示(使用AudioFileReader

CurrentFile = "c:/Temp/track1 - 0.wav";
wavereader = new AudioFileReader(CurrentFile);
var myEffects = new MyEffects(waveReader)
waveout.Init(myEffects);
waveout.Play();

然后 MyEffects 看起来像这样:

class MyEffects : ISampleProvider
{
    private readonly ISampleProvider source;
    public MyEffects(ISampleProvider source)
    {
        this.source = source;
    }
    public WaveFormat { get { return source.WaveFormat; } }
    public int Read(float[] buffer, int offset, int read)
    {
         var samplesRead = source.Read(buffer, offset, read);
         for(int n = 0; n < samplesRead; n++)
         {
             // do cool stuff here to change the value of 
             // the sample in buffer[offset+n]
         }
         return samplesRead;
    }
}