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.

488 lines
16KB

  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_ALSA
  25. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  26. #include <alsa/asoundlib.h>
  27. BEGIN_JUCE_NAMESPACE
  28. #include "../../../src/juce_appframework/audio/devices/juce_MidiOutput.h"
  29. #include "../../../src/juce_appframework/audio/devices/juce_MidiInput.h"
  30. #include "../../../src/juce_core/threads/juce_Thread.h"
  31. #include "../../../src/juce_core/basics/juce_Time.h"
  32. //==============================================================================
  33. static snd_seq_t* iterateDevices (const bool forInput,
  34. StringArray& deviceNamesFound,
  35. const int deviceIndexToOpen)
  36. {
  37. snd_seq_t* returnedHandle = 0;
  38. snd_seq_t* seqHandle;
  39. if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
  40. : SND_SEQ_OPEN_OUTPUT, 0) == 0)
  41. {
  42. snd_seq_system_info_t* systemInfo;
  43. snd_seq_client_info_t* clientInfo;
  44. if (snd_seq_system_info_malloc (&systemInfo) == 0)
  45. {
  46. if (snd_seq_system_info (seqHandle, systemInfo) == 0
  47. && snd_seq_client_info_malloc (&clientInfo) == 0)
  48. {
  49. int numClients = snd_seq_system_info_get_cur_clients (systemInfo);
  50. while (--numClients >= 0 && returnedHandle == 0)
  51. {
  52. if (snd_seq_query_next_client (seqHandle, clientInfo) == 0)
  53. {
  54. snd_seq_port_info_t* portInfo;
  55. if (snd_seq_port_info_malloc (&portInfo) == 0)
  56. {
  57. int numPorts = snd_seq_client_info_get_num_ports (clientInfo);
  58. const int client = snd_seq_client_info_get_client (clientInfo);
  59. snd_seq_port_info_set_client (portInfo, client);
  60. snd_seq_port_info_set_port (portInfo, -1);
  61. while (--numPorts >= 0)
  62. {
  63. if (snd_seq_query_next_port (seqHandle, portInfo) == 0
  64. && (snd_seq_port_info_get_capability (portInfo)
  65. & (forInput ? SND_SEQ_PORT_CAP_READ
  66. : SND_SEQ_PORT_CAP_WRITE)) != 0)
  67. {
  68. deviceNamesFound.add (snd_seq_client_info_get_name (clientInfo));
  69. if (deviceNamesFound.size() == deviceIndexToOpen + 1)
  70. {
  71. const int sourcePort = snd_seq_port_info_get_port (portInfo);
  72. const int sourceClient = snd_seq_client_info_get_client (clientInfo);
  73. if (sourcePort != -1)
  74. {
  75. snd_seq_set_client_name (seqHandle,
  76. forInput ? "Juce Midi Input"
  77. : "Juce Midi Output");
  78. const int portId
  79. = snd_seq_create_simple_port (seqHandle,
  80. forInput ? "Juce Midi In Port"
  81. : "Juce Midi Out Port",
  82. forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
  83. : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
  84. SND_SEQ_PORT_TYPE_MIDI_GENERIC);
  85. snd_seq_connect_from (seqHandle, portId, sourceClient, sourcePort);
  86. returnedHandle = seqHandle;
  87. }
  88. }
  89. }
  90. }
  91. snd_seq_port_info_free (portInfo);
  92. }
  93. }
  94. }
  95. snd_seq_client_info_free (clientInfo);
  96. }
  97. snd_seq_system_info_free (systemInfo);
  98. }
  99. if (returnedHandle == 0)
  100. snd_seq_close (seqHandle);
  101. }
  102. deviceNamesFound.appendNumbersToDuplicates (true, true);
  103. return returnedHandle;
  104. }
  105. static snd_seq_t* createDevice (const bool forInput,
  106. const String& deviceNameToOpen)
  107. {
  108. snd_seq_t* seqHandle = 0;
  109. if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
  110. : SND_SEQ_OPEN_OUTPUT, 0) == 0)
  111. {
  112. snd_seq_set_client_name (seqHandle,
  113. (const char*) (forInput ? (deviceNameToOpen + T(" Input"))
  114. : (deviceNameToOpen + T(" Output"))));
  115. const int portId
  116. = snd_seq_create_simple_port (seqHandle,
  117. forInput ? "in"
  118. : "out",
  119. forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
  120. : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
  121. forInput ? SND_SEQ_PORT_TYPE_APPLICATION
  122. : SND_SEQ_PORT_TYPE_MIDI_GENERIC);
  123. if (portId < 0)
  124. {
  125. snd_seq_close (seqHandle);
  126. seqHandle = 0;
  127. }
  128. }
  129. return seqHandle;
  130. }
  131. //==============================================================================
  132. class MidiOutputDevice
  133. {
  134. public:
  135. MidiOutputDevice (MidiOutput* const midiOutput_,
  136. snd_seq_t* const seqHandle_)
  137. :
  138. midiOutput (midiOutput_),
  139. seqHandle (seqHandle_),
  140. maxEventSize (16 * 1024)
  141. {
  142. jassert (seqHandle != 0 && midiOutput != 0);
  143. snd_midi_event_new (maxEventSize, &midiParser);
  144. }
  145. ~MidiOutputDevice()
  146. {
  147. snd_midi_event_free (midiParser);
  148. snd_seq_close (seqHandle);
  149. }
  150. void sendMessageNow (const MidiMessage& message)
  151. {
  152. if (message.getRawDataSize() > maxEventSize)
  153. {
  154. maxEventSize = message.getRawDataSize();
  155. snd_midi_event_free (midiParser);
  156. snd_midi_event_new (maxEventSize, &midiParser);
  157. }
  158. snd_seq_event_t event;
  159. snd_seq_ev_clear (&event);
  160. snd_midi_event_encode (midiParser,
  161. message.getRawData(),
  162. message.getRawDataSize(),
  163. &event);
  164. snd_midi_event_reset_encode (midiParser);
  165. snd_seq_ev_set_source (&event, 0);
  166. snd_seq_ev_set_subs (&event);
  167. snd_seq_ev_set_direct (&event);
  168. snd_seq_event_output (seqHandle, &event);
  169. snd_seq_drain_output (seqHandle);
  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, 500) > 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 (3000);
  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. //==============================================================================
  350. #else
  351. //==============================================================================
  352. // (These are just stub functions if ALSA is unavailable...)
  353. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  354. BEGIN_JUCE_NAMESPACE
  355. #include "../../../src/juce_appframework/audio/devices/juce_MidiOutput.h"
  356. #include "../../../src/juce_appframework/audio/devices/juce_MidiInput.h"
  357. //==============================================================================
  358. const StringArray MidiOutput::getDevices() { return StringArray(); }
  359. int MidiOutput::getDefaultDeviceIndex() { return 0; }
  360. MidiOutput* MidiOutput::openDevice (int) { return 0; }
  361. MidiOutput* MidiOutput::createNewDevice (const String&) { return 0; }
  362. MidiOutput::~MidiOutput() {}
  363. void MidiOutput::reset() {}
  364. bool MidiOutput::getVolume (float&, float&) { return false; }
  365. void MidiOutput::setVolume (float, float) {}
  366. void MidiOutput::sendMessageNow (const MidiMessage&) {}
  367. MidiInput::MidiInput (const String& name_)
  368. : name (name_),
  369. internal (0)
  370. {}
  371. MidiInput::~MidiInput() {}
  372. void MidiInput::start() {}
  373. void MidiInput::stop() {}
  374. int MidiInput::getDefaultDeviceIndex() { return 0; }
  375. const StringArray MidiInput::getDevices() { return StringArray(); }
  376. MidiInput* MidiInput::openDevice (int, MidiInputCallback*) { return 0; }
  377. MidiInput* MidiInput::createNewDevice (const String&, MidiInputCallback*) { return 0; }
  378. END_JUCE_NAMESPACE
  379. #endif