Encoding in WP7 with NSpeex works, decoding the stream on a JavaEE server with JSpeex does not

471 Views Asked by At

As topic states, I have no problem in encoding audio in Windows Phone 7 with NSpeex(v1.1.1, uses Speex v1.2rc1). I have verified this by first encoding a stream, and then right after decoding it back again, add wav headers and send it back to server on which the wav plays just fine. But if I send the encoded stream to server and try to decode it with JSpeex(v0.9.7, uses Speex v1.0.3) I only get StreamCorruptedExceptions of different sorts with fiddling with the decoding settings.

Am I hitting version incopability here, or am I just doing something plain wrong? If someone has any knowledge about this configuration I would be thankful for any help. The code I'm using now:

Phone side:

SpeexEncoder encoder = new SpeexEncoder(BandMode.Wide);
// set encoding quality to lowest (which will generate the smallest size in the fastest time)
encoder.Quality = 1;
int inDataSize = _recordedBytes / 2; //_recordedBytes is the lenght of the _recordedBuffer(which is a byte array)

// convert to short array
short[] data = new short[inDataSize];
int sampleIndex = 0;
for (int index = 0; index < _recordedBytes; index += 2, sampleIndex++)
{
    data[sampleIndex] = BitConverter.ToInt16(_recordedBuffer, index);
}

// note: the number of samples per frame must be a multiple of encoder.FrameSize
inDataSize = inDataSize - inDataSize % encoder.FrameSize; //framesize is 320
var encodedData = new byte[_recordedBytes];
int encodedBytes = encoder.Encode(data, 0, inDataSize, encodedData, 0, _recordedBytes);
if (encodedBytes != 0)
{
    byte[] inDataSizeBuf = BitConverter.GetBytes(inDataSize);
    byte[] sizeBuf = BitConverter.GetBytes(encodedBytes + inDataSizeBuf.Length);
    byte[] returnBuf = new byte[encodedBytes + sizeBuf.Length + inDataSizeBuf.Length];
    sizeBuf.CopyTo(returnBuf, 0);
    inDataSizeBuf.CopyTo(returnBuf, sizeBuf.Length);
    Array.Copy(encodedData, 0, returnBuf, sizeBuf.Length + inDataSizeBuf.Length, encodedBytes);
}

Server side:

SpeexDecoder speexDecoder = new SpeexDecoder();         
if(speexDecoder.init(1, 16000, 1, true)) { //params: mode(1 = wide), sample rate, channels, enhancement(what ever that is)
    speexDecoder.processData(encodedSpeex, 0, 320); //params: encoded byte array, offset, frame size
    byte[] decoded = new byte[speexDecoder.getProcessedDataByteSize()];
    speexDecoder.getProcessedData(decoded, 0);
}

Error, happens with processData() (with current settings used in code):

java.io.StreamCorruptedException: Invalid sideband mode encountered. (2nd sideband): 6
at org.xiph.speex.NbDecoder.decode(Unknown Source)
at org.xiph.speex.SbDecoder.decode(Unknown Source)
at org.xiph.speex.SpeexDecoder.processData(Unknown Source)
at org.xiph.speex.SpeexDecoder.processData(Unknown Source)
at mun.testi.SpeexTesti.main(SpeexTesti.java:35)
0

There are 0 best solutions below