【发布时间】:2011-07-10 16:07:50
【问题描述】:
我正在尝试读取包含原始音频的文件并使用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 可以完美运行。
【问题讨论】:
-
你的巴士有问题。
-
错误可能在这一行:'ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, 20000);'我似乎无法确定,因为我得到的只是“总线错误”
-
@Kerrek SB:我尝试逐块注释代码。错误是在包含
FLAC__stream_encoder_process_interleaved的块中。您是否看到任何明显的错误? -
不,但我不知道那个图书馆。谁给你的错误?它似乎不是来自你自己的代码,所以你没有做足够的错误检查,我会说。如果你注释掉那一行,错误会消失吗?
-
您是否正在使用 PowerPC 机器在 MacOS X 上工作?该库由用户“benski”编译,但您没有源代码。这只是一个警告;这意味着您将无法单步执行库代码并查看源代码行。这可能无关紧要,除非 SIGBUS 出现在库函数中。