Question

I have a WAV file with 2^16 values between 0 to 2. I'd like to change it to have 2^8 values... which means it will has less information but will take less space.

I have the formula for the conversion but i don't know how to access the data.

I've read the file as following:

 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));

 while (wave.Position < wave.Length)
   {
    read = wave.Read(buffer,0,16348); 
   }

now i can use either the wave or the buffer.

how can i modify the wave to have 2^8 ? i am suppose to pass of each value and to convert it.. but i don't know how to access the values for the conversion.

if for example i had an array of floats which represent the wav file I'll know how to continue.

Was it helpful?

Solution

To access the data you use the reader's Read method:

var myReader = new WaveFilereader(filename);
int bytesRead = 0;
var readChunck = new byte[1024];
do
{
   //Read 1024 bytes at a time, will return 0 when there are no more bytes to Read
   bytesRead = myReader.Read(readChunk, 0 , readChunk.length );

   //Process the bytes here

}
while(bytesRead != 0)

Alternatively you can make your life easier by just resample the audio using the media foundation resampler.You can downsample the audio with the Media foundation resampler by doing something like this:

   //Read 2-channel Audio with sample rate 44.1Khz 
    var myReader = new WaveFileReader(filename); 

    //New Waveformat has 2-channels and sample rate 22KHz
    var myOutputFormat = new WaveFormat(22000,2); 

    //Resample
    var resampledAudio = new MediaFoundationResampler(myReader, myOutputFormat)  
                                                     { ResamplerQuality = 60 });

For more info on Audio Conversion see THIS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top