jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.8KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. Copyright (C) 2013 Samsung Electronics
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include "JackOpenSLESDriver.h"
  18. #include "JackDriverLoader.h"
  19. #include "JackThreadedDriver.h"
  20. #include "JackEngineControl.h"
  21. #include "JackGraphManager.h"
  22. #include "JackCompilerDeps.h"
  23. #include <iostream>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <math.h>
  27. #include <android/log.h>
  28. #include "opensl_io.h"
  29. #define JACK_OPENSLES_DEFAULT_SAMPLERATE 48000
  30. #define JACK_OPENSLES_DEFAULT_BUFFER_SIZE 960
  31. namespace Jack
  32. {
  33. static OPENSL_STREAM *pOpenSL_stream;
  34. int JackOpenSLESDriver::Open(jack_nframes_t buffer_size,
  35. jack_nframes_t samplerate,
  36. bool capturing,
  37. bool playing,
  38. int inchannels,
  39. int outchannels,
  40. bool monitor,
  41. const char* capture_driver_uid,
  42. const char* playback_driver_uid,
  43. jack_nframes_t capture_latency,
  44. jack_nframes_t playback_latency) {
  45. jack_log("JackOpenSLESDriver::Open");
  46. // Generic JackAudioDriver Open
  47. if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
  48. capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  49. return -1;
  50. }
  51. if (capturing) {
  52. inbuffer = (float *) malloc(sizeof(float) * buffer_size); //mono input
  53. memset(inbuffer, 0, sizeof(float) * buffer_size);
  54. }
  55. if (playing) {
  56. outbuffer = (float *) malloc(sizeof(float) * buffer_size * 2); //stereo output
  57. memset(outbuffer, 0, sizeof(float) * buffer_size * 2);
  58. }
  59. pOpenSL_stream = android_OpenAudioDevice(samplerate, capturing ? 1 : 0, playing ? 2 : 0, buffer_size);
  60. if (pOpenSL_stream == NULL) return -1;
  61. return 0;
  62. }
  63. int JackOpenSLESDriver::Close() {
  64. jack_log("JackOpenSLESDriver::Close");
  65. // Generic audio driver close
  66. int res = JackAudioDriver::Close();
  67. android_CloseAudioDevice(pOpenSL_stream);
  68. if (inbuffer) {
  69. free(inbuffer);
  70. inbuffer = NULL;
  71. }
  72. if (outbuffer) {
  73. free(outbuffer);
  74. outbuffer = NULL;
  75. }
  76. return res;
  77. }
  78. int JackOpenSLESDriver::Read() {
  79. //jack_log("JackOpenSLESDriver::Read");
  80. jack_default_audio_sample_t* inputBuffer_1 = GetInputBuffer(0);
  81. jack_default_audio_sample_t* inputBuffer_2 = GetInputBuffer(1);
  82. if (inbuffer) {
  83. int samps = android_AudioIn(pOpenSL_stream,inbuffer,fEngineControl->fBufferSize);
  84. for (int i = 0; i < samps; i++) {
  85. *(inputBuffer_1 + i) = *(inbuffer + i);
  86. *(inputBuffer_2 + i) = *(inbuffer + i);
  87. }
  88. } else {
  89. for (int i = 0; i < fCaptureChannels; i++) {
  90. memset(GetInputBuffer(i), 0, sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize); //silence
  91. }
  92. }
  93. return 0;
  94. }
  95. int JackOpenSLESDriver::Write() {
  96. //jack_log("JackOpenSLESDriver::Write");
  97. jack_default_audio_sample_t* outputBuffer_1 = GetOutputBuffer(0);
  98. jack_default_audio_sample_t* outputBuffer_2 = GetOutputBuffer(1);
  99. if (outbuffer) {
  100. android_AudioOut(pOpenSL_stream, outbuffer, fEngineControl->fBufferSize * 2); //stereo output
  101. for (unsigned int i = 0, j = 0; i < fEngineControl->fBufferSize; i++) {
  102. *(outbuffer + j) = *(outputBuffer_1 + i); j++;
  103. *(outbuffer + j) = *(outputBuffer_2 + i); j++;
  104. }
  105. }
  106. return 0;
  107. }
  108. int JackOpenSLESDriver::SetBufferSize(jack_nframes_t buffer_size) {
  109. jack_log("JackOpenSLESDriver::SetBufferSize");
  110. JackAudioDriver::SetBufferSize(buffer_size);
  111. return 0;
  112. }
  113. } // end of namespace
  114. #ifdef __cplusplus
  115. extern "C"
  116. {
  117. #endif
  118. SERVER_EXPORT jack_driver_desc_t * driver_get_descriptor () {
  119. jack_driver_desc_t * desc;
  120. jack_driver_desc_filler_t filler;
  121. jack_driver_param_value_t value;
  122. desc = jack_driver_descriptor_construct("opensles", JackDriverMaster, "Timer based backend", &filler);
  123. value.ui = 2U;
  124. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamUInt, &value, NULL, "Number of capture ports", NULL);
  125. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamUInt, &value, NULL, "Number of playback ports", NULL);
  126. value.ui = 48000U;
  127. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  128. value.i = 0;
  129. jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
  130. value.ui = JACK_OPENSLES_DEFAULT_BUFFER_SIZE;
  131. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  132. value.ui = 21333U;
  133. jack_driver_descriptor_add_parameter(desc, &filler, "wait", 'w', JackDriverParamUInt, &value, NULL, "Number of usecs to wait between engine processes", NULL);
  134. return desc;
  135. }
  136. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  137. jack_nframes_t sample_rate = JACK_OPENSLES_DEFAULT_SAMPLERATE;
  138. jack_nframes_t buffer_size = JACK_OPENSLES_DEFAULT_BUFFER_SIZE;
  139. unsigned int capture_ports = 0;
  140. unsigned int playback_ports = 2;
  141. int wait_time = 0;
  142. const JSList * node;
  143. const jack_driver_param_t * param;
  144. bool monitor = false;
  145. for (node = params; node; node = jack_slist_next (node)) {
  146. param = (const jack_driver_param_t *) node->data;
  147. switch (param->character) {
  148. case 'C':
  149. capture_ports = param->value.ui;
  150. break;
  151. case 'P':
  152. playback_ports = param->value.ui;
  153. break;
  154. case 'r':
  155. sample_rate = param->value.ui;
  156. break;
  157. case 'p':
  158. buffer_size = param->value.ui;
  159. break;
  160. case 'w':
  161. wait_time = param->value.ui;
  162. break;
  163. case 'm':
  164. monitor = param->value.i;
  165. break;
  166. }
  167. }
  168. if (wait_time > 0) {
  169. buffer_size = lroundf((wait_time * sample_rate) / 1000000.0f);
  170. if (buffer_size > BUFFER_SIZE_MAX) {
  171. buffer_size = BUFFER_SIZE_MAX;
  172. jack_error("Buffer size set to %d", BUFFER_SIZE_MAX);
  173. }
  174. }
  175. Jack::JackDriverClientInterface* driver = new Jack::JackThreadedDriver(new Jack::JackOpenSLESDriver("system", "opensles_pcm", engine, table));
  176. if (driver->Open(buffer_size, sample_rate, capture_ports? 1 : 0, playback_ports? 1 : 0, capture_ports, playback_ports, monitor, "opensles", "opensles", 0, 0) == 0) {
  177. return driver;
  178. } else {
  179. delete driver;
  180. return NULL;
  181. }
  182. }
  183. #ifdef __cplusplus
  184. }
  185. #endif