使用 QAudioInput 在 linux 中录制并在 windows 中播放

Record in linux with QAudioInput and play it in windows

出于我的目的,我想以原始格式(仅样本)、8kHz、16 位(小端)和 1 通道录制声音。然后,我想将这些样本传输到 windows 并使用 QAudioOutput 播放。所以我有两个独立的程序:一个用 QAudioInput 录制声音,另一个给出一个包含一些样本的文件,然后我用 QAudioOutput 播放它。下面是我创建 QAudioInput 和 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);

}

这些程序分别在 windows 和 Linux 中运行良好。但是,当我在 Linux 中录制语音并在 windows 中播放时,它有很多噪音。

我发现 windows 和 Linux 中捕获的样本不同。第一张图片与 Linux 中捕获的声音有关,第二张图片与 windows.

中捕获的声音有关

在 Linux 中捕获的声音:

在 Windows 中捕获的声音:

更多细节是 Windows 和 Linux 中的沉默是不同的。我尝试了很多东西,包括交换字节,尽管我在两个平台上都设置了小端。

现在,我对alsa配置有疑问。是否有任何遗漏的设置?

你觉得不使用QAudioInput直接录语音会不会好一些?

声音是UnSignedInt,但是sample value有负值和正值!看来你在捕获方面遇到了麻烦。将 QAudioFormat::UnSignedInt 更改为 QAudioFormat::SignedInt