PDA

View Full Version : 'Pop' at start of sound playback using QAudioOutput



semajnosnibor
15th March 2012, 21:33
I'm having an issue playing a simple 'wav' file using QAudioOutput. Previously I used the QSound::PlaySound() to play the same wav and it worked fine, however I wanted a bit more control (mainly stop, pause, resume). However when I play the sound (any wav file) using QAudioOutput I get a 'pop' noise at the start of every sound. This did not occur with the QSound::PlaySound().

I've tried tweaking the QAudioFormat, but no luck there. I did basically just copy the code from the QAudioOutput help documents, with the exception that I determine the QAudioFormat by the WAV file's header. Note that I've also tested setting the QAudioFormat manually so I'm fairly confident the error is not there.

So basically when I want to play my wav file, I create a new SoundPlayer object, passing it the filename as a QString. The sound is played once the object is constructed.

SoundPlayer Constructor



SoundPlayer::SoundPlayer(QString fileName, QWidget *parent) :
QWidget(parent)
{
this->inputFile.setFileName(fileName);
this->inputFile.open(QIODevice::ReadOnly);

QAudioFormat format = getWaveFormat(fileName);
format.setCodec("audio/pcm");

this->audio = new QAudioOutput(format, this);

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

//play the sound file 'inputFile'
audio->start(&inputFile);

}

semajnosnibor
16th May 2012, 23:21
Well once again with this forum I find myself having to answer my own question. Anyway after finally going back to look at this problem again I realized my previous mistake. I was reading the header of the sound file to get the format, that part was correct. However once I passed the QFile onto QAudioOutput I was starting at byte 0. So it was trying to interpret all the header data as sound samples, hence the 'pop'. Anyway the actual 'DATA' starts a bit after the header, so in the constructor I now seek ahead to the data marker 'data' and then skip over the next 4 bytes after that (the subchuck2 size info). And voila, no more popping!

cenime
13th November 2013, 19:08
Hi semajnosnibor. I'm having the same issue with the 'pop' sound. I'm not that experienced with Qt, so do you still have the code to share it? just the part where you modifies the constructor to seek the data marker. This will be very helpful for me.

Thanks!

semajnosnibor
13th November 2013, 20:05
Took some digging but I found it. Hopefully this works for you. The modification was actually within the 'getWaveFormat' function. It basically reads all the necessary format info from the wave file header, and then sets the play start position of the file.
Basically when you want to play the file, create a new object of SoundPlayer with the filename as an argument. The sound will play automatically, and should emit the signal 'soundDone()' when done.

ex:

SoundPlayer *sPlay = new SoundPlayer(fName, this); //sound will start playing automatically
connect(sPlay, SIGNAL(soundDone()), SLOT(soundDone())); //delete the sPlay object when done

Probably the SoundPlayer code could be optimized quite a bit. I only focussed on getting it working, then when I did I happened to move on to another project (typical!).

Cheers,
James

rsafir
20th June 2014, 08:31
Hello. Can you upload the math.h/.cpp too? Thank you a lot!!