Results 1 to 4 of 4

Thread: Questions about QAudioOutput, QIODevice. Play wav file

  1. #1
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X

    Unhappy Questions about QAudioOutput, QIODevice. Play wav file

    Hello everybody

    I'am writing a program that will play wav files and add some sound effects.
    And now I want to ask you some questions about problems, that I faced.

    First of all I tried to use write method from QIODevice
    Qt Code:
    1. QAudioDevice * m_audioOutput;
    2. QIODevice * m_output;
    3. QFile file;
    4.  
    5. ....
    6. file.seek(44);
    7. buff = file.read(44000);
    8.  
    9. m_audioOutput = new QAudioDevice(format,this);
    10. m_output = m_audioOutput->start();
    11.  
    12. int size = m_output->bytesToWrite(); //size =0
    13. int bytesWritten = m_output->write(buff.data(),buff.size()); // bytesWritten = 8192
    To copy to clipboard, switch view to plain text mode 

    Why was written only 8192 bytes, instead of 44000?

    Ok, i sad to myself. It isn't a big problem

    Qt Code:
    1. QQueue<QByteArray> queue;
    2.  
    3. for (int i=0;i<50;i++)
    4. {
    5. queue.enqueue(file.read(8192));
    6. }
    7.  
    8. QObject::connect(m_output,SIGNAL(bytesWritten(qint64)),&loop,SLOT(quit()));
    9. while (!queue.isEmpty())
    10. {
    11. buff = queue.dequeue();
    12. m_output->write(buff.data(),buff.size());
    13. loop.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I heard a small pop from speakers. It was first 8192 bytes written to QIODevice. Then program freezes in the loop.
    Why signal bytesWritten() doesn't stop loop?
    Ok, I said to myself, lets try something else.

    Qt Code:
    1. QQueue<QByteArray> queue;
    2.  
    3. for (int i=0;i<50;i++)
    4. {
    5. queue.enqueue(file.read(8192));
    6. }
    7.  
    8. QObject::connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),&loop,SLOT(quit()));
    9. while (!queue.isEmpty())
    10. {
    11. buff = queue.dequeue();
    12. m_output->write(buff.data(),buff.size());
    13. loop.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Oh yeah! I hear music, but with lags. It was like 8192 bytes played, then silence for about half of a second, then another 8192 bytes was played.

    So I decide to change everything.
    I decide to use timer.

    Qt Code:
    1. #define TIMER_PLAY 30
    2.  
    3. QQueue<QByteArray> queue;
    4.  
    5. int bytesToRead = TIMER_PLAY*44100*4/1000; //bytesToRead - it is how many bytes will play in 30 milliseconds between timerEvent
    6. for (int i=0;i<50;i++)
    7. {
    8. queue.enqueue(file.read(bytesToRead));
    9. }
    10.  
    11. startTimer(TIMER_PLAY,Qt::PreciseTimer); // class Player : public QObject
    12.  
    13. void Player::timerEvent(QTimerEvent *)
    14. {
    15. buff = queue.dequeue();
    16. m_output->write(buff.data(),buff.size());
    17. }
    To copy to clipboard, switch view to plain text mode 

    I hear music! it works. I was happy
    My program was growing bigger and bigger. I add some QThreads and some math calculations. And faced new problem.
    Timer can't gave me exactly 30 milliseconds and now i can hear some annoying lags (milliseconds).
    So i decide to write a post here.

    Could you help me please.
    1. Why I can write only 8192 bytes to QIODevice?
    2. What i'm doing wrong with loops? What should I do to hear music?

    Thank you for your attention.
    Last edited by Smosia; 18th March 2014 at 19:04.

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

    Default Re: Questions about QAudioOutput, QIODevice. Play wav file

    The problems you are having are because of assuming you can stick an infinite amount of water into a finite pipe in an infinitely short amount of time. If you have 100kB of data and the device you are using only buffers 8kB of data then that's the amount of data you can stick into it without overfilling the buffer. You can easily see that if you check the return value of write(). A proper approach is to monitor this value and repeat the write with the remaining data once the device buffer level drops (e.g. when bytesWritten() is emitted).
    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.


  3. #3
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Questions about QAudioOutput, QIODevice. Play wav file

    I tried that approach. I wrote about that in my post. It doesn't work.
    Qt Code:
    1. QQueue<QByteArray> queue;
    2.  
    3. for (int i=0;i<50;i++)
    4. {
    5. queue.enqueue(file.read(8192));
    6. }
    7.  
    8. QObject::connect(m_output,SIGNAL(bytesWritten(qint64)),&loop,SLOT(quit())); //bytesWritten doesn't stop the loop;
    9. while (!queue.isEmpty())
    10. {
    11. buff = queue.dequeue();
    12. m_output->write(buff.data(),buff.size());
    13. loop.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I tried to use method QIODevice::waitForBytesWritten() but doesn't work, because my audio device has no buffer (m_output->bytesToWrite() returns 0)

  4. #4
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Questions about QAudioOutput, QIODevice. Play wav file

    I find method QAudioOutput::setBufferSize(int). Now i can change buffer size. How I could be so blind
    But question is still open. How to make bytesWritten() signal work. It doesn't call after write() ends. What i am doing wrong?

Similar Threads

  1. QAudioOutput + FFMPEG
    By Biberbaer in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2015, 17:28
  2. QUdpSocket & QAudioOutput & QIODevice
    By Mkhitar in forum Qt Programming
    Replies: 0
    Last Post: 3rd October 2013, 13:50
  3. QAudioOutput & QAudioFormat::Float
    By Biberbaer in forum Qt Programming
    Replies: 0
    Last Post: 23rd April 2012, 23:28
  4. QAudioOutput + not UI thread
    By medved6 in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2010, 19:11
  5. QAudioOutput help
    By XavierQT in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 10:35

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.