PolySoundManager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "polycode/core/PolySoundManager.h"
  20. #include "polycode/core/PolyCore.h"
  21. #include "polycode/core/PolyLogger.h"
  22. #include <stdint.h>
  23. #include <limits>
  24. using namespace Polycode;
  25. #ifndef INT16_MAX
  26. #define INT16_MAX (std::numeric_limits<int16_t>::max())
  27. #endif
  28. SoundManager::SoundManager() {
  29. audioInterface = NULL;
  30. testVal = 0.0;
  31. leftOver = 0.0;
  32. mixer = new AudioMixer();
  33. mixer->globalVolume = 1.0;
  34. }
  35. void SoundManager::setGlobalVolume(Number globalVolume) {
  36. mixer->globalVolume = globalVolume;
  37. }
  38. void SoundManager::setListenerPosition(const Vector3 &position) {
  39. if(mixer) {
  40. mixer->listenerPosition = position;
  41. }
  42. }
  43. void SoundManager::setListenerOrientation(const Quaternion &orientation) {
  44. if(mixer) {
  45. mixer->listenerOrientation = orientation;
  46. }
  47. }
  48. bool SoundManager::recordSound(unsigned int rate, unsigned int sampleSize) {
  49. // NOAL_TODO
  50. /*
  51. if(captureDevice) {
  52. Logger::log("Error: Audio capture already in progress\n");
  53. return false;
  54. }
  55. captureDevice = alcCaptureOpenDevice(NULL, rate, AL_FORMAT_STEREO16, sampleSize);
  56. if (alGetError() != AL_NO_ERROR) {
  57. captureDevice = NULL;
  58. return false;
  59. }
  60. recordingBufferRate = rate;
  61. recordingBuffer = (ALbyte*) malloc(1);
  62. recordingBufferSize = 0;
  63. alcCaptureStart(captureDevice);
  64. return true;
  65. */
  66. return false;
  67. }
  68. Sound *SoundManager::stopRecording(bool generateFloatBuffer) {
  69. /*
  70. if(!captureDevice) {
  71. Logger::log("No recording in process\n");
  72. return NULL;
  73. }
  74. alcCaptureStop(captureDevice);
  75. alcCaptureCloseDevice(captureDevice);
  76. captureDevice = NULL;
  77. Sound *newSound = new Sound(recordingBufferSize, (const char*)recordingBuffer, 2, recordingBufferRate, 16, generateFloatBuffer);
  78. free(recordingBuffer);
  79. return newSound;
  80. */
  81. // NOAL_TODO
  82. return NULL;
  83. }
  84. void SoundManager::registerSound(Sound *sound) {
  85. Services()->getCore()->lockMutex(mixer->mixerMutex);
  86. mixer->sounds.push_back(sound);
  87. Services()->getCore()->unlockMutex(mixer->mixerMutex);
  88. }
  89. void SoundManager::unregisterSound(Sound *sound) {
  90. Services()->getCore()->lockMutex(mixer->mixerMutex);
  91. for(int i=0; i < mixer->sounds.size(); i++) {
  92. if(mixer->sounds[i] == sound) {
  93. mixer->sounds.erase(mixer->sounds.begin()+i);
  94. Services()->getCore()->unlockMutex(mixer->mixerMutex);
  95. return;
  96. }
  97. }
  98. Services()->getCore()->unlockMutex(mixer->mixerMutex);
  99. }
  100. void SoundManager::setAudioInterface(AudioInterface *audioInterface) {
  101. this->audioInterface = audioInterface;
  102. if (!mixer->mixerMutex) {
  103. mixer->mixerMutex = Services()->getCore()->createMutex();
  104. }
  105. if(audioInterface) {
  106. audioInterface->setMixer(mixer);
  107. }
  108. }
  109. AudioInterface::AudioInterface() {
  110. }
  111. void AudioInterface::setMixer(AudioMixer *mixer) {
  112. this->mixer = mixer;
  113. }
  114. AudioMixer *AudioInterface::getMixer() {
  115. return mixer;
  116. }
  117. inline Number mixSamples(Number A, Number B) {
  118. if (A < 0 && B < 0 ) {
  119. return (A + B) - (A * B)/-1.0;
  120. } else if (A > 0 && B > 0 ) {
  121. return (A + B) - (A * B)/1.0;
  122. } else {
  123. return A + B;
  124. }
  125. }
  126. AudioMixer::AudioMixer() {
  127. mixerMutex = NULL;
  128. }
  129. AudioMixer::~AudioMixer() {
  130. delete mixerMutex;
  131. }
  132. void AudioMixer::mixIntoBuffer(int16_t *buffer, unsigned int numSamples) {
  133. mixerMutex->lock();
  134. if (sounds.size() == 0) {
  135. memset(buffer, 0, sizeof(int16_t) * numSamples * POLY_NUM_CHANNELS);
  136. mixerMutex->unlock();
  137. return;
  138. }
  139. for(int i=0; i < sounds.size(); i++) {
  140. sounds[i]->updateStream(numSamples);
  141. }
  142. int16_t *bufferPtr = buffer;
  143. for(int i=0; i < numSamples; i++) {
  144. Number mixResults[POLY_NUM_CHANNELS];
  145. memset(mixResults, 0, sizeof(Number) * POLY_NUM_CHANNELS);
  146. int mixNum = 0;
  147. for(int i=0; i < sounds.size(); i++) {
  148. if(sounds[i]->isPlaying()) {
  149. for(int c=0; c < POLY_NUM_CHANNELS; c++) {
  150. Number sampleA = mixResults[c];
  151. Number sampleB = sounds[i]->getSampleAsNumber(sounds[i]->getOffset(), c, listenerPosition, listenerOrientation);
  152. if(mixNum == 0) {
  153. mixResults[c] = sampleB;
  154. } else {
  155. mixResults[c] = mixSamples(sampleA, sampleB);
  156. }
  157. }
  158. sounds[i]->setOffset(sounds[i]->getOffset()+1);
  159. mixNum++;
  160. }
  161. }
  162. for(int c=0; c < POLY_NUM_CHANNELS; c++) {
  163. *bufferPtr = (int16_t)(((Number)INT16_MAX) * (mixResults[c] * globalVolume));
  164. bufferPtr++;
  165. }
  166. }
  167. mixerMutex->unlock();
  168. }
  169. void SoundManager::Update() {
  170. }
  171. SoundManager::~SoundManager() {
  172. delete audioInterface;
  173. }