The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

454 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE 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 JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../juce_Config.h"
  24. #if JUCE_BUILD_GUI_CLASSES
  25. #if JUCE_ALSA
  26. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  27. #include <alsa/asoundlib.h>
  28. BEGIN_JUCE_NAMESPACE
  29. #include "../../../src/juce_appframework/audio/devices/juce_MidiOutput.h"
  30. #include "../../../src/juce_appframework/audio/devices/juce_MidiInput.h"
  31. #include "../../../src/juce_core/threads/juce_Thread.h"
  32. #include "../../../src/juce_core/basics/juce_Time.h"
  33. //==============================================================================
  34. static snd_seq_t* iterateDevices (const bool forInput,
  35. StringArray& deviceNamesFound,
  36. const int deviceIndexToOpen)
  37. {
  38. snd_seq_t* returnedHandle = 0;
  39. snd_seq_t* seqHandle;
  40. if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
  41. : SND_SEQ_OPEN_OUTPUT, 0) == 0)
  42. {
  43. snd_seq_system_info_t* systemInfo;
  44. snd_seq_client_info_t* clientInfo;
  45. if (snd_seq_system_info_malloc (&systemInfo) == 0)
  46. {
  47. if (snd_seq_system_info (seqHandle, systemInfo) == 0
  48. && snd_seq_client_info_malloc (&clientInfo) == 0)
  49. {
  50. int numClients = snd_seq_system_info_get_cur_clients (systemInfo);
  51. while (--numClients >= 0 && returnedHandle == 0)
  52. {
  53. if (snd_seq_query_next_client (seqHandle, clientInfo) == 0)
  54. {
  55. snd_seq_port_info_t* portInfo;
  56. if (snd_seq_port_info_malloc (&portInfo) == 0)
  57. {
  58. int numPorts = snd_seq_client_info_get_num_ports (clientInfo);
  59. const int client = snd_seq_client_info_get_client (clientInfo);
  60. snd_seq_port_info_set_client (portInfo, client);
  61. snd_seq_port_info_set_port (portInfo, -1);
  62. while (--numPorts >= 0)
  63. {
  64. if (snd_seq_query_next_port (seqHandle, portInfo) == 0
  65. && (snd_seq_port_info_get_capability (portInfo)
  66. & (forInput ? SND_SEQ_PORT_CAP_READ
  67. : SND_SEQ_PORT_CAP_WRITE)) != 0)
  68. {
  69. deviceNamesFound.add (snd_seq_client_info_get_name (clientInfo));
  70. if (deviceNamesFound.size() == deviceIndexToOpen + 1)
  71. {
  72. const int sourcePort = snd_seq_port_info_get_port (portInfo);
  73. const int sourceClient = snd_seq_client_info_get_client (clientInfo);
  74. if (sourcePort != -1)
  75. {
  76. snd_seq_set_client_name (seqHandle,
  77. forInput ? "Juce Midi Input"
  78. : "Juce Midi Output");
  79. const int portId
  80. = snd_seq_create_simple_port (seqHandle,
  81. forInput ? "Juce Midi In Port"
  82. : "Juce Midi Out Port",
  83. forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
  84. : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
  85. SND_SEQ_PORT_TYPE_MIDI_GENERIC);
  86. snd_seq_connect_from (seqHandle, portId, sourceClient, sourcePort);
  87. returnedHandle = seqHandle;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. snd_seq_port_info_free (portInfo);
  94. }
  95. }
  96. }
  97. snd_seq_client_info_free (clientInfo);
  98. }
  99. snd_seq_system_info_free (systemInfo);
  100. }
  101. if (returnedHandle == 0)
  102. snd_seq_close (seqHandle);
  103. }
  104. return returnedHandle;
  105. }
  106. static snd_seq_t* createDevice (const bool forInput,
  107. const String& deviceNameToOpen)
  108. {
  109. snd_seq_t* seqHandle = 0;
  110. if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
  111. : SND_SEQ_OPEN_OUTPUT, 0) == 0)
  112. {
  113. snd_seq_set_client_name (seqHandle,
  114. (const char*) (forInput ? (deviceNameToOpen + T(" Input"))
  115. : (deviceNameToOpen + T(" Output"))));
  116. const int portId
  117. = snd_seq_create_simple_port (seqHandle,
  118. forInput ? "in"
  119. : "out",
  120. forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
  121. : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
  122. forInput ? SND_SEQ_PORT_TYPE_APPLICATION
  123. : SND_SEQ_PORT_TYPE_MIDI_GENERIC);
  124. if (portId < 0)
  125. {
  126. snd_seq_close (seqHandle);
  127. seqHandle = 0;
  128. }
  129. }
  130. return seqHandle;
  131. }
  132. //==============================================================================
  133. class MidiOutputDevice
  134. {
  135. public:
  136. MidiOutputDevice (MidiOutput* const midiOutput_,
  137. snd_seq_t* const seqHandle_)
  138. :
  139. midiOutput (midiOutput_),
  140. seqHandle (seqHandle_),
  141. maxEventSize (16 * 1024)
  142. {
  143. jassert (seqHandle != 0 && midiOutput != 0);
  144. snd_midi_event_new (maxEventSize, &midiParser);
  145. }
  146. ~MidiOutputDevice()
  147. {
  148. snd_midi_event_free (midiParser);
  149. snd_seq_close (seqHandle);
  150. }
  151. void sendMessageNow (const MidiMessage& message)
  152. {
  153. if (message.getRawDataSize() > maxEventSize)
  154. {
  155. maxEventSize = message.getRawDataSize();
  156. snd_midi_event_free (midiParser);
  157. snd_midi_event_new (maxEventSize, &midiParser);
  158. }
  159. snd_seq_event_t event;
  160. snd_seq_ev_clear (&event);
  161. snd_midi_event_encode (midiParser,
  162. message.getRawData(),
  163. message.getRawDataSize(),
  164. &event);
  165. snd_midi_event_reset_encode (midiParser);
  166. snd_seq_ev_set_source (&event, 0);
  167. snd_seq_ev_set_subs (&event);
  168. snd_seq_ev_set_direct (&event);
  169. snd_seq_event_output_direct (seqHandle, &event);
  170. }
  171. juce_UseDebuggingNewOperator
  172. private:
  173. MidiOutput* const midiOutput;
  174. snd_seq_t* const seqHandle;
  175. snd_midi_event_t* midiParser;
  176. int maxEventSize;
  177. };
  178. const StringArray MidiOutput::getDevices()
  179. {
  180. StringArray devices;
  181. iterateDevices (false, devices, -1);
  182. return devices;
  183. }
  184. int MidiOutput::getDefaultDeviceIndex()
  185. {
  186. return 0;
  187. }
  188. MidiOutput* MidiOutput::openDevice (int deviceIndex)
  189. {
  190. MidiOutput* newDevice = 0;
  191. StringArray devices;
  192. snd_seq_t* const handle = iterateDevices (false, devices, deviceIndex);
  193. if (handle != 0)
  194. {
  195. newDevice = new MidiOutput();
  196. newDevice->internal = new MidiOutputDevice (newDevice, handle);
  197. }
  198. return newDevice;
  199. }
  200. MidiOutput* MidiOutput::createNewDevice (const String& deviceName)
  201. {
  202. MidiOutput* newDevice = 0;
  203. snd_seq_t* const handle = createDevice (false, deviceName);
  204. if (handle != 0)
  205. {
  206. newDevice = new MidiOutput();
  207. newDevice->internal = new MidiOutputDevice (newDevice, handle);
  208. }
  209. return newDevice;
  210. }
  211. MidiOutput::MidiOutput()
  212. : internal (0)
  213. {
  214. }
  215. MidiOutput::~MidiOutput()
  216. {
  217. MidiOutputDevice* const device = (MidiOutputDevice*) internal;
  218. delete device;
  219. }
  220. void MidiOutput::reset()
  221. {
  222. }
  223. bool MidiOutput::getVolume (float& leftVol, float& rightVol)
  224. {
  225. return false;
  226. }
  227. void MidiOutput::setVolume (float leftVol, float rightVol)
  228. {
  229. }
  230. void MidiOutput::sendMessageNow (const MidiMessage& message)
  231. {
  232. ((MidiOutputDevice*) internal)->sendMessageNow (message);
  233. }
  234. //==============================================================================
  235. class MidiInputThread : public Thread
  236. {
  237. public:
  238. MidiInputThread (MidiInput* const midiInput_,
  239. snd_seq_t* const seqHandle_,
  240. MidiInputCallback* const callback_)
  241. : Thread (T("Juce MIDI Input")),
  242. midiInput (midiInput_),
  243. seqHandle (seqHandle_),
  244. callback (callback_)
  245. {
  246. jassert (seqHandle != 0 && callback != 0 && midiInput != 0);
  247. }
  248. ~MidiInputThread()
  249. {
  250. snd_seq_close (seqHandle);
  251. }
  252. void run()
  253. {
  254. const int maxEventSize = 16 * 1024;
  255. snd_midi_event_t* midiParser;
  256. if (snd_midi_event_new (maxEventSize, &midiParser) >= 0)
  257. {
  258. uint8* const buffer = (uint8*) juce_malloc (maxEventSize);
  259. const int numPfds = snd_seq_poll_descriptors_count (seqHandle, POLLIN);
  260. struct pollfd* const pfd = (struct pollfd*) alloca (numPfds * sizeof (struct pollfd));
  261. snd_seq_poll_descriptors (seqHandle, pfd, numPfds, POLLIN);
  262. while (! threadShouldExit())
  263. {
  264. if (poll (pfd, numPfds, 100000) > 0)
  265. {
  266. snd_seq_event_t* inputEvent = 0;
  267. snd_seq_nonblock (seqHandle, 1);
  268. do
  269. {
  270. if (snd_seq_event_input (seqHandle, &inputEvent) >= 0)
  271. {
  272. // xxx what about SYSEXes that are too big for the buffer?
  273. const int numBytes = snd_midi_event_decode (midiParser, buffer, maxEventSize, inputEvent);
  274. snd_midi_event_reset_decode (midiParser);
  275. if (numBytes > 0)
  276. {
  277. const MidiMessage message ((const uint8*) buffer,
  278. numBytes,
  279. Time::getMillisecondCounter() * 0.001);
  280. callback->handleIncomingMidiMessage (midiInput, message);
  281. }
  282. snd_seq_free_event (inputEvent);
  283. }
  284. }
  285. while (snd_seq_event_input_pending (seqHandle, 0) > 0);
  286. snd_seq_free_event (inputEvent);
  287. }
  288. }
  289. snd_midi_event_free (midiParser);
  290. juce_free (buffer);
  291. }
  292. };
  293. juce_UseDebuggingNewOperator
  294. private:
  295. MidiInput* const midiInput;
  296. snd_seq_t* const seqHandle;
  297. MidiInputCallback* const callback;
  298. };
  299. //==============================================================================
  300. MidiInput::MidiInput (const String& name_)
  301. : name (name_),
  302. internal (0)
  303. {
  304. }
  305. MidiInput::~MidiInput()
  306. {
  307. stop();
  308. MidiInputThread* const thread = (MidiInputThread*) internal;
  309. delete thread;
  310. }
  311. void MidiInput::start()
  312. {
  313. ((MidiInputThread*) internal)->startThread();
  314. }
  315. void MidiInput::stop()
  316. {
  317. ((MidiInputThread*) internal)->stopThread (2000);
  318. }
  319. int MidiInput::getDefaultDeviceIndex()
  320. {
  321. return 0;
  322. }
  323. const StringArray MidiInput::getDevices()
  324. {
  325. StringArray devices;
  326. iterateDevices (true, devices, -1);
  327. return devices;
  328. }
  329. MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback)
  330. {
  331. MidiInput* newDevice = 0;
  332. StringArray devices;
  333. snd_seq_t* const handle = iterateDevices (true, devices, deviceIndex);
  334. if (handle != 0)
  335. {
  336. newDevice = new MidiInput (devices [deviceIndex]);
  337. newDevice->internal = new MidiInputThread (newDevice, handle, callback);
  338. }
  339. return newDevice;
  340. }
  341. MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback)
  342. {
  343. MidiInput* newDevice = 0;
  344. snd_seq_t* const handle = createDevice (true, deviceName);
  345. if (handle != 0)
  346. {
  347. newDevice = new MidiInput (deviceName);
  348. newDevice->internal = new MidiInputThread (newDevice, handle, callback);
  349. }
  350. return newDevice;
  351. }
  352. END_JUCE_NAMESPACE
  353. #endif
  354. #endif