出于我的目的,我想以原始格式(仅样本)、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 中的声音有关。
更多细节是 Windows 和 Linux 中的静音是不同的。我尝试了很多事情,包括交换字节,尽管我在两个平台上都设置了小端。
现在,我对 alsa 配置有疑问。是否有任何遗漏的设置?
你觉得不使用QAudioInput直接录语音会不会好一些?
最佳答案
语音是UnSignedInt,但是sample value有负值和正值!看来你在捕获方面遇到了麻烦。将 QAudioFormat::UnSignedInt
更改为 QAudioFormat::SignedInt
。
关于c++ - 在 linux 中使用 QAudioInput 录制并在 windows 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494638/