Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record in linux with QAudioInput and play it in windows

Tags:

c++

qt

audio

alsa

For my purpose, I want to record sounds in raw format(only samples), 8kHz, 16bit(little endian) and 1 channel. Then, I would like to transfer those samples to the windows and play it with QAudioOutput. So I have two separated programs: one for recording voice with QAudioInput, and other one gives a file which is contained some samples, then I play it with QAudioOutput. Below is my source code for creating QAudioInput and QAudioOutput.

//Initialize audio
void AudioBuffer::initializeAudio()
{
  m_format.setFrequency(8000); //set frequency to 8000
  m_format.setChannels(1); //set channels to mono
  m_format.setSampleSize(16); //set sample sze to 16 bit
  m_format.setSampleType(QAudioFormat::UnSignedInt ); //Sample type as usigned integer sample
  m_format.setByteOrder(QAudioFormat::LittleEndian); //Byte order
  m_format.setCodec("audio/pcm"); //set codec as simple audio/pcm

  QAudioDeviceInfo infoIn(QAudioDeviceInfo::defaultInputDevice());
  if (!infoIn.isFormatSupported(m_format))
  {
      //Default format not supported - trying to use nearest
      m_format = infoIn.nearestFormat(m_format);
  }

  QAudioDeviceInfo infoOut(QAudioDeviceInfo::defaultOutputDevice());

  if (!infoOut.isFormatSupported(m_format))
  {
     //Default format not supported - trying to use nearest
     m_format = infoOut.nearestFormat(m_format);
  }
  createAudioInput();
  createAudioOutput();
}

void AudioBuffer::createAudioOutput()
{
  m_audioOutput = new QAudioOutput(m_Outputdevice, m_format, this);
}

void AudioBuffer::createAudioInput()
{
   if (m_input != 0) {
     disconnect(m_input, 0, this, 0);
     m_input = 0;
   } 

   m_audioInput = new QAudioInput(m_Inputdevice, m_format, this);

}

These programs work well in windows and Linux separately. However, it has a lot of noise when I record a voice in Linux and play it in windows.

I figure out captured samples in windows and Linux are different. First picture is related to captured sound in Linux and second one for windows.

Captured sound in Linux: captured sound in Linux

Captured sound in Windows: captured sound in Windows

A bit more on details is that silence in Windows and Linux is different. I tried many things including swapping bytes, even though I set little endian in both platforms.

Now, I am in doubt about alsa configuration. Are there any missed settings?

Do you think it will be better if I record voice directly without using QAudioInput?

like image 627
Ari Avatar asked Oct 20 '22 02:10

Ari


1 Answers

The voice is UnSignedInt, but sample value has both negative and positive value! It seems you had trouble in capturing. Change QAudioFormat::UnSignedInt to QAudioFormat::SignedInt.

like image 125
Arman Avatar answered Nov 03 '22 19:11

Arman