我正在尝试读取包含原始音频的文件,并使用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库已正确安装,因为example programs可以正常工作。
答案 0 :(得分:1)
在main()
中,您不会检查文件是否已成功打开。问题可能是您在fread()
操作中使用空指针。同样,在write_callback()
函数中,您的代码显示了无敌的假设。 (此外,如果您的回调被多次调用,第二次调用将覆盖第一次调用产生的数据。但是,这是一个不同的问题。)
您不检查fread()
读取了多少个2字节单位。你也没有检查fwrite()
写的数据量 - 它是否成功?
您应该能够使用gdb
或类似的调试器来查看故障发生的位置。
您也可以使用valgrind
来发现问题。
您不需要if
和while
:
if (ok)
{
while (ok)
{
...
}
}
单独循环就足够了;如果ok
在第一个周期为假,它将被执行零次。如果在while
循环之后和if
结束之前有声明,那么两者都是必要的。
通常,当您尝试访问未对齐的数据对象时,RISC芯片上会发生SIGBUS(总线错误)。目前尚不清楚哪一行可能会导致此代码中的麻烦。尽管之前有关于“空指针”的评论,但通常最终会出现SIGSEGV(分段违规),而不是SIGBUS。
答案 1 :(得分:0)
您可以通过
来查看核心文件gdb <executable name> <corefile name>
然后说“哪里”看回溯。这将有助于您了解失败的原因(但正如@JohnathanLeffler所说,您有一些可以通过检查发现的错误。)
答案 2 :(得分:0)
让你知道更多细节,同时期望一些指针来解决你的问题,比如 - 哪个平台,编译器,编译器选项(是否打开任何优化等等)。
如果在未对齐的存储器地址上执行了一些存储器读或写操作,则抛出AFAIK'BUS ERROR'。但是最近没有看到总线错误,因为现代系统(编译器,平台)已经消除了对数据的任何硬性和快速需求以与特定边界对齐,除非它是正在开发代码的专用硬件架构。
现在针对这个特殊问题 -
(我假设它在Linux下),所以使用-g开关为gcc构建代码。
然后尝试使用gdb [您的可执行文件]。这应该告诉错误后代码退出的函数/代码行。查看该函数/ line /之前和之后的代码!
我的猜测(疯狂猜测)是您将样本写入/读取到pcm [i]的位置,因为它似乎是FLAC__int32类型的用户定义数据。但是嘿,这可能是遥远的,直到你自己调试。古德勒克!