在QT中,我可以将音频输入定义为:
m_audioInput = new QAudioInput(m_Inputdevice, m_format, this);
m_input = m_audioInput->start();
在我的应用程序中,我想使用麦克风并从声卡读取。
现在,如果我想看看有多少字节可以从音频缓冲区中读取,我使用:
qint64 len = m_audioInput->bytesReady();
看起来len
是采样率和每个样本的位数的函数。
我的问题是,有没有办法控制len
,而不改变采样率?换句话说,我想控制音频卡,使其以较短的块读取数据并发出就绪信号。
答案 0 :(得分:0)
您可以通过设置正确的格式参数(例如频率,样本大小)来控制声卡。为此,您需要使用QAudioFormat
类。
除此之外,没有其他方法可以从Qt控制声卡。
reference的示例:
QFile outputFile; // class member.
QAudioInput* audio; // class member.
outputFile.setFileName("/tmp/test.raw");
outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
QAudioFormat format;
// set up the format you want, eg.
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format)) {
qWarning()<<"default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audio = new QAudioInput(format, this);
QTimer::singleShot(3000, this, SLOT(stopRecording()));
audio->start(&outputFile);
// Records audio for 3000ms