c++qtaudioqiodevice

How to get rid of memcpy in QIoDevice


I'm using Qt to make some audio output. There is an example audioouput, where pure virtual function qint64 QIODevice::readData ( char * data, qint64 maxSize ) is reimplemented like this(this is not actual code from example).

qint64 Generator::readData(char *data, qint64 len)
{
    memcpy(data, m_buffer.data(), len);
    return len;
}

So, we get pointer to data, copy memory from our buffer to this data. I'm wondering whether I can just assign pointer data to new location, like this:

qint64 Generator::readData(char *data, qint64 len)
{
    data = m_buffer.data();
    return len;
}

Because, memcpy is slow and C. So can I? Thank you!

OK, just not to receive comments "what is slow": first - memcpy slower than pointer assignment second - yes, I have latency in sound playing, I want to make program optimal at every point.

P.S. I'm trying to do it, and I have no sound, so maybe answer is no. Then why?


Solution

  • The signature of this method only lets you return the data by copying them. Assigning the pointer is pointless because this parameter is passed as a copy, so the assigned value won't be visible outside your method.

    memcpy is not at all slow for realtime audio output (unless you are doing it on an 8-bit processor).

    Too slow audio code can cause gaps in the sound but not latency. Latency is a result of a too big audio buffer.