我正在研究將向某些設備提供音頻輸入的應用程序。該設備預計音頻輸入以原始音頻數據流(16位,48kHz)的形式提供。因此,無論波形文件(8位,16位,24位,32位等)中的音頻數據的格式如何,我都想從WAV文件中提取原始音頻數據。我計劃爲此使用libsndFile庫。我修改libsndfile的C++代碼示例如下圖所示:使用C++中的libsndfile從WAV文件中提取原始音頻數據
#include "stdafx.h"
#include <sndfile.hh>
static void create_file (const char * fname, int format, const short* buffer,const unsigned int& len)
{
// file ;
int channels = 1 ; //A Mono wave file.
int srate = 48000 ;
printf ("Creating file named '%s'\n", fname) ;
SndfileHandle file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ;
int x = file.write (buffer, len) ;
}
static void read_file (const char * fname)
{
SndfileHandle file ;
file = SndfileHandle (fname) ;
const unsigned int uiBuffLen = file.channels() * file.frames();
short* data = new short [uiBuffLen] ;
memset(data,0x00,uiBuffLen);
int x = file.command(SFC_SET_SCALE_FLOAT_INT_READ, (void*)data, uiBuffLen);
file.read (data, uiBuffLen) ; //Read the audio data in the form of 16 bit short integer
//Now create a new wave file with audio data in the form of 16 bit short integers
create_file ("ConvertedFile.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16,data, (const unsigned int&)uiBuffLen) ;
//Now fill a buffer containing audio data and dump it into a file so that the same can be fed to a device expecting the raw audio data
unsigned char* bytBuffer = new unsigned char[uiBuffLen*2];
memset(bytBuffer, 0x00, uiBuffLen*2);
file.readRaw(bytBuffer, uiBuffLen*2);
FILE * pFile;
pFile = fopen ("RawAudio.dat","w");
if (pFile!=NULL)
{
fwrite(bytBuffer, 1, uiBuffLen*2, pFile);
fclose (pFile);
}
delete [] data;
delete [] bytBuffer;
}
int _tmain(int argc, _TCHAR* argv[])
{
//The sample file is a Mono file containing audio data in float format.
const char * fname = "MonoWavFile.wav" ;
read_file (fname) ;
return 0;
}
好了,上面的代碼可能看起來很可怕,但我只是在尋找此刻的想法。我使用了一個文件「MonoWaveFile.wav」,它是一個單聲道文件,具有32位浮點值的音頻數據。 我使用libsndfile庫創建一個新文件「ConvertedFile.wav」。該文件具有16位PCM格式的音頻數據。我在媒體播放器中播放這個文件,我發現轉換已經正確完成。
然後,我創建另一個文件「RawAudio.dat」來只保存音頻數據,我可以使用它來將音頻輸入饋送到設備。該文件已創建,當我將其發送到設備時,音頻根本不適用。這表明我正在做出可怕的錯誤。任何人都可以讓我知道我在做什麼錯?我從來沒有做過這樣的事情,所以我會很感激,如果我得到任何幫助。
我已經共享此鏈路上的Python和C++代碼:http://stackoverflow.com/questions/43425176/plot-fft-of-wav-file-using-c/43447735#43447735 –
見簡單的4行示例[這裏](https://github.com/erikd/libsndfile/blob/master/examples/sndfilehandle.cc#L52)。 – bcattle