我在将短缓冲区写入 HD 上的 wav 文件时遇到问题。
我遵循了一些教程,但它们都提供了不同的方法。
无论如何,这就是我实现它的方式,但由于某种原因它不起作用。当我尝试打印出 outfile
的结果时,我得到一个 0
,并且没有任何内容写入磁盘。
此外,我尝试了不同的路径,有时有文件名,有时没有文件名。
更新: 当我将路径更改为仅文件名时(例如 hello.wav
而不是 ~/Documents/hello.wav
), 打印出 outfile
返回一个内存位置,例如 0x2194000
。
void gilbertAnalysis::writeWAV(float * buffer, int bufferSize){
SF_INFO sfinfo ;
sfinfo.channels = 1;
sfinfo.samplerate = 44100;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
const char* path = "~/Documents/hello.wav";
SNDFILE * outfile = sf_open(path, SFM_WRITE, &sfinfo);
sf_count_t count = sf_write_float(outfile, &buffer[0], bufferSize) ;
sf_write_sync(outfile);
sf_close(outfile);
}
最佳答案
来自 libsndfile 文档:
On success, the sf_open function returns a non-NULL pointer which should be passed as the first parameter to all subsequent libsndfile calls dealing with that audio file. On fail, the sf_open function returns a NULL pointer. An explanation of the error can obtained by passing NULL to sf_strerror.
如果 outfile 是 0x2194000 且不为空,那么您可能正确打开了文件。
尝试使用 sf_strerror() 查看在提供完整文件路径而不仅仅是文件名时出现的错误。
关于c++ - 使用 libsndfile 写入 wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22530515/