Audio plugin host https://kx.studio/carla
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.

juce_android_Midi.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  20. METHOD (getJuceAndroidMidiInputDevices, "getJuceAndroidMidiInputDevices", "()[Ljava/lang/String;") \
  21. METHOD (getJuceAndroidMidiOutputDevices, "getJuceAndroidMidiOutputDevices", "()[Ljava/lang/String;") \
  22. METHOD (openMidiInputPortWithJuceIndex, "openMidiInputPortWithJuceIndex", "(IJ)L" JUCE_ANDROID_ACTIVITY_CLASSPATH "$JuceMidiPort;") \
  23. METHOD (openMidiOutputPortWithJuceIndex, "openMidiOutputPortWithJuceIndex", "(I)L" JUCE_ANDROID_ACTIVITY_CLASSPATH "$JuceMidiPort;") \
  24. METHOD (getInputPortNameForJuceIndex, "getInputPortNameForJuceIndex", "(I)Ljava/lang/String;") \
  25. METHOD (getOutputPortNameForJuceIndex, "getOutputPortNameForJuceIndex", "(I)Ljava/lang/String;")
  26. DECLARE_JNI_CLASS (MidiDeviceManager, JUCE_ANDROID_ACTIVITY_CLASSPATH "$MidiDeviceManager")
  27. #undef JNI_CLASS_MEMBERS
  28. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  29. METHOD (start, "start", "()V" )\
  30. METHOD (stop, "stop", "()V") \
  31. METHOD (close, "close", "()V") \
  32. METHOD (sendMidi, "sendMidi", "([BII)V")
  33. DECLARE_JNI_CLASS (JuceMidiPort, JUCE_ANDROID_ACTIVITY_CLASSPATH "$JuceMidiPort")
  34. #undef JNI_CLASS_MEMBERS
  35. //==============================================================================
  36. class AndroidMidiInput
  37. {
  38. public:
  39. AndroidMidiInput (MidiInput* midiInput, int portIdx,
  40. juce::MidiInputCallback* midiInputCallback, jobject deviceManager)
  41. : juceMidiInput (midiInput),
  42. callback (midiInputCallback),
  43. midiConcatenator (2048),
  44. javaMidiDevice (getEnv()->CallObjectMethod (deviceManager,
  45. MidiDeviceManager.openMidiInputPortWithJuceIndex,
  46. (jint) portIdx,
  47. (jlong) this))
  48. {
  49. }
  50. ~AndroidMidiInput()
  51. {
  52. if (jobject d = javaMidiDevice.get())
  53. {
  54. getEnv()->CallVoidMethod (d, JuceMidiPort.close);
  55. javaMidiDevice.clear();
  56. }
  57. }
  58. bool isOpen() const noexcept
  59. {
  60. return javaMidiDevice != nullptr;
  61. }
  62. void start()
  63. {
  64. if (jobject d = javaMidiDevice.get())
  65. getEnv()->CallVoidMethod (d, JuceMidiPort.start);
  66. }
  67. void stop()
  68. {
  69. if (jobject d = javaMidiDevice.get())
  70. getEnv()->CallVoidMethod (d, JuceMidiPort.stop);
  71. callback = nullptr;
  72. }
  73. void receive (jbyteArray byteArray, jlong offset, jint len, jlong timestamp)
  74. {
  75. jassert (byteArray != nullptr);
  76. jbyte* data = getEnv()->GetByteArrayElements (byteArray, nullptr);
  77. HeapBlock<uint8> buffer (static_cast<size_t> (len));
  78. std::memcpy (buffer.get(), data + offset, static_cast<size_t> (len));
  79. midiConcatenator.pushMidiData (buffer.get(),
  80. len, static_cast<double> (timestamp) * 1.0e-9,
  81. juceMidiInput, *callback);
  82. getEnv()->ReleaseByteArrayElements (byteArray, data, 0);
  83. }
  84. private:
  85. MidiInput* juceMidiInput;
  86. MidiInputCallback* callback;
  87. MidiDataConcatenator midiConcatenator;
  88. GlobalRef javaMidiDevice;
  89. };
  90. //==============================================================================
  91. class AndroidMidiOutput
  92. {
  93. public:
  94. AndroidMidiOutput (jobject midiDevice)
  95. : javaMidiDevice (midiDevice)
  96. {
  97. }
  98. ~AndroidMidiOutput()
  99. {
  100. if (jobject d = javaMidiDevice.get())
  101. {
  102. getEnv()->CallVoidMethod (d, JuceMidiPort.close);
  103. javaMidiDevice.clear();
  104. }
  105. }
  106. void send (jbyteArray byteArray, jint offset, jint len)
  107. {
  108. if (jobject d = javaMidiDevice.get())
  109. getEnv()->CallVoidMethod (d,
  110. JuceMidiPort.sendMidi,
  111. byteArray, offset, len);
  112. }
  113. private:
  114. GlobalRef javaMidiDevice;
  115. };
  116. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024JuceMidiInputPort), handleReceive,
  117. void, (JNIEnv* env, jobject, jlong host, jbyteArray byteArray,
  118. jint offset, jint count, jlong timestamp))
  119. {
  120. // Java may create a Midi thread which JUCE doesn't know about and this callback may be
  121. // received on this thread. Java will have already created a JNI Env for this new thread,
  122. // which we need to tell Juce about
  123. setEnv (env);
  124. reinterpret_cast<AndroidMidiInput*> (host)->receive (byteArray, offset, count, timestamp);
  125. }
  126. //==============================================================================
  127. class AndroidMidiDeviceManager
  128. {
  129. public:
  130. AndroidMidiDeviceManager()
  131. : deviceManager (android.activity.callObjectMethod (JuceAppActivity.getAndroidMidiDeviceManager))
  132. {
  133. }
  134. String getInputPortNameForJuceIndex (int idx)
  135. {
  136. if (jobject dm = deviceManager.get())
  137. {
  138. LocalRef<jstring> string ((jstring) getEnv()->CallObjectMethod (dm, MidiDeviceManager.getInputPortNameForJuceIndex, idx));
  139. return juceString (string);
  140. }
  141. return {};
  142. }
  143. String getOutputPortNameForJuceIndex (int idx)
  144. {
  145. if (jobject dm = deviceManager.get())
  146. {
  147. LocalRef<jstring> string ((jstring) getEnv()->CallObjectMethod (dm, MidiDeviceManager.getOutputPortNameForJuceIndex, idx));
  148. return juceString (string);
  149. }
  150. return {};
  151. }
  152. StringArray getDevices (bool input)
  153. {
  154. if (jobject dm = deviceManager.get())
  155. {
  156. jobjectArray jDevices
  157. = (jobjectArray) getEnv()->CallObjectMethod (dm, input ? MidiDeviceManager.getJuceAndroidMidiInputDevices
  158. : MidiDeviceManager.getJuceAndroidMidiOutputDevices);
  159. // Create a local reference as converting this
  160. // to a JUCE string will call into JNI
  161. LocalRef<jobjectArray> devices (jDevices);
  162. return javaStringArrayToJuce (devices);
  163. }
  164. return {};
  165. }
  166. AndroidMidiInput* openMidiInputPortWithIndex (int idx, MidiInput* juceMidiInput, juce::MidiInputCallback* callback)
  167. {
  168. if (jobject dm = deviceManager.get())
  169. {
  170. ScopedPointer<AndroidMidiInput> androidMidiInput (new AndroidMidiInput (juceMidiInput, idx, callback, dm));
  171. if (androidMidiInput->isOpen())
  172. return androidMidiInput.release();
  173. }
  174. return nullptr;
  175. }
  176. AndroidMidiOutput* openMidiOutputPortWithIndex (int idx)
  177. {
  178. if (jobject dm = deviceManager.get())
  179. if (jobject javaMidiPort = getEnv()->CallObjectMethod (dm, MidiDeviceManager.openMidiOutputPortWithJuceIndex, (jint) idx))
  180. return new AndroidMidiOutput (javaMidiPort);
  181. return nullptr;
  182. }
  183. private:
  184. static StringArray javaStringArrayToJuce (jobjectArray jStrings)
  185. {
  186. StringArray retval;
  187. JNIEnv* env = getEnv();
  188. const int count = env->GetArrayLength (jStrings);
  189. for (int i = 0; i < count; ++i)
  190. {
  191. LocalRef<jstring> string ((jstring) env->GetObjectArrayElement (jStrings, i));
  192. retval.add (juceString (string));
  193. }
  194. return retval;
  195. }
  196. GlobalRef deviceManager;
  197. };
  198. //==============================================================================
  199. StringArray MidiOutput::getDevices()
  200. {
  201. AndroidMidiDeviceManager manager;
  202. return manager.getDevices (false);
  203. }
  204. int MidiOutput::getDefaultDeviceIndex()
  205. {
  206. return 0;
  207. }
  208. MidiOutput* MidiOutput::openDevice (int index)
  209. {
  210. if (index < 0)
  211. return nullptr;
  212. AndroidMidiDeviceManager manager;
  213. String midiOutputName = manager.getOutputPortNameForJuceIndex (index);
  214. if (midiOutputName.isEmpty())
  215. {
  216. // you supplied an invalid device index!
  217. jassertfalse;
  218. return nullptr;
  219. }
  220. if (AndroidMidiOutput* midiOutput = manager.openMidiOutputPortWithIndex (index))
  221. {
  222. MidiOutput* retval = new MidiOutput (midiOutputName);
  223. retval->internal = midiOutput;
  224. return retval;
  225. }
  226. return nullptr;
  227. }
  228. MidiOutput::~MidiOutput()
  229. {
  230. stopBackgroundThread();
  231. delete reinterpret_cast<AndroidMidiOutput*> (internal);
  232. }
  233. void MidiOutput::sendMessageNow (const MidiMessage& message)
  234. {
  235. if (AndroidMidiOutput* androidMidi = reinterpret_cast<AndroidMidiOutput*>(internal))
  236. {
  237. JNIEnv* env = getEnv();
  238. const int messageSize = message.getRawDataSize();
  239. LocalRef<jbyteArray> messageContent = LocalRef<jbyteArray> (env->NewByteArray (messageSize));
  240. jbyteArray content = messageContent.get();
  241. jbyte* rawBytes = env->GetByteArrayElements (content, nullptr);
  242. std::memcpy (rawBytes, message.getRawData(), static_cast<size_t> (messageSize));
  243. env->ReleaseByteArrayElements (content, rawBytes, 0);
  244. androidMidi->send (content, (jint) 0, (jint) messageSize);
  245. }
  246. }
  247. //==============================================================================
  248. MidiInput::MidiInput (const String& nm) : name (nm)
  249. {
  250. }
  251. StringArray MidiInput::getDevices()
  252. {
  253. AndroidMidiDeviceManager manager;
  254. return manager.getDevices (true);
  255. }
  256. int MidiInput::getDefaultDeviceIndex()
  257. {
  258. return 0;
  259. }
  260. MidiInput* MidiInput::openDevice (int index, juce::MidiInputCallback* callback)
  261. {
  262. if (index < 0)
  263. return nullptr;
  264. AndroidMidiDeviceManager manager;
  265. String midiInputName = manager.getInputPortNameForJuceIndex (index);
  266. if (midiInputName.isEmpty())
  267. {
  268. // you supplied an invalid device index!
  269. jassertfalse;
  270. return nullptr;
  271. }
  272. ScopedPointer<MidiInput> midiInput (new MidiInput (midiInputName));
  273. midiInput->internal = manager.openMidiInputPortWithIndex (index, midiInput, callback);
  274. return midiInput->internal != nullptr ? midiInput.release()
  275. : nullptr;
  276. }
  277. void MidiInput::start()
  278. {
  279. if (AndroidMidiInput* mi = reinterpret_cast<AndroidMidiInput*> (internal))
  280. mi->start();
  281. }
  282. void MidiInput::stop()
  283. {
  284. if (AndroidMidiInput* mi = reinterpret_cast<AndroidMidiInput*> (internal))
  285. mi->stop();
  286. }
  287. MidiInput::~MidiInput()
  288. {
  289. delete reinterpret_cast<AndroidMidiInput*> (internal);
  290. }
  291. } // namespace juce