C代码问题..任何人都可以帮忙吗?

时间:2011-01-28 02:47:07

标签: c speex

我想解码一个speex文件并转换成PCM wave ..我正在尝试编译他们给出的speex示例代码..它没有给出任何编译错误。但是当我跑它时它什么也没做。

在标有“问题区域”的行后,即使是printf也没有被解雇。代码以某种方式崩溃了。 你们能帮助我吗? OUTPUT: FRAME_SIZE 320 nbbytes 139928553 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;
}

3 个答案:

答案 0 :(得分:3)

printf()通常不是衡量崩溃的好方法。使用fprintf(stderr,...而不是.printf()的输出被缓冲,输出到stderr不是。我怀疑你的问题会在以后发生。

答案 1 :(得分:1)

正如您所写,“nbbytes值为1399285583”......

十六进制值为0x5367674F,看起来像某种String头...

答案 2 :(得分:0)

此代码假定了很多关于int和short的大小。我猜想假设int的示例代码是32位,你可能使用64位整数。这足以让它失败。无论如何,很难说明您提供的信息。