[Speex-dev] speex_bits_write / speex_bits_read_from - getting Static

Mark Pearce markwpearce at gmail.com
Tue Nov 20 13:09:07 PST 2007


Hello,

I'm having a problem using Speex - I'm getting white noise/static from the
following process:
(Load File) -> (Encode) -> (Decode) -> (Play)

I know there's no issue with the loading or playing because (Load File)->
(Play) plays the file properly.

I believe the problem lies with the speex_bits_write and
speex_bits_read_from functions, because if I change my encode function to
return the whole SpeexBits struct and my decode function to accept a
SpeexBits struct, it plays the vocoded audio without any problem.


****************** Managed C++ Code ***********************************

//Public function -- m_encoderState is a void* member, m_bits is a
SpeexBits* member
array<Byte>^ SpeexEncoder::Encode(array<short>^ inputFrame)
{

    //begin lazy man's way of converting to regular C
    unsigned int numSamples = inputFrame->Length;
    short* inputptr = (short*) malloc(numSamples*sizeof(short));

    unsigned int i = 0;
    for(i = 0; i<numSamples; i++)
      inputptr[i] = (short) inputFrame[i];

    //end lazy man's conversion

    speex_bits_reset(m_bits);

    //preprocess the input
    //speex_preprocess(m_preprocess, inputptr);


    //encode
    speex_encode_int(m_encoderState, inputptr, m_bits);


    char outputFrame[SPEEX_MAX_ENCODE_SIZE_PER_FRAME];

    free(inputptr);

    unsigned int numBytes = speex_bits_write(m_bits, outputFrame,
SPEEX_MAX_ENCODE_SIZE_PER_FRAME);

   //begin lazy man's conversion to Managed C++
    array<Byte>^ output = gcnew array<Byte>(numBytes);
    for(i = 0; i< numBytes; i++)
        output[i] = (unsigned char)outputFrame[i];
    //end lazy man's conversion

    return output;
}




//Public function -- m_decoderState is a void* member, m_bits is a
SpeexBits* member
array<short>^ SpeexDecoder::Decode(array<Byte>^ inputBytes)
{

     short* outputFrame = (short*) malloc(m_frameSize*sizeof(short));
    speex_bits_reset(m_bits);
    if(inputBytes != nullptr)
    {
      //begin lazy man's way of converting to regular C
      std::string input;
      unsigned int inputLength = inputBytes->Length;
      input.resize(inputLength);
      unsigned int i = 0;
      for(i = 0; i<inputLength; i++)
        input[i] = (unsigned char) inputBytes[i];
      //end lazy man's conversion

      speex_bits_read_from(m_bits, (char*) input.c_str(), inputLength);
    }
    else
    {
      speex_bits_read_from(m_bits, NULL, 0);
    }

    //decode the data
    speex_decode_int(m_decoderState, m_bits, (spx_int16_t*) outputFrame);

   //begin lazy man's conversion to Managed C++
    array<short>^ output = gcnew array<short>(m_frameSize);
    int i = 0;
    for(i = 0; i<m_frameSize; i++)
        output[i] = outputFrame[i];
    free(outputFrame);
   //end lazy man's conversion

    return output;
}


************


Thanks for any help,

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xiph.org/pipermail/speex-dev/attachments/20071120/18b5ff3d/attachment.html


More information about the Speex-dev mailing list