Results 1 to 7 of 7

Thread: Appending data to the buffer of a QAudioOutput

  1. #1
    Join Date
    Nov 2011
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Appending data to the buffer of a QAudioOutput

    Hi,

    I'm writing an application which plays audio using QAudioOutput.
    The audio is decoded by libavcodec in a separate thread and passed to the main thread in pieces.
    The main thread starts playing the audio that was already decoded and keeps playing while new audio data is appended to the buffer.

    To do so I am using a QBuffer opened in ReadWrite-mode.

    Qt Code:
    1. Buffer = new QBuffer;
    2. Buffer->open(QIODevice::ReadWrite);
    3.  
    4. Output = new QAudioOutput(*Format);
    5.  
    6. Buffer->seek(0);
    7. Buffer->write(Array->data(), Array->length());
    8. Buffer->seek(0);
    9.  
    10. Output->start(Buffer);
    To copy to clipboard, switch view to plain text mode 

    When I append data to the buffer after having called start() I need to reset the position where data is read and written to it's initial value:

    Qt Code:
    1. qint64 ActPos = Buffer->pos();
    2. Buffer->seek(Buffer->size());
    3. Buffer->write(Array->data(), Array->length());
    4. Buffer->seek(ActPos);
    To copy to clipboard, switch view to plain text mode 

    This seems to work as far as I can't hear any disruptions in the played audio, but I doubt that this is the optimum method.

    How does QAudioOutput manage this buffer?
    Does it grab the bytes one by one or are they copied to the audio memory in chunks?
    Does it matter if the buffer isn't available for a short moment e.g. while I am writing to it?

    Thanks in advance for your answers!

  2. The following user says thank you to PLM for this useful post:

    suspiria (19th March 2012)

  3. #2
    Join Date
    Nov 2011
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Appending data to the buffer of a QAudioOutput

    I also noticed some strange behaviour of QAudioOuput's processedUSecs().
    This variable seems to be updated in jumps of 40ms. Changing the notify intervall had no effect on it.

    Why should one use a 64-bit integer to store multiples of 40ms?
    Might that be a bug?

    I also noticed some strange behaviour of QAudioOuput's processedUSecs().
    This variable seems to be updated in jumps of 40ms. Changing the notify intervall had no effect on it.

    Why should one use a 64-bit integer to store multiples of 40ms?
    Might that be a bug?

  4. #3

    Default Re: Appending data to the buffer of a QAudioOutput

    In order to have no disruptions due to writing into the buffer you may use external QByteArray:
    Qt Code:
    1. QByteArray data();
    2. QBuffer buffer(&data);
    3. // use buffer ...
    4. data.append("abc", 3); // this extra data is now available in the buffer, buffer's pos doesn't change
    5. // use buffer further more...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Aug 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Smile Re: Appending data to the buffer of a QAudioOutput

    Hello, I also writing an application which plays audio using QAudioOutput. Can you tell me how to achieve it ? Thanks!

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Appending data to the buffer of a QAudioOutput

    You could start by looking at the obvious documentation for QAudioOutput. Then in the Multimedia Examples you could read the Audio Output example. Once you have done that you might be able to ask targeted questions in a thread you have not hijacked.

  7. #6
    Join Date
    Aug 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Smile Re: Appending data to the buffer of a QAudioOutput

    Quote Originally Posted by ChrisW67 View Post
    You could start by looking at the obvious documentation for QAudioOutput. Then in the Multimedia Examples you could read the Audio Output example. Once you have done that you might be able to ask targeted questions in a thread you have not hijacked.
    Thank you!
    There is my main code

    QAudioOutput *m_audioOutput;
    RepletQIODevice *m_audioListenFile;
    m_audioOutput->start(m_audioListenFile);


    RepletQIODevice is Inherited from QIODevice, and I Reappeared "readData" function.


    qint64 RepletQIODevice::readData(char *data, qint64 maxlen)
    {
    memset(data, 0, maxlen);

    if (listenArray.size() < maxlen) {
    maxlen = listenArray.size();
    }

    if (maxlen > 0) {
    memcpy(data, listenArray.left(maxlen).data(), maxlen);
    listenArray.remove(0, maxlen);
    }

    return maxlen;
    }


    If listenArray had no data to read by readData, my application will will not respond when I Execute "m_audioOutput->stop", I can only force quit.
    I don't know why!
    Can you help me?
    By the way , My english level is very low, please forgive me. Haha......

  8. #7
    Join Date
    Aug 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Appending data to the buffer of a QAudioOutput

    Haha......This problem has been solved. The problem is not here.

Similar Threads

  1. QAudioOutput Play Audio Buffer chat voice ?
    By Thành Viên Mới in forum Qt Programming
    Replies: 5
    Last Post: 11th May 2011, 20:35
  2. QaudioOutput play buffer audio real time disconnect network ?
    By Thành Viên Mới in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2011, 13:20
  3. Clear QTcpSocket buffer before using receiving new data
    By tbscope in forum Qt Programming
    Replies: 2
    Last Post: 26th February 2011, 07:50
  4. QAudioOutput buffer underrun
    By BartBlackMagic in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2010, 18:44
  5. data is not appending to the file
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 3rd April 2008, 13:39

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.