[Speex-dev] Speex stereo encoding

Денис Ромашенко romius99 at mail.ru
Mon Nov 28 02:27:38 PST 2011


Hi.
 I trying to encode PCM16 8000Hz stereo data to speex and put it into the .flv file format. But at the output I can hear only noise. What I doing wrong? Here is the code:
void main()
{
    SpeexBits bits; 
    void *enc_state; 
    int frame_size;
    int quality = 10;
    char cbits[MAX_FRAME_BYTES];
    FILE *fin, *speex;
    short input[MAX_FRAME_SIZE];
    int nbBytes;
    int channels = 0;
    int format = 0;
    int rate = 0;
    spx_int32_t fileSize;

//Open input wav file and read wav header
    fin = fopen("StereoPcm.wav", "rb");
    read_wav_header(fin, &rate, &channels, &format, &fileSize);

//Initialize bits and encoder
    speex_bits_init(&bits);
    enc_state = speex_encoder_init(&speex_nb_mode); 

//Set quality and get framesize
    speex_encoder_ctl(enc_state,SPEEX_SET_QUALITY,&quality); 
    speex_encoder_ctl(enc_state,SPEEX_GET_FRAME_SIZE,&frame_size);

//Create output .flv file
    FlvFileFormat flvFile(8, "StereoSpeex.flv", 11, 1, 1, channels - 1);

//Encode frame by frame
    while (nbBytes > 0)
    {
        nbBytes = fread(input, 1, frame_size * 2, fin);
        speex_bits_reset(&bits);

//If the input file is stereo calling to speex_encode_stereo_int
        if (channels==2)
        {
            speex_encode_stereo_int(input, frame_size, &bits);
        }

//Encode the input data and write it to the cbits
        speex_encode_int(enc_state, input, &bits);
        nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES);

//Add encoded frame to .flv file
        flvFile.AddTag(nbBytes, (unsigned char*)cbits);
    }

//Close the files and destroy encoder
    fclose(fin);
    fclose(speex);
    speex_bits_destroy(&bits);
    speex_encoder_destroy(enc_state);
}

And another question. Is the libspeex thread safe?


More information about the Speex-dev mailing list