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.

449 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. {
  213. MidiOutputDevice* const device = (MidiOutputDevice*) internal;
  214. delete device;
  215. }
  216. void MidiOutput::reset()
  217. {
  218. }
  219. bool MidiOutput::getVolume (float& leftVol, float& rightVol)
  220. {
  221. return false;
  222. }
  223. void MidiOutput::setVolume (float leftVol, float rightVol)
  224. {
  225. }
  226. void MidiOutput::sendMessageNow (const MidiMessage& message)
  227. {
  228. ((MidiOutputDevice*) internal)->sendMessageNow (message);
  229. }
  230. //==============================================================================
  231. class MidiInputThread : public Thread
  232. {
  233. public:
  234. MidiInputThread (MidiInput* const midiInput_,
  235. snd_seq_t* const seqHandle_,
  236. MidiInputCallback* const callback_)
  237. : Thread (T("Juce MIDI Input")),
  238. midiInput (midiInput_),
  239. seqHandle (seqHandle_),
  240. callback (callback_)
  241. {
  242. jassert (seqHandle != 0 && callback != 0 && midiInput != 0);
  243. }
  244. ~MidiInputThread()
  245. {
  246. snd_seq_close (seqHandle);
  247. }
  248. void run()
  249. {
  250. const int maxEventSize = 16 * 1024;
  251. snd_midi_event_t* midiParser;
  252. if (snd_midi_event_new (maxEventSize, &midiParser) >= 0)
  253. {
  254. uint8* const buffer = (uint8*) juce_malloc (maxEventSize);
  255. const int numPfds = snd_seq_poll_descriptors_count (seqHandle, POLLIN);
  256. struct pollfd* const pfd = (struct pollfd*) alloca (numPfds * sizeof (struct pollfd));
  257. snd_seq_poll_descriptors (seqHandle, pfd, numPfds, POLLIN);
  258. while (! threadShouldExit())
  259. {
  260. if (poll (pfd, numPfds, 100000) > 0)
  261. {
  262. snd_seq_event_t* inputEvent = 0;
  263. snd_seq_nonblock (seqHandle, 1);
  264. do
  265. {
  266. if (snd_seq_event_input (seqHandle, &inputEvent) >= 0)
  267. {
  268. // xxx what about SYSEXes that are too big for the buffer?
  269. const int numBytes = snd_midi_event_decode (midiParser, buffer, maxEventSize, inputEvent);
  270. snd_midi_event_reset_decode (midiParser);
  271. if (numBytes > 0)
  272. {
  273. const MidiMessage message ((const uint8*) buffer,
  274. numBytes,
  275. Time::getMillisecondCounter() * 0.001);
  276. callback->handleIncomingMidiMessage (midiInput, message);
  277. }
  278. snd_seq_free_event (inputEvent);
  279. }
  280. }
  281. while (snd_seq_event_input_pending (seqHandle, 0) > 0);
  282. snd_seq_free_event (inputEvent);
  283. }
  284. }
  285. snd_midi_event_free (midiParser);
  286. juce_free (buffer);
  287. }
  288. };
  289. juce_UseDebuggingNewOperator
  290. private:
  291. MidiInput* const midiInput;
  292. snd_seq_t* const seqHandle;
  293. MidiInputCallback* const callback;
  294. };
  295. //==============================================================================
  296. MidiInput::MidiInput (const String& name_)
  297. : name (name_),
  298. internal (0)
  299. {
  300. }
  301. MidiInput::~MidiInput()
  302. {
  303. stop();
  304. MidiInputThread* const thread = (MidiInputThread*) internal;
  305. delete thread;
  306. }
  307. void MidiInput::start()
  308. {
  309. ((MidiInputThread*) internal)->startThread();
  310. }
  311. void MidiInput::stop()
  312. {
  313. ((MidiInputThread*) internal)->stopThread (2000);
  314. }
  315. int MidiInput::getDefaultDeviceIndex()
  316. {
  317. return 0;
  318. }
  319. const StringArray MidiInput::getDevices()
  320. {
  321. StringArray devices;
  322. iterateDevices (true, devices, -1);
  323. return devices;
  324. }
  325. MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback)
  326. {
  327. MidiInput* newDevice = 0;
  328. StringArray devices;
  329. snd_seq_t* const handle = iterateDevices (true, devices, deviceIndex);
  330. if (handle != 0)
  331. {
  332. newDevice = new MidiInput (devices [deviceIndex]);
  333. newDevice->internal = new MidiInputThread (newDevice, handle, callback);
  334. }
  335. return newDevice;
  336. }
  337. MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback)
  338. {
  339. MidiInput* newDevice = 0;
  340. snd_seq_t* const handle = createDevice (true, deviceName);
  341. if (handle != 0)
  342. {
  343. newDevice = new MidiInput (deviceName);
  344. newDevice->internal = new MidiInputThread (newDevice, handle, callback);
  345. }
  346. return newDevice;
  347. }
  348. END_JUCE_NAMESPACE
  349. #endif
  350. #endif