Hello. Please tell me how to remove the delay when sending voice from the microphone to the speakers.
(OS Linux Mint 18.1, pulseaudio)
Qt Code:
  1. #include <iostream>
  2. #include <cassert>
  3. #include <QCoreApplication>
  4. #include <QAudioInput>
  5. #include <QAudioOutput>
  6. #include <QBuffer>
  7. #include <QDebug>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QCoreApplication app(argc, argv);
  12.  
  13. QBuffer rdBuff;
  14. QBuffer wrBuff;
  15. wrBuff.open(QBuffer::WriteOnly);
  16. rdBuff.open(QBuffer::ReadOnly);
  17.  
  18. QObject::connect(&wrBuff, &QIODevice::bytesWritten, [&wrBuff, &rdBuff](qint64)
  19. {
  20. // remove all data that was already read
  21. rdBuff.buffer().remove(0, rdBuff.pos());
  22.  
  23. // set pointer to the beginning of the unread data
  24. const auto res = rdBuff.seek(0);
  25. assert(res);
  26.  
  27. // write new data
  28. rdBuff.buffer().append(wrBuff.buffer());
  29.  
  30. // remove all data that was already written
  31. wrBuff.buffer().clear();
  32. wrBuff.seek(0);
  33. });
  34.  
  35. const auto decideAudioFormat = [](const QAudioDeviceInfo& devInfo)
  36. {
  37. QAudioFormat format;
  38. format.setSampleRate(8000);
  39. format.setChannelCount(1);
  40. format.setSampleSize(16);
  41. format.setCodec("audio/pcm");
  42. format.setByteOrder(QAudioFormat::LittleEndian);
  43. format.setSampleType(QAudioFormat::SignedInt);
  44.  
  45.  
  46. if (devInfo.isFormatSupported(format))
  47. {
  48. return format;
  49. }
  50. };
  51.  
  52. QAudioInput audioInput(decideAudioFormat(QAudioDeviceInfo::defaultInputDevice()));
  53. QAudioOutput audioOutput(decideAudioFormat(QAudioDeviceInfo::defaultOutputDevice()));
  54.  
  55. audioInput.start(&wrBuff);
  56. audioOutput.start(&rdBuff);
  57. return app.exec();
  58. }
To copy to clipboard, switch view to plain text mode