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.

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