jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.4KB

  1. /*
  2. Copyright (C) 2010 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __TiPhoneCoreAudioRenderer__
  16. #define __TiPhoneCoreAudioRenderer__
  17. #include <AudioToolbox/AudioConverter.h>
  18. #include <AudioToolbox/AudioServices.h>
  19. #include <AudioUnit/AudioUnit.h>
  20. #define MAX_CHANNELS 256
  21. #define OPEN_ERR -1
  22. #define NO_ERR 0
  23. typedef void (*AudioCallback) (int frames, float** inputs, float** outputs, void* arg);
  24. class TiPhoneCoreAudioRenderer
  25. {
  26. private:
  27. AudioUnit fAUHAL;
  28. AudioCallback fAudioCallback;
  29. void* fCallbackArg;
  30. int fDevNumInChans;
  31. int fDevNumOutChans;
  32. AudioBufferList* fCAInputData;
  33. float* fInChannel[MAX_CHANNELS];
  34. float* fOutChannel[MAX_CHANNELS];
  35. static OSStatus Render(void *inRefCon,
  36. AudioUnitRenderActionFlags *ioActionFlags,
  37. const AudioTimeStamp *inTimeStamp,
  38. UInt32 inBusNumber,
  39. UInt32 inNumberFrames,
  40. AudioBufferList *ioData);
  41. static void InterruptionListener(void *inClientData, UInt32 inInterruption);
  42. public:
  43. TiPhoneCoreAudioRenderer(int input, int output)
  44. :fAudioCallback(NULL), fCallbackArg(NULL), fDevNumInChans(input), fDevNumOutChans(output), fCAInputData(NULL)
  45. {
  46. memset(fInChannel, 0, sizeof(float*) * MAX_CHANNELS);
  47. memset(fOutChannel, 0, sizeof(float*) * MAX_CHANNELS);
  48. for (int i = 0; i < fDevNumInChans; i++) {
  49. fInChannel[i] = new float[8192];
  50. }
  51. for (int i = 0; i < fDevNumOutChans; i++) {
  52. fOutChannel[i] = new float[8192];
  53. }
  54. }
  55. virtual ~TiPhoneCoreAudioRenderer()
  56. {
  57. for (int i = 0; i < fDevNumInChans; i++) {
  58. delete[] fInChannel[i];
  59. }
  60. for (int i = 0; i < fDevNumOutChans; i++) {
  61. delete[] fOutChannel[i];
  62. }
  63. if (fCAInputData) {
  64. for (int i = 0; i < fDevNumInChans; i++) {
  65. free(fCAInputData->mBuffers[i].mData);
  66. }
  67. free(fCAInputData);
  68. }
  69. }
  70. int Open(int bufferSize, int sampleRate);
  71. int Close();
  72. int Start();
  73. int Stop();
  74. void SetAudioCallback(AudioCallback callback, void* arg)
  75. {
  76. fAudioCallback = callback;
  77. fCallbackArg = arg;
  78. }
  79. void PerformAudioCallback(int frames)
  80. {
  81. if (fAudioCallback) {
  82. fAudioCallback(frames, fInChannel, fOutChannel, fCallbackArg);
  83. }
  84. }
  85. };
  86. typedef TiPhoneCoreAudioRenderer * TiPhoneCoreAudioRendererPtr;
  87. #endif