AudioController.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "Base.h"
  2. #include "AudioController.h"
  3. #include "AudioListener.h"
  4. #include "AudioBuffer.h"
  5. #include "AudioSource.h"
  6. namespace gameplay
  7. {
  8. AudioController::AudioController()
  9. : _alcDevice(NULL), _alcContext(NULL), _pausingSource(NULL), _streamingThreadActive(true)
  10. {
  11. }
  12. AudioController::~AudioController()
  13. {
  14. }
  15. void AudioController::initialize()
  16. {
  17. _alcDevice = alcOpenDevice(NULL);
  18. if (!_alcDevice)
  19. {
  20. GP_ERROR("Unable to open OpenAL device.\n");
  21. return;
  22. }
  23. _alcContext = alcCreateContext(_alcDevice, NULL);
  24. ALCenum alcErr = alcGetError(_alcDevice);
  25. if (!_alcContext || alcErr != ALC_NO_ERROR)
  26. {
  27. alcCloseDevice(_alcDevice);
  28. GP_ERROR("Unable to create OpenAL context. Error: %d\n", alcErr);
  29. return;
  30. }
  31. alcMakeContextCurrent(_alcContext);
  32. alcErr = alcGetError(_alcDevice);
  33. if (alcErr != ALC_NO_ERROR)
  34. {
  35. GP_ERROR("Unable to make OpenAL context current. Error: %d\n", alcErr);
  36. }
  37. _streamingMutex.reset(new std::mutex());
  38. }
  39. void AudioController::finalize()
  40. {
  41. GP_ASSERT(_streamingSources.empty());
  42. if (_streamingThread.get())
  43. {
  44. _streamingThreadActive = false;
  45. _streamingThread->join();
  46. _streamingThread.reset(NULL);
  47. }
  48. alcMakeContextCurrent(NULL);
  49. if (_alcContext)
  50. {
  51. alcDestroyContext(_alcContext);
  52. _alcContext = NULL;
  53. }
  54. if (_alcDevice)
  55. {
  56. alcCloseDevice(_alcDevice);
  57. _alcDevice = NULL;
  58. }
  59. }
  60. void AudioController::pause()
  61. {
  62. std::set<AudioSource*>::iterator itr = _playingSources.begin();
  63. // For each source that is playing, pause it.
  64. AudioSource* source = NULL;
  65. while (itr != _playingSources.end())
  66. {
  67. GP_ASSERT(*itr);
  68. source = *itr;
  69. _pausingSource = source;
  70. source->pause();
  71. _pausingSource = NULL;
  72. itr++;
  73. }
  74. }
  75. void AudioController::resume()
  76. {
  77. alcMakeContextCurrent(_alcContext);
  78. std::set<AudioSource*>::iterator itr = _playingSources.begin();
  79. // For each source that is playing, resume it.
  80. AudioSource* source = NULL;
  81. while (itr != _playingSources.end())
  82. {
  83. GP_ASSERT(*itr);
  84. source = *itr;
  85. source->resume();
  86. itr++;
  87. }
  88. }
  89. void AudioController::update(float elapsedTime)
  90. {
  91. AudioListener* listener = AudioListener::getInstance();
  92. if (listener)
  93. {
  94. AL_CHECK( alListenerf(AL_GAIN, listener->getGain()) );
  95. AL_CHECK( alListenerfv(AL_ORIENTATION, (ALfloat*)listener->getOrientation()) );
  96. AL_CHECK( alListenerfv(AL_VELOCITY, (ALfloat*)&listener->getVelocity()) );
  97. AL_CHECK( alListenerfv(AL_POSITION, (ALfloat*)&listener->getPosition()) );
  98. }
  99. }
  100. void AudioController::addPlayingSource(AudioSource* source)
  101. {
  102. if (_playingSources.find(source) == _playingSources.end())
  103. {
  104. _playingSources.insert(source);
  105. if (source->isStreamed())
  106. {
  107. GP_ASSERT(_streamingSources.find(source) == _streamingSources.end());
  108. bool startThread = _streamingSources.empty() && _streamingThread.get() == NULL;
  109. _streamingMutex->lock();
  110. _streamingSources.insert(source);
  111. _streamingMutex->unlock();
  112. if (startThread)
  113. _streamingThread.reset(new std::thread(&streamingThreadProc, this));
  114. }
  115. }
  116. }
  117. void AudioController::removePlayingSource(AudioSource* source)
  118. {
  119. if (_pausingSource != source)
  120. {
  121. std::set<AudioSource*>::iterator iter = _playingSources.find(source);
  122. if (iter != _playingSources.end())
  123. {
  124. _playingSources.erase(iter);
  125. if (source->isStreamed())
  126. {
  127. GP_ASSERT(_streamingSources.find(source) != _streamingSources.end());
  128. _streamingMutex->lock();
  129. _streamingSources.erase(source);
  130. _streamingMutex->unlock();
  131. }
  132. }
  133. }
  134. }
  135. void AudioController::streamingThreadProc(void* arg)
  136. {
  137. AudioController* controller = (AudioController*)arg;
  138. while (controller->_streamingThreadActive)
  139. {
  140. controller->_streamingMutex->lock();
  141. std::for_each(controller->_streamingSources.begin(), controller->_streamingSources.end(), std::mem_fn(&AudioSource::streamDataIfNeeded));
  142. controller->_streamingMutex->unlock();
  143. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  144. }
  145. }
  146. }