Cannot save speex encoded audio as wav

Oct 20, 2011 at 9:16 PM

I'm trying to encode an wav file to speex, and then write the speex file as wav, but when I play the "encoded" wav file, I'm receiving an error, saying that the file is corrupted. The code is:

public void EncodeAudio(byte[] wavRawData)
{
   var encoder = new SpeexEncoder(BandMode.Wide);
   var pcmWaveWriter = new PcmWaveWriter(0, 10, 44100, 2, encoder.FrameSize, true);
   
   pcmWaveWriter.Open(@"MYPATH\audio.speex.wav");
   pcmWaveWriter.WriteHeader("Test conversion");

   var inDataSize = wavRawData.Length / 2;
   inDataSize = inDataSize - inDataSize % encoder.FrameSize;

   var inData = new short[inDataSize];

   for (var index = 0; index < inDataSize; index++)
   {
      inData[index] = BitConverter.ToInt16(wavRawData, index * 2);
   }
   
   for (var offset = 0; offset + encoder.FrameSize <= inDataSize; offset += encoder.FrameSize)
   {
      var encodedBuffer = new byte[1024];
      var encodedBytes = encoder.Encode(inData, offset, encoder.FrameSize, encodedBuffer, 0, encoder.FrameSize);

      var chunk = new byte[encodedBytes];
      Array.Copy(encodedBuffer, 0, chunk, 0, encodedBytes);
      pcmWaveWriter.WritePacket(chunk, 0, chunk.Length);
   }

   pcmWaveWriter.Close();
}

Please, what's wrong?

Coordinator
Oct 21, 2011 at 5:12 AM

I don't think your wav player is supporting Speex format. Also check your arguments when creating the PcmWaveWriter. Sampling rate, channels, etc. seem to be wrong.

Am 20.10.2011 22:16 schrieb "andreborges" <notifications@codeplex.com>:

From: andreborges

I'm trying to encode an wav file to speex, and then write the speex file as wav, but when I play the "encoded" wav file, I'm receiving an error, saying that the file is corrupted. The code is:

public void EncodeAudio(byte[] wavRawData)
{
var encoder = new SpeexEncoder(BandMode.Wide);
var pcmWaveWriter = new PcmWaveWriter(0, 10, 44100, 2, encoder.FrameSize, true);

pcmWaveWriter.Open(@"MYPATH\audio.speex.wav");
pcmWaveWriter.WriteHeader("Test conversion");

var inDataSize = wavRawData.Length / 2;
inDataSize = inDataSize - inDataSize % encoder.FrameSize;

var inData = new short[inDataSize];

for (var index = 0; index < inDataSize; index++)
{
inData[index] = BitConverter.ToInt16(wavRawData, index * 2);
}

for (var offset = 0; offset + encoder.FrameSize <= inDataSize; offset += encoder.FrameSize)
{
var encodedBuffer = new byte[1024];
var encodedBytes = encoder.Encode(inData, offset, encoder.FrameSize, encodedBuffer, 0, encoder.FrameSize);

var chunk = new byte[encodedBytes];
Array.Copy(encodedBuffer, 0, chunk, 0, encodedBytes);
pcmWaveWriter.WritePacket(chunk, 0, chunk.Length);
}

pcmWaveWriter.Close();
}

Please, what's wrong?

Read the full discussion online.

To add a post to this discussion, reply to this email (nspeex@discussions.codeplex.com)

To start a new discussion for this project, email nspeex@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Dec 26, 2011 at 9:21 PM

Andre,

why do you have these two lines:

var inDataSize = wavRawData.Length / 2;
inDataSize = inDataSize - inDataSize % encoder.FrameSize;