Search code examples
cspeex

C code problem..Can anyone help?


I want to decode a speex file and convert into a PCM wave..I am trying to compile the speex sample code they have given..It's not giving any compilation error. but it does nothing when i run it..

After the line marked "problem area" even the printf is not getting fired. The code is somehow getting crashed. can you guys help me? OUTPUT: FRAME_SIZE 320 nbbytes 139928553 loop count after fprintf

#include <speex/speex.h>
#include <stdio.h>
#include <stdlib.h>
/*The frame size in hardcoded for this sample code but it doesn't have to be*/
#define FRAME_SIZE 320      


int main(int argc, char **argv)
{
    char *outFile;
    FILE *fin;
    FILE *fout;
    /*Holds the audio that will be written to file (16 bits per sample)*/
    short out[FRAME_SIZE];
    /*Speex handle samples as float, so we need an array of floats*/
    float output[FRAME_SIZE];
    char cbits[4096];
    int nbBytes;
    /*Holds the state of the decoder*/
    void *state;
    /*Holds bits so they can be read and written to by the Speex routines*/
    SpeexBits bits;
    int i, tmp;
    long lSize;
    size_t result;
    char *buffer;

    if (argc != 2)
    {
        printf("Warning: 2 arguments needed!\n");
        return 0;
    }
    /*Create a new decoder state in narrowband mode*/
    state = speex_decoder_init(&speex_nb_mode);

    /*Set the perceptual enhancement on*/ 
    tmp=8;
    speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);

    outFile = argv[1];
    if ((fin = fopen(outFile, "r+")) == NULL)
    {
        printf("Warning: Cannot open file!\n");
        return 0;
    }
    if ((fout = fopen("newFile.wav", "w+")) == NULL)
    {
        printf("Warning: Cannot open file!\n");
        return 0;
    }

    /*Initialization of the structure that holds the bits*/
    speex_bits_init(&bits);
    printf("FRAME SIZE: %i\n", FRAME_SIZE);


    while (!(feof(fin)))
    {

        /*Read the size encoded by sampleenc, this part will likely be 
          different in your application*/

        fread(&nbBytes, sizeof(int), 1, fin);
        fprintf (stderr, "nbBytes: %d\n", nbBytes); //It's reading the bytes and storing successfully
        printf("loop count after fprintf \n" );

        /*Read the "packet" encoded by sampleenc*/
        fread(cbits, 1, nbBytes, fin); // Problem area
        printf("after fread \n" );

        /*Copy the data into the bit-stream struct*/
        speex_bits_read_from(&bits, cbits, nbBytes);

        /*Decode the data*/
        speex_decode(state, &bits, output);

        /*Copy from float to short (16 bits) for output*/
        for (i=0;i<FRAME_SIZE;i++){
            out[i]=output[i];
        }

        printf("loop count after for \n" );

        /*Write the decoded audio to file*/
        fwrite(out, sizeof(short), FRAME_SIZE, fout);

        printf("loop count\n" );
    }

    /*Destroy the decoder state*/
    speex_decoder_destroy(state);
    /*Destroy the bit-stream truct*/
    speex_bits_destroy(&bits);
    fclose(fout);
    fclose(fin);
    return 0;
}

Solution

  • printf() is generally not a good measure of crashedness. Use fprintf(stderr, ... instead. printf()'s output is buffered, the output to stderr is not. I suspect your problem occurs later.