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

问题描述 投票:0回答:1

我正在使用Naudio作为Visual Studio中的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();
    }
c# audio naudio
1个回答
1
投票

您需要创建自己的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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.