Results 1 to 1 of 1

Thread: QIODevice read() problem (reads more than maxSize)

  1. #1
    Join Date
    Jan 2010
    Location
    Poland Warsaw
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QIODevice read() problem (reads more than maxSize)

    Hello.

    I'm writing an application that captures audio from microphone input using QtMultimedia (QAudioInput). Based on the "spectrum" demo from http://qt.gitorious.org/qt/qt/trees/4.7/demos/spectrum I wrote "audioDataReady()" slot:

    Qt Code:
    1. void Engine::audioDataReady()
    2. {
    3. Q_ASSERT(0 == m_bufferPosition);
    4. const qint64 bytesReady = m_audioInput->bytesReady();
    5. const qint64 bytesSpace = m_buffer.size() - m_dataLength;
    6. const qint64 bytesToRead = qMin(bytesReady, bytesSpace);
    7. const qint64 bytesRead = m_audioInputIODevice->read(
    8. m_buffer.data() + m_dataLength,
    9. bytesToRead);
    10.  
    11. if (bytesRead) {
    12. m_dataLength += bytesRead;
    13. qDebug() << "bytesReady:" << bytesReady << "| bytesSpace:" << bytesSpace << "| bytesToRead:" << bytesToRead << "| bytesRead:" << bytesRead; // added line
    14. emit dataLengthChanged(dataLength());
    15. }
    16.  
    17. if (m_buffer.size() == m_dataLength)
    18. stopRecording();
    19. }
    To copy to clipboard, switch view to plain text mode 

    First few lines of output are:
    bytesReady: 680 | bytesSpace: 160000 | bytesToRead: 680 | bytesRead: 1700
    bytesReady: 680 | bytesSpace: 158300 | bytesToRead: 680 | bytesRead: 1360
    bytesReady: 1020 | bytesSpace: 156940 | bytesToRead: 1020 | bytesRead: 1700
    ...

    The question is: why QIODevice::read ( char * data, qint64 maxSize ) reads more bytes than second argument tells to read.

    P.S. After 10 seconds of recording, "spectrum" demo exits with "Buffer overflow" error what is normal I think because read() method tries to read eg. 1700 bytes but there is only eg. 1000 bytes free.
    Last edited by m15ch4; 22nd February 2011 at 12:52.

Similar Threads

  1. XML read problem
    By Archa4 in forum Newbie
    Replies: 2
    Last Post: 7th February 2011, 11:12
  2. Replies: 1
    Last Post: 16th June 2009, 09:09
  3. Using QIODevice to read a GPS filenode?
    By hickscorp in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 3rd December 2007, 22:32
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.