Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Individual Samples Access & Modification

$
0
0

Yes, NAudio is designed to give you access to the individual audio samples. However, there are lots of different audio formats (bit depths, compression types etc), so there is no one simple way that works for everything. The WaveFileReader class will give you access to the raw byte data. You can then convert each pair of bytes into 16 bit samples yourself, or take advantage of some of the NAudio helper classes to allow you to more easily work directly with 16 bit audio (assuming your audio is 16 bit). Then you use the WaveFileWriter class to write your modified audio back out to disk.

If you want to deal with audio as floats, as you say, use the new AudioFileReader class which will return a ISampleProvider, which makes it very easy to examine the value of each sample as a float as it comes through the Read method. You would create your own ISampleProvider whose Read method reads from the source AudioFileReader and examines and modifies the audio:

   var reader = new AudioFileReader();

   var mySampleProvider = new MySampleProvider(reader);

   WaveFileWriter.CreateWaveFile(myWaveProvider, "example.wav");

 

class MySampleProvider: ISampleProvider

   private int Read(float[] buffer, int offset, int count)

{

   int samplesRead = source.Read(buffer, offset, count);

  // TODO: examine and optionally change the contents of buffer

   return samplesRead;

}


Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>