我需要尽可能快地读入波形文件。我目前正在做的事情如下:
SFINFO sfinfo;
SNDFILE *wavFilefd = sf_open ("mySong.wav", SFM_READ, &sfinfo);
int readBlockSize = 1024*1024; // 1MB
if( sfinfo.frames * 2 < readBlockSize )
{
// actually I don't know where this factor two comes from, but it work's for me
readBlockSize = sfinfo.frames * 2;
}
short tmpSignal[readBlockSize];
int readcount = -1;
std::vector< short > wavVector;
while ((readcount = sf_readf_short (wavFilefd, tmpSignal, readBlockSize)) > 0)
{
++nRead;
wavVector.reserve( (nRead-1) * readBlockSize + nRead);
wavVector.insert( wavVector.end(), tmpSignal, tmpSignal+readcount);
}
sf_close(wavFilefd);
到目前为止,此代码运行良好。
问题:当我将 readBlockSize 的值增加 10 倍,然后尝试读取 115212164 字节的波形文件时,我在执行sf_readf_short
.
从帮助我得到“sf_readf_XXXX 函数返回读取的帧数。除非在读取过程中到达文件末尾,否则返回值应等于请求的帧数。尝试读取超出文件末尾不会导致错误,但将导致 sf_readf_XXXX 函数返回少于请求的帧数,如果已经在文件末尾,则返回 0。”
所以我希望这也适用于 10MB。
感谢您的任何提示。