Collection of tools useful for audio production
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.

1262 lines
37KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2012 Filipe Coelho <falktx@gmail.com>
  4. *
  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. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #include "carla_engine.h"
  18. #include "carla_plugin.h"
  19. CARLA_BACKEND_START_NAMESPACE
  20. // -------------------------------------------------------------------
  21. CarlaEngine::CarlaEngine()
  22. : m_checkThread(this),
  23. #ifndef BUILD_BRIDGE
  24. m_osc(this),
  25. m_oscData(nullptr),
  26. #endif
  27. m_callback(nullptr),
  28. #ifdef Q_COMPILER_INITIALIZER_LISTS
  29. m_callbackPtr(nullptr),
  30. m_carlaPlugins{nullptr},
  31. m_uniqueNames{nullptr},
  32. m_insPeak{0.0},
  33. m_outsPeak{0.0}
  34. #else
  35. m_callbackPtr(nullptr)
  36. #endif
  37. {
  38. qDebug("CarlaEngine::CarlaEngine()");
  39. type = CarlaEngineTypeNull;
  40. name = nullptr;
  41. bufferSize = 0;
  42. sampleRate = 0.0;
  43. #ifndef Q_COMPILER_INITIALIZER_LISTS
  44. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  45. {
  46. m_carlaPlugins[i] = nullptr;
  47. m_uniqueNames[i] = nullptr;
  48. }
  49. for (unsigned short i=0; i < MAX_PLUGINS * MAX_PEAKS; i++)
  50. {
  51. m_insPeak[i] = 0.0;
  52. m_outsPeak[i] = 0.0;
  53. }
  54. #endif
  55. }
  56. CarlaEngine::~CarlaEngine()
  57. {
  58. qDebug("CarlaEngine::~CarlaEngine()");
  59. if (name)
  60. free((void*)name);
  61. }
  62. // -------------------------------------------------------------------
  63. // static values
  64. int CarlaEngine::maxClientNameSize()
  65. {
  66. #ifdef CARLA_ENGINE_JACK
  67. # ifndef BUILD_BRIDGE
  68. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  69. # endif
  70. return jack_client_name_size();
  71. #endif
  72. return STR_MAX/2;
  73. }
  74. int CarlaEngine::maxPortNameSize()
  75. {
  76. #ifdef CARLA_ENGINE_JACK
  77. # ifndef BUILD_BRIDGE
  78. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  79. # endif
  80. return jack_port_name_size();
  81. #endif
  82. return STR_MAX;
  83. }
  84. // -------------------------------------------------------------------
  85. // plugin management
  86. short CarlaEngine::getNewPluginId() const
  87. {
  88. qDebug("CarlaEngine::getNewPluginId()");
  89. #ifdef BUILD_BRIDGE
  90. static const unsigned short max = MAX_PLUGINS;
  91. #else
  92. static const unsigned short max = (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK) ? 16 : MAX_PLUGINS;
  93. #endif
  94. for (unsigned short i=0; i < max; i++)
  95. {
  96. if (! m_carlaPlugins[i])
  97. return i;
  98. }
  99. return -1;
  100. }
  101. CarlaPlugin* CarlaEngine::getPlugin(const unsigned short id) const
  102. {
  103. qDebug("CarlaEngine::getPlugin(%i)", id);
  104. Q_ASSERT(id < MAX_PLUGINS);
  105. if (id < MAX_PLUGINS)
  106. return m_carlaPlugins[id];
  107. return nullptr;
  108. }
  109. const char* CarlaEngine::getUniqueName(const char* const name)
  110. {
  111. qDebug("CarlaEngine::getUniqueName(\"%s\")", name);
  112. QString qname(name);
  113. if (qname.isEmpty())
  114. qname = "(No name)";
  115. qname.truncate(maxClientNameSize()-5-1); // 5 = strlen(" (10)")
  116. qname.replace(":", "."); // ":" is used in JACK1 to split client/port names
  117. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  118. {
  119. // Check if unique name already exists
  120. if (m_uniqueNames[i] && qname == m_uniqueNames[i])
  121. {
  122. // Check if string has already been modified
  123. uint len = qname.size();
  124. // 1 digit, ex: " (2)"
  125. if (qname.at(len-4) == QChar(' ') && qname.at(len-3) == QChar('(') && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  126. {
  127. int number = qname.at(len-2).toAscii()-'0';
  128. if (number == 9)
  129. // next number is 10, 2 digits
  130. qname.replace(" (9)", " (10)");
  131. else
  132. qname[len-2] = QChar('0'+number+1);
  133. continue;
  134. }
  135. // 2 digits, ex: " (11)"
  136. if (qname.at(len-5) == QChar(' ') && qname.at(len-4) == QChar('(') && qname.at(len-3).isDigit() && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  137. {
  138. QChar n2 = qname.at(len-2);
  139. QChar n3 = qname.at(len-3);
  140. if (n2 == QChar('9'))
  141. {
  142. n2 = QChar('0');
  143. n3 = QChar(n3.toAscii()+1);
  144. }
  145. else
  146. n2 = QChar(n2.toAscii()+1);
  147. qname[len-2] = n2;
  148. qname[len-3] = n3;
  149. continue;
  150. }
  151. // Modify string if not
  152. qname += " (2)";
  153. }
  154. }
  155. return strdup(qname.toUtf8().constData());
  156. }
  157. short CarlaEngine::addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra)
  158. {
  159. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, extra);
  160. }
  161. short CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra)
  162. {
  163. qDebug("CarlaEngine::addPlugin(%s, %s, \"%s\", \"%s\", \"%s\", %p)", BinaryType2str(btype), PluginType2str(ptype), filename, name, label, extra);
  164. CarlaPlugin::initializer init = {
  165. this,
  166. filename,
  167. name,
  168. label
  169. };
  170. CarlaPlugin* plugin = nullptr;
  171. #ifndef BUILD_BRIDGE
  172. if (btype != BINARY_NATIVE)
  173. {
  174. # ifdef CARLA_ENGINE_JACK
  175. if (carlaOptions.process_mode != CarlaBackend::PROCESS_MODE_MULTIPLE_CLIENTS)
  176. {
  177. setLastError("Can only use bridged plugins in JACK Multi-Client mode");
  178. return -1;
  179. }
  180. # endif
  181. if (type != CarlaEngineTypeJack)
  182. {
  183. setLastError("Can only use bridged plugins with JACK backend");
  184. return -1;
  185. }
  186. plugin = CarlaPlugin::newBridge(init, btype, ptype);
  187. }
  188. else
  189. #endif
  190. {
  191. switch (ptype)
  192. {
  193. case PLUGIN_NONE:
  194. break;
  195. case PLUGIN_LADSPA:
  196. plugin = CarlaPlugin::newLADSPA(init, extra);
  197. break;
  198. case PLUGIN_DSSI:
  199. plugin = CarlaPlugin::newDSSI(init, extra);
  200. break;
  201. case PLUGIN_LV2:
  202. plugin = CarlaPlugin::newLV2(init);
  203. break;
  204. case PLUGIN_VST:
  205. plugin = CarlaPlugin::newVST(init);
  206. break;
  207. case PLUGIN_GIG:
  208. plugin = CarlaPlugin::newGIG(init);
  209. break;
  210. case PLUGIN_SF2:
  211. plugin = CarlaPlugin::newSF2(init);
  212. break;
  213. case PLUGIN_SFZ:
  214. plugin = CarlaPlugin::newSFZ(init);
  215. break;
  216. }
  217. }
  218. if (! plugin)
  219. return -1;
  220. const short id = plugin->id();
  221. m_carlaPlugins[id] = plugin;
  222. m_uniqueNames[id] = plugin->name();
  223. return id;
  224. }
  225. bool CarlaEngine::removePlugin(const unsigned short id)
  226. {
  227. qDebug("CarlaEngine::removePlugin(%i)", id);
  228. Q_ASSERT(id < MAX_PLUGINS);
  229. CarlaPlugin* const plugin = m_carlaPlugins[id];
  230. if (plugin && plugin->id() == id)
  231. {
  232. processLock();
  233. plugin->setEnabled(false);
  234. processUnlock();
  235. m_checkThread.stopNow();
  236. delete plugin;
  237. m_carlaPlugins[id] = nullptr;
  238. m_uniqueNames[id] = nullptr;
  239. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  240. {
  241. for (unsigned short i=id; i < MAX_PLUGINS-1; i++)
  242. {
  243. m_carlaPlugins[i] = m_carlaPlugins[i+1];
  244. m_uniqueNames[i] = m_uniqueNames[i+1];
  245. if (m_carlaPlugins[i])
  246. m_carlaPlugins[i]->setId(i);
  247. }
  248. }
  249. if (isRunning())
  250. m_checkThread.start(QThread::HighPriority);
  251. return true;
  252. }
  253. qCritical("CarlaEngine::removePlugin(%i) - could not find plugin", id);
  254. setLastError("Could not find plugin to remove");
  255. return false;
  256. }
  257. void CarlaEngine::removeAllPlugins()
  258. {
  259. qDebug("CarlaEngine::removeAllPlugins()");
  260. if (m_checkThread.isRunning())
  261. m_checkThread.stopNow();
  262. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  263. {
  264. CarlaPlugin* const plugin = m_carlaPlugins[i];
  265. if (plugin)
  266. {
  267. processLock();
  268. plugin->setEnabled(false);
  269. processUnlock();
  270. delete plugin;
  271. m_carlaPlugins[i] = nullptr;
  272. m_uniqueNames[i] = nullptr;
  273. }
  274. }
  275. if (isRunning())
  276. m_checkThread.start(QThread::HighPriority);
  277. }
  278. void CarlaEngine::idlePluginGuis()
  279. {
  280. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  281. {
  282. CarlaPlugin* const plugin = m_carlaPlugins[i];
  283. if (plugin && plugin->enabled())
  284. plugin->idleGui();
  285. }
  286. }
  287. // -------------------------------------------------------------------
  288. // protected calls
  289. void CarlaEngine::bufferSizeChanged(uint32_t newBufferSize)
  290. {
  291. qDebug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  292. bufferSize = newBufferSize;
  293. for (unsigned short i=0; i < MAX_PLUGINS; i++)
  294. {
  295. if (m_carlaPlugins[i] && m_carlaPlugins[i]->enabled())
  296. m_carlaPlugins[i]->bufferSizeChanged(newBufferSize);
  297. }
  298. }
  299. // -------------------------------------------------------------------------------------------------------------------
  300. // Carla Engine Client
  301. CarlaEngineClient::CarlaEngineClient(const CarlaEngineType& type_, const CarlaEngineClientNativeHandle& handle_)
  302. : type(type_),
  303. handle(handle_)
  304. {
  305. qDebug("CarlaEngineClient::CarlaEngineClient()");
  306. m_active = false;
  307. }
  308. CarlaEngineClient::~CarlaEngineClient()
  309. {
  310. qDebug("CarlaEngineClient::~CarlaEngineClient()");
  311. Q_ASSERT(! m_active);
  312. #ifndef BUILD_BRIDGE
  313. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  314. #endif
  315. {
  316. #ifdef CARLA_ENGINE_JACK
  317. if (handle.jackClient)
  318. jack_client_close(handle.jackClient);
  319. #endif
  320. #ifdef CARLA_ENGINE_RTAUDIO
  321. if (handle.rtAudioPtr)
  322. delete handle.rtAudioPtr;
  323. #endif
  324. }
  325. }
  326. void CarlaEngineClient::activate()
  327. {
  328. qDebug("CarlaEngineClient::activate()");
  329. Q_ASSERT(! m_active);
  330. #ifndef BUILD_BRIDGE
  331. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  332. #endif
  333. {
  334. if (! m_active)
  335. {
  336. #ifdef CARLA_ENGINE_JACK
  337. if (handle.jackClient)
  338. jack_activate(handle.jackClient);
  339. #endif
  340. #ifdef CARLA_ENGINE_RTAUDIO
  341. if (handle.rtAudioPtr)
  342. handle.rtAudioPtr->startStream();
  343. #endif
  344. }
  345. }
  346. m_active = true;
  347. }
  348. void CarlaEngineClient::deactivate()
  349. {
  350. qDebug("CarlaEngineClient::deactivate()");
  351. Q_ASSERT(m_active);
  352. #ifndef BUILD_BRIDGE
  353. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  354. #endif
  355. {
  356. if (m_active)
  357. {
  358. #ifdef CARLA_ENGINE_JACK
  359. if (handle.jackClient)
  360. jack_deactivate(handle.jackClient);
  361. #endif
  362. #ifdef CARLA_ENGINE_RTAUDIO
  363. if (handle.rtAudioPtr)
  364. handle.rtAudioPtr->stopStream();
  365. #endif
  366. }
  367. }
  368. m_active = false;
  369. }
  370. bool CarlaEngineClient::isActive() const
  371. {
  372. qDebug("CarlaEngineClient::isActive()");
  373. return m_active;
  374. }
  375. bool CarlaEngineClient::isOk() const
  376. {
  377. qDebug("CarlaEngineClient::isOk()");
  378. #ifndef BUILD_BRIDGE
  379. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  380. #endif
  381. {
  382. #ifdef CARLA_ENGINE_JACK
  383. return bool(handle.jackClient);
  384. // FIXME
  385. #endif
  386. }
  387. return true;
  388. }
  389. const CarlaEngineBasePort* CarlaEngineClient::addPort(const CarlaEnginePortType type, const char* const name, const bool isInput)
  390. {
  391. qDebug("CarlaEngineClient::addPort(%i, \"%s\", %s)", type, name, bool2str(isInput));
  392. CarlaEnginePortNativeHandle portHandle;
  393. #ifdef CARLA_ENGINE_JACK
  394. portHandle.jackClient = handle.jackClient;
  395. #endif
  396. #ifdef CARLA_ENGINE_JACK
  397. # ifndef BUILD_BRIDGE
  398. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  399. # endif
  400. {
  401. switch (type)
  402. {
  403. case CarlaEnginePortTypeAudio:
  404. portHandle.jackPort = jack_port_register(handle.jackClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  405. break;
  406. case CarlaEnginePortTypeControl:
  407. case CarlaEnginePortTypeMIDI:
  408. portHandle.jackPort = jack_port_register(handle.jackClient, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  409. break;
  410. }
  411. }
  412. #else
  413. Q_UNUSED(name);
  414. #endif
  415. switch (type)
  416. {
  417. case CarlaEnginePortTypeAudio:
  418. return new CarlaEngineAudioPort(portHandle, isInput);
  419. case CarlaEnginePortTypeControl:
  420. return new CarlaEngineControlPort(portHandle, isInput);
  421. case CarlaEnginePortTypeMIDI:
  422. return new CarlaEngineMidiPort(portHandle, isInput);
  423. default:
  424. return nullptr;
  425. }
  426. }
  427. // -------------------------------------------------------------------------------------------------------------------
  428. // Carla Engine Port (Base class)
  429. CarlaEngineBasePort::CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle_, bool isInput_)
  430. : isInput(isInput_),
  431. handle(handle_)
  432. {
  433. qDebug("CarlaEngineBasePort::CarlaEngineBasePort(%s)", bool2str(isInput_));
  434. buffer = nullptr;
  435. }
  436. CarlaEngineBasePort::~CarlaEngineBasePort()
  437. {
  438. qDebug("CarlaEngineBasePort::~CarlaEngineBasePort()");
  439. #ifdef CARLA_ENGINE_JACK
  440. # ifndef BUILD_BRIDGE
  441. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  442. # endif
  443. {
  444. if (handle.jackClient && handle.jackPort)
  445. jack_port_unregister(handle.jackClient, handle.jackPort);
  446. }
  447. #endif
  448. }
  449. // -------------------------------------------------------------------------------------------------------------------
  450. // Carla Engine Port (Audio)
  451. CarlaEngineAudioPort::CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  452. : CarlaEngineBasePort(handle, isInput)
  453. {
  454. qDebug("CarlaEngineAudioPort::CarlaEngineAudioPort(%s)", bool2str(isInput));
  455. }
  456. void CarlaEngineAudioPort::initBuffer(CarlaEngine* const /*engine*/)
  457. {
  458. }
  459. #ifdef CARLA_ENGINE_JACK
  460. float* CarlaEngineAudioPort::getJackAudioBuffer(uint32_t nframes)
  461. {
  462. # ifndef BUILD_BRIDGE
  463. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  464. return nullptr;
  465. # endif
  466. Q_ASSERT(handle.jackPort);
  467. return (float*)jack_port_get_buffer(handle.jackPort, nframes);
  468. }
  469. #endif
  470. // -------------------------------------------------------------------------------------------------------------------
  471. // Carla Engine Port (Control)
  472. CarlaEngineControlPort::CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  473. : CarlaEngineBasePort(handle, isInput)
  474. {
  475. qDebug("CarlaEngineControlPort::CarlaEngineControlPort(%s)", bool2str(isInput));
  476. }
  477. void CarlaEngineControlPort::initBuffer(CarlaEngine* const engine)
  478. {
  479. Q_ASSERT(engine);
  480. #ifndef BUILD_BRIDGE
  481. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  482. {
  483. buffer = isInput ? engine->rackControlEventsIn : engine->rackControlEventsOut;
  484. return;
  485. }
  486. #endif
  487. #ifdef CARLA_ENGINE_JACK
  488. if (handle.jackPort)
  489. {
  490. buffer = jack_port_get_buffer(handle.jackPort, engine->getBufferSize());
  491. if (! isInput)
  492. jack_midi_clear_buffer(buffer);
  493. }
  494. #endif
  495. }
  496. uint32_t CarlaEngineControlPort::getEventCount()
  497. {
  498. if (! isInput)
  499. return 0;
  500. Q_ASSERT(buffer);
  501. #ifndef BUILD_BRIDGE
  502. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  503. {
  504. uint32_t count = 0;
  505. const CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  506. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS; i++)
  507. {
  508. if (events[i].type != CarlaEngineEventNull)
  509. count++;
  510. else
  511. break;
  512. }
  513. return count;
  514. }
  515. #endif
  516. #ifdef CARLA_ENGINE_JACK
  517. return jack_midi_get_event_count(buffer);
  518. #else
  519. return 0;
  520. #endif
  521. }
  522. const CarlaEngineControlEvent* CarlaEngineControlPort::getEvent(uint32_t index)
  523. {
  524. if (! isInput)
  525. return nullptr;
  526. Q_ASSERT(buffer);
  527. Q_ASSERT(index < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS);
  528. #ifndef BUILD_BRIDGE
  529. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  530. {
  531. const CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  532. if (index < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS)
  533. return &events[index];
  534. return nullptr;
  535. }
  536. #endif
  537. #ifdef CARLA_ENGINE_JACK
  538. static jack_midi_event_t jackEvent;
  539. static CarlaEngineControlEvent carlaEvent;
  540. if (jack_midi_event_get(&jackEvent, buffer, index) != 0)
  541. return nullptr;
  542. memset(&carlaEvent, 0, sizeof(CarlaEngineControlEvent));
  543. uint8_t midiStatus = jackEvent.buffer[0];
  544. uint8_t midiChannel = midiStatus & 0x0F;
  545. carlaEvent.time = jackEvent.time;
  546. carlaEvent.channel = midiChannel;
  547. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  548. {
  549. uint8_t midiControl = jackEvent.buffer[1];
  550. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  551. {
  552. uint8_t midiBank = jackEvent.buffer[2];
  553. carlaEvent.type = CarlaEngineEventMidiBankChange;
  554. carlaEvent.value = midiBank;
  555. }
  556. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  557. {
  558. carlaEvent.type = CarlaEngineEventAllSoundOff;
  559. }
  560. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  561. {
  562. carlaEvent.type = CarlaEngineEventAllNotesOff;
  563. }
  564. else
  565. {
  566. uint8_t midiValue = jackEvent.buffer[2];
  567. carlaEvent.type = CarlaEngineEventControlChange;
  568. carlaEvent.controller = midiControl;
  569. carlaEvent.value = double(midiValue)/127;
  570. }
  571. return &carlaEvent;
  572. }
  573. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  574. {
  575. uint8_t midiProgram = jackEvent.buffer[1];
  576. carlaEvent.type = CarlaEngineEventMidiProgramChange;
  577. carlaEvent.value = midiProgram;
  578. return &carlaEvent;
  579. }
  580. #endif
  581. return nullptr;
  582. }
  583. void CarlaEngineControlPort::writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value)
  584. {
  585. if (isInput)
  586. return;
  587. Q_ASSERT(buffer);
  588. Q_ASSERT(type != CarlaEngineEventNull);
  589. #ifndef BUILD_BRIDGE
  590. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  591. {
  592. CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  593. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS; i++)
  594. {
  595. if (events[i].type == CarlaEngineEventNull)
  596. {
  597. events[i].type = type;
  598. events[i].time = time;
  599. events[i].value = value;
  600. events[i].channel = channel;
  601. events[i].controller = controller;
  602. break;
  603. }
  604. }
  605. return;
  606. }
  607. #endif
  608. #ifdef CARLA_ENGINE_JACK
  609. if (type == CarlaEngineEventControlChange && MIDI_IS_CONTROL_BANK_SELECT(controller))
  610. type = CarlaEngineEventMidiBankChange;
  611. uint8_t data[4] = { 0 };
  612. switch (type)
  613. {
  614. case CarlaEngineEventNull:
  615. break;
  616. case CarlaEngineEventControlChange:
  617. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  618. data[1] = controller;
  619. data[2] = value * 127;
  620. jack_midi_event_write(buffer, time, data, 3);
  621. break;
  622. case CarlaEngineEventMidiBankChange:
  623. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  624. data[1] = MIDI_CONTROL_BANK_SELECT;
  625. data[2] = value;
  626. jack_midi_event_write(buffer, time, data, 3);
  627. break;
  628. case CarlaEngineEventMidiProgramChange:
  629. data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
  630. data[1] = value;
  631. jack_midi_event_write(buffer, time, data, 2);
  632. break;
  633. case CarlaEngineEventAllSoundOff:
  634. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  635. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  636. jack_midi_event_write(buffer, time, data, 2);
  637. break;
  638. case CarlaEngineEventAllNotesOff:
  639. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  640. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  641. jack_midi_event_write(buffer, time, data, 2);
  642. break;
  643. }
  644. #endif
  645. }
  646. // -------------------------------------------------------------------------------------------------------------------
  647. // Carla Engine Port (MIDI)
  648. CarlaEngineMidiPort::CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  649. : CarlaEngineBasePort(handle, isInput)
  650. {
  651. qDebug("CarlaEngineMidiPort::CarlaEngineMidiPort(%s)", bool2str(isInput));
  652. }
  653. void CarlaEngineMidiPort::initBuffer(CarlaEngine* const engine)
  654. {
  655. Q_ASSERT(engine);
  656. #ifndef BUILD_BRIDGE
  657. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  658. {
  659. buffer = isInput ? engine->rackMidiEventsIn : engine->rackMidiEventsOut;
  660. return;
  661. }
  662. #endif
  663. #ifdef CARLA_ENGINE_JACK
  664. if (handle.jackPort)
  665. {
  666. buffer = jack_port_get_buffer(handle.jackPort, engine->getBufferSize());
  667. if (! isInput)
  668. jack_midi_clear_buffer(buffer);
  669. }
  670. #endif
  671. }
  672. uint32_t CarlaEngineMidiPort::getEventCount()
  673. {
  674. if (! isInput)
  675. return 0;
  676. Q_ASSERT(buffer);
  677. #ifndef BUILD_BRIDGE
  678. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  679. {
  680. uint32_t count = 0;
  681. const CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  682. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_MIDI_EVENTS; i++)
  683. {
  684. if (events[i].size > 0)
  685. count++;
  686. else
  687. break;
  688. }
  689. return count;
  690. }
  691. #endif
  692. #ifdef CARLA_ENGINE_JACK
  693. return jack_midi_get_event_count(buffer);
  694. #else
  695. return 0;
  696. #endif
  697. }
  698. const CarlaEngineMidiEvent* CarlaEngineMidiPort::getEvent(uint32_t index)
  699. {
  700. if (! isInput)
  701. return nullptr;
  702. Q_ASSERT(buffer);
  703. Q_ASSERT(index < CarlaEngine::MAX_ENGINE_MIDI_EVENTS);
  704. #ifndef BUILD_BRIDGE
  705. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  706. {
  707. const CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  708. if (index < CarlaEngine::MAX_ENGINE_MIDI_EVENTS)
  709. return &events[index];
  710. return nullptr;
  711. }
  712. #endif
  713. #ifdef CARLA_ENGINE_JACK
  714. static jack_midi_event_t jackEvent;
  715. static CarlaEngineMidiEvent carlaEvent;
  716. if (jack_midi_event_get(&jackEvent, buffer, index) == 0 && jackEvent.size <= 4)
  717. {
  718. carlaEvent.time = jackEvent.time;
  719. carlaEvent.size = jackEvent.size;
  720. memcpy(carlaEvent.data, jackEvent.buffer, jackEvent.size);
  721. return &carlaEvent;
  722. }
  723. #endif
  724. return nullptr;
  725. }
  726. void CarlaEngineMidiPort::writeEvent(uint32_t time, uint8_t* data, uint8_t size)
  727. {
  728. if (isInput)
  729. return;
  730. Q_ASSERT(buffer);
  731. Q_ASSERT(data);
  732. Q_ASSERT(size > 0);
  733. #ifndef BUILD_BRIDGE
  734. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  735. {
  736. if (size > 4)
  737. return;
  738. CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  739. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_MIDI_EVENTS; i++)
  740. {
  741. if (events[i].size == 0)
  742. {
  743. events[i].time = time;
  744. events[i].size = size;
  745. memcpy(events[i].data, data, size);
  746. break;
  747. }
  748. }
  749. return;
  750. }
  751. #endif
  752. #ifdef CARLA_ENGINE_JACK
  753. jack_midi_event_write(buffer, time, data, size);
  754. #endif
  755. }
  756. // -------------------------------------------------------------------------------------------------------------------
  757. // Carla Engine OSC stuff
  758. void CarlaEngine::osc_send_add_plugin(const int32_t pluginId, const char* const pluginName)
  759. {
  760. qDebug("CarlaEngine::osc_send_add_plugin(%i, \"%s\")", pluginId, pluginName);
  761. Q_ASSERT(m_oscData);
  762. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  763. Q_ASSERT(pluginName);
  764. if (m_oscData && m_oscData->target)
  765. {
  766. char target_path[strlen(m_oscData->path)+12];
  767. strcpy(target_path, m_oscData->path);
  768. strcat(target_path, "/add_plugin");
  769. lo_send(m_oscData->target, target_path, "is", pluginId, pluginName);
  770. }
  771. }
  772. void CarlaEngine::osc_send_remove_plugin(const int32_t pluginId)
  773. {
  774. qDebug("CarlaEngine::osc_send_remove_plugin(%i)", pluginId);
  775. Q_ASSERT(m_oscData);
  776. if (m_oscData && m_oscData->target)
  777. {
  778. char target_path[strlen(m_oscData->path)+15];
  779. strcpy(target_path, m_oscData->path);
  780. strcat(target_path, "/remove_plugin");
  781. lo_send(m_oscData->target, target_path, "i", pluginId);
  782. }
  783. }
  784. void CarlaEngine::osc_send_set_plugin_data(const int32_t pluginId, const int32_t type, const int32_t category, const int32_t hints, const char* const realName, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId)
  785. {
  786. qDebug("CarlaEngine::osc_send_set_plugin_data(%i, %i, %i, %i, \"%s\", \"%s\", \"%s\", \"%s\", %li)", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  787. Q_ASSERT(m_oscData);
  788. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  789. Q_ASSERT(type != PLUGIN_NONE);
  790. if (m_oscData && m_oscData->target)
  791. {
  792. char target_path[strlen(m_oscData->path)+17];
  793. strcpy(target_path, m_oscData->path);
  794. strcat(target_path, "/set_plugin_data");
  795. lo_send(m_oscData->target, target_path, "iiiissssh", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  796. }
  797. }
  798. void CarlaEngine::osc_send_set_plugin_ports(const int32_t pluginId, const int32_t audioIns, const int32_t audioOuts, const int32_t midiIns, const int32_t midiOuts, const int32_t cIns, const int32_t cOuts, const int32_t cTotals)
  799. {
  800. qDebug("CarlaEngine::osc_send_set_plugin_ports(%i, %i, %i, %i, %i, %i, %i, %i)", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts, cTotals);
  801. Q_ASSERT(m_oscData);
  802. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  803. if (m_oscData && m_oscData->target)
  804. {
  805. char target_path[strlen(m_oscData->path)+18];
  806. strcpy(target_path, m_oscData->path);
  807. strcat(target_path, "/set_plugin_ports");
  808. lo_send(m_oscData->target, target_path, "iiiiiiii", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts, cTotals);
  809. }
  810. }
  811. void CarlaEngine::osc_send_set_parameter_data(const int32_t pluginId, const int32_t index, const int32_t type, const int32_t hints, const char* const name, const char* const label, const double current)
  812. {
  813. qDebug("CarlaEngine::osc_send_set_parameter_data(%i, %i, %i, %i, \"%s\", \"%s\", %g)", pluginId, index, type, hints, name, label, current);
  814. Q_ASSERT(m_oscData);
  815. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  816. Q_ASSERT(index >= 0);
  817. Q_ASSERT(type != PARAMETER_UNKNOWN);
  818. if (m_oscData && m_oscData->target)
  819. {
  820. char target_path[strlen(m_oscData->path)+20];
  821. strcpy(target_path, m_oscData->path);
  822. strcat(target_path, "/set_parameter_data");
  823. lo_send(m_oscData->target, target_path, "iiiissd", pluginId, index, type, hints, name, label, current);
  824. }
  825. }
  826. void CarlaEngine::osc_send_set_parameter_ranges(const int32_t pluginId, const int32_t index, const double min, const double max, const double def, const double step, const double stepSmall, const double stepLarge)
  827. {
  828. qDebug("CarlaEngine::osc_send_set_parameter_ranges(%i, %i, %g, %g, %g, %g, %g, %g)", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  829. Q_ASSERT(m_oscData);
  830. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  831. Q_ASSERT(index >= 0);
  832. Q_ASSERT(min < max);
  833. if (m_oscData && m_oscData->target)
  834. {
  835. char target_path[strlen(m_oscData->path)+22];
  836. strcpy(target_path, m_oscData->path);
  837. strcat(target_path, "/set_parameter_ranges");
  838. lo_send(m_oscData->target, target_path, "iidddddd", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  839. }
  840. }
  841. void CarlaEngine::osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc)
  842. {
  843. qDebug("CarlaEngine::osc_send_set_parameter_midi_cc(%i, %i, %i)", pluginId, index, cc);
  844. Q_ASSERT(m_oscData);
  845. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  846. Q_ASSERT(index >= 0);
  847. if (m_oscData && m_oscData->target)
  848. {
  849. char target_path[strlen(m_oscData->path)+23];
  850. strcpy(target_path, m_oscData->path);
  851. strcat(target_path, "/set_parameter_midi_cc");
  852. lo_send(m_oscData->target, target_path, "iii", pluginId, index, cc);
  853. }
  854. }
  855. void CarlaEngine::osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel)
  856. {
  857. qDebug("CarlaEngine::osc_send_set_parameter_midi_channel(%i, %i, %i)", pluginId, index, channel);
  858. Q_ASSERT(m_oscData);
  859. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  860. Q_ASSERT(index >= 0);
  861. Q_ASSERT(channel >= 0 && channel < 16);
  862. if (m_oscData && m_oscData->target)
  863. {
  864. char target_path[strlen(m_oscData->path)+28];
  865. strcpy(target_path, m_oscData->path);
  866. strcat(target_path, "/set_parameter_midi_channel");
  867. lo_send(m_oscData->target, target_path, "iii", pluginId, index, channel);
  868. }
  869. }
  870. void CarlaEngine::osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value)
  871. {
  872. #if DEBUG
  873. if (index < 0)
  874. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %s, %g)", pluginId, InternalParametersIndex2str((InternalParametersIndex)index), value);
  875. else
  876. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %i, %g)", pluginId, index, value);
  877. #endif
  878. Q_ASSERT(m_oscData);
  879. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  880. if (m_oscData && m_oscData->target)
  881. {
  882. char target_path[strlen(m_oscData->path)+21];
  883. strcpy(target_path, m_oscData->path);
  884. strcat(target_path, "/set_parameter_value");
  885. lo_send(m_oscData->target, target_path, "iid", pluginId, index, value);
  886. }
  887. }
  888. void CarlaEngine::osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value)
  889. {
  890. qDebug("CarlaEngine::osc_send_set_default_value(%i, %i, %g)", pluginId, index, value);
  891. Q_ASSERT(m_oscData);
  892. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  893. Q_ASSERT(index >= 0);
  894. if (m_oscData && m_oscData->target)
  895. {
  896. char target_path[strlen(m_oscData->path)+19];
  897. strcpy(target_path, m_oscData->path);
  898. strcat(target_path, "/set_default_value");
  899. lo_send(m_oscData->target, target_path, "iid", pluginId, index, value);
  900. }
  901. }
  902. void CarlaEngine::osc_send_set_program(const int32_t pluginId, const int32_t index)
  903. {
  904. qDebug("CarlaEngine::osc_send_set_program(%i, %i)", pluginId, index);
  905. Q_ASSERT(m_oscData);
  906. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  907. if (m_oscData && m_oscData->target)
  908. {
  909. char target_path[strlen(m_oscData->path)+13];
  910. strcpy(target_path, m_oscData->path);
  911. strcat(target_path, "/set_program");
  912. lo_send(m_oscData->target, target_path, "ii", pluginId, index);
  913. }
  914. }
  915. void CarlaEngine::osc_send_set_program_count(const int32_t pluginId, const int32_t count)
  916. {
  917. qDebug("CarlaEngine::osc_send_set_program_count(%i, %i)", pluginId, count);
  918. Q_ASSERT(m_oscData);
  919. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  920. Q_ASSERT(count >= 0);
  921. if (m_oscData && m_oscData->target)
  922. {
  923. char target_path[strlen(m_oscData->path)+19];
  924. strcpy(target_path, m_oscData->path);
  925. strcat(target_path, "/set_program_count");
  926. lo_send(m_oscData->target, target_path, "ii", pluginId, count);
  927. }
  928. }
  929. void CarlaEngine::osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name)
  930. {
  931. qDebug("CarlaEngine::osc_send_set_program_name(%i, %i, %s)", pluginId, index, name);
  932. Q_ASSERT(m_oscData);
  933. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  934. Q_ASSERT(index >= 0);
  935. Q_ASSERT(name);
  936. if (m_oscData && m_oscData->target)
  937. {
  938. char target_path[strlen(m_oscData->path)+18];
  939. strcpy(target_path, m_oscData->path);
  940. strcat(target_path, "/set_program_name");
  941. lo_send(m_oscData->target, target_path, "iis", pluginId, index, name);
  942. }
  943. }
  944. void CarlaEngine::osc_send_set_midi_program(const int32_t pluginId, const int32_t index)
  945. {
  946. qDebug("CarlaEngine::osc_send_set_midi_program(%i, %i)", pluginId, index);
  947. Q_ASSERT(m_oscData);
  948. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  949. if (m_oscData && m_oscData->target)
  950. {
  951. char target_path[strlen(m_oscData->path)+18];
  952. strcpy(target_path, m_oscData->path);
  953. strcat(target_path, "/set_midi_program");
  954. lo_send(m_oscData->target, target_path, "ii", pluginId, index);
  955. }
  956. }
  957. void CarlaEngine::osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count)
  958. {
  959. qDebug("CarlaEngine::osc_send_set_midi_program_count(%i, %i)", pluginId, count);
  960. Q_ASSERT(m_oscData);
  961. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  962. Q_ASSERT(count >= 0);
  963. if (m_oscData && m_oscData->target)
  964. {
  965. char target_path[strlen(m_oscData->path)+24];
  966. strcpy(target_path, m_oscData->path);
  967. strcat(target_path, "/set_midi_program_count");
  968. lo_send(m_oscData->target, target_path, "ii", pluginId, count);
  969. }
  970. }
  971. void CarlaEngine::osc_send_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name)
  972. {
  973. qDebug("CarlaEngine::osc_send_set_midi_program_data(%i, %i, %i, %i, %s)", pluginId, index, bank, program, name);
  974. Q_ASSERT(m_oscData);
  975. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  976. Q_ASSERT(index >= 0);
  977. Q_ASSERT(bank >= 0);
  978. Q_ASSERT(program >= 0);
  979. Q_ASSERT(name);
  980. if (m_oscData && m_oscData->target)
  981. {
  982. char target_path[strlen(m_oscData->path)+23];
  983. strcpy(target_path, m_oscData->path);
  984. strcat(target_path, "/set_midi_program_data");
  985. lo_send(m_oscData->target, target_path, "iiiis", pluginId, index, bank, program, name);
  986. }
  987. }
  988. void CarlaEngine::osc_send_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value)
  989. {
  990. qDebug("CarlaEngine::osc_send_set_input_peak_value(%i, %i, %g)", pluginId, portId, value);
  991. Q_ASSERT(m_oscData);
  992. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  993. Q_ASSERT(portId == 1 || portId == 2);
  994. if (m_oscData && m_oscData->target)
  995. {
  996. char target_path[strlen(m_oscData->path)+22];
  997. strcpy(target_path, m_oscData->path);
  998. strcat(target_path, "/set_input_peak_value");
  999. lo_send(m_oscData->target, target_path, "iid", pluginId, portId, value);
  1000. }
  1001. }
  1002. void CarlaEngine::osc_send_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value)
  1003. {
  1004. qDebug("CarlaEngine::osc_send_set_output_peak_value(%i, %i, %g)", pluginId, portId, value);
  1005. Q_ASSERT(m_oscData);
  1006. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  1007. Q_ASSERT(portId == 1 || portId == 2);
  1008. if (m_oscData && m_oscData->target)
  1009. {
  1010. char target_path[strlen(m_oscData->path)+23];
  1011. strcpy(target_path, m_oscData->path);
  1012. strcat(target_path, "/set_output_peak_value");
  1013. lo_send(m_oscData->target, target_path, "iid", pluginId, portId, value);
  1014. }
  1015. }
  1016. void CarlaEngine::osc_send_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo)
  1017. {
  1018. qDebug("CarlaEngine::osc_send_note_on(%i, %i, %i, %i)", pluginId, channel, note, velo);
  1019. Q_ASSERT(m_oscData);
  1020. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  1021. Q_ASSERT(channel >= 0 && channel < 16);
  1022. Q_ASSERT(note >= 0 && note < 128);
  1023. Q_ASSERT(velo >= 0 && velo < 128);
  1024. if (m_oscData && m_oscData->target)
  1025. {
  1026. char target_path[strlen(m_oscData->path)+9];
  1027. strcpy(target_path, m_oscData->path);
  1028. strcat(target_path, "/note_on");
  1029. lo_send(m_oscData->target, target_path, "iiii", pluginId, channel, note, velo);
  1030. }
  1031. }
  1032. void CarlaEngine::osc_send_note_off(const int32_t pluginId, const int32_t channel, const int32_t note)
  1033. {
  1034. qDebug("CarlaEngine::osc_send_note_off(%i, %i, %i)", pluginId, channel, note);
  1035. Q_ASSERT(m_oscData);
  1036. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  1037. Q_ASSERT(channel >= 0 && channel < 16);
  1038. Q_ASSERT(note >= 0 && note < 128);
  1039. if (m_oscData && m_oscData->target)
  1040. {
  1041. char target_path[strlen(m_oscData->path)+10];
  1042. strcpy(target_path, m_oscData->path);
  1043. strcat(target_path, "/note_off");
  1044. lo_send(m_oscData->target, target_path, "iii", pluginId, channel, note);
  1045. }
  1046. }
  1047. void CarlaEngine::osc_send_exit()
  1048. {
  1049. qDebug("CarlaEngine::osc_send_exit()");
  1050. Q_ASSERT(m_oscData);
  1051. if (m_oscData && m_oscData->target)
  1052. {
  1053. char target_path[strlen(m_oscData->path)+6];
  1054. strcpy(target_path, m_oscData->path);
  1055. strcat(target_path, "/exit");
  1056. lo_send(m_oscData->target, target_path, "");
  1057. }
  1058. }
  1059. CARLA_BACKEND_END_NAMESPACE