audio_driver_jandroid.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* audio_driver_jandroid.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_jandroid.h"
  30. #include "globals.h"
  31. #include "os/os.h"
  32. #include "thread_jandroid.h"
  33. #ifndef ANDROID_NATIVE_ACTIVITY
  34. AudioDriverAndroid* AudioDriverAndroid::s_ad=NULL;
  35. jobject AudioDriverAndroid::io;
  36. jmethodID AudioDriverAndroid::_init_audio;
  37. jmethodID AudioDriverAndroid::_write_buffer;
  38. jmethodID AudioDriverAndroid::_quit;
  39. jmethodID AudioDriverAndroid::_pause;
  40. bool AudioDriverAndroid::active=false;
  41. jclass AudioDriverAndroid::cls;
  42. int AudioDriverAndroid::audioBufferFrames=0;
  43. int AudioDriverAndroid::mix_rate=44100;
  44. bool AudioDriverAndroid::quit=false;
  45. jobject AudioDriverAndroid::audioBuffer = NULL;
  46. void* AudioDriverAndroid::audioBufferPinned = NULL;
  47. Mutex *AudioDriverAndroid::mutex=NULL;
  48. int32_t* AudioDriverAndroid::audioBuffer32=NULL;
  49. const char* AudioDriverAndroid::get_name() const {
  50. return "Android";
  51. }
  52. Error AudioDriverAndroid::init(){
  53. mutex = Mutex::create();
  54. /*
  55. // TODO: pass in/return a (Java) device ID, also whether we're opening for input or output
  56. this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
  57. SDL_CalculateAudioSpec(&this->spec);
  58. if (this->spec.samples == 0) {
  59. // Init failed?
  60. SDL_SetError("Java-side initialization failed!");
  61. return 0;
  62. }
  63. */
  64. // Android_JNI_SetupThread();
  65. // __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
  66. JNIEnv *env = ThreadAndroid::get_env();
  67. int mix_rate = GLOBAL_DEF("audio/mix_rate",44100);
  68. int latency = GLOBAL_DEF("audio/output_latency",25);
  69. latency=50;
  70. unsigned int buffer_size = nearest_power_of_2( latency * mix_rate / 1000 );
  71. if (OS::get_singleton()->is_stdout_verbose()) {
  72. print_line("audio buffer size: "+itos(buffer_size));
  73. }
  74. __android_log_print(ANDROID_LOG_INFO,"godot","Initializing audio! params: %i,%i ",mix_rate,buffer_size);
  75. audioBuffer = env->CallObjectMethod(io,_init_audio, mix_rate, buffer_size);
  76. ERR_FAIL_COND_V( audioBuffer == NULL, ERR_INVALID_PARAMETER);
  77. audioBuffer = env->NewGlobalRef(audioBuffer);
  78. jboolean isCopy = JNI_FALSE;
  79. audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
  80. audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
  81. audioBuffer32 = memnew_arr(int32_t,audioBufferFrames);
  82. return OK;
  83. }
  84. void AudioDriverAndroid::start(){
  85. active=true;
  86. }
  87. void AudioDriverAndroid::setup( jobject p_io) {
  88. JNIEnv *env = ThreadAndroid::get_env();
  89. io=p_io;
  90. jclass c = env->GetObjectClass(io);
  91. cls = (jclass)env->NewGlobalRef(c);
  92. __android_log_print(ANDROID_LOG_INFO,"godot","starting to attempt get methods");
  93. _init_audio = env->GetMethodID(cls, "audioInit", "(II)Ljava/lang/Object;");
  94. if(_init_audio != 0) {
  95. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _init_audio ok!!");
  96. } else {
  97. __android_log_print(ANDROID_LOG_INFO,"godot","audioinit ok!");
  98. }
  99. _write_buffer = env->GetMethodID(cls, "audioWriteShortBuffer", "([S)V");
  100. if(_write_buffer != 0) {
  101. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _write_buffer ok!!");
  102. }
  103. _quit = env->GetMethodID(cls, "audioQuit", "()V");
  104. if(_quit != 0) {
  105. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _quit ok!!");
  106. }
  107. _pause = env->GetMethodID(cls, "audioPause", "(Z)V");
  108. if(_quit != 0) {
  109. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _pause ok!!");
  110. }
  111. }
  112. void AudioDriverAndroid::thread_func(JNIEnv *env) {
  113. jclass cls = env->FindClass("com/android/godot/Godot");
  114. if (cls) {
  115. cls=(jclass)env->NewGlobalRef(cls);
  116. __android_log_print(ANDROID_LOG_INFO,"godot","*******CLASS FOUND!!!");
  117. }
  118. jfieldID fid = env->GetStaticFieldID(cls, "io", "Lcom/android/godot/GodotIO;");
  119. jobject ob = env->GetStaticObjectField(cls,fid);
  120. jobject gob = env->NewGlobalRef(ob);
  121. jclass c = env->GetObjectClass(gob);
  122. jclass lcls = (jclass)env->NewGlobalRef(c);
  123. _write_buffer = env->GetMethodID(lcls, "audioWriteShortBuffer", "([S)V");
  124. if(_write_buffer != 0) {
  125. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _write_buffer ok!!");
  126. }
  127. while(!quit) {
  128. int16_t* ptr = (int16_t*)audioBufferPinned;
  129. int fc = audioBufferFrames;
  130. if (!s_ad->active || mutex->try_lock()!=OK) {
  131. for(int i=0;i<fc;i++) {
  132. ptr[i]=0;
  133. }
  134. } else {
  135. s_ad->audio_server_process(fc/2,audioBuffer32);
  136. mutex->unlock();
  137. for(int i=0;i<fc;i++) {
  138. ptr[i]=audioBuffer32[i]>>16;
  139. }
  140. }
  141. env->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)ptr, JNI_COMMIT);
  142. env->CallVoidMethod(gob, _write_buffer, (jshortArray)audioBuffer);
  143. }
  144. }
  145. int AudioDriverAndroid::get_mix_rate() const {
  146. return mix_rate;
  147. }
  148. AudioDriverSW::OutputFormat AudioDriverAndroid::get_output_format() const{
  149. return OUTPUT_STEREO;
  150. }
  151. void AudioDriverAndroid::lock(){
  152. if (mutex)
  153. mutex->lock();
  154. }
  155. void AudioDriverAndroid::unlock() {
  156. if (mutex)
  157. mutex->unlock();
  158. }
  159. void AudioDriverAndroid::finish(){
  160. JNIEnv *env = ThreadAndroid::get_env();
  161. env->CallVoidMethod(io, _quit);
  162. if (audioBuffer) {
  163. env->DeleteGlobalRef(audioBuffer);
  164. audioBuffer = NULL;
  165. audioBufferPinned = NULL;
  166. }
  167. active=false;
  168. }
  169. void AudioDriverAndroid::set_pause(bool p_pause) {
  170. JNIEnv *env = ThreadAndroid::get_env();
  171. env->CallVoidMethod(io, _pause,p_pause);
  172. }
  173. AudioDriverAndroid::AudioDriverAndroid()
  174. {
  175. s_ad=this;
  176. active=false;
  177. }
  178. #endif