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.

1205 lines
35KB

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