0

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;
}
Matt K
  • 13,370
  • 2
  • 32
  • 51
karthick
  • 11,998
  • 6
  • 56
  • 88

3 Answers3

3

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.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
1

As you wrote, "nbbytes value is 1399285583" ...

Hexa value will be 0x5367674F, which looks like some kind of a String header ...

Alphaneo
  • 12,079
  • 22
  • 71
  • 89
0

This code assumes a lot about the sizes of int and short. I would guess that the sample code assumed int is 32 bits, where you are probably using 64 bit integers. This would be enough to make it fail. In any case it is hard to tell with the information you have provided.

ergosys
  • 47,835
  • 5
  • 49
  • 70
  • @above: now i changed my code like this fread(&nbBytes, 1, 1, fin); to avoid the int issue and it started to process. But my pcm file is not playing – karthick Jan 28 '11 at 03:12
  • You need to make nbBytes the size of the value in the header and fread as many bytes as the type. For example, for 32 bits, use int32_t from stdint.h, and sizeof(int32_t) as the number of bytes. And I hope you have the same endian machine as the data file uses, because that's another can of worms. – ergosys Jan 28 '11 at 03:24
  • @above: But in the documentation i saw that..decoding will be done according to the machine endianness.. I gave 32 bits and still it's the same...sizeOf(int) in my machine is 32 bits only – karthick Jan 28 '11 at 03:30