1

我正在尝试读取包含原始音频的文件并使用FLAC对其进行编码。当我运行程序时,我得到一个“总线错误”。有什么问题?
我正在使用以下行在 OS X 10.6.8 上进行编译:

gcc nsFlacEncoder.c -I/opt/local/include -lflac -m32 -o flac_enc

#include "FLAC/stream_encoder.h"
#define READSIZE 40000
char buffer[READSIZE];
FLAC__int32 pcm[READSIZE/2];

FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const  FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
{
    FILE * fp;
    fp = fopen("rec.flac","rw");
    fwrite(buffer, 1, bytes, fp);
    fclose(fp);
    return 0;
}

int rawToFlac()
{
    FLAC__bool ok = true;
    FLAC__StreamEncoder *encoder = 0;
    FLAC__StreamEncoderInitStatus init_status;
    unsigned sample_rate = 16000;
    unsigned channels = 1;
    unsigned bps = 16;

    if((encoder=FLAC__stream_encoder_new()) == NULL){
        printf("Error!");
        return 1;
    }

    ok &= FLAC__stream_encoder_set_verify(encoder, true);
    ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
    ok &= FLAC__stream_encoder_set_channels(encoder, channels);
    ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
    ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
    ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, READSIZE);

    if(ok){
        init_status = FLAC__stream_encoder_init_stream(encoder, &write_callback, NULL, NULL, NULL, /*client_data=*/NULL);
        if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK){
            printf("Encoder not initiated");
            return 1;
        }
    }

    if(ok){
        while(ok)
        {       
            /* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
            size_t i;
            for(i = 0; i < 20000; i++) {
                /* inefficient but simple and works on big- or little-endian machines */
                pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
            }
            /* feed samples to encoder */
            ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, 20000);
        }
    }

    ok &= FLAC__stream_encoder_finish(encoder);
    printf("Finished.");

    FLAC__stream_encoder_delete(encoder);   
    return 0;
}

int main()
{

    FILE *file;
    file = fopen("recording","rb");
    fread(buffer,2, 20000, file);
    rawToFlac();
    fclose(file);
    return 0;
}

跑步gdb flac_enc给了我这个:

warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitmath.o" - no debug information available for "bitmath.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitreader.o" - no debug information available for "bitreader.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/bitwriter.o" - no debug information available for "bitwriter.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/cpu.o" - no debug information available for "cpu.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/crc.o" - no debug information available for "crc.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/fixed.o" - no debug information available for "fixed.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/format.o" - no debug information available for "format.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/lpc.o" - no debug information available for "lpc.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/md5.o" - no debug information available for "md5.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/memory.o" - no debug information available for "memory.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/metadata_iterators.o" - no debug information available for "metadata_iterators.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/metadata_object.o" - no debug information available for "metadata_object.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_decoder.o" - no debug information available for "stream_decoder.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_encoder.o" - no debug information available for "stream_encoder.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/stream_encoder_framing.o" - no debug information available for "stream_encoder_framing.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/window.o" - no debug information available for "window.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_decoder_aspect.o" - no debug information available for "ogg_decoder_aspect.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_encoder_aspect.o" - no debug information available for "ogg_encoder_aspect.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_helper.o" - no debug information available for "ogg_helper.c".
warning: Could not find object file "/Users/benski/Desktop/flac-1.2.1/src/libFLAC/.libs/ogg_mapping.o" - no debug information available for "ogg_mapping.c".

这很奇怪,因为我的系统上没有用户“benski”。但我确信 FLAC 库已正确安装,因为示例程序可以完美运行。

4

3 回答 3

1

main()中,您不检查文件是否已成功打开。问题可能是您在fread()操作中使用了空指针。同样,在write_callback()函数中,您的代码显示了不可战胜的假设。(另外,如果你的回调被多次调用,第二次调用会覆盖第一次调用产生的数据。然而,这是一个不同的问题。)

您不会检查fread(). 您也没有检查写入了多少数据fwrite()- 它是否成功?

您应该能够使用gdb或类似的调试器来查看故障发生的位置。

您也可以使用它valgrind来发现问题。

您不需要同时使用 theifwhilein:

if (ok)
{
    while (ok)
    {
        ...
    }
}

单独的循环就足够了;ok如果在第一个周期为假,它将被执行零次。如果在while循环之后和结束之前有一个语句,if那么两者都是必要的。

通常,当您尝试访问未对齐的数据对象时,RISC 芯片上会发生 SIGBUS(总线错误)。目前尚不清楚哪一行可能导致此代码中的问题。尽管前面有关于“空指针”的评论,但它通常以 SIGSEGV(分段违规)而不是 SIGBUS 告终。

于 2011-07-10T16:13:44.040 回答
0

当您期望一些指针来解决您的问题时,请务必了解更多细节,例如 - 哪个平台、编译器、编译器选项(是否打开了任何优化等)。

AFAIK 如果在未对齐的内存地址上完成某些内存读取或写入操作,则会引发“总线错误”。但是最近没有看到总线错误,因为现代系统(编译器,平台)已经消除了对数据与特定边界对齐的任何硬性和快速需求,除非它是正在为其开发代码的专用硬件架构。

现在关于这个特殊的问题,你有 -

(我假设它在 Linux 下),所以使用 gcc 的 -g 开关构建代码。

然后尝试使用 gdb [您的可执行文件]。这应该告诉代码在错误后退出哪个函数/代码行。查看该函数/行/之前和之后的代码!

我的猜测(疯狂猜测)是您将样本写入/读取到 pcm[i] 的位置,因为它似乎是 FLAC__int32 类型的用户定义数据。但是,嘿,这可能还很遥远,直到您自己调试。祝你好运!

于 2011-07-10T16:24:21.783 回答
0

您可以通过说来查看核心文件

gdb <executable name> <corefile name> 

然后说“在哪里”查看回溯。这将帮助您查看失败的原因(但正如@JohnathanLeffler 指出的那样,您有几个可以通过检查发现的错误)。

于 2011-07-10T16:17:43.360 回答