audio_driver_opensl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* audio_driver_opensl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #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. const int32_t *src_buff = mixdown_buffer;
  54. int16_t *ptr = (int16_t *)buffers[last_free];
  55. last_free = (last_free + 1) % BUFFER_COUNT;
  56. for (unsigned int i = 0; i < buffer_size * 2; i++) {
  57. ptr[i] = src_buff[i] >> 16;
  58. }
  59. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  60. }
  61. void AudioDriverOpenSL::_buffer_callbacks(
  62. SLAndroidSimpleBufferQueueItf queueItf,
  63. void *pContext) {
  64. AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext;
  65. ad->_buffer_callback(queueItf);
  66. }
  67. AudioDriverOpenSL *AudioDriverOpenSL::s_ad = nullptr;
  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. //bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
  106. //bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */
  107. /* Setup the format of the content in the buffer queue */
  108. pcm.formatType = SL_DATAFORMAT_PCM;
  109. pcm.numChannels = 2;
  110. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  111. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  112. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  113. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  114. #ifdef BIG_ENDIAN_ENABLED
  115. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  116. #else
  117. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  118. #endif
  119. audioSource.pFormat = (void *)&pcm;
  120. audioSource.pLocator = (void *)&loc_bufq;
  121. /* Setup the data sink structure */
  122. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  123. locator_outputmix.outputMix = OutputMix;
  124. audioSink.pLocator = (void *)&locator_outputmix;
  125. audioSink.pFormat = nullptr;
  126. /* Initialize the context for Buffer queue callbacks */
  127. //cntxt.pDataBase = (void*)&pcmData;
  128. //cntxt.pData = cntxt.pDataBase;
  129. //cntxt.size = sizeof(pcmData);
  130. /* Create the music player */
  131. {
  132. const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND };
  133. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  134. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req);
  135. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  136. }
  137. /* Realizing the player in synchronous mode. */
  138. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  139. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  140. /* Get seek and play interfaces */
  141. res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf);
  142. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  143. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  144. (void *)&bufferQueueItf);
  145. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  146. /* Setup to receive buffer queue event callbacks */
  147. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this);
  148. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  149. last_free = 0;
  150. //fill up buffers
  151. for (int i = 0; i < BUFFER_COUNT; i++) {
  152. /* Enqueue a few buffers to get the ball rolling */
  153. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */
  154. }
  155. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  156. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  157. active = true;
  158. }
  159. void AudioDriverOpenSL::_record_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) {
  160. for (int i = 0; i < rec_buffer.size(); i++) {
  161. int32_t sample = rec_buffer[i] << 16;
  162. input_buffer_write(sample);
  163. input_buffer_write(sample); // call twice to convert to Stereo
  164. }
  165. SLresult res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  166. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  167. }
  168. void AudioDriverOpenSL::_record_buffer_callbacks(SLAndroidSimpleBufferQueueItf queueItf, void *pContext) {
  169. AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext;
  170. ad->_record_buffer_callback(queueItf);
  171. }
  172. Error AudioDriverOpenSL::capture_init_device() {
  173. SLDataLocator_IODevice loc_dev = {
  174. SL_DATALOCATOR_IODEVICE,
  175. SL_IODEVICE_AUDIOINPUT,
  176. SL_DEFAULTDEVICEID_AUDIOINPUT,
  177. nullptr
  178. };
  179. SLDataSource recSource = { &loc_dev, nullptr };
  180. SLDataLocator_AndroidSimpleBufferQueue loc_bq = {
  181. SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
  182. 2
  183. };
  184. SLDataFormat_PCM format_pcm = {
  185. SL_DATAFORMAT_PCM,
  186. 1,
  187. SL_SAMPLINGRATE_44_1,
  188. SL_PCMSAMPLEFORMAT_FIXED_16,
  189. SL_PCMSAMPLEFORMAT_FIXED_16,
  190. SL_SPEAKER_FRONT_CENTER,
  191. SL_BYTEORDER_LITTLEENDIAN
  192. };
  193. SLDataSink recSnk = { &loc_bq, &format_pcm };
  194. const SLInterfaceID ids[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
  195. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  196. SLresult res = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recSnk, 2, ids, req);
  197. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  198. res = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE);
  199. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  200. res = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void *)&recordItf);
  201. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  202. res = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void *)&recordBufferQueueItf);
  203. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  204. res = (*recordBufferQueueItf)->RegisterCallback(recordBufferQueueItf, _record_buffer_callbacks, this);
  205. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  206. SLuint32 state;
  207. res = (*recordItf)->GetRecordState(recordItf, &state);
  208. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  209. if (state != SL_RECORDSTATE_STOPPED) {
  210. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  211. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  212. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  213. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  214. }
  215. const int rec_buffer_frames = 2048;
  216. rec_buffer.resize(rec_buffer_frames);
  217. input_buffer_init(rec_buffer_frames);
  218. res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  219. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  220. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING);
  221. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  222. return OK;
  223. }
  224. Error AudioDriverOpenSL::capture_start() {
  225. if (OS::get_singleton()->request_permission("RECORD_AUDIO")) {
  226. return capture_init_device();
  227. }
  228. return OK;
  229. }
  230. Error AudioDriverOpenSL::capture_stop() {
  231. SLuint32 state;
  232. SLresult res = (*recordItf)->GetRecordState(recordItf, &state);
  233. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  234. if (state != SL_RECORDSTATE_STOPPED) {
  235. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  236. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  237. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  238. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  239. }
  240. return OK;
  241. }
  242. int AudioDriverOpenSL::get_mix_rate() const {
  243. return 44100; // hardcoded for Android, as selected by SL_SAMPLINGRATE_44_1
  244. }
  245. AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const {
  246. return SPEAKER_MODE_STEREO;
  247. }
  248. void AudioDriverOpenSL::lock() {
  249. if (active)
  250. mutex.lock();
  251. }
  252. void AudioDriverOpenSL::unlock() {
  253. if (active)
  254. mutex.unlock();
  255. }
  256. void AudioDriverOpenSL::finish() {
  257. (*sl)->Destroy(sl);
  258. }
  259. void AudioDriverOpenSL::set_pause(bool p_pause) {
  260. pause = p_pause;
  261. if (active) {
  262. if (pause) {
  263. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  264. } else {
  265. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  266. }
  267. }
  268. }
  269. AudioDriverOpenSL::AudioDriverOpenSL() {
  270. s_ad = this;
  271. }