我正在制作一个程序,从麦克风录音,然后使用 libsndfile 将其编码为 OGG 文件。
大约一个月前,我制作了这个程序的控制台版本,只是为了确保录制和编码功能良好。现在,当我开始将此程序作为窗口应用程序时,我发现唯一错误的是编码为 ogg 的函数。
这不是编译器或链接器错误,而是运行时错误。当我调用 sf_format_check 函数时,它返回 false,因此输出文件的参数有问题。所以我开始手动检查它是否在 sf_format_check 函数中并且一切都正确。但是当我编译旧的控制台版本时,一切都正常了。
所以这是我的问题这种行为的原因是什么?
这是我的函数。
static void encodeOgg (const char *infilename, const char *outfilename, int filetype)
{
static short buffer [BUFFER_LEN] ;
SNDFILE *infile, *outfile ;
SF_INFO sfinfo,sf_in ;
int readcount ;
fflush (stdout) ;
sf_in.samplerate=SAMPLE_RATE;//44100
sf_in.channels=NUM_CHANNELS;//1
sf_in.format=SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
if (! (infile = sf_open (infilename, SFM_READ, &sf_in))){
error("Could not open output file") ;
exit (1) ;
}
sfinfo = sf_in;
sfinfo.format = filetype ;//SF_FORMAT_OGG | SF_FORMAT_VORBIS
if (! sf_format_check (&sfinfo)){ //Here's the place where function exits
sf_close (infile) ;
error("Invalid encoding\n") ;
exit (1) ;
}
if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))){
error("Error : could not open output file") ;
exit (1) ;
}
while ((readcount = sf_read_short (infile, buffer, BUFFER_LEN)) > 0)
{
sf_write_short (outfile, buffer, readcount) ;
}
sf_close (infile) ;
sf_close (outfile) ;
return ;
}
最佳答案
将 sf_in 中的每个值分配给 sfinfo,而不是使用直接分配结构 (sfinfo=sf_in;)
sfinfo.sf_count_t=sf_in.sf_count_t;
...
sfinfo.seekable=sf_in.seekable;
typedef struct
{ sf_count_t frames ; /* Used to be called samples. */
int samplerate ;
int channels ;
int format ;
int sections ;
int seekable ;
} SF_INFO ;
关于c - 使用 VS2010 libsndfile 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347051/