1

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.

  • By 2^8 and 2^16, do you refer to the samples' bit width? – stakx - no longer contributing May 02 '14 at 11:29
  • it will downgrade the quality of the file... for example in images if i have 256 colors which are 2^8 possible values.. i'd like to downgrade it to black and white which is 2^1 values. that ofcurse is just an example to make it clear.... –  May 02 '14 at 11:52
  • possible duplicate of [change wav file ( to 16KHz and 8bit ) with using NAudio](http://stackoverflow.com/questions/6647730/change-wav-file-to-16khz-and-8bit-with-using-naudio) – stakx - no longer contributing May 06 '14 at 14:22

1 Answers1

2

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.

KillaKem
  • 995
  • 1
  • 13
  • 29
  • That last code snippet appears to affect sample rate and number of channels only but keeps the sample bit width (which is what the OP wants to change)...? – stakx - no longer contributing May 06 '14 at 10:40
  • I don't think the OP wanted to change the bit depth of the audio, all he wanted to do was to reduce the number of samples in the audio from 2^16 to 2^8, which is achieved by down-sampling.Changing the bit depth only affects how many bits are used to represent one sample, it won't affect the number of samples. – KillaKem May 06 '14 at 13:43
  • 1
    Check out the OP's comment above where he makes an analogy to 2^8 = 256 colors and 2^1 = black & white. I think it's clear from this that he's concerned about bit width/depth. – stakx - no longer contributing May 06 '14 at 13:58
  • 1
    Then I think the OP's question is a duplicate of this one : http://stackoverflow.com/questions/6647730/change-wav-file-to-16khz-and-8bit-with-using-naudio – KillaKem May 06 '14 at 14:14