【问题标题】:C code problem..Can anyone help?C代码问题..有人可以帮忙吗?
【发布时间】:2011-01-28 02:47:07
【问题描述】:

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

在标记为“问题区域”的行之后,甚至 printf 也没有被触发。代码不知何故崩溃了。 你们能帮帮我吗? 输出: FRAME_SIZE 320 字节 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;
}

【问题讨论】:

  • 不幸的是,它不可读(对于像我这样疲惫的老眼睛)。
  • 我继续缩进它...现在应该更容易看,虽然仍然在较长的一边。
  • @above 逻辑仅在 while 循环内.. 看看.. @jefromi 谢谢
  • while (!(feof(fout))) 大概是这样吧?
  • @above 是的,对不起,我的错..即使它在那个地方有输入流,它也不起作用

标签: c speex


【解决方案1】:

printf() 通常不是衡量崩溃的好方法。使用 fprintf(stderr, ... 代替。printf() 的输出是缓冲的,到 stderr 的输出不是。我怀疑你的问题稍后会出现。

【讨论】:

  • 很可能,如果 isatty() 为真。 ;-)
【解决方案2】:

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

Hexa 值将是 0x5367674F,看起来像是某种字符串头 ...

【讨论】:

  • 那么你是说那个 fread() 语句中存在一些转换问题?
  • 不,你从错误的偏移量读取,这是神奇的数字
  • @above: 现在我像这样改变了我的代码 fread(&nbBytes, 1, 1, fin);它开始处理。但是我的 pcm 文件没有播放
  • @Karthick OMG,是的,我们可以为您完成所有工作,如果您将薪水交给所有这些人...
【解决方案3】:

这段代码对 int 和 short 的大小做了很多假设。我猜想示例代码假定 int 是 32 位,您可能使用的是 64 位整数。这足以让它失败。无论如何,根据您提供的信息很难判断。

【讨论】:

  • @above: 现在我像这样改变了我的代码 fread(&nbBytes, 1, 1, fin);为了避免 int 问题,它开始处理。但是我的 pcm 文件没有播放
  • 您需要使 nbBytes 成为标头中值的大小,并读取与类型一样多的字节。例如,对于 32 位,使用 stdint.h 中的 int32_t,并使用 sizeof(int32_t) 作为字节数。我希望你有与数据文件使用相同的字节序机器,因为那是另一个蠕虫罐头。
  • @above:但在文档中我看到..解码将根据机器字节序完成..我给了 32 位,但它仍然是相同的......我的机器中的 sizeOf(int)仅 32 位
猜你喜欢
  • 2011-07-10
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多