我正在Visual Studio中将Naudio用于C#音频项目。我正在寻找一个简单的工作示例,该示例如何在Wave文件到达声卡之前对其进行调整。这是我想做的一个不起作用的例子:

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

关于c# - 在使用naudio播放期间调整字节流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49060059/

10-16 09:37