Results 1 to 4 of 4

Thread: Questions about QAudioOutput, QIODevice. Play wav file

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 20:04.

Similar Threads

  1. QAudioOutput + FFMPEG
    By Biberbaer in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2015, 18:28
  2. QUdpSocket & QAudioOutput & QIODevice
    By Mkhitar in forum Qt Programming
    Replies: 0
    Last Post: 3rd October 2013, 14:50
  3. QAudioOutput & QAudioFormat::Float
    By Biberbaer in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2012, 00:28
  4. QAudioOutput + not UI thread
    By medved6 in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2010, 20:11
  5. QAudioOutput help
    By XavierQT in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 11: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.