audio_driver_iphone.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************/
  2. /* audio_driver_iphone.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. #include "audio_driver_iphone.h"
  30. Error AudioDriverIphone::init() {
  31. active = false;
  32. channels = 2;
  33. AudioStreamBasicDescription strdesc;
  34. strdesc.mFormatID = kAudioFormatLinearPCM;
  35. strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  36. strdesc.mChannelsPerFrame = channels;
  37. strdesc.mSampleRate = 44100;
  38. strdesc.mFramesPerPacket = 1;
  39. strdesc.mBitsPerChannel = 16;
  40. strdesc.mBytesPerFrame =
  41. strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
  42. strdesc.mBytesPerPacket =
  43. strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
  44. OSStatus result = noErr;
  45. AURenderCallbackStruct callback;
  46. AudioComponentDescription desc;
  47. AudioComponent comp = NULL;
  48. const AudioUnitElement output_bus = 0;
  49. const AudioUnitElement bus = output_bus;
  50. const AudioUnitScope scope = kAudioUnitScope_Input;
  51. zeromem(&desc, sizeof(desc));
  52. desc.componentType = kAudioUnitType_Output;
  53. desc.componentSubType = kAudioUnitSubType_RemoteIO; /* !!! FIXME: ? */
  54. comp = AudioComponentFindNext(NULL, &desc);
  55. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  56. result = AudioComponentInstanceNew(comp, &audio_unit);
  57. ERR_FAIL_COND_V(result != noErr, FAILED);
  58. ERR_FAIL_COND_V(comp == NULL, FAILED);
  59. result = AudioUnitSetProperty(audio_unit,
  60. kAudioUnitProperty_StreamFormat,
  61. scope, bus, &strdesc, sizeof(strdesc));
  62. ERR_FAIL_COND_V(result != noErr, FAILED);
  63. zeromem(&callback, sizeof(AURenderCallbackStruct));
  64. callback.inputProc = &AudioDriverIphone::output_callback;
  65. callback.inputProcRefCon = this;
  66. result = AudioUnitSetProperty(audio_unit,
  67. kAudioUnitProperty_SetRenderCallback,
  68. scope, bus, &callback, sizeof(callback));
  69. ERR_FAIL_COND_V(result != noErr, FAILED);
  70. result = AudioUnitInitialize(audio_unit);
  71. ERR_FAIL_COND_V(result != noErr, FAILED);
  72. result = AudioOutputUnitStart(audio_unit);
  73. ERR_FAIL_COND_V(result != noErr, FAILED);
  74. const int samples = 1024;
  75. samples_in = memnew_arr(int32_t, samples); // whatever
  76. buffer_frames = samples / channels;
  77. return FAILED;
  78. };
  79. OSStatus AudioDriverIphone::output_callback(void *inRefCon,
  80. AudioUnitRenderActionFlags * ioActionFlags,
  81. const AudioTimeStamp * inTimeStamp,
  82. UInt32 inBusNumber, UInt32 inNumberFrames,
  83. AudioBufferList * ioData) {
  84. AudioBuffer *abuf;
  85. AudioDriverIphone* ad = (AudioDriverIphone*)inRefCon;
  86. bool mix = true;
  87. if (!ad->active)
  88. mix = false;
  89. else if (ad->mutex) {
  90. mix = ad->mutex->try_lock() == OK;
  91. };
  92. if (!mix) {
  93. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  94. abuf = &ioData->mBuffers[i];
  95. zeromem(abuf->mData, abuf->mDataByteSize);
  96. };
  97. return 0;
  98. };
  99. int frames_left;
  100. for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
  101. abuf = &ioData->mBuffers[i];
  102. frames_left = inNumberFrames;
  103. int16_t* out = (int16_t*)abuf->mData;
  104. while (frames_left) {
  105. int frames = MIN(frames_left, ad->buffer_frames);
  106. //ad->lock();
  107. ad->audio_server_process(frames, ad->samples_in);
  108. //ad->unlock();
  109. for(int i = 0; i < frames * ad->channels; i++) {
  110. out[i] = ad->samples_in[i]>>16;
  111. }
  112. frames_left -= frames;
  113. out += frames * ad->channels;
  114. };
  115. };
  116. if (ad->mutex)
  117. ad->mutex->unlock();
  118. return 0;
  119. };
  120. void AudioDriverIphone::start() {
  121. active = true;
  122. };
  123. int AudioDriverIphone::get_mix_rate() const {
  124. return 44100;
  125. };
  126. AudioDriverSW::OutputFormat AudioDriverIphone::get_output_format() const {
  127. return OUTPUT_STEREO;
  128. };
  129. void AudioDriverIphone::lock() {
  130. if (active && mutex)
  131. mutex->lock();
  132. };
  133. void AudioDriverIphone::unlock() {
  134. if (active && mutex)
  135. mutex->unlock();
  136. };
  137. void AudioDriverIphone::finish() {
  138. memdelete_arr(samples_in);
  139. };
  140. AudioDriverIphone::AudioDriverIphone() {
  141. mutex=Mutex::create();//NULL;
  142. };
  143. AudioDriverIphone::~AudioDriverIphone() {
  144. };