qAudioOutputWrapper.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "qAudioOutputWrapper.h"
  2. #include <sstream>
  3. #include <QDebug>
  4. #include <QAudioOutput>
  5. #include <QFile>
  6. #include <QThread>
  7. #include "qDebugWrapper.h"
  8. #include "qAudioOutputEvent.h"
  9. AudioOutput createAudioOutput(int frequency, int channels, int sampleSize)
  10. {
  11. const char* codec = "audio/pcm";
  12. QAudioFormat::Endian byteOrder = QAudioFormat::LittleEndian;
  13. QAudioFormat::SampleType sampleType = QAudioFormat::SignedInt;
  14. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  15. if (!info.supportedSampleRates().contains(frequency))
  16. {
  17. Q_PRINT("Error:: Selected frequency is not supported by qt backend!");
  18. return 0;
  19. }
  20. if (!info.supportedChannelCounts().contains(channels))
  21. {
  22. Q_PRINT("Error:: Selected channel count is not supported by qt backend!");
  23. return 0;
  24. }
  25. if (!info.supportedSampleSizes().contains(sampleSize))
  26. {
  27. Q_PRINT("Selected sample size is not supported by qt backend!");
  28. return 0;
  29. }
  30. if (!info.supportedCodecs().contains(codec))
  31. {
  32. Q_PRINT("Error:: Selected codec is not supported by qt backend!");
  33. return 0;
  34. }
  35. if (!info.supportedByteOrders().contains(byteOrder))
  36. {
  37. Q_PRINT("Error:: Selected byte order is not supported by qt backend!");
  38. return 0;
  39. }
  40. if (!info.supportedSampleTypes().contains(sampleType))
  41. {
  42. Q_PRINT("Error:: Selected sample type is not supported by qt backend!");
  43. return 0;
  44. }
  45. QAudioFormat qAudioFormat;
  46. qAudioFormat.setSampleRate(frequency);
  47. qAudioFormat.setChannelCount(channels);
  48. qAudioFormat.setSampleSize(sampleSize);
  49. qAudioFormat.setCodec(codec);
  50. qAudioFormat.setByteOrder(byteOrder);
  51. qAudioFormat.setSampleType(sampleType);
  52. if (!info.isFormatSupported(qAudioFormat))
  53. {
  54. Q_PRINT("Error:: Selected format not supported by qt backend!");
  55. return 0;
  56. }
  57. QAudioOutput* qAudioOutput = new QAudioOutput(qAudioFormat);
  58. qAudioOutput->setBufferSize(4096 * 4);
  59. return (void*)qAudioOutput;
  60. }
  61. AudioBuffer startAudioOutput(AudioOutput audioOutput)
  62. {
  63. Q_ASSERT(audioOutput);
  64. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  65. if (qAudioOutput->state() != QAudio::StoppedState)
  66. {
  67. Q_PRINT("Error:: Invalid State!");
  68. return 0;
  69. }
  70. //AudioOutputEvent* event = new AudioOutputEvent();
  71. //if (!event->connect(qAudioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(onStateChanged(QAudio::State))))
  72. //{
  73. // return 0;
  74. //}
  75. QIODevice* io = qAudioOutput->start();
  76. return (void*)io;
  77. }
  78. int isAudioOutputReady(AudioOutput audioOutput)
  79. {
  80. Q_ASSERT(audioOutput);
  81. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  82. QAudio::State state = qAudioOutput->state();
  83. return state == QAudio::IdleState || state == QAudio::ActiveState;
  84. }
  85. int getAudioBufferFreeSize(AudioOutput audioOutput)
  86. {
  87. Q_ASSERT(audioOutput);
  88. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  89. return qAudioOutput->bytesFree();
  90. }
  91. int getAudioBufferPeriodSize(AudioOutput audioOutput)
  92. {
  93. Q_ASSERT(audioOutput);
  94. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  95. return qAudioOutput->periodSize();
  96. }
  97. void writeAudioBuffer(AudioBuffer audioBuffer, const char* data, int bufferSizeInBytes)
  98. {
  99. Q_ASSERT(audioBuffer);
  100. QIODevice* qIO = (QIODevice*)audioBuffer;
  101. qIO->write(data, bufferSizeInBytes);
  102. }
  103. void stopAudioOutput(AudioOutput audioOutput)
  104. {
  105. Q_ASSERT(audioOutput);
  106. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  107. qAudioOutput->stop();
  108. }
  109. void destroyAudioOutput(AudioOutput audioOutput)
  110. {
  111. Q_ASSERT(audioOutput);
  112. QAudioOutput* qAudioOutput = (QAudioOutput*)audioOutput;
  113. delete qAudioOutput;
  114. }