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.

1239 lines
36KB

  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 CarlaEngineClientNativeHandle& handle_)
  302. : handle(handle_)
  303. {
  304. qDebug("CarlaEngineClient::CarlaEngineClient()");
  305. m_active = false;
  306. }
  307. CarlaEngineClient::~CarlaEngineClient()
  308. {
  309. qDebug("CarlaEngineClient::~CarlaEngineClient()");
  310. Q_ASSERT(! m_active);
  311. #ifdef CARLA_ENGINE_JACK
  312. # ifndef BUILD_BRIDGE
  313. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  314. # endif
  315. {
  316. if (handle.client)
  317. jack_client_close(handle.client);
  318. }
  319. #endif
  320. }
  321. void CarlaEngineClient::activate()
  322. {
  323. qDebug("CarlaEngineClient::activate()");
  324. Q_ASSERT(! m_active);
  325. #ifdef CARLA_ENGINE_JACK
  326. # ifndef BUILD_BRIDGE
  327. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  328. # endif
  329. {
  330. if (handle.client && ! m_active)
  331. jack_activate(handle.client);
  332. }
  333. #endif
  334. m_active = true;
  335. }
  336. void CarlaEngineClient::deactivate()
  337. {
  338. qDebug("CarlaEngineClient::deactivate()");
  339. Q_ASSERT(m_active);
  340. #ifdef CARLA_ENGINE_JACK
  341. # ifndef BUILD_BRIDGE
  342. if (carlaOptions.process_mode == PROCESS_MODE_MULTIPLE_CLIENTS)
  343. # endif
  344. {
  345. if (handle.client && m_active)
  346. jack_deactivate(handle.client);
  347. }
  348. #endif
  349. m_active = false;
  350. }
  351. bool CarlaEngineClient::isActive() const
  352. {
  353. qDebug("CarlaEngineClient::isActive()");
  354. return m_active;
  355. }
  356. bool CarlaEngineClient::isOk() const
  357. {
  358. qDebug("CarlaEngineClient::isOk()");
  359. #ifdef CARLA_ENGINE_JACK
  360. # ifndef BUILD_BRIDGE
  361. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  362. # endif
  363. return bool(handle.client);
  364. #endif
  365. return true;
  366. }
  367. const CarlaEngineBasePort* CarlaEngineClient::addPort(const CarlaEnginePortType type, const char* const name, const bool isInput)
  368. {
  369. qDebug("CarlaEngineClient::addPort(%i, \"%s\", %s)", type, name, bool2str(isInput));
  370. CarlaEnginePortNativeHandle portHandle;
  371. #ifdef CARLA_ENGINE_JACK
  372. portHandle.client = handle.client;
  373. #endif
  374. #ifdef CARLA_ENGINE_JACK
  375. # ifndef BUILD_BRIDGE
  376. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  377. # endif
  378. {
  379. switch (type)
  380. {
  381. case CarlaEnginePortTypeAudio:
  382. portHandle.port = jack_port_register(handle.client, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  383. break;
  384. case CarlaEnginePortTypeControl:
  385. case CarlaEnginePortTypeMIDI:
  386. portHandle.port = jack_port_register(handle.client, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
  387. break;
  388. }
  389. }
  390. #else
  391. Q_UNUSED(name);
  392. #endif
  393. switch (type)
  394. {
  395. case CarlaEnginePortTypeAudio:
  396. return new CarlaEngineAudioPort(portHandle, isInput);
  397. case CarlaEnginePortTypeControl:
  398. return new CarlaEngineControlPort(portHandle, isInput);
  399. case CarlaEnginePortTypeMIDI:
  400. return new CarlaEngineMidiPort(portHandle, isInput);
  401. default:
  402. return nullptr;
  403. }
  404. }
  405. // -------------------------------------------------------------------------------------------------------------------
  406. // Carla Engine Port (Base class)
  407. CarlaEngineBasePort::CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle_, bool isInput_)
  408. : isInput(isInput_),
  409. handle(handle_)
  410. {
  411. qDebug("CarlaEngineBasePort::CarlaEngineBasePort(%s)", bool2str(isInput_));
  412. buffer = nullptr;
  413. }
  414. CarlaEngineBasePort::~CarlaEngineBasePort()
  415. {
  416. qDebug("CarlaEngineBasePort::~CarlaEngineBasePort()");
  417. #ifdef CARLA_ENGINE_JACK
  418. # ifndef BUILD_BRIDGE
  419. if (carlaOptions.process_mode != PROCESS_MODE_CONTINUOUS_RACK)
  420. # endif
  421. {
  422. if (handle.client && handle.port)
  423. jack_port_unregister(handle.client, handle.port);
  424. }
  425. #endif
  426. }
  427. // -------------------------------------------------------------------------------------------------------------------
  428. // Carla Engine Port (Audio)
  429. CarlaEngineAudioPort::CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  430. : CarlaEngineBasePort(handle, isInput)
  431. {
  432. qDebug("CarlaEngineAudioPort::CarlaEngineAudioPort(%s)", bool2str(isInput));
  433. }
  434. void CarlaEngineAudioPort::initBuffer(CarlaEngine* const /*engine*/)
  435. {
  436. }
  437. #ifdef CARLA_ENGINE_JACK
  438. float* CarlaEngineAudioPort::getJackAudioBuffer(uint32_t nframes)
  439. {
  440. # ifndef BUILD_BRIDGE
  441. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  442. return nullptr;
  443. # endif
  444. Q_ASSERT(handle.port);
  445. return (float*)jack_port_get_buffer(handle.port, nframes);
  446. }
  447. #endif
  448. // -------------------------------------------------------------------------------------------------------------------
  449. // Carla Engine Port (Control)
  450. CarlaEngineControlPort::CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  451. : CarlaEngineBasePort(handle, isInput)
  452. {
  453. qDebug("CarlaEngineControlPort::CarlaEngineControlPort(%s)", bool2str(isInput));
  454. }
  455. void CarlaEngineControlPort::initBuffer(CarlaEngine* const engine)
  456. {
  457. Q_ASSERT(engine);
  458. #ifndef BUILD_BRIDGE
  459. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  460. {
  461. buffer = isInput ? engine->rackControlEventsIn : engine->rackControlEventsOut;
  462. return;
  463. }
  464. #endif
  465. #ifdef CARLA_ENGINE_JACK
  466. if (handle.port)
  467. {
  468. buffer = jack_port_get_buffer(handle.port, engine->getBufferSize());
  469. if (! isInput)
  470. jack_midi_clear_buffer(buffer);
  471. }
  472. #endif
  473. }
  474. uint32_t CarlaEngineControlPort::getEventCount()
  475. {
  476. if (! isInput)
  477. return 0;
  478. Q_ASSERT(buffer);
  479. #ifndef BUILD_BRIDGE
  480. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  481. {
  482. uint32_t count = 0;
  483. const CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  484. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS; i++)
  485. {
  486. if (events[i].type != CarlaEngineEventNull)
  487. count++;
  488. else
  489. break;
  490. }
  491. return count;
  492. }
  493. #endif
  494. #ifdef CARLA_ENGINE_JACK
  495. return jack_midi_get_event_count(buffer);
  496. #else
  497. return 0;
  498. #endif
  499. }
  500. const CarlaEngineControlEvent* CarlaEngineControlPort::getEvent(uint32_t index)
  501. {
  502. if (! isInput)
  503. return nullptr;
  504. Q_ASSERT(buffer);
  505. Q_ASSERT(index < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS);
  506. #ifndef BUILD_BRIDGE
  507. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  508. {
  509. const CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  510. if (index < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS)
  511. return &events[index];
  512. return nullptr;
  513. }
  514. #endif
  515. #ifdef CARLA_ENGINE_JACK
  516. static jack_midi_event_t jackEvent;
  517. static CarlaEngineControlEvent carlaEvent;
  518. if (jack_midi_event_get(&jackEvent, buffer, index) != 0)
  519. return nullptr;
  520. memset(&carlaEvent, 0, sizeof(CarlaEngineControlEvent));
  521. uint8_t midiStatus = jackEvent.buffer[0];
  522. uint8_t midiChannel = midiStatus & 0x0F;
  523. carlaEvent.time = jackEvent.time;
  524. carlaEvent.channel = midiChannel;
  525. if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
  526. {
  527. uint8_t midiControl = jackEvent.buffer[1];
  528. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  529. {
  530. uint8_t midiBank = jackEvent.buffer[2];
  531. carlaEvent.type = CarlaEngineEventMidiBankChange;
  532. carlaEvent.value = midiBank;
  533. }
  534. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  535. {
  536. carlaEvent.type = CarlaEngineEventAllSoundOff;
  537. }
  538. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  539. {
  540. carlaEvent.type = CarlaEngineEventAllNotesOff;
  541. }
  542. else
  543. {
  544. uint8_t midiValue = jackEvent.buffer[2];
  545. carlaEvent.type = CarlaEngineEventControlChange;
  546. carlaEvent.controller = midiControl;
  547. carlaEvent.value = double(midiValue)/127;
  548. }
  549. return &carlaEvent;
  550. }
  551. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
  552. {
  553. uint8_t midiProgram = jackEvent.buffer[1];
  554. carlaEvent.type = CarlaEngineEventMidiProgramChange;
  555. carlaEvent.value = midiProgram;
  556. return &carlaEvent;
  557. }
  558. #endif
  559. return nullptr;
  560. }
  561. void CarlaEngineControlPort::writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value)
  562. {
  563. if (isInput)
  564. return;
  565. Q_ASSERT(buffer);
  566. Q_ASSERT(type != CarlaEngineEventNull);
  567. #ifndef BUILD_BRIDGE
  568. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  569. {
  570. CarlaEngineControlEvent* const events = (CarlaEngineControlEvent*)buffer;
  571. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_CONTROL_EVENTS; i++)
  572. {
  573. if (events[i].type == CarlaEngineEventNull)
  574. {
  575. events[i].type = type;
  576. events[i].time = time;
  577. events[i].value = value;
  578. events[i].channel = channel;
  579. events[i].controller = controller;
  580. break;
  581. }
  582. }
  583. return;
  584. }
  585. #endif
  586. #ifdef CARLA_ENGINE_JACK
  587. if (type == CarlaEngineEventControlChange && MIDI_IS_CONTROL_BANK_SELECT(controller))
  588. type = CarlaEngineEventMidiBankChange;
  589. uint8_t data[4] = { 0 };
  590. switch (type)
  591. {
  592. case CarlaEngineEventNull:
  593. break;
  594. case CarlaEngineEventControlChange:
  595. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  596. data[1] = controller;
  597. data[2] = value * 127;
  598. jack_midi_event_write(buffer, time, data, 3);
  599. break;
  600. case CarlaEngineEventMidiBankChange:
  601. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  602. data[1] = MIDI_CONTROL_BANK_SELECT;
  603. data[2] = value;
  604. jack_midi_event_write(buffer, time, data, 3);
  605. break;
  606. case CarlaEngineEventMidiProgramChange:
  607. data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
  608. data[1] = value;
  609. jack_midi_event_write(buffer, time, data, 2);
  610. break;
  611. case CarlaEngineEventAllSoundOff:
  612. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  613. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  614. jack_midi_event_write(buffer, time, data, 2);
  615. break;
  616. case CarlaEngineEventAllNotesOff:
  617. data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
  618. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  619. jack_midi_event_write(buffer, time, data, 2);
  620. break;
  621. }
  622. #endif
  623. }
  624. // -------------------------------------------------------------------------------------------------------------------
  625. // Carla Engine Port (MIDI)
  626. CarlaEngineMidiPort::CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, bool isInput)
  627. : CarlaEngineBasePort(handle, isInput)
  628. {
  629. qDebug("CarlaEngineMidiPort::CarlaEngineMidiPort(%s)", bool2str(isInput));
  630. }
  631. void CarlaEngineMidiPort::initBuffer(CarlaEngine* const engine)
  632. {
  633. Q_ASSERT(engine);
  634. #ifndef BUILD_BRIDGE
  635. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  636. {
  637. buffer = isInput ? engine->rackMidiEventsIn : engine->rackMidiEventsOut;
  638. return;
  639. }
  640. #endif
  641. #ifdef CARLA_ENGINE_JACK
  642. if (handle.port)
  643. {
  644. buffer = jack_port_get_buffer(handle.port, engine->getBufferSize());
  645. if (! isInput)
  646. jack_midi_clear_buffer(buffer);
  647. }
  648. #endif
  649. }
  650. uint32_t CarlaEngineMidiPort::getEventCount()
  651. {
  652. if (! isInput)
  653. return 0;
  654. Q_ASSERT(buffer);
  655. #ifndef BUILD_BRIDGE
  656. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  657. {
  658. uint32_t count = 0;
  659. const CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  660. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_MIDI_EVENTS; i++)
  661. {
  662. if (events[i].size > 0)
  663. count++;
  664. else
  665. break;
  666. }
  667. return count;
  668. }
  669. #endif
  670. #ifdef CARLA_ENGINE_JACK
  671. return jack_midi_get_event_count(buffer);
  672. #else
  673. return 0;
  674. #endif
  675. }
  676. const CarlaEngineMidiEvent* CarlaEngineMidiPort::getEvent(uint32_t index)
  677. {
  678. if (! isInput)
  679. return nullptr;
  680. Q_ASSERT(buffer);
  681. Q_ASSERT(index < CarlaEngine::MAX_ENGINE_MIDI_EVENTS);
  682. #ifndef BUILD_BRIDGE
  683. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  684. {
  685. const CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  686. if (index < CarlaEngine::MAX_ENGINE_MIDI_EVENTS)
  687. return &events[index];
  688. return nullptr;
  689. }
  690. #endif
  691. #ifdef CARLA_ENGINE_JACK
  692. static jack_midi_event_t jackEvent;
  693. static CarlaEngineMidiEvent carlaEvent;
  694. if (jack_midi_event_get(&jackEvent, buffer, index) == 0 && jackEvent.size <= 4)
  695. {
  696. carlaEvent.time = jackEvent.time;
  697. carlaEvent.size = jackEvent.size;
  698. memcpy(carlaEvent.data, jackEvent.buffer, jackEvent.size);
  699. return &carlaEvent;
  700. }
  701. #endif
  702. return nullptr;
  703. }
  704. void CarlaEngineMidiPort::writeEvent(uint32_t time, uint8_t* data, uint8_t size)
  705. {
  706. if (isInput)
  707. return;
  708. Q_ASSERT(buffer);
  709. Q_ASSERT(data);
  710. Q_ASSERT(size > 0);
  711. #ifndef BUILD_BRIDGE
  712. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  713. {
  714. if (size > 4)
  715. return;
  716. CarlaEngineMidiEvent* const events = (CarlaEngineMidiEvent*)buffer;
  717. for (unsigned short i=0; i < CarlaEngine::MAX_ENGINE_MIDI_EVENTS; i++)
  718. {
  719. if (events[i].size == 0)
  720. {
  721. events[i].time = time;
  722. events[i].size = size;
  723. memcpy(events[i].data, data, size);
  724. break;
  725. }
  726. }
  727. return;
  728. }
  729. #endif
  730. #ifdef CARLA_ENGINE_JACK
  731. jack_midi_event_write(buffer, time, data, size);
  732. #endif
  733. }
  734. // -------------------------------------------------------------------------------------------------------------------
  735. // Carla Engine OSC stuff
  736. void CarlaEngine::osc_send_add_plugin(const int32_t pluginId, const char* const pluginName)
  737. {
  738. qDebug("CarlaEngine::osc_send_add_plugin(%i, \"%s\")", pluginId, pluginName);
  739. Q_ASSERT(m_oscData);
  740. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  741. Q_ASSERT(pluginName);
  742. if (m_oscData && m_oscData->target)
  743. {
  744. char target_path[strlen(m_oscData->path)+12];
  745. strcpy(target_path, m_oscData->path);
  746. strcat(target_path, "/add_plugin");
  747. lo_send(m_oscData->target, target_path, "is", pluginId, pluginName);
  748. }
  749. }
  750. void CarlaEngine::osc_send_remove_plugin(const int32_t pluginId)
  751. {
  752. qDebug("CarlaEngine::osc_send_remove_plugin(%i)", pluginId);
  753. Q_ASSERT(m_oscData);
  754. if (m_oscData && m_oscData->target)
  755. {
  756. char target_path[strlen(m_oscData->path)+15];
  757. strcpy(target_path, m_oscData->path);
  758. strcat(target_path, "/remove_plugin");
  759. lo_send(m_oscData->target, target_path, "i", pluginId);
  760. }
  761. }
  762. 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)
  763. {
  764. 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);
  765. Q_ASSERT(m_oscData);
  766. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  767. Q_ASSERT(type != PLUGIN_NONE);
  768. if (m_oscData && m_oscData->target)
  769. {
  770. char target_path[strlen(m_oscData->path)+17];
  771. strcpy(target_path, m_oscData->path);
  772. strcat(target_path, "/set_plugin_data");
  773. lo_send(m_oscData->target, target_path, "iiiissssh", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  774. }
  775. }
  776. 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)
  777. {
  778. qDebug("CarlaEngine::osc_send_set_plugin_ports(%i, %i, %i, %i, %i, %i, %i, %i)", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts, cTotals);
  779. Q_ASSERT(m_oscData);
  780. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  781. if (m_oscData && m_oscData->target)
  782. {
  783. char target_path[strlen(m_oscData->path)+18];
  784. strcpy(target_path, m_oscData->path);
  785. strcat(target_path, "/set_plugin_ports");
  786. lo_send(m_oscData->target, target_path, "iiiiiiii", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts, cTotals);
  787. }
  788. }
  789. 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)
  790. {
  791. qDebug("CarlaEngine::osc_send_set_parameter_data(%i, %i, %i, %i, \"%s\", \"%s\", %g)", pluginId, index, type, hints, name, label, current);
  792. Q_ASSERT(m_oscData);
  793. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  794. Q_ASSERT(index >= 0);
  795. Q_ASSERT(type != PARAMETER_UNKNOWN);
  796. if (m_oscData && m_oscData->target)
  797. {
  798. char target_path[strlen(m_oscData->path)+20];
  799. strcpy(target_path, m_oscData->path);
  800. strcat(target_path, "/set_parameter_data");
  801. lo_send(m_oscData->target, target_path, "iiiissd", pluginId, index, type, hints, name, label, current);
  802. }
  803. }
  804. 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)
  805. {
  806. qDebug("CarlaEngine::osc_send_set_parameter_ranges(%i, %i, %g, %g, %g, %g, %g, %g)", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  807. Q_ASSERT(m_oscData);
  808. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  809. Q_ASSERT(index >= 0);
  810. Q_ASSERT(min < max);
  811. if (m_oscData && m_oscData->target)
  812. {
  813. char target_path[strlen(m_oscData->path)+22];
  814. strcpy(target_path, m_oscData->path);
  815. strcat(target_path, "/set_parameter_ranges");
  816. lo_send(m_oscData->target, target_path, "iidddddd", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  817. }
  818. }
  819. void CarlaEngine::osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc)
  820. {
  821. qDebug("CarlaEngine::osc_send_set_parameter_midi_cc(%i, %i, %i)", pluginId, index, cc);
  822. Q_ASSERT(m_oscData);
  823. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  824. Q_ASSERT(index >= 0);
  825. if (m_oscData && m_oscData->target)
  826. {
  827. char target_path[strlen(m_oscData->path)+23];
  828. strcpy(target_path, m_oscData->path);
  829. strcat(target_path, "/set_parameter_midi_cc");
  830. lo_send(m_oscData->target, target_path, "iii", pluginId, index, cc);
  831. }
  832. }
  833. void CarlaEngine::osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel)
  834. {
  835. qDebug("CarlaEngine::osc_send_set_parameter_midi_channel(%i, %i, %i)", pluginId, index, channel);
  836. Q_ASSERT(m_oscData);
  837. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  838. Q_ASSERT(index >= 0);
  839. Q_ASSERT(channel >= 0 && channel < 16);
  840. if (m_oscData && m_oscData->target)
  841. {
  842. char target_path[strlen(m_oscData->path)+28];
  843. strcpy(target_path, m_oscData->path);
  844. strcat(target_path, "/set_parameter_midi_channel");
  845. lo_send(m_oscData->target, target_path, "iii", pluginId, index, channel);
  846. }
  847. }
  848. void CarlaEngine::osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value)
  849. {
  850. #if DEBUG
  851. if (index < 0)
  852. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %s, %g)", pluginId, InternalParametersIndex2str((InternalParametersIndex)index), value);
  853. else
  854. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %i, %g)", pluginId, index, value);
  855. #endif
  856. Q_ASSERT(m_oscData);
  857. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  858. if (m_oscData && m_oscData->target)
  859. {
  860. char target_path[strlen(m_oscData->path)+21];
  861. strcpy(target_path, m_oscData->path);
  862. strcat(target_path, "/set_parameter_value");
  863. lo_send(m_oscData->target, target_path, "iid", pluginId, index, value);
  864. }
  865. }
  866. void CarlaEngine::osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value)
  867. {
  868. qDebug("CarlaEngine::osc_send_set_default_value(%i, %i, %g)", pluginId, index, value);
  869. Q_ASSERT(m_oscData);
  870. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  871. Q_ASSERT(index >= 0);
  872. if (m_oscData && m_oscData->target)
  873. {
  874. char target_path[strlen(m_oscData->path)+19];
  875. strcpy(target_path, m_oscData->path);
  876. strcat(target_path, "/set_default_value");
  877. lo_send(m_oscData->target, target_path, "iid", pluginId, index, value);
  878. }
  879. }
  880. void CarlaEngine::osc_send_set_program(const int32_t pluginId, const int32_t index)
  881. {
  882. qDebug("CarlaEngine::osc_send_set_program(%i, %i)", pluginId, index);
  883. Q_ASSERT(m_oscData);
  884. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  885. if (m_oscData && m_oscData->target)
  886. {
  887. char target_path[strlen(m_oscData->path)+13];
  888. strcpy(target_path, m_oscData->path);
  889. strcat(target_path, "/set_program");
  890. lo_send(m_oscData->target, target_path, "ii", pluginId, index);
  891. }
  892. }
  893. void CarlaEngine::osc_send_set_program_count(const int32_t pluginId, const int32_t count)
  894. {
  895. qDebug("CarlaEngine::osc_send_set_program_count(%i, %i)", pluginId, count);
  896. Q_ASSERT(m_oscData);
  897. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  898. Q_ASSERT(count >= 0);
  899. if (m_oscData && m_oscData->target)
  900. {
  901. char target_path[strlen(m_oscData->path)+19];
  902. strcpy(target_path, m_oscData->path);
  903. strcat(target_path, "/set_program_count");
  904. lo_send(m_oscData->target, target_path, "ii", pluginId, count);
  905. }
  906. }
  907. void CarlaEngine::osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name)
  908. {
  909. qDebug("CarlaEngine::osc_send_set_program_name(%i, %i, %s)", pluginId, index, name);
  910. Q_ASSERT(m_oscData);
  911. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  912. Q_ASSERT(index >= 0);
  913. Q_ASSERT(name);
  914. if (m_oscData && m_oscData->target)
  915. {
  916. char target_path[strlen(m_oscData->path)+18];
  917. strcpy(target_path, m_oscData->path);
  918. strcat(target_path, "/set_program_name");
  919. lo_send(m_oscData->target, target_path, "iis", pluginId, index, name);
  920. }
  921. }
  922. void CarlaEngine::osc_send_set_midi_program(const int32_t pluginId, const int32_t index)
  923. {
  924. qDebug("CarlaEngine::osc_send_set_midi_program(%i, %i)", pluginId, index);
  925. Q_ASSERT(m_oscData);
  926. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  927. if (m_oscData && m_oscData->target)
  928. {
  929. char target_path[strlen(m_oscData->path)+18];
  930. strcpy(target_path, m_oscData->path);
  931. strcat(target_path, "/set_midi_program");
  932. lo_send(m_oscData->target, target_path, "ii", pluginId, index);
  933. }
  934. }
  935. void CarlaEngine::osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count)
  936. {
  937. qDebug("CarlaEngine::osc_send_set_midi_program_count(%i, %i)", pluginId, count);
  938. Q_ASSERT(m_oscData);
  939. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  940. Q_ASSERT(count >= 0);
  941. if (m_oscData && m_oscData->target)
  942. {
  943. char target_path[strlen(m_oscData->path)+24];
  944. strcpy(target_path, m_oscData->path);
  945. strcat(target_path, "/set_midi_program_count");
  946. lo_send(m_oscData->target, target_path, "ii", pluginId, count);
  947. }
  948. }
  949. 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)
  950. {
  951. qDebug("CarlaEngine::osc_send_set_midi_program_data(%i, %i, %i, %i, %s)", pluginId, index, bank, program, name);
  952. Q_ASSERT(m_oscData);
  953. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  954. Q_ASSERT(index >= 0);
  955. Q_ASSERT(bank >= 0);
  956. Q_ASSERT(program >= 0);
  957. Q_ASSERT(name);
  958. if (m_oscData && m_oscData->target)
  959. {
  960. char target_path[strlen(m_oscData->path)+23];
  961. strcpy(target_path, m_oscData->path);
  962. strcat(target_path, "/set_midi_program_data");
  963. lo_send(m_oscData->target, target_path, "iiiis", pluginId, index, bank, program, name);
  964. }
  965. }
  966. void CarlaEngine::osc_send_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value)
  967. {
  968. qDebug("CarlaEngine::osc_send_set_input_peak_value(%i, %i, %g)", pluginId, portId, value);
  969. Q_ASSERT(m_oscData);
  970. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  971. Q_ASSERT(portId == 1 || portId == 2);
  972. if (m_oscData && m_oscData->target)
  973. {
  974. char target_path[strlen(m_oscData->path)+22];
  975. strcpy(target_path, m_oscData->path);
  976. strcat(target_path, "/set_input_peak_value");
  977. lo_send(m_oscData->target, target_path, "iid", pluginId, portId, value);
  978. }
  979. }
  980. void CarlaEngine::osc_send_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value)
  981. {
  982. qDebug("CarlaEngine::osc_send_set_output_peak_value(%i, %i, %g)", pluginId, portId, value);
  983. Q_ASSERT(m_oscData);
  984. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  985. Q_ASSERT(portId == 1 || portId == 2);
  986. if (m_oscData && m_oscData->target)
  987. {
  988. char target_path[strlen(m_oscData->path)+23];
  989. strcpy(target_path, m_oscData->path);
  990. strcat(target_path, "/set_output_peak_value");
  991. lo_send(m_oscData->target, target_path, "iid", pluginId, portId, value);
  992. }
  993. }
  994. void CarlaEngine::osc_send_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo)
  995. {
  996. qDebug("CarlaEngine::osc_send_note_on(%i, %i, %i, %i)", pluginId, channel, note, velo);
  997. Q_ASSERT(m_oscData);
  998. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  999. Q_ASSERT(channel >= 0 && channel < 16);
  1000. Q_ASSERT(note >= 0 && note < 128);
  1001. Q_ASSERT(velo >= 0 && velo < 128);
  1002. if (m_oscData && m_oscData->target)
  1003. {
  1004. char target_path[strlen(m_oscData->path)+9];
  1005. strcpy(target_path, m_oscData->path);
  1006. strcat(target_path, "/note_on");
  1007. lo_send(m_oscData->target, target_path, "iiii", pluginId, channel, note, velo);
  1008. }
  1009. }
  1010. void CarlaEngine::osc_send_note_off(const int32_t pluginId, const int32_t channel, const int32_t note)
  1011. {
  1012. qDebug("CarlaEngine::osc_send_note_off(%i, %i, %i)", pluginId, channel, note);
  1013. Q_ASSERT(m_oscData);
  1014. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  1015. Q_ASSERT(channel >= 0 && channel < 16);
  1016. Q_ASSERT(note >= 0 && note < 128);
  1017. if (m_oscData && m_oscData->target)
  1018. {
  1019. char target_path[strlen(m_oscData->path)+10];
  1020. strcpy(target_path, m_oscData->path);
  1021. strcat(target_path, "/note_off");
  1022. lo_send(m_oscData->target, target_path, "iii", pluginId, channel, note);
  1023. }
  1024. }
  1025. void CarlaEngine::osc_send_exit()
  1026. {
  1027. qDebug("CarlaEngine::osc_send_exit()");
  1028. Q_ASSERT(m_oscData);
  1029. if (m_oscData && m_oscData->target)
  1030. {
  1031. char target_path[strlen(m_oscData->path)+6];
  1032. strcpy(target_path, m_oscData->path);
  1033. strcat(target_path, "/exit");
  1034. lo_send(m_oscData->target, target_path, "");
  1035. }
  1036. }
  1037. CARLA_BACKEND_END_NAMESPACE