audio_driver_osx.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef OSX_ENABLED
  31. #include "audio_driver_osx.h"
  32. static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *__nullable inClientData) {
  33. AudioDriverOSX *driver = (AudioDriverOSX *)inClientData;
  34. driver->reopen();
  35. return noErr;
  36. }
  37. Error AudioDriverOSX::initDevice() {
  38. AudioStreamBasicDescription strdesc;
  39. strdesc.mFormatID = kAudioFormatLinearPCM;
  40. strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  41. strdesc.mChannelsPerFrame = channels;
  42. strdesc.mSampleRate = 44100;
  43. strdesc.mFramesPerPacket = 1;
  44. strdesc.mBitsPerChannel = 16;
  45. strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
  46. strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
  47. OSStatus result;
  48. AURenderCallbackStruct callback;
  49. AudioComponentDescription desc;
  50. AudioComponent comp = NULL;
  51. const AudioUnitElement output_bus = 0;
  52. const AudioUnitElement bus = output_bus;
  53. const AudioUnitScope scope = kAudioUnitScope_Input;
  54. zeromem(&desc, sizeof(desc));
  55. desc.componentType = kAudioUnitType_Output;
  56. desc.componentSubType = kAudioUnitSubType_HALOutput;
  57. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  58. comp = AudioComponentFindNext(NULL, &desc);
  59. ERR_FAIL_COND_V(comp == NULL, FAILED);
  60. result = AudioComponentInstanceNew(comp, &audio_unit);
  61. ERR_FAIL_COND_V(result != noErr, FAILED);
  62. result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, 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, kAudioUnitProperty_SetRenderCallback, scope, bus, &callback, sizeof(callback));
  68. ERR_FAIL_COND_V(result != noErr, FAILED);
  69. result = AudioUnitInitialize(audio_unit);
  70. ERR_FAIL_COND_V(result != noErr, FAILED);
  71. return OK;
  72. }
  73. Error AudioDriverOSX::finishDevice() {
  74. OSStatus result;
  75. if (active) {
  76. result = AudioOutputUnitStop(audio_unit);
  77. ERR_FAIL_COND_V(result != noErr, FAILED);
  78. active = false;
  79. }
  80. result = AudioUnitUninitialize(audio_unit);
  81. ERR_FAIL_COND_V(result != noErr, FAILED);
  82. return OK;
  83. }
  84. Error AudioDriverOSX::init() {
  85. OSStatus result;
  86. mutex = Mutex::create();
  87. active = false;
  88. channels = 2;
  89. outputDeviceAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
  90. outputDeviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
  91. outputDeviceAddress.mElement = kAudioObjectPropertyElementMaster;
  92. result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
  93. ERR_FAIL_COND_V(result != noErr, FAILED);
  94. const int samples = 1024;
  95. samples_in = memnew_arr(int32_t, samples); // whatever
  96. buffer_frames = samples / channels;
  97. return initDevice();
  98. };
  99. Error AudioDriverOSX::reopen() {
  100. Error err;
  101. bool restart = false;
  102. lock();
  103. if (active) {
  104. restart = true;
  105. }
  106. err = finishDevice();
  107. if (err != OK) {
  108. ERR_PRINT("finishDevice failed");
  109. unlock();
  110. return err;
  111. }
  112. err = initDevice();
  113. if (err != OK) {
  114. ERR_PRINT("initDevice failed");
  115. unlock();
  116. return err;
  117. }
  118. if (restart) {
  119. start();
  120. }
  121. unlock();
  122. return OK;
  123. }
  124. OSStatus AudioDriverOSX::output_callback(void *inRefCon,
  125. AudioUnitRenderActionFlags *ioActionFlags,
  126. const AudioTimeStamp *inTimeStamp,
  127. UInt32 inBusNumber, UInt32 inNumberFrames,
  128. AudioBufferList *ioData) {
  129. AudioDriverOSX *ad = (AudioDriverOSX *)inRefCon;
  130. if (!ad->active || !ad->try_lock()) {
  131. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  132. AudioBuffer *abuf = &ioData->mBuffers[i];
  133. zeromem(abuf->mData, abuf->mDataByteSize);
  134. };
  135. return 0;
  136. };
  137. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  138. AudioBuffer *abuf = &ioData->mBuffers[i];
  139. int frames_left = inNumberFrames;
  140. int16_t *out = (int16_t *)abuf->mData;
  141. while (frames_left) {
  142. int frames = MIN(frames_left, ad->buffer_frames);
  143. ad->audio_server_process(frames, ad->samples_in);
  144. for (int j = 0; j < frames * ad->channels; j++) {
  145. out[j] = ad->samples_in[j] >> 16;
  146. }
  147. frames_left -= frames;
  148. out += frames * ad->channels;
  149. };
  150. };
  151. ad->unlock();
  152. return 0;
  153. };
  154. void AudioDriverOSX::start() {
  155. if (!active) {
  156. OSStatus result = AudioOutputUnitStart(audio_unit);
  157. if (result != noErr) {
  158. ERR_PRINT("AudioOutputUnitStart failed");
  159. } else {
  160. active = true;
  161. }
  162. }
  163. };
  164. int AudioDriverOSX::get_mix_rate() const {
  165. return 44100;
  166. };
  167. AudioDriver::SpeakerMode AudioDriverOSX::get_speaker_mode() const {
  168. return SPEAKER_MODE_STEREO;
  169. };
  170. void AudioDriverOSX::lock() {
  171. if (mutex)
  172. mutex->lock();
  173. };
  174. void AudioDriverOSX::unlock() {
  175. if (mutex)
  176. mutex->unlock();
  177. };
  178. bool AudioDriverOSX::try_lock() {
  179. if (mutex)
  180. return mutex->try_lock() == OK;
  181. return true;
  182. }
  183. void AudioDriverOSX::finish() {
  184. OSStatus result;
  185. finishDevice();
  186. result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
  187. if (result != noErr) {
  188. ERR_PRINT("AudioObjectRemovePropertyListener failed");
  189. }
  190. if (mutex) {
  191. memdelete(mutex);
  192. mutex = NULL;
  193. }
  194. if (samples_in) {
  195. memdelete_arr(samples_in);
  196. samples_in = NULL;
  197. }
  198. };
  199. AudioDriverOSX::AudioDriverOSX() {
  200. mutex = NULL;
  201. samples_in = NULL;
  202. };
  203. AudioDriverOSX::~AudioDriverOSX(){};
  204. #endif