Results 1 to 15 of 15

Thread: QAudioOutput in a QThread - problem.

  1. #1
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation QAudioOutput in a QThread - problem.

    Hi there!

    This is my first post on this great forum
    I found here many good answers and examples. But I cannot find solution for this one...

    I'm writing a quite big project with network programming and i have to play sound when my client connect and send some message to server.
    First step which i made was to create an object of my class which encapsulate QAudioOutput and just use function (QAudioOutput::start()) from examples which shows how to play sound using this Qt class. That works ok but when client send more same message in short time not all sounds were played. So i decided to use QThread subclass to hear multiple sounds at once. Unfortunately the sound wasn't heard.

    Can someone could help me with this problem?

    I post here en example of my other short app in which i tried join thread and QAudioOutput. Same result - i don't hear sound as if file wasn't played or not send to audio device from the thread.

    Qt Code:
    1. PlayFile::PlayFile(QString fn) : format(0), info(new QAudioDeviceInfo(QAudioDeviceInfo::defaultOutputDevice())), ao(0), file(0) {
    2. filename = fn;
    3. setQuality();
    4. printSupportedFeatures();
    5. // setObjectName("mythread");
    6. }
    7.  
    8. void PlayFile::setQuality() {
    9. if(!format)
    10. format = new QAudioFormat();
    11. format->setCodec("audio/pcm");
    12. format->setChannels(1);
    13. format->setFrequency(11025);
    14. // format->setFrequency(22050);
    15. format->setSampleSize(8);
    16. }
    17.  
    18. void PlayFile::sPlayFile() {
    19. // qDebug("Odtwarzanie dzwieku...");
    20. if(!file) {
    21. file = new QFile(filename);
    22. file->open(QIODevice::ReadOnly);
    23. }
    24. if(!ao)
    25. ao = new QAudioOutput(*info, *format);
    26. ao->start(file);
    27. qDebug("error: %d", ao->error());
    28. }
    29.  
    30. void PlayFile::run() {
    31. // qDebug("Watek %s", this->objectName().toStdString().c_str());
    32. sPlayFile();
    33. // qDebug("buffer %d", ao->bufferSize());
    34. // qDebug("bytes %d", ao->bytesFree());
    35. }
    36.  
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication a(argc, argv);
    40. PlayFile* pf = new PlayFile("./alarm.wav");
    41. // using this slot to start thread doesn't produce sound on output.. why?
    42. pf->start();
    43. // when i use this fuction (slot) i hear the sound
    44. // pf->sPlayFile();
    45. MainWindow w;
    46. w.show();
    47. return a.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    Please help me to correct this problem.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    I have a theory, but I am not sure its correct.
    But we can test it easy:
    put 2 seconds sleep in your run function before sPlayFile();.

    Do you get the sound played then?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAudioOutput in a QThread - problem.

    I would probably first check if the audio format is supported by your backend.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    I would probably first check if the audio format is supported by your backend.
    Well, he said he gets sound when he plays the function outside the thread... so...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAudioOutput in a QThread - problem.

    Oh, I missed that... After reading the post again carefully I have a strong doubt using threads would solve the problem at all. If the audio output is busy playing one sound, pushing more data to it from another thread won't cause two sounds to play simoultaneously, one needs a mixer for that. Otherwise the second sound will play only after the first one will have finished playing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    Hi,

    Maybe you can stop the current playing sound and reproduce the new sound.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    If the audio output is busy playing one sound, pushing more data to it from another thread won't cause two sounds to play simoultaneously, one needs a mixer for that.
    True, but that is still not his problem, since he only plays the sound once, over a thread.
    I have a suspition, that it has to do with the fact the thread runs (and probably finishes) before QApplication::exec() is called.
    My guess is, that there are some events that need the QApplication event loop... but as I said, this is only a guess.
    This is why I suggested the sleep.
    Another way to test it would be to start the thread after QApplication::exec() has been called.

    When this is solved, the problem with multiple sounds on the sound system with out a mixer problem will still be there though...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAudioOutput in a QThread - problem.

    Thanks to all for your responds.

    Yes it's true that it has something to do with main event loop.
    I check in debug and I hear a sound only when QApplication::exec() was processing.
    So maybe I need do something with local event loop.
    I'll still searching though.

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    So maybe I need do something with local event loop.
    I thought as much.
    Nothing special to do, just start the thread after QApplication::exec() has been called, from your widget for example.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QAudioOutput in a QThread - problem.

    @high_flyer:
    Thanks for your replies.
    Pause between calling my function didn't change anything.
    I tried to play a sound in separate thread when i press the QButton widget but it doesn't help neither.

    I really don't know what to do next.
    I start to thinking that is not possible to play sound in different threads than GUI's one...

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    Have a look at QEventLoop, maybe you could do something with that.

    But I think playing sound in threads is more a design problem, if you design properly, you probably wont need this.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAudioOutput in a QThread - problem.

    Quote Originally Posted by high_flyer View Post
    True, but that is still not his problem, since he only plays the sound once, over a thread.
    I don't think so, look:
    That works ok but when client send more same message in short time not all sounds were played.
    Sound is played asynchronously so there is no benefit in doing that in a thread. You can run the event loop of the thread and the sound will probably play fine but it won't change the fact that sounds won't be able to mix. It could even crash the application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioOutput in a QThread - problem.

    You can run the event loop of the thread and the sound will probably play fine but it won't change the fact that sounds won't be able to mix.
    Yes, as I said before, I agree with that too.
    These were to separate problems - one getting the events through, the other - parallel audio with out mixing.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAudioOutput in a QThread - problem.

    So. Somebody now have a solution for use QAudioOutput within a QThread ?
    Somebody speek about Events but how and which events ?
    Please somebody help me.

    Daniel

  15. #15
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAudioOutput in a QThread - problem.

    Hi.
    I write for other people, because i already found a solution.
    The key is : from the thread you create a QAudioOutput and start it, you need to enter the event loop, and this is only possible after executing exec().
    So after start you QAudioOutput, in the run() function you need to write :
    exec()
    This is new for me, because i come from the World of MFC, and there for calling to IDirectSound, where i engage a sound without need to enter a event loop.
    In fact, now i transport everything i do before with MFC, (for Windows), a project from before 10 years ago, to QT.
    I am already 8 years Linux-user, but i want compatibility with every operativ system.
    Greatings. Daniel

Similar Threads

  1. QAudioOutput buffer underrun
    By BartBlackMagic in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2010, 17:44
  2. Replies: 1
    Last Post: 29th July 2010, 13:35
  3. QAudioOutput help
    By XavierQT in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 10:35
  4. Qthread Problem
    By sroger in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2009, 07:35
  5. QThread problem
    By MrShahi in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2008, 12:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.