TaterKing
TaterKing

Reputation: 75

Adjust bytes stream during playback with naudio

I am Using Naudio for a a C# audio project in Visual studio. I am looking for a simple working example of how to the adjust the read stream of a wave file before it reaches the sound card. here is a non-working example of what i want to do:

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

Upvotes: 0

Views: 559

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

You need to create your own implementation of ISampleProvider to perform the audio manipulation. In each call to Read you read from the source provider (which will be the wavefilereader converted to a sample provider. And then you perform your DSP.

So the playback code will look like this (using AudioFileReader)

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

And then MyEffects looks like this:

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

Upvotes: 1

Related Questions