Audio plugin host https://kx.studio/carla
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.

2573 lines
88KB

  1. /*
  2. * Carla Engine
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. /* TODO:
  18. * - add more checks to oscSend_* stuff
  19. * - complete processRack(): carefully add to input, sorted events
  20. * - implement processPatchbay()
  21. * - implement oscSend_control_switch_plugins()
  22. * - proper find&load plugins
  23. * - uncomment CarlaPlugin::newAU and newCSOUND
  24. * - something about the peaks?
  25. */
  26. #include "CarlaEngineInternal.hpp"
  27. #include "CarlaBackendUtils.hpp"
  28. #include "CarlaStateUtils.hpp"
  29. #include "CarlaMIDI.h"
  30. #ifdef HAVE_JUCE
  31. # include "juce_audio_basics.h"
  32. using juce::FloatVectorOperations;
  33. #else
  34. # include <cmath>
  35. #endif
  36. #include <QtCore/QFile>
  37. #include <QtCore/QFileInfo>
  38. #include <QtCore/QTextStream>
  39. CARLA_BACKEND_START_NAMESPACE
  40. #if 0
  41. } // Fix editor indentation
  42. #endif
  43. // -----------------------------------------------------------------------
  44. // Fallback data
  45. static EngineEvent kFallbackEngineEvent;
  46. // -----------------------------------------------------------------------
  47. // Carla Engine port (Abstract)
  48. CarlaEnginePort::CarlaEnginePort(const CarlaEngine& engine, const bool isInput)
  49. : fEngine(engine),
  50. fIsInput(isInput)
  51. {
  52. carla_debug("CarlaEnginePort::CarlaEnginePort(%s)", bool2str(isInput));
  53. }
  54. CarlaEnginePort::~CarlaEnginePort()
  55. {
  56. carla_debug("CarlaEnginePort::~CarlaEnginePort()");
  57. }
  58. // -----------------------------------------------------------------------
  59. // Carla Engine Audio port
  60. CarlaEngineAudioPort::CarlaEngineAudioPort(const CarlaEngine& engine, const bool isInput)
  61. : CarlaEnginePort(engine, isInput),
  62. fBuffer(nullptr)
  63. {
  64. carla_debug("CarlaEngineAudioPort::CarlaEngineAudioPort(%s)", bool2str(isInput));
  65. }
  66. CarlaEngineAudioPort::~CarlaEngineAudioPort()
  67. {
  68. carla_debug("CarlaEngineAudioPort::~CarlaEngineAudioPort()");
  69. }
  70. void CarlaEngineAudioPort::initBuffer()
  71. {
  72. }
  73. // -----------------------------------------------------------------------
  74. // Carla Engine CV port
  75. CarlaEngineCVPort::CarlaEngineCVPort(const CarlaEngine& engine, const bool isInput)
  76. : CarlaEnginePort(engine, isInput),
  77. fBuffer(nullptr),
  78. fProcessMode(engine.getProccessMode())
  79. {
  80. carla_debug("CarlaEngineCVPort::CarlaEngineCVPort(%s)", bool2str(isInput));
  81. if (fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  82. fBuffer = new float[engine.getBufferSize()];
  83. }
  84. CarlaEngineCVPort::~CarlaEngineCVPort()
  85. {
  86. carla_debug("CarlaEngineCVPort::~CarlaEngineCVPort()");
  87. if (fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  90. delete[] fBuffer;
  91. fBuffer = nullptr;
  92. }
  93. }
  94. void CarlaEngineCVPort::initBuffer()
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  97. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS,);
  98. #ifdef HAVE_JUCE
  99. FloatVectorOperations::clear(fBuffer, fEngine.getBufferSize());
  100. #else
  101. carla_zeroFloat(fBuffer, fEngine.getBufferSize());
  102. #endif
  103. }
  104. void CarlaEngineCVPort::setBufferSize(const uint32_t bufferSize)
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  107. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS,);
  108. delete[] fBuffer;
  109. fBuffer = new float[bufferSize];
  110. }
  111. // -----------------------------------------------------------------------
  112. // Carla Engine Event port
  113. CarlaEngineEventPort::CarlaEngineEventPort(const CarlaEngine& engine, const bool isInput)
  114. : CarlaEnginePort(engine, isInput),
  115. fBuffer(nullptr),
  116. fProcessMode(engine.getProccessMode())
  117. {
  118. carla_debug("CarlaEngineEventPort::CarlaEngineEventPort(%s)", bool2str(isInput));
  119. static bool sFallbackEngineEventNeedsInit = true;
  120. if (sFallbackEngineEventNeedsInit)
  121. {
  122. kFallbackEngineEvent.clear();
  123. sFallbackEngineEventNeedsInit = false;
  124. }
  125. if (fProcessMode == ENGINE_PROCESS_MODE_PATCHBAY)
  126. fBuffer = new EngineEvent[kEngineMaxInternalEventCount];
  127. }
  128. CarlaEngineEventPort::~CarlaEngineEventPort()
  129. {
  130. carla_debug("CarlaEngineEventPort::~CarlaEngineEventPort()");
  131. if (fProcessMode == ENGINE_PROCESS_MODE_PATCHBAY)
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  134. delete[] fBuffer;
  135. fBuffer = nullptr;
  136. }
  137. }
  138. void CarlaEngineEventPort::initBuffer()
  139. {
  140. if (fProcessMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || fProcessMode == ENGINE_PROCESS_MODE_BRIDGE)
  141. fBuffer = fEngine.getInternalEventBuffer(fIsInput);
  142. else if (fProcessMode == ENGINE_PROCESS_MODE_PATCHBAY && ! fIsInput)
  143. carla_zeroStruct<EngineEvent>(fBuffer, kEngineMaxInternalEventCount);
  144. }
  145. uint32_t CarlaEngineEventPort::getEventCount() const
  146. {
  147. CARLA_SAFE_ASSERT_RETURN(fIsInput, 0);
  148. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, 0);
  149. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, 0);
  150. uint32_t i=0;
  151. for (; i < kEngineMaxInternalEventCount; ++i)
  152. {
  153. if (fBuffer[i].type == kEngineEventTypeNull)
  154. break;
  155. }
  156. return i;
  157. }
  158. const EngineEvent& CarlaEngineEventPort::getEvent(const uint32_t index)
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(fIsInput, kFallbackEngineEvent);
  161. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, kFallbackEngineEvent);
  162. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, kFallbackEngineEvent);
  163. CARLA_SAFE_ASSERT_RETURN(index < kEngineMaxInternalEventCount, kFallbackEngineEvent);
  164. return fBuffer[index];
  165. }
  166. const EngineEvent& CarlaEngineEventPort::getEventUnchecked(const uint32_t index)
  167. {
  168. return fBuffer[index];
  169. }
  170. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value)
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(! fIsInput, false);
  173. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  174. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  175. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  176. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  177. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  178. if (type == kEngineControlEventTypeParameter)
  179. {
  180. CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  181. }
  182. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  183. for (uint32_t i=0; i < kEngineMaxInternalEventCount; ++i)
  184. {
  185. if (fBuffer[i].type != kEngineEventTypeNull)
  186. continue;
  187. EngineEvent& event(fBuffer[i]);
  188. event.type = kEngineEventTypeControl;
  189. event.time = time;
  190. event.channel = channel;
  191. event.ctrl.type = type;
  192. event.ctrl.param = param;
  193. event.ctrl.value = fixedValue;
  194. return true;
  195. }
  196. carla_stderr2("CarlaEngineEventPort::writeControlEvent() - buffer full");
  197. return false;
  198. }
  199. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t size, const uint8_t* const data)
  200. {
  201. CARLA_SAFE_ASSERT_RETURN(! fIsInput, false);
  202. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  203. CARLA_SAFE_ASSERT_RETURN(fProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && fProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  204. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  205. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= EngineMidiEvent::kDataSize, false);
  206. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  207. for (uint32_t i=0; i < kEngineMaxInternalEventCount; ++i)
  208. {
  209. if (fBuffer[i].type != kEngineEventTypeNull)
  210. continue;
  211. EngineEvent& event(fBuffer[i]);
  212. event.type = kEngineEventTypeMidi;
  213. event.time = time;
  214. event.channel = channel;
  215. event.midi.port = port;
  216. event.midi.size = size;
  217. event.midi.data[0] = MIDI_GET_STATUS_FROM_DATA(data);
  218. for (uint8_t j=1; j < size; ++j)
  219. event.midi.data[j] = data[j];
  220. return true;
  221. }
  222. carla_stderr2("CarlaEngineEventPort::writeMidiEvent() - buffer full");
  223. return false;
  224. }
  225. // -----------------------------------------------------------------------
  226. // Carla Engine client (Abstract)
  227. CarlaEngineClient::CarlaEngineClient(const CarlaEngine& engine)
  228. : fEngine(engine),
  229. fActive(false),
  230. fLatency(0)
  231. {
  232. carla_debug("CarlaEngineClient::CarlaEngineClient()");
  233. }
  234. CarlaEngineClient::~CarlaEngineClient()
  235. {
  236. CARLA_ASSERT(! fActive);
  237. carla_debug("CarlaEngineClient::~CarlaEngineClient()");
  238. }
  239. void CarlaEngineClient::activate()
  240. {
  241. CARLA_ASSERT(! fActive);
  242. carla_debug("CarlaEngineClient::activate()");
  243. fActive = true;
  244. }
  245. void CarlaEngineClient::deactivate()
  246. {
  247. CARLA_ASSERT(fActive);
  248. carla_debug("CarlaEngineClient::deactivate()");
  249. fActive = false;
  250. }
  251. bool CarlaEngineClient::isActive() const noexcept
  252. {
  253. return fActive;
  254. }
  255. bool CarlaEngineClient::isOk() const noexcept
  256. {
  257. return true;
  258. }
  259. uint32_t CarlaEngineClient::getLatency() const noexcept
  260. {
  261. return fLatency;
  262. }
  263. void CarlaEngineClient::setLatency(const uint32_t samples) noexcept
  264. {
  265. fLatency = samples;
  266. }
  267. CarlaEnginePort* CarlaEngineClient::addPort(const EnginePortType portType, const char* const name, const bool isInput)
  268. {
  269. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  270. carla_debug("CarlaEngineClient::addPort(%i:%s, \"%s\", %s)", portType, EnginePortType2Str(portType), name, bool2str(isInput));
  271. switch (portType)
  272. {
  273. case kEnginePortTypeNull:
  274. break;
  275. case kEnginePortTypeAudio:
  276. return new CarlaEngineAudioPort(fEngine, isInput);
  277. case kEnginePortTypeCV:
  278. return new CarlaEngineCVPort(fEngine, isInput);
  279. case kEnginePortTypeEvent:
  280. return new CarlaEngineEventPort(fEngine, isInput);
  281. }
  282. carla_stderr("CarlaEngineClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  283. return nullptr;
  284. }
  285. // -----------------------------------------------------------------------
  286. // Carla Engine
  287. CarlaEngine::CarlaEngine()
  288. : pData(new CarlaEngineProtectedData(this))
  289. {
  290. carla_debug("CarlaEngine::CarlaEngine()");
  291. }
  292. CarlaEngine::~CarlaEngine()
  293. {
  294. carla_debug("CarlaEngine::~CarlaEngine()");
  295. delete pData;
  296. }
  297. // -----------------------------------------------------------------------
  298. // Static calls
  299. unsigned int CarlaEngine::getDriverCount()
  300. {
  301. carla_debug("CarlaEngine::getDriverCount()");
  302. unsigned int count = 1; // JACK
  303. #ifndef BUILD_BRIDGE
  304. count += getRtAudioApiCount();
  305. # ifdef HAVE_JUCE
  306. count += getJuceApiCount();
  307. # endif
  308. #endif
  309. return count;
  310. }
  311. const char* CarlaEngine::getDriverName(const unsigned int index)
  312. {
  313. carla_debug("CarlaEngine::getDriverName(%i)", index);
  314. if (index == 0)
  315. return "JACK";
  316. #ifndef BUILD_BRIDGE
  317. const unsigned int rtAudioIndex(index-1);
  318. if (rtAudioIndex < getRtAudioApiCount())
  319. return getRtAudioApiName(rtAudioIndex);
  320. # ifdef HAVE_JUCE
  321. const unsigned int juceIndex(index-rtAudioIndex-1);
  322. if (juceIndex < getJuceApiCount())
  323. return getJuceApiName(juceIndex);
  324. # endif
  325. #endif
  326. carla_stderr("CarlaEngine::getDriverName(%i) - invalid index", index);
  327. return nullptr;
  328. }
  329. const char* const* CarlaEngine::getDriverDeviceNames(const unsigned int index)
  330. {
  331. carla_debug("CarlaEngine::getDriverDeviceNames(%i)", index);
  332. if (index == 0) // JACK
  333. {
  334. static const char* ret[3] = { "Auto-Connect OFF", "Auto-Connect ON", nullptr };
  335. return ret;
  336. }
  337. #ifndef BUILD_BRIDGE
  338. const unsigned int rtAudioIndex(index-1);
  339. if (rtAudioIndex < getRtAudioApiCount())
  340. return getRtAudioApiDeviceNames(rtAudioIndex);
  341. # ifdef HAVE_JUCE
  342. const unsigned int juceIndex(index-rtAudioIndex-1);
  343. if (juceIndex < getJuceApiCount())
  344. return getJuceApiDeviceNames(juceIndex);
  345. # endif
  346. #endif
  347. carla_stderr("CarlaEngine::getDriverDeviceNames(%i) - invalid index", index);
  348. return nullptr;
  349. }
  350. const EngineDriverDeviceInfo* CarlaEngine::getDriverDeviceInfo(const unsigned int index, const char* const deviceName)
  351. {
  352. carla_debug("CarlaEngine::getDriverDeviceInfo(%i, \"%s\")", index, deviceName);
  353. if (index == 0) // JACK
  354. {
  355. static uint32_t bufSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  356. static EngineDriverDeviceInfo devInfo;
  357. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE;
  358. devInfo.bufferSizes = bufSizes;
  359. devInfo.sampleRates = nullptr;
  360. return &devInfo;
  361. }
  362. #ifndef BUILD_BRIDGE
  363. const unsigned int rtAudioIndex(index-1);
  364. if (rtAudioIndex < getRtAudioApiCount())
  365. return getRtAudioDeviceInfo(rtAudioIndex, deviceName);
  366. # ifdef HAVE_JUCE
  367. const unsigned int juceIndex(index-rtAudioIndex-1);
  368. if (juceIndex < getJuceApiCount())
  369. return getJuceDeviceInfo(juceIndex, deviceName);
  370. # endif
  371. #endif
  372. carla_stderr("CarlaEngine::getDriverDeviceNames(%i, \"%s\") - invalid index", index, deviceName);
  373. return nullptr;
  374. }
  375. CarlaEngine* CarlaEngine::newDriverByName(const char* const driverName)
  376. {
  377. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', nullptr);
  378. carla_debug("CarlaEngine::newDriverByName(\"%s\")", driverName);
  379. if (std::strcmp(driverName, "JACK") == 0)
  380. return newJack();
  381. // common
  382. if (std::strncmp(driverName, "JACK ", 5) == 0)
  383. return newRtAudio(AUDIO_API_JACK);
  384. // linux
  385. if (std::strcmp(driverName, "ALSA") == 0)
  386. return newRtAudio(AUDIO_API_ALSA);
  387. if (std::strcmp(driverName, "OSS") == 0)
  388. return newRtAudio(AUDIO_API_OSS);
  389. if (std::strcmp(driverName, "PulseAudio") == 0)
  390. return newRtAudio(AUDIO_API_PULSE);
  391. // macos
  392. if (std::strcmp(driverName, "CoreAudio") == 0)
  393. return newRtAudio(AUDIO_API_CORE);
  394. // windows
  395. if (std::strcmp(driverName, "ASIO") == 0)
  396. return newRtAudio(AUDIO_API_ASIO);
  397. if (std::strcmp(driverName, "DirectSound") == 0)
  398. return newRtAudio(AUDIO_API_DS);
  399. carla_stderr("CarlaEngine::newDriverByName(\"%s\") - invalid driver name", driverName);
  400. return nullptr;
  401. }
  402. // -----------------------------------------------------------------------
  403. // Maximum values
  404. unsigned int CarlaEngine::getMaxClientNameSize() const noexcept
  405. {
  406. return STR_MAX/2;
  407. }
  408. unsigned int CarlaEngine::getMaxPortNameSize() const noexcept
  409. {
  410. return STR_MAX;
  411. }
  412. unsigned int CarlaEngine::getCurrentPluginCount() const noexcept
  413. {
  414. return pData->curPluginCount;
  415. }
  416. unsigned int CarlaEngine::getMaxPluginNumber() const noexcept
  417. {
  418. return pData->maxPluginNumber;
  419. }
  420. // -----------------------------------------------------------------------
  421. // Virtual, per-engine type calls
  422. bool CarlaEngine::init(const char* const clientName)
  423. {
  424. CARLA_SAFE_ASSERT_RETURN_ERR(pData->name.isEmpty(), "Invalid engine internal data (err #1)");
  425. CARLA_SAFE_ASSERT_RETURN_ERR(pData->oscData == nullptr, "Invalid engine internal data (err #2)");
  426. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins == nullptr, "Invalid engine internal data (err #3)");
  427. CARLA_SAFE_ASSERT_RETURN_ERR(pData->bufEvents.in == nullptr, "Invalid engine internal data (err #4)");
  428. CARLA_SAFE_ASSERT_RETURN_ERR(pData->bufEvents.out == nullptr, "Invalid engine internal data (err #5)");
  429. CARLA_SAFE_ASSERT_RETURN_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  430. carla_debug("CarlaEngine::init(\"%s\")", clientName);
  431. pData->aboutToClose = false;
  432. pData->curPluginCount = 0;
  433. pData->maxPluginNumber = 0;
  434. pData->nextPluginId = 0;
  435. switch (pData->options.processMode)
  436. {
  437. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  438. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  439. pData->maxPluginNumber = MAX_DEFAULT_PLUGINS;
  440. break;
  441. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  442. pData->maxPluginNumber = MAX_RACK_PLUGINS;
  443. pData->bufEvents.in = new EngineEvent[kEngineMaxInternalEventCount];
  444. pData->bufEvents.out = new EngineEvent[kEngineMaxInternalEventCount];
  445. break;
  446. case ENGINE_PROCESS_MODE_PATCHBAY:
  447. pData->maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  448. break;
  449. case ENGINE_PROCESS_MODE_BRIDGE:
  450. pData->maxPluginNumber = 1;
  451. pData->bufEvents.in = new EngineEvent[kEngineMaxInternalEventCount];
  452. pData->bufEvents.out = new EngineEvent[kEngineMaxInternalEventCount];
  453. break;
  454. }
  455. CARLA_SAFE_ASSERT_RETURN_ERR(pData->maxPluginNumber != 0, "Invalid engine process mode");
  456. pData->nextPluginId = pData->maxPluginNumber;
  457. pData->name = clientName;
  458. pData->name.toBasic();
  459. pData->timeInfo.clear();
  460. pData->plugins = new EnginePluginData[pData->maxPluginNumber];
  461. pData->osc.init(clientName);
  462. #ifndef BUILD_BRIDGE
  463. pData->oscData = pData->osc.getControlData();
  464. #endif
  465. pData->nextAction.ready();
  466. pData->thread.start();
  467. callback(ENGINE_CALLBACK_ENGINE_STARTED, 0, 0, 0, 0.0f, getCurrentDriverName());
  468. return true;
  469. }
  470. bool CarlaEngine::close()
  471. {
  472. CARLA_SAFE_ASSERT_RETURN_ERR(pData->name.isNotEmpty(), "Invalid engine internal data (err #6)");
  473. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #7)");
  474. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data (err #8)");
  475. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #9)");
  476. carla_debug("CarlaEngine::close()");
  477. pData->thread.stop(500);
  478. pData->nextAction.ready();
  479. #ifndef BUILD_BRIDGE
  480. oscSend_control_exit();
  481. #endif
  482. pData->osc.close();
  483. pData->oscData = nullptr;
  484. pData->aboutToClose = true;
  485. pData->curPluginCount = 0;
  486. pData->maxPluginNumber = 0;
  487. pData->nextPluginId = 0;
  488. if (pData->plugins != nullptr)
  489. {
  490. delete[] pData->plugins;
  491. pData->plugins = nullptr;
  492. }
  493. if (pData->bufEvents.in != nullptr)
  494. {
  495. delete[] pData->bufEvents.in;
  496. pData->bufEvents.in = nullptr;
  497. }
  498. if (pData->bufEvents.out != nullptr)
  499. {
  500. delete[] pData->bufEvents.out;
  501. pData->bufEvents.out = nullptr;
  502. }
  503. pData->name.clear();
  504. callback(ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  505. return true;
  506. }
  507. void CarlaEngine::idle()
  508. {
  509. CARLA_ASSERT(pData->nextAction.opcode == kEnginePostActionNull); // TESTING, remove later
  510. CARLA_ASSERT(pData->nextPluginId == pData->maxPluginNumber); // TESTING, remove later
  511. CARLA_ASSERT(pData->plugins != nullptr); // this one too maybe
  512. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  513. {
  514. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  515. if (plugin != nullptr && plugin->isEnabled())
  516. plugin->idle();
  517. }
  518. }
  519. CarlaEngineClient* CarlaEngine::addClient(CarlaPlugin* const)
  520. {
  521. return new CarlaEngineClient(*this);
  522. }
  523. // -----------------------------------------------------------------------
  524. // Plugin management
  525. bool CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, const void* const extra)
  526. {
  527. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #10)");
  528. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId <= pData->maxPluginNumber, "Invalid engine internal data (err #11)");
  529. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #12)");
  530. CARLA_SAFE_ASSERT_RETURN_ERR(btype != BINARY_NONE, "Invalid plugin params (err #1)");
  531. CARLA_SAFE_ASSERT_RETURN_ERR(ptype != PLUGIN_NONE, "Invalid plugin params (err #2)");
  532. CARLA_SAFE_ASSERT_RETURN_ERR((filename != nullptr && filename[0] != '\0') || (label != nullptr && label[0] != '\0'), "Invalid plugin params (err #3)");
  533. carla_debug("CarlaEngine::addPlugin(%i:%s, %i:%s, \"%s\", \"%s\", \"%s\", %p)", btype, BinaryType2Str(btype), ptype, PluginType2Str(ptype), filename, name, label, extra);
  534. unsigned int id;
  535. CarlaPlugin* oldPlugin = nullptr;
  536. if (pData->nextPluginId < pData->curPluginCount)
  537. {
  538. id = pData->nextPluginId;
  539. pData->nextPluginId = pData->maxPluginNumber;
  540. oldPlugin = pData->plugins[id].plugin;
  541. CARLA_SAFE_ASSERT_RETURN_ERR(oldPlugin != nullptr, "Invalid replace plugin Id");
  542. }
  543. else
  544. {
  545. id = pData->curPluginCount;
  546. if (id == pData->maxPluginNumber)
  547. {
  548. setLastError("Maximum number of plugins reached");
  549. return false;
  550. }
  551. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins[id].plugin == nullptr, "Invalid engine internal data (err #13)");
  552. }
  553. CarlaPlugin::Initializer init = {
  554. this,
  555. id,
  556. filename,
  557. name,
  558. label
  559. };
  560. CarlaPlugin* plugin = nullptr;
  561. #if 0 //ndef BUILD_BRIDGE
  562. const char* bridgeBinary;
  563. switch (btype)
  564. {
  565. case BINARY_POSIX32:
  566. bridgeBinary = pData->options.bridge_posix32.isNotEmpty() ? (const char*)pData->options.bridge_posix32 : nullptr;
  567. break;
  568. case BINARY_POSIX64:
  569. bridgeBinary = pData->options.bridge_posix64.isNotEmpty() ? (const char*)pData->options.bridge_posix64 : nullptr;
  570. break;
  571. case BINARY_WIN32:
  572. bridgeBinary = pData->options.bridge_win32.isNotEmpty() ? (const char*)pData->options.bridge_win32 : nullptr;
  573. break;
  574. case BINARY_WIN64:
  575. bridgeBinary = pData->options.bridge_win64.isNotEmpty() ? (const char*)pData->options.bridge_win64 : nullptr;
  576. break;
  577. default:
  578. bridgeBinary = nullptr;
  579. break;
  580. }
  581. # ifndef CARLA_OS_WIN
  582. if (btype == BINARY_NATIVE && pData->options.bridge_native.isNotEmpty())
  583. bridgeBinary = (const char*)pData->options.bridge_native;
  584. # endif
  585. if (btype != BINARY_NATIVE || (pData->options.preferPluginBridges && bridgeBinary != nullptr))
  586. {
  587. if (bridgeBinary != nullptr)
  588. {
  589. plugin = CarlaPlugin::newBridge(init, btype, ptype, bridgeBinary);
  590. }
  591. # ifdef CARLA_OS_LINUX
  592. else if (btype == BINARY_WIN32)
  593. {
  594. // fallback to dssi-vst
  595. QFileInfo fileInfo(filename);
  596. CarlaString label2(fileInfo.fileName().toUtf8().constData());
  597. label2.replace(' ', '*');
  598. CarlaPlugin::Initializer init2 = {
  599. this,
  600. id,
  601. "/usr/lib/dssi/dssi-vst.so",
  602. name,
  603. (const char*)label2
  604. };
  605. char* const oldVstPath(getenv("VST_PATH"));
  606. carla_setenv("VST_PATH", fileInfo.absoluteDir().absolutePath().toUtf8().constData());
  607. plugin = CarlaPlugin::newDSSI(init2);
  608. if (oldVstPath != nullptr)
  609. carla_setenv("VST_PATH", oldVstPath);
  610. }
  611. # endif
  612. else
  613. {
  614. setLastError("This Carla build cannot handle this binary");
  615. return false;
  616. }
  617. }
  618. else
  619. #endif // BUILD_BRIDGE
  620. {
  621. setLastError("Invalid or unsupported plugin type");
  622. switch (ptype)
  623. {
  624. case PLUGIN_NONE:
  625. break;
  626. case PLUGIN_INTERNAL:
  627. plugin = CarlaPlugin::newNative(init);
  628. break;
  629. case PLUGIN_LADSPA:
  630. plugin = CarlaPlugin::newLADSPA(init, (const LADSPA_RDF_Descriptor*)extra);
  631. break;
  632. case PLUGIN_DSSI:
  633. plugin = CarlaPlugin::newDSSI(init);
  634. break;
  635. case PLUGIN_LV2:
  636. plugin = CarlaPlugin::newLV2(init);
  637. break;
  638. case PLUGIN_VST:
  639. plugin = CarlaPlugin::newVST(init);
  640. break;
  641. case PLUGIN_AU:
  642. //plugin = CarlaPlugin::newAU(init);
  643. break;
  644. case PLUGIN_CSOUND:
  645. //plugin = CarlaPlugin::newCSOUND(init);
  646. break;
  647. case PLUGIN_GIG:
  648. plugin = CarlaPlugin::newGIG(init, (extra != nullptr));
  649. break;
  650. case PLUGIN_SF2:
  651. plugin = CarlaPlugin::newSF2(init, (extra != nullptr));
  652. break;
  653. case PLUGIN_SFZ:
  654. plugin = CarlaPlugin::newSFZ(init, (extra != nullptr));
  655. break;
  656. }
  657. }
  658. if (plugin == nullptr)
  659. return false;
  660. plugin->registerToOscClient();
  661. EnginePluginData& pluginData(pData->plugins[id]);
  662. pluginData.plugin = plugin;
  663. pluginData.insPeak[0] = 0.0f;
  664. pluginData.insPeak[1] = 0.0f;
  665. pluginData.outsPeak[0] = 0.0f;
  666. pluginData.outsPeak[1] = 0.0f;
  667. if (oldPlugin != nullptr)
  668. {
  669. delete oldPlugin;
  670. callback(ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0.0f, plugin->getName());
  671. }
  672. else
  673. {
  674. ++pData->curPluginCount;
  675. callback(ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0.0f, plugin->getName());
  676. }
  677. return true;
  678. }
  679. bool CarlaEngine::removePlugin(const unsigned int id)
  680. {
  681. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #14)");
  682. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data (err #15)");
  683. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #16)");
  684. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id (err #1)");
  685. carla_debug("CarlaEngine::removePlugin(%i)", id);
  686. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  687. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  688. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data (err #17)");
  689. pData->thread.stop(500);
  690. const bool lockWait(isRunning() && pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS);
  691. const CarlaEngineProtectedData::ScopedActionLock sal(pData, kEnginePostActionRemovePlugin, id, 0, lockWait);
  692. #ifndef BUILD_BRIDGE
  693. if (isOscControlRegistered())
  694. oscSend_control_remove_plugin(id);
  695. #endif
  696. delete plugin;
  697. if (isRunning() && ! pData->aboutToClose)
  698. pData->thread.start();
  699. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  700. return true;
  701. }
  702. bool CarlaEngine::removeAllPlugins()
  703. {
  704. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #18)");
  705. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data (err #19)");
  706. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #20)");
  707. carla_debug("CarlaEngine::removeAllPlugins()");
  708. if (pData->curPluginCount == 0)
  709. return true;
  710. pData->thread.stop(500);
  711. const bool lockWait(isRunning());
  712. const CarlaEngineProtectedData::ScopedActionLock sal(pData, kEnginePostActionZeroCount, 0, 0, lockWait);
  713. for (unsigned int i=0; i < pData->maxPluginNumber; ++i)
  714. {
  715. EnginePluginData& pluginData(pData->plugins[i]);
  716. if (pluginData.plugin != nullptr)
  717. {
  718. delete pluginData.plugin;
  719. pluginData.plugin = nullptr;
  720. }
  721. pluginData.insPeak[0] = 0.0f;
  722. pluginData.insPeak[1] = 0.0f;
  723. pluginData.outsPeak[0] = 0.0f;
  724. pluginData.outsPeak[1] = 0.0f;
  725. }
  726. if (isRunning() && ! pData->aboutToClose)
  727. pData->thread.start();
  728. return true;
  729. }
  730. const char* CarlaEngine::renamePlugin(const unsigned int id, const char* const newName)
  731. {
  732. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data (err #21)");
  733. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data (err #22)");
  734. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #23)");
  735. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id (err #2)");
  736. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  737. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  738. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  739. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  740. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data (err #24)");
  741. if (const char* const name = getUniquePluginName(newName))
  742. {
  743. plugin->setName(name);
  744. return name;
  745. }
  746. setLastError("Unable to get new unique plugin name");
  747. return nullptr;
  748. }
  749. bool CarlaEngine::clonePlugin(const unsigned int id)
  750. {
  751. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #25)");
  752. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data (err #26)");
  753. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #27)");
  754. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id (err #3)");
  755. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  756. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  757. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  758. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data (err #28)");
  759. char label[STR_MAX+1];
  760. carla_zeroChar(label, STR_MAX+1);
  761. plugin->getLabel(label);
  762. const unsigned int pluginCountBefore(pData->curPluginCount);
  763. if (! addPlugin(plugin->getBinaryType(), plugin->getType(), plugin->getFilename(), plugin->getName(), label, plugin->getExtraStuff()))
  764. return false;
  765. CARLA_ASSERT(pluginCountBefore+1 == pData->curPluginCount);
  766. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  767. newPlugin->loadSaveState(plugin->getSaveState());
  768. return true;
  769. }
  770. bool CarlaEngine::replacePlugin(const unsigned int id)
  771. {
  772. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #29)");
  773. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data (err #30)");
  774. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #31)");
  775. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id (err #4)");
  776. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  777. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  778. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  779. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data (err #32)");
  780. pData->nextPluginId = id;
  781. return true;
  782. }
  783. bool CarlaEngine::switchPlugins(const unsigned int idA, const unsigned int idB)
  784. {
  785. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data (err #33)");
  786. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data (err #34)");
  787. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #35)");
  788. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  789. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id (err #5)");
  790. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id (err #6)");
  791. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  792. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  793. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  794. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch (err #1)");
  795. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch (err #2)");
  796. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data (err #36)");
  797. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data (err #37)");
  798. pData->thread.stop(500);
  799. const bool lockWait(isRunning() && pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS);
  800. const CarlaEngineProtectedData::ScopedActionLock sal(pData, kEnginePostActionSwitchPlugins, idA, idB, lockWait);
  801. #ifndef BUILD_BRIDGE // TODO
  802. //if (isOscControlRegistered())
  803. // oscSend_control_switch_plugins(idA, idB);
  804. #endif
  805. if (isRunning() && ! pData->aboutToClose)
  806. pData->thread.start();
  807. return true;
  808. }
  809. CarlaPlugin* CarlaEngine::getPlugin(const unsigned int id)
  810. {
  811. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data (err #38)");
  812. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data (err #39)");
  813. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data (err #40)");
  814. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id (err #7)");
  815. carla_debug("CarlaEngine::getPlugin(%i) [count:%i]", id, pData->curPluginCount);
  816. return pData->plugins[id].plugin;
  817. }
  818. CarlaPlugin* CarlaEngine::getPluginUnchecked(const unsigned int id) const noexcept
  819. {
  820. return pData->plugins[id].plugin;
  821. }
  822. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  823. {
  824. CARLA_SAFE_ASSERT_RETURN(pData->plugins != nullptr, nullptr);
  825. CARLA_SAFE_ASSERT_RETURN(pData->maxPluginNumber != 0, nullptr);
  826. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  827. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  828. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  829. static CarlaMutex m;
  830. const CarlaMutex::ScopedLocker sl(m);
  831. static CarlaString sname;
  832. sname = name;
  833. if (sname.isEmpty())
  834. {
  835. sname = "(No name)";
  836. return (const char*)sname;
  837. }
  838. sname.truncate(getMaxClientNameSize()-5-1); // 5 = strlen(" (10)")
  839. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  840. for (unsigned short i=0; i < pData->curPluginCount; ++i)
  841. {
  842. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  843. // Check if unique name doesn't exist
  844. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  845. {
  846. if (sname != pluginName)
  847. continue;
  848. }
  849. // Check if string has already been modified
  850. {
  851. const size_t len(sname.length());
  852. // 1 digit, ex: " (2)"
  853. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  854. {
  855. int number = sname[len-2] - '0';
  856. if (number == 9)
  857. {
  858. // next number is 10, 2 digits
  859. sname.truncate(len-4);
  860. sname += " (10)";
  861. //sname.replace(" (9)", " (10)");
  862. }
  863. else
  864. sname[len-2] = char('0' + number + 1);
  865. continue;
  866. }
  867. // 2 digits, ex: " (11)"
  868. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  869. {
  870. char n2 = sname[len-2];
  871. char n3 = sname[len-3];
  872. if (n2 == '9')
  873. {
  874. n2 = '0';
  875. n3 += 1;
  876. }
  877. else
  878. n2 += 1;
  879. sname[len-2] = n2;
  880. sname[len-3] = n3;
  881. continue;
  882. }
  883. }
  884. // Modify string if not
  885. sname += " (2)";
  886. }
  887. return (const char*)sname;
  888. }
  889. // -----------------------------------------------------------------------
  890. // Project management
  891. bool CarlaEngine::loadFile(const char* const filename)
  892. {
  893. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename (err #1)");
  894. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  895. QFileInfo fileInfo(filename);
  896. if (! fileInfo.exists())
  897. {
  898. setLastError("File does not exist");
  899. return false;
  900. }
  901. if (! fileInfo.isFile())
  902. {
  903. setLastError("Not a file");
  904. return false;
  905. }
  906. if (! fileInfo.isReadable())
  907. {
  908. setLastError("File is not readable");
  909. return false;
  910. }
  911. CarlaString baseName(fileInfo.baseName().toUtf8().constData());
  912. CarlaString extension(fileInfo.suffix().toLower().toUtf8().constData());
  913. extension.toLower();
  914. // -------------------------------------------------------------------
  915. if (extension == "carxp" || extension == "carxs")
  916. return loadProject(filename);
  917. // -------------------------------------------------------------------
  918. if (extension == "csd")
  919. return addPlugin(PLUGIN_CSOUND, filename, baseName, baseName);
  920. if (extension == "gig")
  921. return addPlugin(PLUGIN_GIG, filename, baseName, baseName);
  922. if (extension == "sf2")
  923. return addPlugin(PLUGIN_SF2, filename, baseName, baseName);
  924. if (extension == "sfz")
  925. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName);
  926. // -------------------------------------------------------------------
  927. if (extension == "aiff" || extension == "flac" || extension == "oga" || extension == "ogg" || extension == "w64" || extension == "wav")
  928. {
  929. #ifdef WANT_AUDIOFILE
  930. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile"))
  931. {
  932. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  933. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  934. return true;
  935. }
  936. return false;
  937. #else
  938. setLastError("This Carla build does not have Audio file support");
  939. return false;
  940. #endif
  941. }
  942. if (extension == "3g2" || extension == "3gp" || extension == "aac" || extension == "ac3" || extension == "amr" || extension == "ape" ||
  943. extension == "mp2" || extension == "mp3" || extension == "mpc" || extension == "wma")
  944. {
  945. #ifdef WANT_AUDIOFILE
  946. # ifdef HAVE_FFMPEG
  947. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile"))
  948. {
  949. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  950. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  951. return true;
  952. }
  953. return false;
  954. # else
  955. setLastError("This Carla build has Audio file support, but not libav/ffmpeg");
  956. return false;
  957. # endif
  958. #else
  959. setLastError("This Carla build does not have Audio file support");
  960. return false;
  961. #endif
  962. }
  963. // -------------------------------------------------------------------
  964. if (extension == "mid" || extension == "midi")
  965. {
  966. #ifdef WANT_MIDIFILE
  967. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile"))
  968. {
  969. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  970. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  971. return true;
  972. }
  973. return false;
  974. #else
  975. setLastError("This Carla build does not have MIDI file support");
  976. return false;
  977. #endif
  978. }
  979. // -------------------------------------------------------------------
  980. // ZynAddSubFX
  981. if (extension == "xmz" || extension == "xiz")
  982. {
  983. #ifdef WANT_ZYNADDSUBFX
  984. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "zynaddsubfx"))
  985. {
  986. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  987. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  988. return true;
  989. }
  990. return false;
  991. #else
  992. setLastError("This Carla build does not have ZynAddSubFX support");
  993. return false;
  994. #endif
  995. }
  996. // -------------------------------------------------------------------
  997. setLastError("Unknown file extension");
  998. return false;
  999. }
  1000. bool CarlaEngine::loadProject(const char* const filename)
  1001. {
  1002. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename (err #2)");
  1003. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  1004. QFile file(filename);
  1005. if (! file.open(QIODevice::ReadOnly | QIODevice::Text))
  1006. return false;
  1007. QDomDocument xml;
  1008. xml.setContent(file.readAll());
  1009. file.close();
  1010. QDomNode xmlNode(xml.documentElement());
  1011. const bool isPreset(xmlNode.toElement().tagName().compare("carla-preset", Qt::CaseInsensitive) == 0);
  1012. if (xmlNode.toElement().tagName().compare("carla-project", Qt::CaseInsensitive) != 0 && ! isPreset)
  1013. {
  1014. setLastError("Not a valid Carla project or preset file");
  1015. return false;
  1016. }
  1017. for (QDomNode node = xmlNode.firstChild(); ! node.isNull(); node = node.nextSibling())
  1018. {
  1019. if (isPreset || node.toElement().tagName().compare("plugin", Qt::CaseInsensitive) == 0)
  1020. {
  1021. SaveState saveState;
  1022. fillSaveStateFromXmlNode(saveState, isPreset ? xmlNode : node);
  1023. CARLA_SAFE_ASSERT_CONTINUE(saveState.type != nullptr);
  1024. const void* extraStuff = nullptr;
  1025. // check if using GIG, SF2 or SFZ 16outs
  1026. static const char kUse16OutsSuffix[] = " (16 outs)";
  1027. if (CarlaString(saveState.label).endsWith(kUse16OutsSuffix))
  1028. {
  1029. if (std::strcmp(saveState.type, "GIG") == 0 || std::strcmp(saveState.type, "SF2") == 0 || std::strcmp(saveState.type, "SFZ") == 0)
  1030. extraStuff = (void*)0x1; // non-null
  1031. }
  1032. // TODO - proper find&load plugins
  1033. if (addPlugin(getPluginTypeFromString(saveState.type), saveState.binary, saveState.name, saveState.label, extraStuff))
  1034. {
  1035. if (CarlaPlugin* plugin = getPlugin(pData->curPluginCount-1))
  1036. plugin->loadSaveState(saveState);
  1037. }
  1038. }
  1039. if (isPreset)
  1040. break;
  1041. }
  1042. return true;
  1043. }
  1044. bool CarlaEngine::saveProject(const char* const filename)
  1045. {
  1046. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename (err #3)");
  1047. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  1048. QFile file(filename);
  1049. if (! file.open(QIODevice::WriteOnly | QIODevice::Text))
  1050. return false;
  1051. QTextStream out(&file);
  1052. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1053. out << "<!DOCTYPE CARLA-PROJECT>\n";
  1054. out << "<CARLA-PROJECT VERSION='2.0'>\n";
  1055. bool firstPlugin = true;
  1056. char strBuf[STR_MAX+1];
  1057. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1058. {
  1059. CarlaPlugin* const plugin = pData->plugins[i].plugin;
  1060. if (plugin != nullptr && plugin->isEnabled())
  1061. {
  1062. if (! firstPlugin)
  1063. out << "\n";
  1064. strBuf[0] = '\0';
  1065. plugin->getRealName(strBuf);
  1066. if (strBuf[0] != '\0')
  1067. out << QString(" <!-- %1 -->\n").arg(xmlSafeString(strBuf, true));
  1068. QString content;
  1069. fillXmlStringFromSaveState(content, plugin->getSaveState());
  1070. out << " <Plugin>\n";
  1071. out << content;
  1072. out << " </Plugin>\n";
  1073. firstPlugin = false;
  1074. }
  1075. }
  1076. out << "</CARLA-PROJECT>\n";
  1077. file.close();
  1078. return true;
  1079. }
  1080. // -----------------------------------------------------------------------
  1081. // Information (base)
  1082. /*!
  1083. * Get the current engine driver hints.
  1084. */
  1085. unsigned int CarlaEngine::getHints() const noexcept
  1086. {
  1087. return pData->hints;
  1088. }
  1089. /*!
  1090. * Get the current buffer size.
  1091. */
  1092. uint32_t CarlaEngine::getBufferSize() const noexcept
  1093. {
  1094. return pData->bufferSize;
  1095. }
  1096. /*!
  1097. * Get the current sample rate.
  1098. */
  1099. double CarlaEngine::getSampleRate() const noexcept
  1100. {
  1101. return pData->sampleRate;
  1102. }
  1103. /*!
  1104. * Get the current engine name.
  1105. */
  1106. const char* CarlaEngine::getName() const noexcept
  1107. {
  1108. return (const char*)pData->name;
  1109. }
  1110. /*!
  1111. * Get the current engine proccess mode.
  1112. */
  1113. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  1114. {
  1115. return pData->options.processMode;
  1116. }
  1117. /*!
  1118. * Get the current engine options (read-only).
  1119. */
  1120. const EngineOptions& CarlaEngine::getOptions() const noexcept
  1121. {
  1122. return pData->options;
  1123. }
  1124. /*!
  1125. * Get the current Time information (read-only).
  1126. */
  1127. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  1128. {
  1129. return pData->timeInfo;
  1130. }
  1131. // -----------------------------------------------------------------------
  1132. // Information (peaks)
  1133. // FIXME
  1134. float CarlaEngine::getInputPeak(const unsigned int pluginId, const unsigned short id) const
  1135. {
  1136. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  1137. CARLA_SAFE_ASSERT_RETURN(id == 1 || id == 2, 0.0f);
  1138. return pData->plugins[pluginId].insPeak[id-1];
  1139. }
  1140. float CarlaEngine::getOutputPeak(const unsigned int pluginId, const unsigned short id) const
  1141. {
  1142. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  1143. CARLA_SAFE_ASSERT_RETURN(id == 1 || id == 2, 0.0f);
  1144. return pData->plugins[pluginId].outsPeak[id-1];
  1145. }
  1146. // -----------------------------------------------------------------------
  1147. // Callback
  1148. void CarlaEngine::callback(const EngineCallbackOpcode action, const unsigned int pluginId, const int value1, const int value2, const float value3, const char* const valueStr)
  1149. {
  1150. carla_debug("CarlaEngine::callback(%s, %i, %i, %i, %f, \"%s\")", EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  1151. if (pData->callback != nullptr)
  1152. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  1153. }
  1154. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr)
  1155. {
  1156. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  1157. pData->callback = func;
  1158. pData->callbackPtr = ptr;
  1159. }
  1160. // -----------------------------------------------------------------------
  1161. // Patchbay
  1162. bool CarlaEngine::patchbayConnect(int, int)
  1163. {
  1164. setLastError("Unsupported operation");
  1165. return false;
  1166. }
  1167. bool CarlaEngine::patchbayDisconnect(int)
  1168. {
  1169. setLastError("Unsupported operation");
  1170. return false;
  1171. }
  1172. bool CarlaEngine::patchbayRefresh()
  1173. {
  1174. setLastError("Unsupported operation");
  1175. return false;
  1176. }
  1177. // -----------------------------------------------------------------------
  1178. // Transport
  1179. void CarlaEngine::transportPlay()
  1180. {
  1181. pData->time.playing = true;
  1182. }
  1183. void CarlaEngine::transportPause()
  1184. {
  1185. pData->time.playing = false;
  1186. }
  1187. void CarlaEngine::transportRelocate(const uint32_t frame)
  1188. {
  1189. pData->time.frame = frame;
  1190. }
  1191. // -----------------------------------------------------------------------
  1192. // Error handling
  1193. const char* CarlaEngine::getLastError() const noexcept
  1194. {
  1195. return (const char*)pData->lastError;
  1196. }
  1197. void CarlaEngine::setLastError(const char* const error)
  1198. {
  1199. pData->lastError = error;
  1200. }
  1201. void CarlaEngine::setAboutToClose()
  1202. {
  1203. carla_debug("CarlaEngine::setAboutToClose()");
  1204. pData->aboutToClose = true;
  1205. }
  1206. // -----------------------------------------------------------------------
  1207. // Global options
  1208. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr)
  1209. {
  1210. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  1211. #ifndef BUILD_BRIDGE
  1212. if (option >= ENGINE_OPTION_PROCESS_MODE && option < ENGINE_OPTION_PATH_RESOURCES && isRunning())
  1213. return carla_stderr("CarlaEngine::setOption(%s, %i, \"%s\") - Cannot set this option while engine is running!", EngineOption2Str(option), value, valueStr);
  1214. #endif
  1215. switch (option)
  1216. {
  1217. case ENGINE_OPTION_DEBUG:
  1218. break;
  1219. case ENGINE_OPTION_PROCESS_MODE:
  1220. if (value < ENGINE_PROCESS_MODE_SINGLE_CLIENT || value > ENGINE_PROCESS_MODE_PATCHBAY)
  1221. return carla_stderr("CarlaEngine::setOption(ENGINE_OPTION_PROCESS_MODE, %i, \"%s\") - invalid value", value, valueStr);
  1222. pData->options.processMode = static_cast<EngineProcessMode>(value);
  1223. break;
  1224. case ENGINE_OPTION_TRANSPORT_MODE:
  1225. if (value < ENGINE_TRANSPORT_MODE_INTERNAL || value > ENGINE_TRANSPORT_MODE_JACK)
  1226. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_TRANSPORT_MODE, %i, \"%s\") - invalid value", value, valueStr);
  1227. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  1228. break;
  1229. case ENGINE_OPTION_FORCE_STEREO:
  1230. pData->options.forceStereo = (value != 0);
  1231. break;
  1232. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  1233. pData->options.preferPluginBridges = (value != 0);
  1234. break;
  1235. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1236. pData->options.preferUiBridges = (value != 0);
  1237. break;
  1238. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1239. pData->options.uisAlwaysOnTop = (value != 0);
  1240. break;
  1241. case ENGINE_OPTION_MAX_PARAMETERS:
  1242. if (value < 1)
  1243. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_MAX_PARAMETERS, %i, \"%s\") - invalid value", value, valueStr);
  1244. pData->options.maxParameters = static_cast<uint>(value);
  1245. break;
  1246. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1247. if (value < 1)
  1248. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_UI_BRIDGES_TIMEOUT, %i, \"%s\") - invalid value", value, valueStr);
  1249. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1250. break;
  1251. case ENGINE_OPTION_AUDIO_NUM_PERIODS:
  1252. if (value < 2 || value > 3)
  1253. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_AUDIO_NUM_PERIODS, %i, \"%s\") - invalid value", value, valueStr);
  1254. pData->options.audioNumPeriods = static_cast<uint>(value);
  1255. break;
  1256. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1257. if (value < 8)
  1258. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_AUDIO_BUFFER_SIZE, %i, \"%s\") - invalid value", value, valueStr);
  1259. pData->options.audioBufferSize = static_cast<uint>(value);
  1260. break;
  1261. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1262. if (value < 22050)
  1263. return carla_stderr2("carla_set_engine_option(ENGINE_OPTION_AUDIO_SAMPLE_RATE, %i, \"%s\") - invalid value", value, valueStr);
  1264. pData->options.audioSampleRate = static_cast<uint>(value);
  1265. break;
  1266. case ENGINE_OPTION_AUDIO_DEVICE:
  1267. pData->options.audioDevice = valueStr;
  1268. break;
  1269. case ENGINE_OPTION_PATH_BINARIES:
  1270. pData->options.binaryDir = valueStr;
  1271. break;
  1272. case ENGINE_OPTION_PATH_RESOURCES:
  1273. pData->options.resourceDir = valueStr;
  1274. break;
  1275. }
  1276. }
  1277. // -----------------------------------------------------------------------
  1278. // OSC Stuff
  1279. #ifdef BUILD_BRIDGE
  1280. bool CarlaEngine::isOscBridgeRegistered() const noexcept
  1281. {
  1282. return (pData->oscData != nullptr);
  1283. }
  1284. #else
  1285. bool CarlaEngine::isOscControlRegistered() const noexcept
  1286. {
  1287. return pData->osc.isControlRegistered();
  1288. }
  1289. #endif
  1290. void CarlaEngine::idleOsc()
  1291. {
  1292. pData->osc.idle();
  1293. }
  1294. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1295. {
  1296. return pData->osc.getServerPathTCP();
  1297. }
  1298. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1299. {
  1300. return pData->osc.getServerPathUDP();
  1301. }
  1302. #ifdef BUILD_BRIDGE
  1303. void CarlaEngine::setOscBridgeData(const CarlaOscData* const oscData) noexcept
  1304. {
  1305. pData->oscData = oscData;
  1306. }
  1307. #endif
  1308. // -----------------------------------------------------------------------
  1309. // Helper functions
  1310. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1311. {
  1312. return isInput ? pData->bufEvents.in : pData->bufEvents.out;
  1313. }
  1314. void CarlaEngine::registerEnginePlugin(const unsigned int id, CarlaPlugin* const plugin)
  1315. {
  1316. CARLA_SAFE_ASSERT_RETURN(id == pData->curPluginCount,);
  1317. carla_debug("CarlaEngine::registerEnginePlugin(%i, %p)", id, plugin);
  1318. pData->plugins[id].plugin = plugin;
  1319. }
  1320. // -----------------------------------------------------------------------
  1321. // Internal stuff
  1322. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1323. {
  1324. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1325. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1326. {
  1327. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1328. if (plugin != nullptr && plugin->isEnabled())
  1329. plugin->bufferSizeChanged(newBufferSize);
  1330. }
  1331. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, newBufferSize, 0, 0.0f, nullptr);
  1332. }
  1333. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1334. {
  1335. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1336. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1337. {
  1338. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1339. if (plugin != nullptr && plugin->isEnabled())
  1340. plugin->sampleRateChanged(newSampleRate);
  1341. }
  1342. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, newSampleRate, nullptr);
  1343. }
  1344. void CarlaEngine::offlineModeChanged(const bool isOffline)
  1345. {
  1346. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOffline));
  1347. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1348. {
  1349. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1350. if (plugin != nullptr && plugin->isEnabled())
  1351. plugin->offlineModeChanged(isOffline);
  1352. }
  1353. }
  1354. void CarlaEngine::runPendingRtEvents()
  1355. {
  1356. pData->doNextPluginAction(true);
  1357. if (pData->time.playing)
  1358. pData->time.frame += pData->bufferSize;
  1359. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  1360. {
  1361. pData->timeInfo.playing = pData->time.playing;
  1362. pData->timeInfo.frame = pData->time.frame;
  1363. }
  1364. }
  1365. void CarlaEngine::setPluginPeaks(const unsigned int pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1366. {
  1367. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1368. pluginData.insPeak[0] = inPeaks[0];
  1369. pluginData.insPeak[1] = inPeaks[1];
  1370. pluginData.outsPeak[0] = outPeaks[0];
  1371. pluginData.outsPeak[1] = outPeaks[1];
  1372. }
  1373. #ifndef BUILD_BRIDGE
  1374. void CarlaEngine::processRack(float* inBufReal[2], float* outBuf[2], const uint32_t frames)
  1375. {
  1376. CARLA_SAFE_ASSERT_RETURN(pData->bufEvents.in != nullptr,);
  1377. CARLA_SAFE_ASSERT_RETURN(pData->bufEvents.out != nullptr,);
  1378. // safe copy
  1379. float inBuf0[frames];
  1380. float inBuf1[frames];
  1381. float* inBuf[2] = { inBuf0, inBuf1 };
  1382. #ifdef HAVE_JUCE
  1383. // initialize audio inputs
  1384. FloatVectorOperations::copy(inBuf0, inBufReal[0], frames);
  1385. FloatVectorOperations::copy(inBuf1, inBufReal[1], frames);
  1386. // initialize audio outputs (zero)
  1387. FloatVectorOperations::clear(outBuf[0], frames);
  1388. FloatVectorOperations::clear(outBuf[1], frames);
  1389. #else
  1390. // initialize audio inputs
  1391. carla_copyFloat(inBuf0, inBufReal[0], frames);
  1392. carla_copyFloat(inBuf1, inBufReal[1], frames);
  1393. // initialize audio outputs (zero)
  1394. carla_zeroFloat(outBuf[0], frames);
  1395. carla_zeroFloat(outBuf[1], frames);
  1396. #endif
  1397. // initialize event outputs (zero)
  1398. carla_zeroStruct<EngineEvent>(pData->bufEvents.out, kEngineMaxInternalEventCount);
  1399. bool processed = false;
  1400. uint32_t oldAudioInCount = 0;
  1401. uint32_t oldMidiOutCount = 0;
  1402. // process plugins
  1403. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1404. {
  1405. CarlaPlugin* const plugin = pData->plugins[i].plugin;
  1406. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock())
  1407. continue;
  1408. if (processed)
  1409. {
  1410. #ifdef HAVE_JUCE
  1411. // initialize audio inputs (from previous outputs)
  1412. FloatVectorOperations::copy(inBuf0, outBuf[0], frames);
  1413. FloatVectorOperations::copy(inBuf1, outBuf[1], frames);
  1414. // initialize audio outputs (zero)
  1415. FloatVectorOperations::clear(outBuf[0], frames);
  1416. FloatVectorOperations::clear(outBuf[1], frames);
  1417. #else
  1418. // initialize audio inputs (from previous outputs)
  1419. carla_copyFloat(inBuf0, outBuf[0], frames);
  1420. carla_copyFloat(inBuf1, outBuf[1], frames);
  1421. // initialize audio outputs (zero)
  1422. carla_zeroFloat(outBuf[0], frames);
  1423. carla_zeroFloat(outBuf[1], frames);
  1424. #endif
  1425. // if plugin has no midi out, add previous events
  1426. if (oldMidiOutCount == 0 && pData->bufEvents.in[0].type != kEngineEventTypeNull)
  1427. {
  1428. if (pData->bufEvents.out[0].type != kEngineEventTypeNull)
  1429. {
  1430. // TODO: carefully add to input, sorted events
  1431. }
  1432. // else nothing needed
  1433. }
  1434. else
  1435. {
  1436. // initialize event inputs from previous outputs
  1437. std::memcpy(pData->bufEvents.in, pData->bufEvents.out, sizeof(EngineEvent)*kEngineMaxInternalEventCount);
  1438. // initialize event outputs (zero)
  1439. std::memset(pData->bufEvents.out, 0, sizeof(EngineEvent)*kEngineMaxInternalEventCount);
  1440. }
  1441. }
  1442. oldAudioInCount = plugin->getAudioInCount();
  1443. oldMidiOutCount = plugin->getMidiOutCount();
  1444. // process
  1445. plugin->initBuffers();
  1446. plugin->process(inBuf, outBuf, frames);
  1447. plugin->unlock();
  1448. // if plugin has no audio inputs, add input buffer
  1449. if (oldAudioInCount == 0)
  1450. {
  1451. #ifdef HAVE_JUCE
  1452. FloatVectorOperations::add(outBuf[0], inBuf0, frames);
  1453. FloatVectorOperations::add(outBuf[1], inBuf1, frames);
  1454. #else
  1455. carla_addFloat(outBuf[0], inBuf0, frames);
  1456. carla_addFloat(outBuf[1], inBuf1, frames);
  1457. #endif
  1458. }
  1459. // set peaks
  1460. {
  1461. EnginePluginData& pluginData(pData->plugins[i]);
  1462. #ifdef HAVE_JUCE
  1463. float tmpMin, tmpMax;
  1464. if (oldAudioInCount > 0)
  1465. {
  1466. FloatVectorOperations::findMinAndMax(inBuf0, frames, tmpMin, tmpMax);
  1467. pluginData.insPeak[0] = carla_max<float>(std::abs(tmpMin), std::abs(tmpMax), 1.0f);
  1468. FloatVectorOperations::findMinAndMax(inBuf1, frames, tmpMin, tmpMax);
  1469. pluginData.insPeak[1] = carla_max<float>(std::abs(tmpMin), std::abs(tmpMax), 1.0f);
  1470. }
  1471. else
  1472. {
  1473. pluginData.insPeak[0] = 0.0f;
  1474. pluginData.insPeak[1] = 0.0f;
  1475. }
  1476. if (oldMidiOutCount > 0)
  1477. {
  1478. FloatVectorOperations::findMinAndMax(outBuf[0], frames, tmpMin, tmpMax);
  1479. pluginData.outsPeak[0] = carla_max<float>(std::abs(tmpMin), std::abs(tmpMax), 1.0f);
  1480. FloatVectorOperations::findMinAndMax(outBuf[1], frames, tmpMin, tmpMax);
  1481. pluginData.outsPeak[1] = carla_max<float>(std::abs(tmpMin), std::abs(tmpMax), 1.0f);
  1482. }
  1483. else
  1484. {
  1485. pluginData.outsPeak[0] = 0.0f;
  1486. pluginData.outsPeak[1] = 0.0f;
  1487. }
  1488. #else
  1489. float peak1, peak2;
  1490. if (oldAudioInCount > 0)
  1491. {
  1492. peak1 = peak2 = 0.0f;
  1493. for (uint32_t k=0; k < frames; ++k)
  1494. {
  1495. peak1 = carla_max<float>(peak1, std::fabs(inBuf0[k]), 1.0f);
  1496. peak2 = carla_max<float>(peak2, std::fabs(inBuf1[k]), 1.0f);
  1497. }
  1498. pluginData.insPeak[0] = peak1;
  1499. pluginData.insPeak[1] = peak2;
  1500. }
  1501. else
  1502. {
  1503. pluginData.insPeak[0] = 0.0f;
  1504. pluginData.insPeak[1] = 0.0f;
  1505. }
  1506. if (oldMidiOutCount > 0)
  1507. {
  1508. peak1 = peak2 = 0.0f;
  1509. for (uint32_t k=0; k < frames; ++k)
  1510. {
  1511. peak1 = carla_max<float>(peak1, std::fabs(outBuf[0][k]), 1.0f);
  1512. peak2 = carla_max<float>(peak2, std::fabs(outBuf[1][k]), 1.0f);
  1513. }
  1514. pluginData.outsPeak[0] = peak1;
  1515. pluginData.outsPeak[1] = peak2;
  1516. }
  1517. else
  1518. {
  1519. pluginData.outsPeak[0] = 0.0f;
  1520. pluginData.outsPeak[1] = 0.0f;
  1521. }
  1522. #endif
  1523. }
  1524. processed = true;
  1525. }
  1526. }
  1527. void CarlaEngine::processPatchbay(float** inBuf, float** outBuf, const uint32_t bufCount[2], const uint32_t frames)
  1528. {
  1529. // TODO
  1530. return;
  1531. // unused, for now
  1532. (void)inBuf;
  1533. (void)outBuf;
  1534. (void)bufCount;
  1535. (void)frames;
  1536. }
  1537. #endif
  1538. // -----------------------------------------------------------------------
  1539. // Bridge/Controller OSC stuff
  1540. #ifndef BUILD_BRIDGE
  1541. void CarlaEngine::oscSend_control_add_plugin_start(const int32_t pluginId, const char* const pluginName)
  1542. {
  1543. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1544. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1545. CARLA_SAFE_ASSERT_RETURN(pluginName != nullptr && pluginName[0] != '\0',);
  1546. carla_debug("CarlaEngine::oscSend_control_add_plugin_start(%i, \"%s\")", pluginId, pluginName);
  1547. if (pData->oscData->target != nullptr)
  1548. {
  1549. char targetPath[std::strlen(pData->oscData->path)+18];
  1550. std::strcpy(targetPath, pData->oscData->path);
  1551. std::strcat(targetPath, "/add_plugin_start");
  1552. lo_send(pData->oscData->target, targetPath, "is", pluginId, pluginName);
  1553. }
  1554. }
  1555. void CarlaEngine::oscSend_control_add_plugin_end(const int32_t pluginId)
  1556. {
  1557. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1558. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1559. carla_debug("CarlaEngine::oscSend_control_add_plugin_end(%i)", pluginId);
  1560. if (pData->oscData->target != nullptr)
  1561. {
  1562. char targetPath[std::strlen(pData->oscData->path)+16];
  1563. std::strcpy(targetPath, pData->oscData->path);
  1564. std::strcat(targetPath, "/add_plugin_end");
  1565. lo_send(pData->oscData->target, targetPath, "i", pluginId);
  1566. }
  1567. }
  1568. void CarlaEngine::oscSend_control_remove_plugin(const int32_t pluginId)
  1569. {
  1570. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1571. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->curPluginCount),);
  1572. carla_debug("CarlaEngine::oscSend_control_remove_plugin(%i)", pluginId);
  1573. if (pData->oscData->target != nullptr)
  1574. {
  1575. char targetPath[std::strlen(pData->oscData->path)+15];
  1576. std::strcpy(targetPath, pData->oscData->path);
  1577. std::strcat(targetPath, "/remove_plugin");
  1578. lo_send(pData->oscData->target, targetPath, "i", pluginId);
  1579. }
  1580. }
  1581. void CarlaEngine::oscSend_control_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)
  1582. {
  1583. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1584. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1585. CARLA_SAFE_ASSERT_RETURN(type != PLUGIN_NONE,);
  1586. CARLA_SAFE_ASSERT_RETURN(realName != nullptr && realName[0] != '\0',);
  1587. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1588. CARLA_SAFE_ASSERT_RETURN(maker != nullptr,);
  1589. CARLA_SAFE_ASSERT_RETURN(copyright != nullptr,);
  1590. carla_debug("CarlaEngine::oscSend_control_set_plugin_data(%i, %i, %i, %i, \"%s\", \"%s\", \"%s\", \"%s\", " P_INT64 ")", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  1591. if (pData->oscData->target != nullptr)
  1592. {
  1593. char targetPath[std::strlen(pData->oscData->path)+17];
  1594. std::strcpy(targetPath, pData->oscData->path);
  1595. std::strcat(targetPath, "/set_plugin_data");
  1596. lo_send(pData->oscData->target, targetPath, "iiiissssh", pluginId, type, category, hints, realName, label, maker, copyright, uniqueId);
  1597. }
  1598. }
  1599. void CarlaEngine::oscSend_control_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)
  1600. {
  1601. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1602. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1603. carla_debug("CarlaEngine::oscSend_control_set_plugin_ports(%i, %i, %i, %i, %i, %i, %i)", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts);
  1604. if (pData->oscData->target != nullptr)
  1605. {
  1606. char targetPath[std::strlen(pData->oscData->path)+18];
  1607. std::strcpy(targetPath, pData->oscData->path);
  1608. std::strcat(targetPath, "/set_plugin_ports");
  1609. lo_send(pData->oscData->target, targetPath, "iiiiiii", pluginId, audioIns, audioOuts, midiIns, midiOuts, cIns, cOuts);
  1610. }
  1611. }
  1612. void CarlaEngine::oscSend_control_set_parameter_data(const int32_t pluginId, const int32_t index, const int32_t hints, const char* const name, const char* const unit, const float current)
  1613. {
  1614. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1615. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1616. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1617. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  1618. CARLA_SAFE_ASSERT_RETURN(unit != nullptr,);
  1619. carla_debug("CarlaEngine::oscSend_control_set_parameter_data(%i, %i, %i, %i, \"%s\", \"%s\", %f)", pluginId, index, hints, name, unit, current);
  1620. if (pData->oscData->target != nullptr)
  1621. {
  1622. char targetPath[std::strlen(pData->oscData->path)+20];
  1623. std::strcpy(targetPath, pData->oscData->path);
  1624. std::strcat(targetPath, "/set_parameter_data");
  1625. lo_send(pData->oscData->target, targetPath, "iiissf", pluginId, index, hints, name, unit, current);
  1626. }
  1627. }
  1628. void CarlaEngine::oscSend_control_set_parameter_ranges(const int32_t pluginId, const int32_t index, const float min, const float max, const float def, const float step, const float stepSmall, const float stepLarge)
  1629. {
  1630. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1631. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1632. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1633. CARLA_SAFE_ASSERT_RETURN(min < max,);
  1634. carla_debug("CarlaEngine::oscSend_control_set_parameter_ranges(%i, %i, %f, %f, %f, %f, %f, %f)", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  1635. if (pData->oscData->target != nullptr)
  1636. {
  1637. char targetPath[std::strlen(pData->oscData->path)+22];
  1638. std::strcpy(targetPath, pData->oscData->path);
  1639. std::strcat(targetPath, "/set_parameter_ranges");
  1640. lo_send(pData->oscData->target, targetPath, "iiffffff", pluginId, index, min, max, def, step, stepSmall, stepLarge);
  1641. }
  1642. }
  1643. void CarlaEngine::oscSend_control_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc)
  1644. {
  1645. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1646. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1647. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1648. carla_debug("CarlaEngine::oscSend_control_set_parameter_midi_cc(%i, %i, %i)", pluginId, index, cc);
  1649. if (pData->oscData->target != nullptr)
  1650. {
  1651. char targetPath[std::strlen(pData->oscData->path)+23];
  1652. std::strcpy(targetPath, pData->oscData->path);
  1653. std::strcat(targetPath, "/set_parameter_midi_cc");
  1654. lo_send(pData->oscData->target, targetPath, "iii", pluginId, index, cc);
  1655. }
  1656. }
  1657. void CarlaEngine::oscSend_control_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel)
  1658. {
  1659. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1660. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1661. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1662. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS,);
  1663. carla_debug("CarlaEngine::oscSend_control_set_parameter_midi_channel(%i, %i, %i)", pluginId, index, channel);
  1664. if (pData->oscData->target != nullptr)
  1665. {
  1666. char targetPath[std::strlen(pData->oscData->path)+28];
  1667. std::strcpy(targetPath, pData->oscData->path);
  1668. std::strcat(targetPath, "/set_parameter_midi_channel");
  1669. lo_send(pData->oscData->target, targetPath, "iii", pluginId, index, channel);
  1670. }
  1671. }
  1672. void CarlaEngine::oscSend_control_set_parameter_value(const int32_t pluginId, const int32_t index, const float value)
  1673. {
  1674. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1675. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1676. carla_debug("CarlaEngine::oscSend_control_set_parameter_value(%i, %i:%s, %f)", pluginId, index, (index < 0) ? InternalParameterIndex2Str(static_cast<InternalParameterIndex>(index)) : "(none)", value);
  1677. if (pData->oscData->target != nullptr)
  1678. {
  1679. char targetPath[std::strlen(pData->oscData->path)+21];
  1680. std::strcpy(targetPath, pData->oscData->path);
  1681. std::strcat(targetPath, "/set_parameter_value");
  1682. lo_send(pData->oscData->target, targetPath, "iif", pluginId, index, value);
  1683. }
  1684. }
  1685. void CarlaEngine::oscSend_control_set_default_value(const int32_t pluginId, const int32_t index, const float value)
  1686. {
  1687. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1688. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1689. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1690. carla_debug("CarlaEngine::oscSend_control_set_default_value(%i, %i, %f)", pluginId, index, value);
  1691. if (pData->oscData->target != nullptr)
  1692. {
  1693. char targetPath[std::strlen(pData->oscData->path)+19];
  1694. std::strcpy(targetPath, pData->oscData->path);
  1695. std::strcat(targetPath, "/set_default_value");
  1696. lo_send(pData->oscData->target, targetPath, "iif", pluginId, index, value);
  1697. }
  1698. }
  1699. void CarlaEngine::oscSend_control_set_program(const int32_t pluginId, const int32_t index)
  1700. {
  1701. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1702. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1703. carla_debug("CarlaEngine::oscSend_control_set_program(%i, %i)", pluginId, index);
  1704. if (pData->oscData->target != nullptr)
  1705. {
  1706. char targetPath[std::strlen(pData->oscData->path)+13];
  1707. std::strcpy(targetPath, pData->oscData->path);
  1708. std::strcat(targetPath, "/set_program");
  1709. lo_send(pData->oscData->target, targetPath, "ii", pluginId, index);
  1710. }
  1711. }
  1712. void CarlaEngine::oscSend_control_set_program_count(const int32_t pluginId, const int32_t count)
  1713. {
  1714. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1715. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1716. CARLA_SAFE_ASSERT_RETURN(count >= 0,);
  1717. carla_debug("CarlaEngine::oscSend_control_set_program_count(%i, %i)", pluginId, count);
  1718. if (pData->oscData->target != nullptr)
  1719. {
  1720. char targetPath[std::strlen(pData->oscData->path)+19];
  1721. std::strcpy(targetPath, pData->oscData->path);
  1722. std::strcat(targetPath, "/set_program_count");
  1723. lo_send(pData->oscData->target, targetPath, "ii", pluginId, count);
  1724. }
  1725. }
  1726. void CarlaEngine::oscSend_control_set_program_name(const int32_t pluginId, const int32_t index, const char* const name)
  1727. {
  1728. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1729. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1730. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1731. CARLA_SAFE_ASSERT_RETURN(name != nullptr,);
  1732. carla_debug("CarlaEngine::oscSend_control_set_program_name(%i, %i, \"%s\")", pluginId, index, name);
  1733. if (pData->oscData->target != nullptr)
  1734. {
  1735. char targetPath[std::strlen(pData->oscData->path)+18];
  1736. std::strcpy(targetPath, pData->oscData->path);
  1737. std::strcat(targetPath, "/set_program_name");
  1738. lo_send(pData->oscData->target, targetPath, "iis", pluginId, index, name);
  1739. }
  1740. }
  1741. void CarlaEngine::oscSend_control_set_midi_program(const int32_t pluginId, const int32_t index)
  1742. {
  1743. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1744. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1745. carla_debug("CarlaEngine::oscSend_control_set_midi_program(%i, %i)", pluginId, index);
  1746. if (pData->oscData->target != nullptr)
  1747. {
  1748. char targetPath[std::strlen(pData->oscData->path)+18];
  1749. std::strcpy(targetPath, pData->oscData->path);
  1750. std::strcat(targetPath, "/set_midi_program");
  1751. lo_send(pData->oscData->target, targetPath, "ii", pluginId, index);
  1752. }
  1753. }
  1754. void CarlaEngine::oscSend_control_set_midi_program_count(const int32_t pluginId, const int32_t count)
  1755. {
  1756. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1757. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1758. CARLA_SAFE_ASSERT_RETURN(count >= 0,);
  1759. carla_debug("CarlaEngine::oscSend_control_set_midi_program_count(%i, %i)", pluginId, count);
  1760. if (pData->oscData->target != nullptr)
  1761. {
  1762. char targetPath[std::strlen(pData->oscData->path)+24];
  1763. std::strcpy(targetPath, pData->oscData->path);
  1764. std::strcat(targetPath, "/set_midi_program_count");
  1765. lo_send(pData->oscData->target, targetPath, "ii", pluginId, count);
  1766. }
  1767. }
  1768. void CarlaEngine::oscSend_control_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name)
  1769. {
  1770. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1771. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->maxPluginNumber),);
  1772. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  1773. CARLA_SAFE_ASSERT_RETURN(bank >= 0,);
  1774. CARLA_SAFE_ASSERT_RETURN(program >= 0,);
  1775. CARLA_SAFE_ASSERT_RETURN(name != nullptr,);
  1776. carla_debug("CarlaEngine::oscSend_control_set_midi_program_data(%i, %i, %i, %i, \"%s\")", pluginId, index, bank, program, name);
  1777. if (pData->oscData->target != nullptr)
  1778. {
  1779. char targetPath[std::strlen(pData->oscData->path)+23];
  1780. std::strcpy(targetPath, pData->oscData->path);
  1781. std::strcat(targetPath, "/set_midi_program_data");
  1782. lo_send(pData->oscData->target, targetPath, "iiiis", pluginId, index, bank, program, name);
  1783. }
  1784. }
  1785. void CarlaEngine::oscSend_control_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo)
  1786. {
  1787. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1788. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->curPluginCount),);
  1789. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS,);
  1790. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE,);
  1791. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1792. carla_debug("CarlaEngine::oscSend_control_note_on(%i, %i, %i, %i)", pluginId, channel, note, velo);
  1793. if (pData->oscData->target != nullptr)
  1794. {
  1795. char targetPath[std::strlen(pData->oscData->path)+9];
  1796. std::strcpy(targetPath, pData->oscData->path);
  1797. std::strcat(targetPath, "/note_on");
  1798. lo_send(pData->oscData->target, targetPath, "iiii", pluginId, channel, note, velo);
  1799. }
  1800. }
  1801. void CarlaEngine::oscSend_control_note_off(const int32_t pluginId, const int32_t channel, const int32_t note)
  1802. {
  1803. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1804. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->curPluginCount),);
  1805. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS,);
  1806. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE,);
  1807. carla_debug("CarlaEngine::oscSend_control_note_off(%i, %i, %i)", pluginId, channel, note);
  1808. if (pData->oscData->target != nullptr)
  1809. {
  1810. char targetPath[std::strlen(pData->oscData->path)+10];
  1811. std::strcpy(targetPath, pData->oscData->path);
  1812. std::strcat(targetPath, "/note_off");
  1813. lo_send(pData->oscData->target, targetPath, "iii", pluginId, channel, note);
  1814. }
  1815. }
  1816. void CarlaEngine::oscSend_control_set_peaks(const int32_t pluginId)
  1817. {
  1818. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1819. CARLA_SAFE_ASSERT_RETURN(pluginId >= 0 && pluginId < static_cast<int32_t>(pData->curPluginCount),);
  1820. const EnginePluginData& epData(pData->plugins[pluginId]);
  1821. if (pData->oscData->target != nullptr)
  1822. {
  1823. char targetPath[std::strlen(pData->oscData->path)+22];
  1824. std::strcpy(targetPath, pData->oscData->path);
  1825. std::strcat(targetPath, "/set_peaks");
  1826. lo_send(pData->oscData->target, targetPath, "iffff", pluginId, epData.insPeak[0], epData.insPeak[1], epData.outsPeak[0], epData.outsPeak[1]);
  1827. }
  1828. }
  1829. void CarlaEngine::oscSend_control_exit()
  1830. {
  1831. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1832. carla_debug("CarlaEngine::oscSend_control_exit()");
  1833. if (pData->oscData->target != nullptr)
  1834. {
  1835. char targetPath[std::strlen(pData->oscData->path)+6];
  1836. std::strcpy(targetPath, pData->oscData->path);
  1837. std::strcat(targetPath, "/exit");
  1838. lo_send(pData->oscData->target, targetPath, "");
  1839. }
  1840. }
  1841. #else
  1842. void CarlaEngine::oscSend_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total)
  1843. {
  1844. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1845. CARLA_SAFE_ASSERT_RETURN(total >= 0 && total >= ins + outs,);
  1846. carla_debug("CarlaEngine::oscSend_bridge_audio_count(%i, %i, %i)", ins, outs, total);
  1847. if (pData->oscData->target != nullptr)
  1848. {
  1849. char targetPath[std::strlen(pData->oscData->path)+20];
  1850. std::strcpy(targetPath, pData->oscData->path);
  1851. std::strcat(targetPath, "/bridge_audio_count");
  1852. lo_send(pData->oscData->target, targetPath, "iii", ins, outs, total);
  1853. }
  1854. }
  1855. void CarlaEngine::oscSend_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total)
  1856. {
  1857. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1858. CARLA_SAFE_ASSERT_RETURN(total >= 0 && total >= ins + outs,);
  1859. carla_debug("CarlaEngine::oscSend_bridge_midi_count(%i, %i, %i)", ins, outs, total);
  1860. if (pData->oscData->target != nullptr)
  1861. {
  1862. char targetPath[std::strlen(pData->oscData->path)+19];
  1863. std::strcpy(targetPath, pData->oscData->path);
  1864. std::strcat(targetPath, "/bridge_midi_count");
  1865. lo_send(pData->oscData->target, targetPath, "iii", ins, outs, total);
  1866. }
  1867. }
  1868. void CarlaEngine::oscSend_bridge_parameter_count(const int32_t ins, const int32_t outs, const int32_t total)
  1869. {
  1870. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1871. CARLA_SAFE_ASSERT_RETURN(total >= 0 && total >= ins + outs,);
  1872. carla_debug("CarlaEngine::oscSend_bridge_parameter_count(%i, %i, %i)", ins, outs, total);
  1873. if (pData->oscData->target != nullptr)
  1874. {
  1875. char targetPath[std::strlen(pData->oscData->path)+24];
  1876. std::strcpy(targetPath, pData->oscData->path);
  1877. std::strcat(targetPath, "/bridge_parameter_count");
  1878. lo_send(pData->oscData->target, targetPath, "iii", ins, outs, total);
  1879. }
  1880. }
  1881. void CarlaEngine::oscSend_bridge_program_count(const int32_t count)
  1882. {
  1883. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1884. CARLA_SAFE_ASSERT_RETURN(count >= 0,);
  1885. carla_debug("CarlaEngine::oscSend_bridge_program_count(%i)", count);
  1886. if (pData->oscData->target != nullptr)
  1887. {
  1888. char targetPath[std::strlen(pData->oscData->path)+22];
  1889. std::strcpy(targetPath, pData->oscData->path);
  1890. std::strcat(targetPath, "/bridge_program_count");
  1891. lo_send(pData->oscData->target, targetPath, "i", count);
  1892. }
  1893. }
  1894. void CarlaEngine::oscSend_bridge_midi_program_count(const int32_t count)
  1895. {
  1896. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1897. CARLA_SAFE_ASSERT_RETURN(count >= 0,);
  1898. carla_debug("CarlaEngine::oscSend_bridge_midi_program_count(%i)", count);
  1899. if (pData->oscData->target != nullptr)
  1900. {
  1901. char targetPath[std::strlen(pData->oscData->path)+27];
  1902. std::strcpy(targetPath, pData->oscData->path);
  1903. std::strcat(targetPath, "/bridge_midi_program_count");
  1904. lo_send(pData->oscData->target, targetPath, "i", count);
  1905. }
  1906. }
  1907. void CarlaEngine::oscSend_bridge_plugin_info(const int32_t category, const int32_t hints, const char* const name, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId)
  1908. {
  1909. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1910. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  1911. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1912. CARLA_SAFE_ASSERT_RETURN(maker != nullptr,);
  1913. CARLA_SAFE_ASSERT_RETURN(copyright != nullptr,);
  1914. carla_debug("CarlaEngine::oscSend_bridge_plugin_info(%i, %i, \"%s\", \"%s\", \"%s\", \"%s\", " P_INT64 ")", category, hints, name, label, maker, copyright, uniqueId);
  1915. if (pData->oscData->target != nullptr)
  1916. {
  1917. char targetPath[std::strlen(pData->oscData->path)+20];
  1918. std::strcpy(targetPath, pData->oscData->path);
  1919. std::strcat(targetPath, "/bridge_plugin_info");
  1920. lo_send(pData->oscData->target, targetPath, "iissssh", category, hints, name, label, maker, copyright, uniqueId);
  1921. }
  1922. }
  1923. void CarlaEngine::oscSend_bridge_parameter_info(const int32_t index, const char* const name, const char* const unit)
  1924. {
  1925. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1926. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  1927. CARLA_SAFE_ASSERT_RETURN(unit != nullptr,);
  1928. carla_debug("CarlaEngine::oscSend_bridge_parameter_info(%i, \"%s\", \"%s\")", index, name, unit);
  1929. if (pData->oscData->target != nullptr)
  1930. {
  1931. char targetPath[std::strlen(pData->oscData->path)+23];
  1932. std::strcpy(targetPath, pData->oscData->path);
  1933. std::strcat(targetPath, "/bridge_parameter_info");
  1934. lo_send(pData->oscData->target, targetPath, "iss", index, name, unit);
  1935. }
  1936. }
  1937. void CarlaEngine::oscSend_bridge_parameter_data(const int32_t index, const int32_t rindex, const int32_t hints, const int32_t midiChannel, const int32_t midiCC)
  1938. {
  1939. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1940. carla_debug("CarlaEngine::oscSend_bridge_parameter_data(%i, %i, %i, %i, %i)", index, rindex, hints, midiChannel, midiCC);
  1941. if (pData->oscData->target != nullptr)
  1942. {
  1943. char targetPath[std::strlen(pData->oscData->path)+23];
  1944. std::strcpy(targetPath, pData->oscData->path);
  1945. std::strcat(targetPath, "/bridge_parameter_data");
  1946. lo_send(pData->oscData->target, targetPath, "iiiii", index, rindex, hints, midiChannel, midiCC);
  1947. }
  1948. }
  1949. void CarlaEngine::oscSend_bridge_parameter_ranges(const int32_t index, const float def, const float min, const float max, const float step, const float stepSmall, const float stepLarge)
  1950. {
  1951. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1952. carla_debug("CarlaEngine::oscSend_bridge_parameter_ranges(%i, %f, %f, %f, %f, %f, %f)", index, def, min, max, step, stepSmall, stepLarge);
  1953. if (pData->oscData->target != nullptr)
  1954. {
  1955. char targetPath[std::strlen(pData->oscData->path)+25];
  1956. std::strcpy(targetPath, pData->oscData->path);
  1957. std::strcat(targetPath, "/bridge_parameter_ranges");
  1958. lo_send(pData->oscData->target, targetPath, "iffffff", index, def, min, max, step, stepSmall, stepLarge);
  1959. }
  1960. }
  1961. void CarlaEngine::oscSend_bridge_program_info(const int32_t index, const char* const name)
  1962. {
  1963. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1964. carla_debug("CarlaEngine::oscSend_bridge_program_info(%i, \"%s\")", index, name);
  1965. if (pData->oscData->target != nullptr)
  1966. {
  1967. char targetPath[std::strlen(pData->oscData->path)+21];
  1968. std::strcpy(targetPath, pData->oscData->path);
  1969. std::strcat(targetPath, "/bridge_program_info");
  1970. lo_send(pData->oscData->target, targetPath, "is", index, name);
  1971. }
  1972. }
  1973. void CarlaEngine::oscSend_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label)
  1974. {
  1975. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1976. carla_debug("CarlaEngine::oscSend_bridge_midi_program_info(%i, %i, %i, \"%s\")", index, bank, program, label);
  1977. if (pData->oscData->target != nullptr)
  1978. {
  1979. char targetPath[std::strlen(pData->oscData->path)+26];
  1980. std::strcpy(targetPath, pData->oscData->path);
  1981. std::strcat(targetPath, "/bridge_midi_program_info");
  1982. lo_send(pData->oscData->target, targetPath, "iiis", index, bank, program, label);
  1983. }
  1984. }
  1985. void CarlaEngine::oscSend_bridge_configure(const char* const key, const char* const value)
  1986. {
  1987. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  1988. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1989. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1990. carla_debug("CarlaEngine::oscSend_bridge_configure(\"%s\", \"%s\")", key, value);
  1991. if (pData->oscData->target != nullptr)
  1992. {
  1993. char targetPath[std::strlen(pData->oscData->path)+18];
  1994. std::strcpy(targetPath, pData->oscData->path);
  1995. std::strcat(targetPath, "/bridge_configure");
  1996. lo_send(pData->oscData->target, targetPath, "ss", key, value);
  1997. }
  1998. }
  1999. void CarlaEngine::oscSend_bridge_set_parameter_value(const int32_t index, const float value)
  2000. {
  2001. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2002. carla_debug("CarlaEngine::oscSend_bridge_set_parameter_value(%i, %f)", index, value);
  2003. if (pData->oscData->target != nullptr)
  2004. {
  2005. char targetPath[std::strlen(pData->oscData->path)+28];
  2006. std::strcpy(targetPath, pData->oscData->path);
  2007. std::strcat(targetPath, "/bridge_set_parameter_value");
  2008. lo_send(pData->oscData->target, targetPath, "if", index, value);
  2009. }
  2010. }
  2011. void CarlaEngine::oscSend_bridge_set_default_value(const int32_t index, const float value)
  2012. {
  2013. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2014. carla_debug("CarlaEngine::oscSend_bridge_set_default_value(%i, %f)", index, value);
  2015. if (pData->oscData->target != nullptr)
  2016. {
  2017. char targetPath[std::strlen(pData->oscData->path)+26];
  2018. std::strcpy(targetPath, pData->oscData->path);
  2019. std::strcat(targetPath, "/bridge_set_default_value");
  2020. lo_send(pData->oscData->target, targetPath, "if", index, value);
  2021. }
  2022. }
  2023. void CarlaEngine::oscSend_bridge_set_program(const int32_t index)
  2024. {
  2025. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2026. carla_debug("CarlaEngine::oscSend_bridge_set_program(%i)", index);
  2027. if (pData->oscData->target != nullptr)
  2028. {
  2029. char targetPath[std::strlen(pData->oscData->path)+20];
  2030. std::strcpy(targetPath, pData->oscData->path);
  2031. std::strcat(targetPath, "/bridge_set_program");
  2032. lo_send(pData->oscData->target, targetPath, "i", index);
  2033. }
  2034. }
  2035. void CarlaEngine::oscSend_bridge_set_midi_program(const int32_t index)
  2036. {
  2037. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2038. carla_debug("CarlaEngine::oscSend_bridge_set_midi_program(%i)", index);
  2039. if (pData->oscData->target != nullptr)
  2040. {
  2041. char targetPath[std::strlen(pData->oscData->path)+25];
  2042. std::strcpy(targetPath, pData->oscData->path);
  2043. std::strcat(targetPath, "/bridge_set_midi_program");
  2044. lo_send(pData->oscData->target, targetPath, "i", index);
  2045. }
  2046. }
  2047. void CarlaEngine::oscSend_bridge_set_custom_data(const char* const type, const char* const key, const char* const value)
  2048. {
  2049. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2050. carla_debug("CarlaEngine::oscSend_bridge_set_custom_data(\"%s\", \"%s\", \"%s\")", type, key, value);
  2051. if (pData->oscData->target != nullptr)
  2052. {
  2053. char targetPath[std::strlen(pData->oscData->path)+24];
  2054. std::strcpy(targetPath, pData->oscData->path);
  2055. std::strcat(targetPath, "/bridge_set_custom_data");
  2056. lo_send(pData->oscData->target, targetPath, "sss", type, key, value);
  2057. }
  2058. }
  2059. void CarlaEngine::oscSend_bridge_set_chunk_data(const char* const chunkFile)
  2060. {
  2061. CARLA_SAFE_ASSERT_RETURN(pData->oscData != nullptr,);
  2062. carla_debug("CarlaEngine::oscSend_bridge_set_chunk_data(\"%s\")", chunkFile);
  2063. if (pData->oscData->target != nullptr)
  2064. {
  2065. char targetPath[std::strlen(pData->oscData->path)+23];
  2066. std::strcpy(targetPath, pData->oscData->path);
  2067. std::strcat(targetPath, "/bridge_set_chunk_data");
  2068. lo_send(pData->oscData->target, targetPath, "s", chunkFile);
  2069. }
  2070. }
  2071. #endif
  2072. // -----------------------------------------------------------------------
  2073. #undef CARLA_SAFE_ASSERT_RETURN_ERR
  2074. CARLA_BACKEND_END_NAMESPACE