0

I'm trying to write raw audio data to a specific file, using Alsa Driver.

Below the code:

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();
xKobalt
  • 1,498
  • 2
  • 13
  • 19

1 Answers1

0

One approach to write many different audio formats including raw is to use the sox audio library (libsox). C++ ALSA classes and libsox classes are built into gtkIOStream.

An example for capturing to a raw audio file comes from the ALSACapture.C source code here. The basic concept is to open the capture device and the raw audio file, then loop through capturing audio and writing to file using 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);
  }

The nice thing about using libsox is that you aren't limited to the raw audio format.

Matt
  • 509
  • 2
  • 14