![](/img/trans.png)
[英]How to Properly Write Received UDP Audio Data to ALSA with C++ on Raspberry Pi
[英]How to write raw audio data to a file captured using Alsa Driver C++
我正在嘗試使用 Alsa Driver 將原始音頻數據寫入特定文件。
代碼下方:
ofstream binaryFile ("file.raw", ios::out | ios::binary);
if(inputData==NULL)
{
inputData = (uint8_t *)malloc(sizeOfDataInBytes);
}
while (audio_en)
{
snd_pcm_sframes_t numAudioFramesRead = snd_pcm_readi(ndi_AudioSupport.m_captureHandle,inputData,
ndi_AudioSupport.m_numAudioFramesPerVideoFrame);
if(numAudioFramesRead > 0)
{
binaryFile.write ((char*)inputData, sizeof (inputData));
}
}
binaryFile.close();
編寫包括 raw 在內的許多不同音頻格式的一種方法是使用 sox 音頻庫 (libsox)。 C++ ALSA 類和 libsox 類內置在 gtkIOStream 中。
捕獲到原始音頻文件的示例來自此處的 ALSACapture.C 源代碼。 基本概念是打開捕獲設備和原始音頻文件,然后循環捕獲音頻並使用 Sox 寫入文件:
Capture capture("default"); // open the default ALSA capture device
Sox<int> sox;
int ret=sox.openWrite("audio.raw", fs, capture.getChannels(), pow(2.,(double)snd_pcm_format_width(format)));
while (continue){
capture>>buffer;
sox.write(buffer);
}
使用 libsox 的好處是您不受原始音頻格式的限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.