audio_driver_osx.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "globals.h"
  33. #include "os/os.h"
  34. #define kOutputBus 0
  35. static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *__nullable inClientData) {
  36. AudioDriverOSX *driver = (AudioDriverOSX *)inClientData;
  37. driver->reopen();
  38. return noErr;
  39. }
  40. Error AudioDriverOSX::initDevice() {
  41. AudioComponentDescription desc;
  42. zeromem(&desc, sizeof(desc));
  43. desc.componentType = kAudioUnitType_Output;
  44. desc.componentSubType = kAudioUnitSubType_HALOutput;
  45. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  46. AudioComponent comp = AudioComponentFindNext(NULL, &desc);
  47. ERR_FAIL_COND_V(comp == NULL, FAILED);
  48. OSStatus result = AudioComponentInstanceNew(comp, &audio_unit);
  49. ERR_FAIL_COND_V(result != noErr, FAILED);
  50. AudioStreamBasicDescription strdesc;
  51. // TODO: Implement this
  52. /*zeromem(&strdesc, sizeof(strdesc));
  53. UInt32 size = sizeof(strdesc);
  54. result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &strdesc, &size);
  55. ERR_FAIL_COND_V(result != noErr, FAILED);
  56. switch (strdesc.mChannelsPerFrame) {
  57. case 2: // Stereo
  58. case 6: // Surround 5.1
  59. case 8: // Surround 7.1
  60. channels = strdesc.mChannelsPerFrame;
  61. break;
  62. default:
  63. // Unknown number of channels, default to stereo
  64. channels = 2;
  65. break;
  66. }*/
  67. mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
  68. zeromem(&strdesc, sizeof(strdesc));
  69. strdesc.mFormatID = kAudioFormatLinearPCM;
  70. strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  71. strdesc.mChannelsPerFrame = channels;
  72. strdesc.mSampleRate = mix_rate;
  73. strdesc.mFramesPerPacket = 1;
  74. strdesc.mBitsPerChannel = 16;
  75. strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
  76. strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
  77. result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc));
  78. ERR_FAIL_COND_V(result != noErr, FAILED);
  79. int latency = GLOBAL_DEF("audio/output_latency", 25);
  80. unsigned int buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  81. if (OS::get_singleton()->is_stdout_verbose()) {
  82. print_line("audio buffer size: " + itos(buffer_size) + " calculated latency: " + itos(buffer_size * 1000 / mix_rate));
  83. }
  84. samples_in.resize(buffer_size);
  85. buffer_frames = buffer_size / channels;
  86. AURenderCallbackStruct callback;
  87. zeromem(&callback, sizeof(AURenderCallbackStruct));
  88. callback.inputProc = &AudioDriverOSX::output_callback;
  89. callback.inputProcRefCon = this;
  90. result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
  91. ERR_FAIL_COND_V(result != noErr, FAILED);
  92. result = AudioUnitInitialize(audio_unit);
  93. ERR_FAIL_COND_V(result != noErr, FAILED);
  94. return OK;
  95. }
  96. Error AudioDriverOSX::finishDevice() {
  97. OSStatus result;
  98. if (active) {
  99. result = AudioOutputUnitStop(audio_unit);
  100. ERR_FAIL_COND_V(result != noErr, FAILED);
  101. active = false;
  102. }
  103. result = AudioUnitUninitialize(audio_unit);
  104. ERR_FAIL_COND_V(result != noErr, FAILED);
  105. return OK;
  106. }
  107. Error AudioDriverOSX::init() {
  108. OSStatus result;
  109. mutex = Mutex::create();
  110. active = false;
  111. channels = 2;
  112. outputDeviceAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
  113. outputDeviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
  114. outputDeviceAddress.mElement = kAudioObjectPropertyElementMaster;
  115. result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
  116. ERR_FAIL_COND_V(result != noErr, FAILED);
  117. return initDevice();
  118. };
  119. Error AudioDriverOSX::reopen() {
  120. bool restart = false;
  121. lock();
  122. if (active) {
  123. restart = true;
  124. }
  125. Error err = finishDevice();
  126. if (err != OK) {
  127. ERR_PRINT("finishDevice failed");
  128. unlock();
  129. return err;
  130. }
  131. err = initDevice();
  132. if (err != OK) {
  133. ERR_PRINT("initDevice failed");
  134. unlock();
  135. return err;
  136. }
  137. if (restart) {
  138. start();
  139. }
  140. unlock();
  141. return OK;
  142. }
  143. OSStatus AudioDriverOSX::output_callback(void *inRefCon,
  144. AudioUnitRenderActionFlags *ioActionFlags,
  145. const AudioTimeStamp *inTimeStamp,
  146. UInt32 inBusNumber, UInt32 inNumberFrames,
  147. AudioBufferList *ioData) {
  148. AudioDriverOSX *ad = (AudioDriverOSX *)inRefCon;
  149. if (!ad->active || !ad->try_lock()) {
  150. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  151. AudioBuffer *abuf = &ioData->mBuffers[i];
  152. zeromem(abuf->mData, abuf->mDataByteSize);
  153. };
  154. return 0;
  155. };
  156. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  157. AudioBuffer *abuf = &ioData->mBuffers[i];
  158. int frames_left = inNumberFrames;
  159. int16_t *out = (int16_t *)abuf->mData;
  160. while (frames_left) {
  161. int frames = MIN(frames_left, ad->buffer_frames);
  162. ad->audio_server_process(frames, ad->samples_in.ptr());
  163. for (int j = 0; j < frames * ad->channels; j++) {
  164. out[j] = ad->samples_in[j] >> 16;
  165. }
  166. frames_left -= frames;
  167. out += frames * ad->channels;
  168. };
  169. };
  170. ad->unlock();
  171. return 0;
  172. };
  173. void AudioDriverOSX::start() {
  174. if (!active) {
  175. OSStatus result = AudioOutputUnitStart(audio_unit);
  176. if (result != noErr) {
  177. ERR_PRINT("AudioOutputUnitStart failed");
  178. } else {
  179. active = true;
  180. }
  181. }
  182. };
  183. int AudioDriverOSX::get_mix_rate() const {
  184. return 44100;
  185. };
  186. AudioDriverSW::OutputFormat AudioDriverOSX::get_output_format() const {
  187. return OUTPUT_STEREO;
  188. };
  189. void AudioDriverOSX::lock() {
  190. if (mutex)
  191. mutex->lock();
  192. };
  193. void AudioDriverOSX::unlock() {
  194. if (mutex)
  195. mutex->unlock();
  196. };
  197. bool AudioDriverOSX::try_lock() {
  198. if (mutex)
  199. return mutex->try_lock() == OK;
  200. return true;
  201. }
  202. void AudioDriverOSX::finish() {
  203. finishDevice();
  204. OSStatus result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
  205. if (result != noErr) {
  206. ERR_PRINT("AudioObjectRemovePropertyListener failed");
  207. }
  208. AURenderCallbackStruct callback;
  209. zeromem(&callback, sizeof(AURenderCallbackStruct));
  210. result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
  211. if (result != noErr) {
  212. ERR_PRINT("AudioUnitSetProperty failed");
  213. }
  214. if (mutex) {
  215. memdelete(mutex);
  216. mutex = NULL;
  217. }
  218. };
  219. AudioDriverOSX::AudioDriverOSX() {
  220. active = false;
  221. mutex = NULL;
  222. mix_rate = 44100;
  223. channels = 2;
  224. samples_in.clear();
  225. };
  226. AudioDriverOSX::~AudioDriverOSX(){};
  227. #endif