【问题标题】:How to write raw audio data to a file captured using Alsa Driver C++如何将原始音频数据写入使用 Alsa Driver C++ 捕获的文件
【发布时间】:2020-11-12 21:19:41
【问题描述】:

我正在尝试使用 Alsa 驱动程序将原始音频数据写入特定文件。

代码下方:

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();

【问题讨论】:

    标签: c++ audio alsa raw-data


    【解决方案1】:

    编写包括 raw 在内的多种不同音频格式的一种方法是使用 sox 音频库 (libsox)。 gtkIOStream 中内置了 C++ ALSA 类和 libsox 类。

    捕获到原始音频文件的示例来自ALSACapture.C source code here。基本概念是打开捕获设备和原始音频文件,然后循环捕获音频并使用 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 的好处是您不受限于原始音频格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2014-10-01
      相关资源
      最近更新 更多