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

标签 c++ qt audio alsa

出于我的目的,我想以原始格式(仅样本)、8kHz、16 位(小端)和 1 channel 录制声音。然后,我想将这些样本传输到窗口并使用 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 中捕获的声音: captured sound in Linux

在 Windows 中捕获的声音: captured sound in Windows

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

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

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

最佳答案

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

关于c++ - 在 linux 中使用 QAudioInput 录制并在 windows 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494638/

相关文章:

c++ - 将现有的(行、列)C++ 模型与 QtQuick(网格、 TableView )一起使用

jar - 处理Jar文件的最小库执行

c++ - QtGui4.lib(QtGui4.dll) : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

c++ - 在 Netbeans 中配置 Opencv

c++ - Qt:QSqlRelationalTableModel 引用不存在的外键

c++ - OpenCL 计算与顺序算法的输出不匹配

c++ - 用 QProcess 包装 SFTP

python - 在 QtCore.Qt.CrossPattern 上设置图案颜色和线条粗细

node.js - 如何使用Node.js在本地主机上播放IBM Watson Text To Speech音频

audio - 比较两种声音的频率内容的最快,最简单的算法