audio_driver_osx.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* audio_driver_osx.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef OSX_ENABLED
  30. #include "audio_driver_osx.h"
  31. Error AudioDriverOSX::init() {
  32. active = false;
  33. channels = 2;
  34. AudioStreamBasicDescription strdesc;
  35. strdesc.mFormatID = kAudioFormatLinearPCM;
  36. strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  37. strdesc.mChannelsPerFrame = channels;
  38. strdesc.mSampleRate = 44100;
  39. strdesc.mFramesPerPacket = 1;
  40. strdesc.mBitsPerChannel = 16;
  41. strdesc.mBytesPerFrame =
  42. strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
  43. strdesc.mBytesPerPacket =
  44. strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
  45. OSStatus result = noErr;
  46. AURenderCallbackStruct callback;
  47. AudioComponentDescription desc;
  48. AudioComponent comp = NULL;
  49. const AudioUnitElement output_bus = 0;
  50. const AudioUnitElement bus = output_bus;
  51. const AudioUnitScope scope = kAudioUnitScope_Input;
  52. zeromem(&desc, sizeof(desc));
  53. desc.componentType = kAudioUnitType_Output;
  54. desc.componentSubType = 0; /* !!! FIXME: ? */
  55. comp = AudioComponentFindNext(NULL, &desc);
  56. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  57. result = AudioComponentInstanceNew(comp, &audio_unit);
  58. ERR_FAIL_COND_V(result != noErr, FAILED);
  59. ERR_FAIL_COND_V(comp == NULL, FAILED);
  60. result = AudioUnitSetProperty(audio_unit,
  61. kAudioUnitProperty_StreamFormat,
  62. scope, bus, &strdesc, sizeof(strdesc));
  63. ERR_FAIL_COND_V(result != noErr, FAILED);
  64. zeromem(&callback, sizeof(AURenderCallbackStruct));
  65. callback.inputProc = &AudioDriverOSX::output_callback;
  66. callback.inputProcRefCon = this;
  67. result = AudioUnitSetProperty(audio_unit,
  68. kAudioUnitProperty_SetRenderCallback,
  69. scope, bus, &callback, sizeof(callback));
  70. ERR_FAIL_COND_V(result != noErr, FAILED);
  71. result = AudioUnitInitialize(audio_unit);
  72. ERR_FAIL_COND_V(result != noErr, FAILED);
  73. result = AudioOutputUnitStart(audio_unit);
  74. ERR_FAIL_COND_V(result != noErr, FAILED);
  75. const int samples = 1024;
  76. samples_in = memnew_arr(int32_t, samples); // whatever
  77. buffer_frames = samples / channels;
  78. return OK;
  79. };
  80. OSStatus AudioDriverOSX::output_callback(void *inRefCon,
  81. AudioUnitRenderActionFlags * ioActionFlags,
  82. const AudioTimeStamp * inTimeStamp,
  83. UInt32 inBusNumber, UInt32 inNumberFrames,
  84. AudioBufferList * ioData) {
  85. AudioBuffer *abuf;
  86. AudioDriverOSX* ad = (AudioDriverOSX*)inRefCon;
  87. bool mix = true;
  88. if (!ad->active)
  89. mix = false;
  90. else if (ad->mutex) {
  91. mix = ad->mutex->try_lock() == OK;
  92. };
  93. if (!mix) {
  94. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  95. abuf = &ioData->mBuffers[i];
  96. zeromem(abuf->mData, abuf->mDataByteSize);
  97. };
  98. return 0;
  99. };
  100. int frames_left;
  101. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  102. abuf = &ioData->mBuffers[i];
  103. frames_left = inNumberFrames;
  104. int16_t* out = (int16_t*)abuf->mData;
  105. while (frames_left) {
  106. int frames = MIN(frames_left, ad->buffer_frames);
  107. //ad->lock();
  108. ad->audio_server_process(frames, ad->samples_in);
  109. //ad->unlock();
  110. for(int i = 0; i < frames * ad->channels; i++) {
  111. out[i] = ad->samples_in[i]>>16;
  112. }
  113. frames_left -= frames;
  114. out += frames * ad->channels;
  115. };
  116. };
  117. if (ad->mutex)
  118. ad->mutex->unlock();
  119. return 0;
  120. };
  121. void AudioDriverOSX::start() {
  122. active = true;
  123. };
  124. int AudioDriverOSX::get_mix_rate() const {
  125. return 44100;
  126. };
  127. AudioDriverSW::OutputFormat AudioDriverOSX::get_output_format() const {
  128. return OUTPUT_STEREO;
  129. };
  130. void AudioDriverOSX::lock() {
  131. if (active && mutex)
  132. mutex->lock();
  133. };
  134. void AudioDriverOSX::unlock() {
  135. if (active && mutex)
  136. mutex->unlock();
  137. };
  138. void AudioDriverOSX::finish() {
  139. if (active)
  140. AudioOutputUnitStop(audio_unit);
  141. memdelete_arr(samples_in);
  142. };
  143. AudioDriverOSX::AudioDriverOSX() {
  144. mutex=Mutex::create();//NULL;
  145. };
  146. AudioDriverOSX::~AudioDriverOSX() {
  147. };
  148. #endif