GEInterfaces.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. *
  3. * GE::GA General interfaces
  4. * tuomo.hirvonen@digia.com
  5. *
  6. */
  7. #include <memory.h>
  8. #include "GEInterfaces.h"
  9. using namespace GE;
  10. /**
  11. * CAudioSource
  12. * common functionality
  13. *
  14. */
  15. IAudioSource::IAudioSource() {
  16. m_next = 0;
  17. };
  18. IAudioSource::~IAudioSource() {
  19. };
  20. /**
  21. * CAudioMixer
  22. *
  23. */
  24. CAudioMixer::CAudioMixer() {
  25. m_sourceList = 0;
  26. m_mixingBuffer = 0;
  27. m_mixingBufferLength = 0;
  28. m_fixedGeneralVolume = 4096;
  29. };
  30. CAudioMixer::~CAudioMixer() {
  31. destroyList();
  32. if (m_mixingBuffer) {
  33. delete [] m_mixingBuffer;
  34. m_mixingBuffer = 0;
  35. };
  36. };
  37. void CAudioMixer::destroyList() {
  38. m_mutex.lock();
  39. IAudioSource *l = m_sourceList;
  40. while (l) {
  41. IAudioSource *n = l->m_next;
  42. delete l;
  43. l = n;
  44. };
  45. m_sourceList = 0;
  46. m_mutex.unlock();
  47. };
  48. IAudioSource* CAudioMixer::addAudioSource( IAudioSource *source ) {
  49. m_mutex.lock();
  50. source->m_next = 0;
  51. if (m_sourceList) {
  52. IAudioSource *l = m_sourceList;
  53. while (l->m_next) l = l->m_next;
  54. l->m_next = source;
  55. } else m_sourceList = source;
  56. m_mutex.unlock();
  57. return source;
  58. };
  59. bool CAudioMixer::removeAudioSource( IAudioSource *source ) {
  60. return true;
  61. };
  62. void CAudioMixer::setGeneralVolume( float vol ) {
  63. m_fixedGeneralVolume = (4096.0f*vol);
  64. };
  65. int CAudioMixer::pullAudio( AUDIO_SAMPLE_TYPE *target, int bufferLength ) {
  66. if (!m_sourceList) return 0;
  67. m_mutex.lock();
  68. if (m_mixingBufferLength<bufferLength) {
  69. if (m_mixingBuffer) delete [] m_mixingBuffer;
  70. m_mixingBufferLength = bufferLength;
  71. m_mixingBuffer = new AUDIO_SAMPLE_TYPE[ m_mixingBufferLength ];
  72. };
  73. memset( target, 0, sizeof( AUDIO_SAMPLE_TYPE ) * bufferLength );
  74. AUDIO_SAMPLE_TYPE *t;
  75. AUDIO_SAMPLE_TYPE *t_target;
  76. AUDIO_SAMPLE_TYPE *s;
  77. IAudioSource *prev = 0;
  78. IAudioSource *l = m_sourceList;
  79. while (l) {
  80. IAudioSource *next = l->m_next;
  81. // process l
  82. int mixed = l->pullAudio( m_mixingBuffer, bufferLength );
  83. if (mixed>0) {
  84. // mix to main..
  85. t = target;
  86. t_target = t+mixed;
  87. s = m_mixingBuffer;
  88. while (t!=t_target) {
  89. *t +=(((*s)*m_fixedGeneralVolume)>>12);
  90. t++;
  91. s++;
  92. };
  93. };
  94. // autodestroy
  95. if (l->canBeDestroyed() == true) { // NOTE, IS UNDER TESTING,... MIGHT CAUSE UNPREDICTABLE CRASHING WITH SOME USE CASES!!!
  96. if (!prev)
  97. m_sourceList = next;
  98. else prev->m_next = next;
  99. delete l;
  100. l = 0;
  101. };
  102. prev = l;
  103. l = next;
  104. };
  105. m_mutex.unlock();
  106. return bufferLength;
  107. };