m15ch4
22nd February 2011, 11:09
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:
void Engine::audioDataReady()
{
Q_ASSERT(0 == m_bufferPosition);
const qint64 bytesReady = m_audioInput->bytesReady();
const qint64 bytesSpace = m_buffer.size() - m_dataLength;
const qint64 bytesToRead = qMin(bytesReady, bytesSpace);
const qint64 bytesRead = m_audioInputIODevice->read(
m_buffer.data() + m_dataLength,
bytesToRead);
if (bytesRead) {
m_dataLength += bytesRead;
qDebug() << "bytesReady:" << bytesReady << "| bytesSpace:" << bytesSpace << "| bytesToRead:" << bytesToRead << "| bytesRead:" << bytesRead; // added line
emit dataLengthChanged(dataLength());
}
if (m_buffer.size() == m_dataLength)
stopRecording();
}
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.
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:
void Engine::audioDataReady()
{
Q_ASSERT(0 == m_bufferPosition);
const qint64 bytesReady = m_audioInput->bytesReady();
const qint64 bytesSpace = m_buffer.size() - m_dataLength;
const qint64 bytesToRead = qMin(bytesReady, bytesSpace);
const qint64 bytesRead = m_audioInputIODevice->read(
m_buffer.data() + m_dataLength,
bytesToRead);
if (bytesRead) {
m_dataLength += bytesRead;
qDebug() << "bytesReady:" << bytesReady << "| bytesSpace:" << bytesSpace << "| bytesToRead:" << bytesToRead << "| bytesRead:" << bytesRead; // added line
emit dataLengthChanged(dataLength());
}
if (m_buffer.size() == m_dataLength)
stopRecording();
}
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.