我是音频编程新手,使用Libsndfile编写16位wav文件时遇到了一些麻烦。因此,我显示代码的部分来自检索IFFT数据之后。目前,我正在设法创建" test.wav"但是大小为0,不包含任何信息;一个垃圾文件。任何帮助都会受到天文数字的赞赏,谢谢!
提取的相关代码:
short audioStream[65536];
float ifftData[65536];
//Represent float data as short
for(int i = 0; i < 65536; i++) {
audioStream[i] = ifftData[i] * 32767;
}
const char *fname = "test.wav";
//SNDFILE
create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
void create_file (const char * fname, int format) {
SNDFILE *file2;
SF_INFO x;
x.samplerate = 44100;
x.frames = 65536;
x.format = format;
x.channels = 1;
file2 = sf_open(fname,SFM_WRITE,&x);
sf_write_short(file2, audioStream, 65536);
sf_close(file2);
}
编辑:添加了有关在sf_open()和sf_write_short()上进行的测试的其他信息。两者== True
if(!file2) {
std::cout << "WAV FILE IS NOT ABLE TO OPEN" << std::endl;
}else if(file2){
std::cout << "WAV FILE OPENED" << std::endl;
}
if(!sf_write_short(file2, audioStream, 65536)){
std::cout << "ERROR WRITING TO WAVE" << std::endl;
}else if(sf_write_short(file2, audioStream, 65536)){
std::cout << "WROTE AUDIOSTREAM TO WAVE" << std::endl;
}