PDA

View Full Version : Qt audio loopback delay



fngost
11th March 2017, 09:13
Hello. Please tell me how to remove the delay when sending voice from the microphone to the speakers.
(OS Linux Mint 18.1, pulseaudio)


#include <iostream>
#include <cassert>
#include <QCoreApplication>
#include <QAudioInput>
#include <QAudioOutput>
#include <QBuffer>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QBuffer rdBuff;
QBuffer wrBuff;
wrBuff.open(QBuffer::WriteOnly);
rdBuff.open(QBuffer::ReadOnly);

QObject::connect(&wrBuff, &QIODevice::bytesWritten, [&wrBuff, &rdBuff](qint64)
{
// remove all data that was already read
rdBuff.buffer().remove(0, rdBuff.pos());

// set pointer to the beginning of the unread data
const auto res = rdBuff.seek(0);
assert(res);

// write new data
rdBuff.buffer().append(wrBuff.buffer());

// remove all data that was already written
wrBuff.buffer().clear();
wrBuff.seek(0);
});

const auto decideAudioFormat = [](const QAudioDeviceInfo& devInfo)
{
QAudioFormat format;
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);


if (devInfo.isFormatSupported(format))
{
return format;
}
};

QAudioInput audioInput(decideAudioFormat(QAudioDeviceInfo::def aultInputDevice()));
QAudioOutput audioOutput(decideAudioFormat(QAudioDeviceInfo::de faultOutputDevice()));

audioInput.start(&wrBuff);
audioOutput.start(&rdBuff);
return app.exec();
}