2015-11-03 58 views
1

我遇到了在OSX上製作基於Libsndfile的音頻應用程序的奇怪問題。讀取和寫入緩衝區中的數據以奇怪且不可預知的方式被破壞。OSX上的Libsndfile破壞數據

這裏是一個簡短的程序能重現問題對我來說:

#include <iostream> 
#include "sndfile.h" 

int main(int argc, const char * argv[]) 
{ 
float* buffer = (float*)malloc(4096*sizeof(float)); 
SNDFILE* file; 
SF_INFO infos; 
infos.format = 0; 
file = sf_open("ABCD.WAV",SFM_READ,&infos); 
if (file==NULL) 
{ 
    std::cout << "LIBSNDFILE ERROR: " << sf_strerror(file) << "\n"; 
} 

int samplesread=1; 
while (samplesread!=0) 
    { 
     samplesread = sf_readf_float(file,buffer,4096); 
     std::cout << " " << samplesread; 
    } 
std::cout << ""; 
sf_close(file); 
free(buffer); 
return 0; 
} 

程序編譯和運行良好,但與Valgrind的運行它揭示了這樣的錯誤:

==933== Invalid write of size 8 
==933== at 0x56EF4B: _platform_bzero$VARIANT$Merom (in /usr/lib/system/libsystem_platform.dylib) 
==933== by 0x2FDBB: psf_memset (in /opt/local/lib/libsndfile.1.dylib) 
==933== by 0x11E0B: sf_readf_float (in /opt/local/lib/libsndfile.1.dylib) 
==933== by 0x100001323: main (in ./sndfiletest) 
==933== Address 0x873270 is 0 bytes after a block of size 16,384 alloc'd 
==933== at 0x4711: malloc (vg_replace_malloc.c:296) 
==933== by 0x100001287: main (in ./sndfiletest 

感謝您的任何提前幫助 -T

回答

0

您的wav文件可能是立體聲。那麼你的緩衝區大小需要是4096 * 2