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.

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