Qt的QAudioOutput推模式 [英] Qt QAudioOutput push mode

查看:2424
本文介绍了Qt的QAudioOutput推模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题关于使用QAudioOutput在一个特定的采样率声音输出设备直接写样本。我正在写在每帧的基础上emualates声音芯片,然后获取包含音频样本,我想写信给音频输出的帧的价值缓冲区的仿真器。目前,测试我的音频输出套路,我分配一个巨大的(5分钟),缓冲区将随机数到,像这样:

I've got a question about using QAudioOutput to directly write samples at a specific sample rate to the sound output device. I'm writing an emulator that emualates sound chips on a per-frame basis, and then gets a buffer containing a frame's worth of audio samples, which I would like to write to the audio output. Currently, to test my audio output routine, I allocate a huge (5 minute) buffer to put random numbers into, like so:

标题:

uint16_t *audio_outputBuffer;
uint32_t audio_bytesRemainingToRead;
QAudioOutput *audio_outputStream;
QIODevice *audio_outputDevice;

实施

audio_outputBuffer = (uint16_t *) malloc((96000 * 4) * 300);
int i = 0;

uint16_t *tempAudioBuffer = audio_outputBuffer;
for(i = 0; i < ((96000 * 4) * 150); i++) {
    *tempAudioBuffer = (uint16_t) rand() & 0xFFFF;
    tempAudioBuffer++;
}

audio_bytesRemainingToRead = (96000 * 4) * 300;

接下来,我建立了我的音频设备的一些基本参数:

Next, I set up my audio device with some basic parameters:

// Set up the format
QAudioFormat format;
format.setFrequency(96000); // Usually this is specified through an UI option
format.setChannels(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);

// There's code here to notify the user of inability to match the format and choose an action, which is omitted for clarity

// Create audio output stream, set up signals
audio_outputStream = new QAudioOutput(format, this);

connect(audio_outputStream, SIGNAL(stateChanged(QAudio::State)), this, SLOT(audio_stateChanged(QAudio::State)));

audio_outputDevice = audio_outputStream->start();

然后,在我的计时器滴答例程,它是由一个QTimer在60 FPS叫,我下面code写一个音频数据的块的QAudioOutput的缓冲区:

Then, in my timer tick routine, which is called by a QTimer at 60 FPS, I do the following code to write a 'chunk' of audio data to the QAudioOutput's buffer:

if(audio_outputDevice->isOpen() && audio_outputStream->state() != QAudio::StoppedState) {
    qint64 bytesOfAudioWrittenToDevice = audio_outputDevice->write((char *) audio_outputBuffer, audio_outputStream->periodSize());
    audio_bytesRemainingToRead -= bytesOfAudioWrittenToDevice;
    qDebug() << "Wrote" << bytesOfAudioWrittenToDevice << "bytes of audio to output device. Remaining bytes to read:" << audio_bytesRemainingToRead;
    qDebug() << "Free bytes in audio output:" << audio_outputStream->bytesFree();
}

一旦我开始音频输出过程中,我得到的控制台上的输出如下:

Once I start the audio output process, I get the following output on the console:

Current audio state: 3 Error: 0 
Wrote 2048 bytes of audio to output device. Remaining bytes to read: 115197952 
Free bytes in audio output: 6144 
Current audio state: 0 Error: 0 
Wrote 2048 bytes of audio to output device. Remaining bytes to read: 115195904 
Free bytes in audio output: 4096 
Wrote 2048 bytes of audio to output device. Remaining bytes to read: 115193856 
Free bytes in audio output: 2048
Wrote 2048 bytes of audio to output device. Remaining bytes to read: 115191808 
Free bytes in audio output: 0 (This and the above line is repeated forever)

对我来说,它看起来像QAudioOutput没有刷新它的内部缓冲区声卡,这与整个无声音出来我的电脑的事情随之而来。

To me, it looks like QAudioOutput isn't flushing it's internal buffer to the sound card, which goes along with the entire "no sound coming out of my computer" thing.

什么会导致这个问题,我怎么能解决这个问题?

What would cause this issue, and how could I fix it?

(顺便说一句,我编译我的code反对的Qt 4.8.1,在Mac OS X 10.7.4。)

(By the way, I'm compiling my code against Qt 4.8.1, on Mac OS X 10.7.4.)

感谢您的任何答案。

推荐答案

前期只想指出:这是的的Qt的错误。为什么?答案是,在WAV规范,8位采样总是无符号的,而16位采样总是签署。任何其他组合不起作用。这是相关的设备,框架不能做任何事情。

Upfront just wanna point out: This is not a Qt bug. Why? The answer is that in the WAV spec', 8-bit samples are always unsigned, whereas 16-bit samples are always signed. Any other combination does not work. This is device related, the framework can not do anything about it.

因此​​,这将无法工作,因为您已设置16位采样大小和无符号整数格式。
是的,解决的办法是:你必须设置样品类型签订了16位分辨率:

So this will not work because you have set 16 bit sample size and unsigned integer format. And yes, the solution is: you have to set the sample type to signed for 16-bit resolution:

format.setSampleType(QAudioFormat::SignedInt);

相反的8位采样,你将不得不把

Inversely for 8-bit samples you would have to put:

format.setSampleType(QAudioFormat:UnsignedInt);

另外这非常类似的问题(相同的问题,但具有8位)显示了,这不是Qt中使用符号或无符号的样本的一个特别的问题,但它是样品的尺寸和类型,重要的组合(音频设备,而不是Qt的;)<一个href=\"http://stackoverflow.com/questions/15594180/qaudiooutput-in-qt5-is-not-producing-any-sound\">QAudioOutput在QT5不产生任何声音

恕我直言,事实上,Qt不采取强制格式正确处理这些案件保健是一个缺陷,但不是缺乏functionnality。

IMHO the fact that Qt does not take care of handling these cases by forcing the correct format is a flaw but not a lack in functionnality.

您可以了解更多有关这本页面的注释部分:的https:/ /ccrma​​.stanford.edu/courses/422/projects/WaveFor​​mat/

You can learn more about this in the notes section of this page: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

这篇关于Qt的QAudioOutput推模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆