/include/AudioDevice.h

http://github.com/digego/extempore · C Header · 193 lines · 130 code · 23 blank · 40 comment · 9 complexity · 0495978addc049f51d2bc83cc5c92e7e MD5 · raw file

  1. /*
  2. * Copyright (c) 2011, Andrew Sorensen
  3. *
  4. * All rights reserved.
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * Neither the name of the authors nor other contributors may be used to endorse
  18. * or promote products derived from this software without specific prior written
  19. * permission.
  20. *
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #ifndef _AUDIO_DEVICE_H
  36. #define _AUDIO_DEVICE_H
  37. #if defined (__APPLE__)
  38. #include <CoreAudio/AudioHardware.h>
  39. #endif
  40. #if defined (COREAUDIO) //__APPLE__)
  41. #include <CoreAudio/AudioHardware.h>
  42. #elif defined (ALSA_AUDIO)
  43. #include <alsa/asoundlib.h>
  44. #else
  45. #include <portaudio.h>
  46. #endif
  47. #include <stdint.h>
  48. #include <vector>
  49. #include "UNIV.h"
  50. #include "EXTThread.h"
  51. typedef float SAMPLE;
  52. typedef void (*dsp_f_ptr_array)(void*, void*, float*, float*, uint64_t, void*);
  53. typedef void (*dsp_f_ptr_sum_array)(void* ,void* ,float** ,float*, uint64_t, void*);
  54. typedef SAMPLE (*dsp_f_ptr)(void*, void*, SAMPLE, uint64_t, uint64_t, const SAMPLE*);
  55. typedef SAMPLE (*dsp_f_ptr_sum)(void*, void*, SAMPLE*, uint64_t, uint64_t, const SAMPLE*);
  56. typedef SAMPLE (*closure_fn_type)(SAMPLE, uint64_t, uint64_t, const SAMPLE*);
  57. namespace extemp
  58. {
  59. class AudioDevice
  60. {
  61. private:
  62. typedef void* (*closure_getter_fn_type)();
  63. public:
  64. static const unsigned MAX_RT_AUDIO_THREADS = 16;
  65. private:
  66. bool m_started;
  67. PaStream* stream;
  68. float* buffer;
  69. closure_getter_fn_type m_dsp_closure;
  70. closure_getter_fn_type m_dsp_mt_closure[128];
  71. dsp_f_ptr dsp_wrapper;
  72. dsp_f_ptr_sum dsp_wrapper_sum;
  73. dsp_f_ptr_array dsp_wrapper_array;
  74. dsp_f_ptr_sum_array dsp_wrapper_sum_array;
  75. SAMPLE* outbuf;
  76. SAMPLE* inbuf;
  77. float* outbuf_f;
  78. float* inbuf_f;
  79. EXTThread* m_threads[MAX_RT_AUDIO_THREADS];
  80. unsigned m_numThreads;
  81. bool m_zeroLatency;
  82. bool m_toggle;
  83. //static AudioDevice* SINGLETON;
  84. static AudioDevice SINGLETON;
  85. private:
  86. bool WrapperSet() const {
  87. return dsp_wrapper || dsp_wrapper_array || dsp_wrapper_sum || dsp_wrapper_sum_array;
  88. }
  89. public:
  90. AudioDevice();
  91. ~AudioDevice();
  92. // start and stop audio processing (which also stops time!!)
  93. void start();
  94. void stop();
  95. bool getZeroLatency() { return m_zeroLatency; }
  96. void setZeroLatency(bool Val) { m_zeroLatency = Val; }
  97. bool getToggle() {
  98. m_toggle = !m_toggle;
  99. return m_toggle;
  100. }
  101. void setDSPClosure(void* Function) {
  102. if (m_dsp_closure) {
  103. printf("You can only set the DSP callback once, but you\ncan re-define that function as often as you like\n");
  104. return;
  105. }
  106. m_dsp_closure = reinterpret_cast<closure_getter_fn_type>(Function);
  107. }
  108. closure_getter_fn_type getDSPClosure() { return m_dsp_closure; }
  109. void setDSPMTClosure(void* Function, int Index) {
  110. if (m_dsp_mt_closure[Index]) {
  111. printf("You can only set the DSP callback once, but you\ncan re-define that function as often as you like\n");
  112. return;
  113. }
  114. m_dsp_mt_closure[Index] = reinterpret_cast<closure_getter_fn_type>(Function);
  115. }
  116. closure_getter_fn_type getDSPMTClosure(int Index) { return m_dsp_mt_closure[Index]; }
  117. void setDSPWrapper(dsp_f_ptr Wrapper) {
  118. if (WrapperSet()) {
  119. return;
  120. }
  121. dsp_wrapper = Wrapper;
  122. }
  123. void setDSPWrapperArray(dsp_f_ptr_array Wrapper) {
  124. if (WrapperSet()) {
  125. return;
  126. }
  127. dsp_wrapper_array = Wrapper;
  128. }
  129. void setDSPMTWrapper(dsp_f_ptr_sum WrapperSum, dsp_f_ptr Wrapper) {
  130. if (WrapperSet()) {
  131. return;
  132. }
  133. dsp_wrapper_sum = WrapperSum;
  134. dsp_wrapper = Wrapper;
  135. }
  136. void setDSPMTWrapperArray(dsp_f_ptr_sum_array WrapperSumArray, dsp_f_ptr_array WrapperArray) {
  137. if (WrapperSet()) {
  138. return;
  139. }
  140. dsp_wrapper_sum_array = WrapperSumArray;
  141. dsp_wrapper_array = WrapperArray;
  142. }
  143. void initMTAudio(int NumThreads, bool ZeroLatency);
  144. void initMTAudioBuf(int,bool);
  145. EXTThread** getMTThreads() { return m_threads; }
  146. int getNumThreads() { return m_numThreads; }
  147. dsp_f_ptr getDSPWrapper() { return dsp_wrapper; }
  148. dsp_f_ptr_array getDSPWrapperArray() { return dsp_wrapper_array; }
  149. dsp_f_ptr_sum getDSPSUMWrapper() { return dsp_wrapper_sum; }
  150. dsp_f_ptr_sum_array getDSPSUMWrapperArray() { return dsp_wrapper_sum_array; }
  151. SAMPLE* getDSPMTInBuffer() { return inbuf; }
  152. SAMPLE* getDSPMTOutBuffer() { return outbuf; }
  153. float* getDSPMTInBufferArray() { return inbuf_f; }
  154. float* getDSPMTOutBufferArray() { return outbuf_f; }
  155. PaStream* getPaStream() { return stream; }
  156. static AudioDevice* I() {
  157. // if (!SINGLETON) {
  158. // SINGLETON = new AudioDevice();
  159. // }
  160. // return SINGLETON;
  161. return &SINGLETON;
  162. }
  163. static double getCPULoad();
  164. static void printDevices();
  165. static double CLOCKBASE;
  166. static double REALTIME;
  167. static double CLOCKOFFSET;
  168. };
  169. } //End Namespace
  170. #endif