С кодом проблема .. Может кто-нибудь помочь? - PullRequest
0 голосов
/ 28 января 2011

Я хочу декодировать файл speex и преобразовать его в волну PCM. Я пытаюсь скомпилировать пример кода speex, который они дали.но он ничего не делает, когда я его запускаю ..

После строки, отмеченной «проблемная область», даже printf не запускаетсяКод каким-то образом падает.ребята, вы можете мне помочь?ВЫХОД: 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 ]

3 голосов
/ 28 января 2011

printf () обычно не является хорошим показателем сбоя. Используйте fprintf (stderr, ... вместо этого. Вывод printf () буферизуется, вывод stderr - нет. Я подозреваю, что ваша проблема возникает позже.

1 голос
/ 28 января 2011

Как вы написали, "значение nbbytes равно 1399285583" ...

Значение Hexa будет равно 0x5367674F, что выглядит как некий заголовок строки ...

0 голосов
/ 28 января 2011

Этот код предполагает многое о размерах int и short. Я предполагаю, что в примере кода предполагается, что int 32-битный, где вы, вероятно, используете 64-битные целые числа. Этого было бы достаточно, чтобы он потерпел неудачу. В любом случае трудно сказать с информацией, которую вы предоставили.

...