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.

1184 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. if (oscData->target)
  704. {
  705. char target_path[strlen(oscData->path)+12];
  706. strcpy(target_path, oscData->path);
  707. strcat(target_path, "/add_plugin");
  708. lo_send(oscData->target, target_path, "is", pluginId, pluginName);
  709. }
  710. }
  711. void CarlaEngine::osc_send_remove_plugin(const int32_t pluginId)
  712. {
  713. qDebug("CarlaEngine::osc_send_remove_plugin(%i)", pluginId);
  714. const CarlaOscData* const oscData = m_osc.getControllerData();
  715. Q_ASSERT(oscData);
  716. if (oscData->target)
  717. {
  718. char target_path[strlen(oscData->path)+15];
  719. strcpy(target_path, oscData->path);
  720. strcat(target_path, "/remove_plugin");
  721. lo_send(oscData->target, target_path, "i", pluginId);
  722. }
  723. }
  724. void CarlaEngine::osc_send_set_plugin_data(const int32_t pluginId, const int32_t type, const int32_t category, const int32_t hints,
  725. 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. if (oscData->target)
  732. {
  733. char target_path[strlen(oscData->path)+17];
  734. strcpy(target_path, oscData->path);
  735. strcat(target_path, "/set_plugin_data");
  736. lo_send(oscData->target, target_path, "iiiissssh", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  737. }
  738. }
  739. 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,
  740. 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,
  755. const char* const name, const char* const label, const double current)
  756. {
  757. qDebug("CarlaEngine::osc_send_set_parameter_data(%i, %i, %i, %i, \"%s\", \"%s\", %g)", pluginId, index, type, hints, name, label, current);
  758. const CarlaOscData* const oscData = m_osc.getControllerData();
  759. Q_ASSERT(oscData);
  760. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  761. if (oscData->target)
  762. {
  763. char target_path[strlen(oscData->path)+20];
  764. strcpy(target_path, oscData->path);
  765. strcat(target_path, "/set_parameter_data");
  766. lo_send(oscData->target, target_path, "iiiissd", pluginId, index, type, hints, name, label, current);
  767. }
  768. }
  769. void CarlaEngine::osc_send_set_parameter_ranges(const int32_t pluginId, const int32_t index, const double min, const double max, const double def,
  770. 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. if (oscData->target)
  777. {
  778. char target_path[strlen(oscData->path)+22];
  779. strcpy(target_path, oscData->path);
  780. strcat(target_path, "/set_parameter_ranges");
  781. lo_send(oscData->target, target_path, "iidddddd", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  782. }
  783. }
  784. void CarlaEngine::osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc)
  785. {
  786. qDebug("CarlaEngine::osc_send_set_parameter_midi_cc(%i, %i, %i)", pluginId, index, cc);
  787. const CarlaOscData* const oscData = m_osc.getControllerData();
  788. Q_ASSERT(oscData);
  789. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  790. if (oscData->target)
  791. {
  792. char target_path[strlen(oscData->path)+23];
  793. strcpy(target_path, oscData->path);
  794. strcat(target_path, "/set_parameter_midi_cc");
  795. lo_send(oscData->target, target_path, "iii", pluginId, index, cc);
  796. }
  797. }
  798. void CarlaEngine::osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel)
  799. {
  800. qDebug("CarlaEngine::osc_send_set_parameter_midi_channel(%i, %i, %i)", pluginId, index, channel);
  801. const CarlaOscData* const oscData = m_osc.getControllerData();
  802. Q_ASSERT(oscData);
  803. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  804. if (oscData->target)
  805. {
  806. char target_path[strlen(oscData->path)+28];
  807. strcpy(target_path, oscData->path);
  808. strcat(target_path, "/set_parameter_midi_channel");
  809. lo_send(oscData->target, target_path, "iii", pluginId, index, channel);
  810. }
  811. }
  812. void CarlaEngine::osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value)
  813. {
  814. #if DEBUG
  815. if (index < 0)
  816. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %s, %g)", pluginId, InternalParametersIndex2str((InternalParametersIndex)index), value);
  817. else
  818. qDebug("CarlaEngine::osc_send_set_parameter_value(%i, %i, %g)", pluginId, index, value);
  819. #endif
  820. const CarlaOscData* const oscData = m_osc.getControllerData();
  821. Q_ASSERT(oscData);
  822. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  823. if (oscData->target)
  824. {
  825. char target_path[strlen(oscData->path)+21];
  826. strcpy(target_path, oscData->path);
  827. strcat(target_path, "/set_parameter_value");
  828. lo_send(oscData->target, target_path, "iid", pluginId, index, value);
  829. }
  830. }
  831. void CarlaEngine::osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value)
  832. {
  833. qDebug("CarlaEngine::osc_send_set_default_value(%i, %i, %g)", pluginId, index, value);
  834. const CarlaOscData* const oscData = m_osc.getControllerData();
  835. Q_ASSERT(oscData);
  836. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  837. if (oscData->target)
  838. {
  839. char target_path[strlen(oscData->path)+19];
  840. strcpy(target_path, oscData->path);
  841. strcat(target_path, "/set_default_value");
  842. lo_send(oscData->target, target_path, "iid", pluginId, index, value);
  843. }
  844. }
  845. void CarlaEngine::osc_send_set_program(const int32_t pluginId, const int32_t index)
  846. {
  847. qDebug("CarlaEngine::osc_send_set_program(%i, %i)", pluginId, index);
  848. const CarlaOscData* const oscData = m_osc.getControllerData();
  849. Q_ASSERT(oscData);
  850. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  851. if (oscData->target)
  852. {
  853. char target_path[strlen(oscData->path)+13];
  854. strcpy(target_path, oscData->path);
  855. strcat(target_path, "/set_program");
  856. lo_send(oscData->target, target_path, "ii", pluginId, index);
  857. }
  858. }
  859. void CarlaEngine::osc_send_set_program_count(const int32_t pluginId, const int32_t count)
  860. {
  861. qDebug("CarlaEngine::osc_send_set_program_count(%i, %i)", pluginId, count);
  862. const CarlaOscData* const oscData = m_osc.getControllerData();
  863. Q_ASSERT(oscData);
  864. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  865. if (oscData->target)
  866. {
  867. char target_path[strlen(oscData->path)+19];
  868. strcpy(target_path, oscData->path);
  869. strcat(target_path, "/set_program_count");
  870. lo_send(oscData->target, target_path, "ii", pluginId, count);
  871. }
  872. }
  873. void CarlaEngine::osc_send_set_program_name(const int32_t plugin_id, const int32_t program_id, const char* const program_name)
  874. {
  875. qDebug("CarlaEngine::osc_send_set_program_name(%i, %i, %s)", plugin_id, program_id, program_name);
  876. const CarlaOscData* const oscData = m_osc.getControllerData();
  877. Q_ASSERT(oscData);
  878. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  879. if (oscData->target)
  880. {
  881. char target_path[strlen(oscData->path)+18];
  882. strcpy(target_path, oscData->path);
  883. strcat(target_path, "/set_program_name");
  884. lo_send(oscData->target, target_path, "iis", plugin_id, program_id, program_name);
  885. }
  886. }
  887. void CarlaEngine::osc_send_set_midi_program(const int32_t plugin_id, const int32_t midi_program_id)
  888. {
  889. qDebug("CarlaEngine::osc_send_set_midi_program(%i, %i)", plugin_id, midi_program_id);
  890. const CarlaOscData* const oscData = m_osc.getControllerData();
  891. Q_ASSERT(oscData);
  892. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  893. if (oscData->target)
  894. {
  895. char target_path[strlen(oscData->path)+18];
  896. strcpy(target_path, oscData->path);
  897. strcat(target_path, "/set_midi_program");
  898. lo_send(oscData->target, target_path, "ii", plugin_id, midi_program_id);
  899. }
  900. }
  901. void CarlaEngine::osc_send_set_midi_program_count(const int32_t plugin_id, const int32_t count)
  902. {
  903. qDebug("CarlaEngine::osc_send_set_midi_program_count(%i, %i)", plugin_id, midi_program_count);
  904. const CarlaOscData* const oscData = m_osc.getControllerData();
  905. Q_ASSERT(oscData);
  906. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  907. if (oscData->target)
  908. {
  909. char target_path[strlen(oscData->path)+24];
  910. strcpy(target_path, oscData->path);
  911. strcat(target_path, "/set_midi_program_count");
  912. lo_send(oscData->target, target_path, "ii", plugin_id, midi_program_count);
  913. }
  914. }
  915. void CarlaEngine::osc_send_set_midi_program_data(const int32_t plugin_id, const int32_t midi_program_id, const int32_t bank_id, const int32_t program_id, const char* const midi_program_name)
  916. {
  917. qDebug("CarlaEngine::osc_send_set_midi_program_data(%i, %i, %i, %i, %s)", plugin_id, midi_program_id, bank_id, program_id, midi_program_name);
  918. const CarlaOscData* const oscData = m_osc.getControllerData();
  919. Q_ASSERT(oscData);
  920. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  921. if (oscData->target)
  922. {
  923. char target_path[strlen(oscData->path)+23];
  924. strcpy(target_path, oscData->path);
  925. strcat(target_path, "/set_midi_program_data");
  926. lo_send(oscData->target, target_path, "iiiis", plugin_id, midi_program_id, bank_id, program_id, midi_program_name);
  927. }
  928. }
  929. void CarlaEngine::osc_send_set_input_peak_value(const int32_t plugin_id, const int32_t port_id, const double value)
  930. {
  931. qDebug("CarlaEngine::osc_send_set_input_peak_value(%i, %i, %f)", plugin_id, port_id, value);
  932. const CarlaOscData* const oscData = m_osc.getControllerData();
  933. Q_ASSERT(oscData);
  934. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  935. if (oscData->target)
  936. {
  937. char target_path[strlen(oscData->path)+22];
  938. strcpy(target_path, oscData->path);
  939. strcat(target_path, "/set_input_peak_value");
  940. lo_send(oscData->target, target_path, "iid", plugin_id, port_id, value);
  941. }
  942. }
  943. void CarlaEngine::osc_send_set_output_peak_value(const int32_t plugin_id, const int32_t port_id, const double value)
  944. {
  945. qDebug("CarlaEngine::osc_send_set_output_peak_value(%i, %i, %f)", plugin_id, port_id, value);
  946. const CarlaOscData* const oscData = m_osc.getControllerData();
  947. Q_ASSERT(oscData);
  948. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  949. if (oscData->target)
  950. {
  951. char target_path[strlen(oscData->path)+23];
  952. strcpy(target_path, oscData->path);
  953. strcat(target_path, "/set_output_peak_value");
  954. lo_send(oscData->target, target_path, "iid", plugin_id, port_id, value);
  955. }
  956. }
  957. void CarlaEngine::osc_send_note_on(const int32_t plugin_id, const int32_t channel, const int32_t note, const int32_t velo)
  958. {
  959. qDebug("CarlaEngine::osc_send_note_on(%i, %i, %i, %i)", plugin_id, channel, note, velo);
  960. const CarlaOscData* const oscData = m_osc.getControllerData();
  961. Q_ASSERT(oscData);
  962. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  963. if (oscData->target)
  964. {
  965. char target_path[strlen(oscData->path)+9];
  966. strcpy(target_path, oscData->path);
  967. strcat(target_path, "/note_on");
  968. lo_send(oscData->target, target_path, "iiii", plugin_id, channel, note, velo);
  969. }
  970. }
  971. void CarlaEngine::osc_send_note_off(const int32_t plugin_id, const int32_t channel, const int32_t note)
  972. {
  973. qDebug("CarlaEngine::osc_send_note_off(%i, %i, %i)", plugin_id, channel, note);
  974. const CarlaOscData* const oscData = m_osc.getControllerData();
  975. Q_ASSERT(oscData);
  976. Q_ASSERT(pluginId >= 0 && pluginId < MAX_PLUGINS);
  977. if (oscData->target)
  978. {
  979. char target_path[strlen(oscData->path)+10];
  980. strcpy(target_path, oscData->path);
  981. strcat(target_path, "/note_off");
  982. lo_send(oscData->target, target_path, "iii", plugin_id, channel, note);
  983. }
  984. }
  985. void CarlaEngine::osc_send_exit()
  986. {
  987. qDebug("CarlaEngine::osc_send_exit()");
  988. const CarlaOscData* const oscData = m_osc.getControllerData();
  989. Q_ASSERT(oscData);
  990. if (oscData->target)
  991. {
  992. char target_path[strlen(oscData->path)+6];
  993. strcpy(target_path, oscData->path);
  994. strcat(target_path, "/exit");
  995. lo_send(oscData->target, target_path, "");
  996. }
  997. }
  998. CARLA_BACKEND_END_NAMESPACE