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

C++ 使用QAudioInput在linux中录制并在windows中播放,c++,qt,audio,alsa,C++,Qt,Audio,Alsa,出于我的目的,我想以原始格式(仅样本)、8kHz、16位(小端)和1个通道录制声音。然后,我想将这些示例传输到windows,并使用QAudioOutput进行播放。所以我有两个独立的程序:一个用于用QAudioInput录制语音,另一个提供一个包含一些示例的文件,然后我用QAudioOutput播放它。下面是我创建QAudioInput和QAudioOutput的源代码 //Initialize audio void AudioBuffer::initializeAudio() { m_f

出于我的目的,我想以原始格式(仅样本)、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中的沉默是不同的。我尝试了很多事情,包括交换字节,尽管我在两种平台上都设置了小的endian

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


您认为如果我不使用QAudioInput直接录制语音会更好吗?

语音是无符号的,但采样值既有负值也有正值!看来你在抓捕方面遇到了麻烦。将
QAudioFormat::UnSignedInt
更改为
QAudioFormat::SignedInt

您能检查两种情况下
是否支持格式设置吗?输入和输出均为真!删除计算
nearestFormat
代码的两行,并将
abort()
放在那里,然后在linux上录制一个新文件,将文件移到windows,然后重试。确保在应用了更改的情况下,在这两个版本上都使用了新版本,并且正在播放从linux机器传输的新捕获的文件。