glue.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "soloud_file.h"
  2. #include "dataqueue.h"
  3. extern "C" {
  4. #include "brl.mod/blitz.mod/blitz.h"
  5. int audio_soloud_file_TStreamFile__eof(BBObject * handle);
  6. void audio_soloud_file_TStreamFile__seek(BBObject * handle, int offset);
  7. int audio_soloud_file_TStreamFile__length(BBObject * handle);
  8. int audio_soloud_file_TStreamFile__pos(BBObject * handle);
  9. int audio_soloud_file_TStreamFile__read(BBObject * handle, unsigned char * dst, int size);
  10. int audio_soloud_file_TStreamFile__destroy(BBObject * handle);
  11. SoLoud::File * bmx_soloud_streamfile_new(BBObject * handle);
  12. void bmx_soloud_streamfile_free(SoLoud::File * file);
  13. }
  14. class TStreamFile : public SoLoud::File {
  15. public:
  16. TStreamFile(BBObject * handle) : maxHandle(handle) {
  17. BBRETAIN(maxHandle);
  18. }
  19. virtual ~TStreamFile() {
  20. audio_soloud_file_TStreamFile__destroy(maxHandle);
  21. BBRELEASE(maxHandle);
  22. }
  23. virtual int eof() {
  24. return audio_soloud_file_TStreamFile__eof(maxHandle);
  25. }
  26. virtual unsigned int read(unsigned char *aDst, unsigned int aBytes) {
  27. return audio_soloud_file_TStreamFile__read(maxHandle, aDst, aBytes);
  28. }
  29. virtual unsigned int length() {
  30. return audio_soloud_file_TStreamFile__length(maxHandle);
  31. }
  32. virtual void seek(int aOffset) {
  33. audio_soloud_file_TStreamFile__seek(maxHandle, aOffset);
  34. }
  35. virtual unsigned int pos() {
  36. return audio_soloud_file_TStreamFile__pos(maxHandle);
  37. }
  38. private:
  39. BBObject * maxHandle;
  40. };
  41. SoLoud::File * bmx_soloud_streamfile_new(BBObject * handle) {
  42. return new TStreamFile(handle);
  43. }
  44. void bmx_soloud_streamfile_free(SoLoud::File * file) {
  45. delete file;
  46. }
  47. namespace SoLoud
  48. {
  49. class QueuedSource;
  50. class QueuedSourceInstance : public AudioSourceInstance
  51. {
  52. QueuedSource *mParent;
  53. public:
  54. QueuedSourceInstance(QueuedSource *aParent);
  55. virtual unsigned int getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize);
  56. virtual bool hasEnded();
  57. };
  58. class QueuedSource : public AudioSource
  59. {
  60. public:
  61. data_queue * queue;
  62. QueuedSource();
  63. virtual ~QueuedSource();
  64. virtual AudioSourceInstance *createInstance();
  65. virtual void queueAudio(void * buffer, size_t size);
  66. };
  67. };
  68. namespace SoLoud
  69. {
  70. QueuedSourceInstance::QueuedSourceInstance(QueuedSource *aParent)
  71. {
  72. mParent = aParent;
  73. }
  74. unsigned int QueuedSourceInstance::getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize)
  75. {
  76. unsigned int offset = 0;
  77. float tmp[512 * MAX_CHANNELS];
  78. unsigned int i, j, k;
  79. unsigned int samples = aSamplesToRead;
  80. for (i = 0; i < aSamplesToRead; i += 512)
  81. {
  82. unsigned int blockSize = (aSamplesToRead - i) > 512 ? 512 : aSamplesToRead - i;
  83. memset(tmp, 0, 512 * MAX_CHANNELS * sizeof(float) );
  84. size_t read = bmx_queue_read(mParent->queue, tmp, blockSize * 4 * 2);
  85. read = read / 8;
  86. offset += read;
  87. for (j = 0; j < blockSize; j++)
  88. {
  89. for (k = 0; k < mChannels; k++)
  90. {
  91. aBuffer[k * aSamplesToRead + i + j] = tmp[j * mChannels + k];
  92. }
  93. }
  94. }
  95. return samples;
  96. }
  97. bool QueuedSourceInstance::hasEnded()
  98. {
  99. return 0;
  100. // This audio source never ends.
  101. //return bmx_queue_count(mParent->queue) == 0;
  102. }
  103. QueuedSource::QueuedSource()
  104. {
  105. mBaseSamplerate = 44100.0;
  106. mChannels = 2;
  107. queue = bmx_queue_new(9216, 9216);
  108. }
  109. QueuedSource::~QueuedSource()
  110. {
  111. stop();
  112. }
  113. void QueuedSource::queueAudio(void * buffer, size_t size)
  114. {
  115. bmx_queue_write(queue, buffer, size);
  116. }
  117. AudioSourceInstance * QueuedSource::createInstance()
  118. {
  119. return new QueuedSourceInstance(this);
  120. }
  121. };
  122. extern "C" {
  123. void bmx_soloud_queued_write(SoLoud::AudioSource * src, void * buffer, size_t size) {
  124. SoLoud::QueuedSource* source = static_cast<SoLoud::QueuedSource*>(src);
  125. source->queueAudio(buffer, size);
  126. }
  127. void bmx_soloud_queued_destroy(void * aClassPtr)
  128. {
  129. delete (SoLoud::QueuedSource *)aClassPtr;
  130. }
  131. void * bmx_soloud_queued_create()
  132. {
  133. return (void *)new SoLoud::QueuedSource();
  134. }
  135. void bmx_soloud_queued_stop(void * aClassPtr) {
  136. SoLoud::QueuedSource * cl = (SoLoud::QueuedSource *)aClassPtr;
  137. cl->stop();
  138. }
  139. void bmx_soloud_queued_setVolume(void * aClassPtr, float aVolume) {
  140. SoLoud::QueuedSource * cl = (SoLoud::QueuedSource *)aClassPtr;
  141. cl->setVolume(aVolume);
  142. }
  143. void bmx_soloud_queued_setLooping(void * aClassPtr, int aLoop) {
  144. SoLoud::QueuedSource * cl = (SoLoud::QueuedSource *)aClassPtr;
  145. cl->setLooping(!!aLoop);
  146. }
  147. void bmx_soloud_queued_setAutoStop(void * aClassPtr, int aAutoStop) {
  148. SoLoud::QueuedSource * cl = (SoLoud::QueuedSource *)aClassPtr;
  149. cl->setAutoStop(!!aAutoStop);
  150. }
  151. size_t bmx_soloud_queued_count(void * aClassPtr) {
  152. SoLoud::QueuedSource * cl = (SoLoud::QueuedSource *)aClassPtr;
  153. return bmx_queue_count(cl->queue);
  154. }
  155. }