audio_driver_opensl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**************************************************************************/
  2. /* audio_driver_opensl.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "audio_driver_opensl.h"
  31. #include <string.h>
  32. #define MAX_NUMBER_INTERFACES 3
  33. #define MAX_NUMBER_OUTPUT_DEVICES 6
  34. /* Structure for passing information to callback function */
  35. void AudioDriverOpenSL::_buffer_callback(
  36. SLAndroidSimpleBufferQueueItf queueItf) {
  37. bool mix = true;
  38. if (pause) {
  39. mix = false;
  40. } else {
  41. mix = mutex.try_lock() == OK;
  42. }
  43. if (mix) {
  44. audio_server_process(buffer_size, mixdown_buffer);
  45. } else {
  46. int32_t *src_buff = mixdown_buffer;
  47. for (unsigned int i = 0; i < buffer_size * 2; i++) {
  48. src_buff[i] = 0;
  49. }
  50. }
  51. if (mix) {
  52. mutex.unlock();
  53. }
  54. const int32_t *src_buff = mixdown_buffer;
  55. int16_t *ptr = (int16_t *)buffers[last_free];
  56. last_free = (last_free + 1) % BUFFER_COUNT;
  57. for (unsigned int i = 0; i < buffer_size * 2; i++) {
  58. ptr[i] = src_buff[i] >> 16;
  59. }
  60. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  61. }
  62. void AudioDriverOpenSL::_buffer_callbacks(
  63. SLAndroidSimpleBufferQueueItf queueItf,
  64. void *pContext) {
  65. AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext;
  66. ad->_buffer_callback(queueItf);
  67. }
  68. const char *AudioDriverOpenSL::get_name() const {
  69. return "Android";
  70. }
  71. Error AudioDriverOpenSL::init() {
  72. SLresult res;
  73. SLEngineOption EngineOption[] = {
  74. { (SLuint32)SL_ENGINEOPTION_THREADSAFE, (SLuint32)SL_BOOLEAN_TRUE }
  75. };
  76. res = slCreateEngine(&sl, 1, EngineOption, 0, nullptr, nullptr);
  77. ERR_FAIL_COND_V_MSG(res != SL_RESULT_SUCCESS, ERR_INVALID_PARAMETER, "Could not initialize OpenSL.");
  78. res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
  79. ERR_FAIL_COND_V_MSG(res != SL_RESULT_SUCCESS, ERR_INVALID_PARAMETER, "Could not realize OpenSL.");
  80. return OK;
  81. }
  82. void AudioDriverOpenSL::start() {
  83. active = false;
  84. SLresult res;
  85. buffer_size = 1024;
  86. for (int i = 0; i < BUFFER_COUNT; i++) {
  87. buffers[i] = memnew_arr(int16_t, buffer_size * 2);
  88. memset(buffers[i], 0, buffer_size * 4);
  89. }
  90. mixdown_buffer = memnew_arr(int32_t, buffer_size * 2);
  91. /* Callback context for the buffer queue callback function */
  92. /* Get the SL Engine Interface which is implicit */
  93. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf);
  94. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  95. {
  96. const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB };
  97. const SLboolean req[1] = { SL_BOOLEAN_FALSE };
  98. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req);
  99. }
  100. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  101. // Realizing the Output Mix object in synchronous mode.
  102. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  103. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  104. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT };
  105. /* Setup the format of the content in the buffer queue */
  106. pcm.formatType = SL_DATAFORMAT_PCM;
  107. pcm.numChannels = 2;
  108. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  109. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  110. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  111. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  112. #ifdef BIG_ENDIAN_ENABLED
  113. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  114. #else
  115. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  116. #endif
  117. audioSource.pFormat = (void *)&pcm;
  118. audioSource.pLocator = (void *)&loc_bufq;
  119. /* Setup the data sink structure */
  120. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  121. locator_outputmix.outputMix = OutputMix;
  122. audioSink.pLocator = (void *)&locator_outputmix;
  123. audioSink.pFormat = nullptr;
  124. /* Create the music player */
  125. {
  126. const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND };
  127. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  128. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req);
  129. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  130. }
  131. /* Realizing the player in synchronous mode. */
  132. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  133. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  134. /* Get seek and play interfaces */
  135. res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf);
  136. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  137. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  138. (void *)&bufferQueueItf);
  139. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  140. /* Setup to receive buffer queue event callbacks */
  141. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this);
  142. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  143. last_free = 0;
  144. //fill up buffers
  145. for (int i = 0; i < BUFFER_COUNT; i++) {
  146. /* Enqueue a few buffers to get the ball rolling */
  147. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */
  148. }
  149. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  150. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  151. active = true;
  152. }
  153. void AudioDriverOpenSL::_record_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) {
  154. for (int i = 0; i < rec_buffer.size(); i++) {
  155. int32_t sample = rec_buffer[i] << 16;
  156. input_buffer_write(sample);
  157. input_buffer_write(sample); // call twice to convert to Stereo
  158. }
  159. SLresult res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  160. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  161. }
  162. void AudioDriverOpenSL::_record_buffer_callbacks(SLAndroidSimpleBufferQueueItf queueItf, void *pContext) {
  163. AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext;
  164. ad->_record_buffer_callback(queueItf);
  165. }
  166. Error AudioDriverOpenSL::capture_init_device() {
  167. SLDataLocator_IODevice loc_dev = {
  168. SL_DATALOCATOR_IODEVICE,
  169. SL_IODEVICE_AUDIOINPUT,
  170. SL_DEFAULTDEVICEID_AUDIOINPUT,
  171. nullptr
  172. };
  173. SLDataSource recSource = { &loc_dev, nullptr };
  174. SLDataLocator_AndroidSimpleBufferQueue loc_bq = {
  175. SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
  176. 2
  177. };
  178. SLDataFormat_PCM format_pcm = {
  179. SL_DATAFORMAT_PCM,
  180. 1,
  181. SL_SAMPLINGRATE_44_1,
  182. SL_PCMSAMPLEFORMAT_FIXED_16,
  183. SL_PCMSAMPLEFORMAT_FIXED_16,
  184. SL_SPEAKER_FRONT_CENTER,
  185. SL_BYTEORDER_LITTLEENDIAN
  186. };
  187. SLDataSink recSnk = { &loc_bq, &format_pcm };
  188. const SLInterfaceID ids[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
  189. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  190. SLresult res = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recSnk, 2, ids, req);
  191. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  192. res = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE);
  193. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  194. res = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void *)&recordItf);
  195. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  196. res = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void *)&recordBufferQueueItf);
  197. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  198. res = (*recordBufferQueueItf)->RegisterCallback(recordBufferQueueItf, _record_buffer_callbacks, this);
  199. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  200. SLuint32 state;
  201. res = (*recordItf)->GetRecordState(recordItf, &state);
  202. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  203. if (state != SL_RECORDSTATE_STOPPED) {
  204. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  205. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  206. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  207. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  208. }
  209. const int rec_buffer_frames = 2048;
  210. rec_buffer.resize(rec_buffer_frames);
  211. input_buffer_init(rec_buffer_frames);
  212. res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  213. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  214. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING);
  215. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  216. return OK;
  217. }
  218. Error AudioDriverOpenSL::capture_start() {
  219. if (OS::get_singleton()->request_permission("RECORD_AUDIO")) {
  220. return capture_init_device();
  221. }
  222. return OK;
  223. }
  224. Error AudioDriverOpenSL::capture_stop() {
  225. SLuint32 state;
  226. SLresult res = (*recordItf)->GetRecordState(recordItf, &state);
  227. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  228. if (state != SL_RECORDSTATE_STOPPED) {
  229. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  230. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  231. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  232. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  233. }
  234. return OK;
  235. }
  236. int AudioDriverOpenSL::get_mix_rate() const {
  237. return 44100; // hardcoded for Android, as selected by SL_SAMPLINGRATE_44_1
  238. }
  239. AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const {
  240. return SPEAKER_MODE_STEREO;
  241. }
  242. void AudioDriverOpenSL::lock() {
  243. if (active) {
  244. mutex.lock();
  245. }
  246. }
  247. void AudioDriverOpenSL::unlock() {
  248. if (active) {
  249. mutex.unlock();
  250. }
  251. }
  252. void AudioDriverOpenSL::finish() {
  253. (*sl)->Destroy(sl);
  254. }
  255. void AudioDriverOpenSL::set_pause(bool p_pause) {
  256. pause = p_pause;
  257. if (active) {
  258. if (pause) {
  259. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  260. } else {
  261. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  262. }
  263. }
  264. }
  265. AudioDriverOpenSL::AudioDriverOpenSL() {
  266. }