user2203624
user2203624

Reputation: 11

QAudioOutput in Qt5 is not producing any sound

I’m working under kubuntu 12.10 and developping an application into which i need to generate some sound into a QIODevice, then play it with QAudioOutput.

I’ve read all the litterature around speaking of how to properly do that, and I think to have done so.

So far I’ve done :

QVector <double> * soundData = SoundGenerator::getSound();

soundBuffer->open(QIODevice::ReadWrite);
QDataStream writeStream(soundBuffer);
foreach(double d, *soundData) {
    char value = d * (2 << 7);
    //  qDebug() << "Value : " << (short int)value;
    writeStream << value;
}

QAudioFormat format;
// Set up the format, eg.
format.setSampleRate(SoundGenerator::getAudioSampleRate());
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());

audio = new QAudioOutput(format, this);
if (audio->error() != QAudio::NoError) {
    qDebug() << "Problem playing sound";
}

connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(aboutToFinish(QAudio::State)));

I have also a call to audio->start(soundBuffer)

— from another slot I do not have any error in the initialization of the QAudioOutput

And I have NO SOUND AT ALL (all other applications have sound, and I’m porting a Qt4 app to Qt5, in Qt4 everything is ok with Phonon)

The aboutToFinish slot is called at the beggining with ActiveState as state, and NoError when calling QAudioOutput::error, but it’s not called anymore, even if waiting far more than the expected generated sound duration.

The sound generation process is not to be put in question, it has been tested by writing wav files, and it works.

Moreover, I have built the multimedia example from Qt’s sources, when it comes to pure audio there is no output (for example in the sprectrum example), on another hand, video plays with the sound perfectly.

Is there any known issue concerning that ? Is that a bug ? Am I doing something wrong ?

Thanks in advance ;)

Upvotes: 1

Views: 4515

Answers (2)

sartem
sartem

Reputation: 11

Try to add:

QEventLoop loop;

loop.exec();

Upvotes: 1

Tchakabam
Tchakabam

Reputation: 505

This does not work because you have set 8 bit sample size and signed integer format.

SOLUTION: You have to set the sample type to unsigned for 8-bit resolution:

format.setSampleType(QAudioFormat::UnsignedInt);

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.

So for 16-bit samples you would have to put:

format.setSampleType(QAudioFormat:SignedInt);

(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).

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

And also the solution to this very similar question (same problem but with 16-bit): Qt QAudioOutput push mode

Upvotes: 4

Related Questions