GEAudioOut.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. *
  4. * Part of the Qt GameEnabler.
  5. */
  6. #include "GEAudioOut.h"
  7. #include <QAudioOutput>
  8. #include <QIODevice>
  9. #include <QtMultimedia/qaudio.h>
  10. #include <QtMultimedia/qaudiodeviceinfo.h>
  11. #include <QString>
  12. #include "trace.h" // For debug macros
  13. // Constants
  14. const int GEDefaultChannelCount(2);
  15. const QString GEDefaultAudioCodec("audio/pcm");
  16. const QAudioFormat::Endian GEByteOrder(QAudioFormat::LittleEndian);
  17. const QAudioFormat::SampleType GESampleType(QAudioFormat::SignedInt);
  18. const int GEThreadSleepTime(1); // Milliseconds
  19. using namespace GE;
  20. /*!
  21. \class Audioout
  22. \brief An object deploying QAudioOutput for sending the pre-mixed/processed
  23. audio data into an actual audio device.
  24. */
  25. /*!
  26. Constructor.
  27. */
  28. AudioOut::AudioOut(AudioSource *source, QObject *parent /* = 0 */)
  29. : QThread(parent),
  30. m_audioOutput(0),
  31. m_outTarget(0),
  32. m_source(source),
  33. m_sendBuffer(0),
  34. m_sendBufferSize(0),
  35. m_samplesMixed(0),
  36. m_threadState(NotRunning),
  37. m_usingThread(false)
  38. {
  39. QAudioFormat format;
  40. format.setFrequency(AUDIO_FREQUENCY);
  41. format.setChannels(GEDefaultChannelCount);
  42. format.setSampleSize(AUDIO_SAMPLE_BITS);
  43. format.setCodec(GEDefaultAudioCodec);
  44. format.setByteOrder(GEByteOrder);
  45. format.setSampleType(GESampleType);
  46. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  47. if (!info.isFormatSupported(format))
  48. format = info.nearestFormat(format);
  49. m_audioOutput = new QAudioOutput(info, format);
  50. m_audioOutput->setBufferSize(4096 * 8);
  51. m_sendBufferSize = 4096 * 8;
  52. m_outTarget = m_audioOutput->start();
  53. DEBUG_INFO("Buffer size: " << m_audioOutput->bufferSize());
  54. m_sendBuffer = new AUDIO_SAMPLE_TYPE[m_sendBufferSize];
  55. #ifndef Q_OS_SYMBIAN
  56. m_usingThread = true;
  57. start();
  58. #endif
  59. }
  60. /*!
  61. Destructor.
  62. */
  63. AudioOut::~AudioOut()
  64. {
  65. if (m_threadState == DoRun) {
  66. // Set the thread to exit run().
  67. m_threadState = DoExit;
  68. }
  69. if (QThread::isRunning() == false) {
  70. m_threadState = NotRunning;
  71. }
  72. while (m_threadState != NotRunning) {
  73. // Wait until the thread is finished.
  74. msleep(50);
  75. }
  76. m_audioOutput->stop();
  77. delete m_audioOutput;
  78. delete [] m_sendBuffer;
  79. }
  80. /*!
  81. For internal notification solution.
  82. */
  83. void AudioOut::audioNotify()
  84. {
  85. tick();
  86. }
  87. /*!
  88. TODO: Document what this method actually does and why it is needed.
  89. Call this method manually only if you are not using a thread (with Symbian).
  90. Note: When using Qt GameEnabler, the GameWindow instance owning this AudioOut
  91. instance will handle calling this method and you should not try to call this
  92. explicitly.
  93. */
  94. void AudioOut::tick()
  95. {
  96. // Fill data to the buffer as much as there is free space available.
  97. int samplesToWrite(m_audioOutput->bytesFree() /
  98. (GEDefaultChannelCount * AUDIO_SAMPLE_BITS / 8));
  99. samplesToWrite *= 2;
  100. if (samplesToWrite <= 0)
  101. return;
  102. if (samplesToWrite > m_sendBufferSize)
  103. samplesToWrite = m_sendBufferSize;
  104. int mixedSamples = m_source->pullAudio(m_sendBuffer, samplesToWrite);
  105. m_outTarget->write((char*)m_sendBuffer, mixedSamples * 2);
  106. }
  107. /*!
  108. From QThread.
  109. Used only in threaded solutions.
  110. */
  111. void AudioOut::run()
  112. {
  113. DEBUG_INFO("Starting thread.");
  114. m_threadState = DoRun;
  115. if (!m_source) {
  116. DEBUG_INFO("No audio source, exiting the thread!");
  117. m_threadState = NotRunning;
  118. return;
  119. }
  120. while (m_threadState == DoRun) {
  121. tick();
  122. msleep(GEThreadSleepTime);
  123. }
  124. DEBUG_INFO("Exiting thread.");
  125. m_threadState = NotRunning;
  126. }