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.

813 lines
27KB

  1. /*
  2. * Carla SFZero Plugin
  3. * Copyright (C) 2018-2023 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifndef STATIC_PLUGIN_TARGET
  20. # define HAVE_SFZ
  21. #endif
  22. #ifdef HAVE_SFZ
  23. #include "CarlaBackendUtils.hpp"
  24. #include "sfzero/SFZero.h"
  25. #include "water/buffers/AudioSampleBuffer.h"
  26. #include "water/files/File.h"
  27. #include "water/midi/MidiMessage.h"
  28. using water::AudioSampleBuffer;
  29. using water::File;
  30. using water::MidiMessage;
  31. using water::String;
  32. // -----------------------------------------------------------------------
  33. CARLA_BACKEND_START_NAMESPACE
  34. // -------------------------------------------------------------------------------------------------------------------
  35. // Fallback data
  36. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  37. static void loadingIdleCallbackFunction(void* ptr)
  38. {
  39. ((CarlaEngine*)ptr)->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  40. }
  41. // -------------------------------------------------------------------------------------------------------------------
  42. class CarlaPluginSFZero : public CarlaPlugin
  43. {
  44. public:
  45. CarlaPluginSFZero(CarlaEngine* const engine, const uint id)
  46. : CarlaPlugin(engine, id),
  47. fSynth(),
  48. fNumVoices(0.0f),
  49. fLabel(nullptr),
  50. fRealName(nullptr)
  51. {
  52. carla_debug("CarlaPluginSFZero::CarlaPluginSFZero(%p, %i)", engine, id);
  53. }
  54. ~CarlaPluginSFZero() override
  55. {
  56. carla_debug("CarlaPluginSFZero::~CarlaPluginSFZero()");
  57. pData->singleMutex.lock();
  58. pData->masterMutex.lock();
  59. if (pData->client != nullptr && pData->client->isActive())
  60. pData->client->deactivate(true);
  61. if (pData->active)
  62. {
  63. deactivate();
  64. pData->active = false;
  65. }
  66. if (fLabel != nullptr)
  67. {
  68. delete[] fLabel;
  69. fLabel = nullptr;
  70. }
  71. if (fRealName != nullptr)
  72. {
  73. delete[] fRealName;
  74. fRealName = nullptr;
  75. }
  76. clearBuffers();
  77. }
  78. // -------------------------------------------------------------------
  79. // Information (base)
  80. PluginType getType() const noexcept override
  81. {
  82. return PLUGIN_SFZ;
  83. }
  84. PluginCategory getCategory() const noexcept override
  85. {
  86. return PLUGIN_CATEGORY_SYNTH;
  87. }
  88. // -------------------------------------------------------------------
  89. // Information (count)
  90. // nothing
  91. // -------------------------------------------------------------------
  92. // Information (current data)
  93. // nothing
  94. // -------------------------------------------------------------------
  95. // Information (per-plugin data)
  96. uint getOptionsAvailable() const noexcept override
  97. {
  98. uint options = 0x0;
  99. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  100. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  101. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  102. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  103. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  104. options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  105. return options;
  106. }
  107. float getParameterValue(const uint32_t parameterId) const noexcept override
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(parameterId == 0, 0.0f);
  110. return fNumVoices;
  111. }
  112. bool getLabel(char* const strBuf) const noexcept override
  113. {
  114. if (fLabel != nullptr)
  115. {
  116. std::strncpy(strBuf, fLabel, STR_MAX);
  117. return true;
  118. }
  119. return CarlaPlugin::getLabel(strBuf);
  120. }
  121. bool getMaker(char* const strBuf) const noexcept override
  122. {
  123. std::strncpy(strBuf, "SFZero engine", STR_MAX);
  124. return true;
  125. }
  126. bool getCopyright(char* const strBuf) const noexcept override
  127. {
  128. std::strncpy(strBuf, "ISC", STR_MAX);
  129. return true;
  130. }
  131. bool getRealName(char* const strBuf) const noexcept override
  132. {
  133. if (fRealName != nullptr)
  134. {
  135. std::strncpy(strBuf, fRealName, STR_MAX);
  136. return true;
  137. }
  138. return CarlaPlugin::getRealName(strBuf);
  139. }
  140. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(parameterId == 0, false);
  143. std::strncpy(strBuf, "Voice Count", STR_MAX);
  144. return true;
  145. }
  146. // -------------------------------------------------------------------
  147. // Set data (state)
  148. // nothing
  149. // -------------------------------------------------------------------
  150. // Set data (internal stuff)
  151. // nothing
  152. // -------------------------------------------------------------------
  153. // Set data (plugin-specific stuff)
  154. // nothing
  155. // -------------------------------------------------------------------
  156. // Set ui stuff
  157. // nothing
  158. // -------------------------------------------------------------------
  159. // Plugin state
  160. void reload() override
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  163. carla_debug("CarlaPluginSFZero::reload() - start");
  164. const EngineProcessMode processMode(pData->engine->getProccessMode());
  165. // Safely disable plugin for reload
  166. const ScopedDisabler sd(this);
  167. if (pData->active)
  168. deactivate();
  169. clearBuffers();
  170. pData->audioOut.createNew(2);
  171. pData->param.createNew(1, false);
  172. const uint portNameSize(pData->engine->getMaxPortNameSize());
  173. CarlaString portName;
  174. // ---------------------------------------
  175. // Audio Outputs
  176. // out-left
  177. portName.clear();
  178. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  179. {
  180. portName = pData->name;
  181. portName += ":";
  182. }
  183. portName += "out-left";
  184. portName.truncate(portNameSize);
  185. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 0);
  186. pData->audioOut.ports[0].rindex = 0;
  187. // out-right
  188. portName.clear();
  189. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  190. {
  191. portName = pData->name;
  192. portName += ":";
  193. }
  194. portName += "out-right";
  195. portName.truncate(portNameSize);
  196. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  197. pData->audioOut.ports[1].rindex = 1;
  198. // ---------------------------------------
  199. // Event Input
  200. portName.clear();
  201. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  202. {
  203. portName = pData->name;
  204. portName += ":";
  205. }
  206. portName += "events-in";
  207. portName.truncate(portNameSize);
  208. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  209. // ---------------------------------------
  210. // Parameters
  211. pData->param.data[0].type = PARAMETER_OUTPUT;
  212. pData->param.data[0].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMATABLE | PARAMETER_IS_INTEGER;
  213. pData->param.data[0].index = 0;
  214. pData->param.data[0].rindex = 0;
  215. pData->param.ranges[0].min = 0.0f;
  216. pData->param.ranges[0].max = 128;
  217. pData->param.ranges[0].def = 0.0f;
  218. pData->param.ranges[0].step = 1.0f;
  219. pData->param.ranges[0].stepSmall = 1.0f;
  220. pData->param.ranges[0].stepLarge = 1.0f;
  221. // ---------------------------------------
  222. // plugin hints
  223. pData->hints = 0x0;
  224. pData->hints |= PLUGIN_IS_SYNTH;
  225. pData->hints |= PLUGIN_CAN_VOLUME;
  226. pData->hints |= PLUGIN_CAN_BALANCE;
  227. // extra plugin hints
  228. pData->extraHints = 0x0;
  229. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  230. bufferSizeChanged(pData->engine->getBufferSize());
  231. reloadPrograms(true);
  232. if (pData->active)
  233. activate();
  234. carla_debug("CarlaPluginSFZero::reload() - end");
  235. }
  236. // -------------------------------------------------------------------
  237. // Plugin processing
  238. void process(const float* const* const, float** const audioOut, const float* const*, float**, const uint32_t frames) override
  239. {
  240. // --------------------------------------------------------------------------------------------------------
  241. // Check if active
  242. if (! pData->active)
  243. {
  244. // disable any output sound
  245. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  246. carla_zeroFloats(audioOut[i], frames);
  247. fNumVoices = 0.0f;
  248. return;
  249. }
  250. // --------------------------------------------------------------------------------------------------------
  251. // Check if needs reset
  252. if (pData->needsReset)
  253. {
  254. fSynth.allNotesOff(0, false);
  255. pData->needsReset = false;
  256. }
  257. // --------------------------------------------------------------------------------------------------------
  258. // Event Input and Processing
  259. {
  260. // ----------------------------------------------------------------------------------------------------
  261. // Setup audio buffer
  262. AudioSampleBuffer audioOutBuffer(audioOut, 2, frames);
  263. // ----------------------------------------------------------------------------------------------------
  264. // MIDI Input (External)
  265. if (pData->extNotes.mutex.tryLock())
  266. {
  267. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  268. {
  269. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  270. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  271. if (note.velo > 0)
  272. fSynth.noteOn(note.channel+1, note.note, static_cast<float>(note.velo)/127.0f);
  273. else
  274. fSynth.noteOff(note.channel+1, note.note, static_cast<float>(note.velo)/127.0f, true);
  275. }
  276. pData->extNotes.data.clear();
  277. pData->extNotes.mutex.unlock();
  278. } // End of MIDI Input (External)
  279. // ----------------------------------------------------------------------------------------------------
  280. // Event Input (System)
  281. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  282. bool allNotesOffSent = false;
  283. #endif
  284. uint32_t timeOffset = 0;
  285. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  286. {
  287. const EngineEvent& event(pData->event.portIn->getEvent(i));
  288. uint32_t eventTime = event.time;
  289. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  290. if (eventTime < timeOffset)
  291. {
  292. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  293. eventTime, timeOffset, pData->name);
  294. eventTime = timeOffset;
  295. }
  296. else if (eventTime > timeOffset)
  297. {
  298. if (processSingle(audioOutBuffer, eventTime - timeOffset, timeOffset))
  299. timeOffset = eventTime;
  300. }
  301. // Control change
  302. switch (event.type)
  303. {
  304. case kEngineEventTypeNull:
  305. break;
  306. case kEngineEventTypeControl:
  307. {
  308. const EngineControlEvent& ctrlEvent = event.ctrl;
  309. switch (ctrlEvent.type)
  310. {
  311. case kEngineControlEventTypeNull:
  312. break;
  313. case kEngineControlEventTypeParameter:
  314. {
  315. float value;
  316. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  317. // Control backend stuff
  318. if (event.channel == pData->ctrlChannel)
  319. {
  320. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  321. {
  322. value = ctrlEvent.normalizedValue;
  323. setDryWetRT(value, true);
  324. }
  325. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  326. {
  327. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  328. setVolumeRT(value, true);
  329. }
  330. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  331. {
  332. float left, right;
  333. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  334. if (value < 0.0f)
  335. {
  336. left = -1.0f;
  337. right = (value*2.0f)+1.0f;
  338. }
  339. else if (value > 0.0f)
  340. {
  341. left = (value*2.0f)-1.0f;
  342. right = 1.0f;
  343. }
  344. else
  345. {
  346. left = -1.0f;
  347. right = 1.0f;
  348. }
  349. setBalanceLeftRT(left, true);
  350. setBalanceRightRT(right, true);
  351. }
  352. }
  353. #endif
  354. // Control plugin parameters
  355. for (uint32_t k=0; k < pData->param.count; ++k)
  356. {
  357. if (pData->param.data[k].midiChannel != event.channel)
  358. continue;
  359. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  360. continue;
  361. if (pData->param.data[k].hints != PARAMETER_INPUT)
  362. continue;
  363. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  364. continue;
  365. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  366. setParameterValueRT(k, value, eventTime, true);
  367. }
  368. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  369. {
  370. fSynth.handleController(event.channel+1, ctrlEvent.param, int(ctrlEvent.normalizedValue*127.0f + 0.5f));
  371. }
  372. break;
  373. }
  374. case kEngineControlEventTypeMidiBank:
  375. case kEngineControlEventTypeMidiProgram:
  376. case kEngineControlEventTypeAllSoundOff:
  377. break;
  378. case kEngineControlEventTypeAllNotesOff:
  379. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  380. {
  381. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  382. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  383. {
  384. allNotesOffSent = true;
  385. postponeRtAllNotesOff();
  386. }
  387. #endif
  388. fSynth.allNotesOff(event.channel+1, true);
  389. }
  390. break;
  391. }
  392. break;
  393. }
  394. case kEngineEventTypeMidi: {
  395. const EngineMidiEvent& midiEvent(event.midi);
  396. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  397. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  398. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  399. continue;
  400. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  401. continue;
  402. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  403. continue;
  404. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  405. continue;
  406. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  407. continue;
  408. // Fix bad note-off
  409. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  410. status = MIDI_STATUS_NOTE_OFF;
  411. // put back channel in data
  412. uint8_t midiData2[midiEvent.size];
  413. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  414. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  415. const MidiMessage midiMessage(midiData2, static_cast<int>(midiEvent.size), 0.0);
  416. fSynth.handleMidiEvent(midiMessage);
  417. if (status == MIDI_STATUS_NOTE_ON)
  418. {
  419. pData->postponeNoteOnRtEvent(true, event.channel, midiData[1], midiData[2]);
  420. }
  421. else if (status == MIDI_STATUS_NOTE_OFF)
  422. {
  423. pData->postponeNoteOffRtEvent(true, event.channel, midiData[1]);
  424. }
  425. } break;
  426. }
  427. }
  428. pData->postRtEvents.trySplice();
  429. if (frames > timeOffset)
  430. processSingle(audioOutBuffer, frames - timeOffset, timeOffset);
  431. } // End of Event Input and Processing
  432. // --------------------------------------------------------------------------------------------------------
  433. // Parameter outputs
  434. fNumVoices = static_cast<float>(fSynth.numVoicesUsed());
  435. }
  436. bool processSingle(AudioSampleBuffer& audioOutBuffer, const uint32_t frames, const uint32_t timeOffset)
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  439. // --------------------------------------------------------------------------------------------------------
  440. // Try lock, silence otherwise
  441. #ifndef STOAT_TEST_BUILD
  442. if (pData->engine->isOffline())
  443. {
  444. pData->singleMutex.lock();
  445. }
  446. else
  447. #endif
  448. if (! pData->singleMutex.tryLock())
  449. {
  450. audioOutBuffer.clear(timeOffset, frames);
  451. return false;
  452. }
  453. // --------------------------------------------------------------------------------------------------------
  454. // Run plugin
  455. fSynth.renderVoices(audioOutBuffer, static_cast<int>(timeOffset), static_cast<int>(frames));
  456. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  457. // --------------------------------------------------------------------------------------------------------
  458. // Post-processing (dry/wet, volume and balance)
  459. {
  460. const bool doVolume = carla_isNotEqual(pData->postProc.volume, 1.0f);
  461. //const bool doBalance = carla_isNotEqual(pData->postProc.balanceLeft, -1.0f) || carla_isNotEqual(pData->postProc.balanceRight, 1.0f);
  462. float* outBufferL = audioOutBuffer.getWritePointer(0, timeOffset);
  463. float* outBufferR = audioOutBuffer.getWritePointer(1, timeOffset);
  464. #if 0
  465. if (doBalance)
  466. {
  467. float* const oldBufLeft = pData->postProc.extraBuffer;
  468. // there was a loop here
  469. {
  470. if (i % 2 == 0)
  471. carla_copyFloats(oldBufLeft, outBuffer[i], frames);
  472. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  473. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  474. for (uint32_t k=0; k < frames; ++k)
  475. {
  476. if (i % 2 == 0)
  477. {
  478. // left
  479. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  480. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  481. }
  482. else
  483. {
  484. // right
  485. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  486. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  487. }
  488. }
  489. }
  490. }
  491. #endif
  492. if (doVolume)
  493. {
  494. const float volume = pData->postProc.volume;
  495. for (uint32_t k=0; k < frames; ++k)
  496. {
  497. *outBufferL++ *= volume;
  498. *outBufferR++ *= volume;
  499. }
  500. }
  501. } // End of Post-processing
  502. #endif
  503. // --------------------------------------------------------------------------------------------------------
  504. pData->singleMutex.unlock();
  505. return true;
  506. }
  507. void sampleRateChanged(const double newSampleRate) override
  508. {
  509. fSynth.setCurrentPlaybackSampleRate(newSampleRate);
  510. }
  511. // -------------------------------------------------------------------
  512. // Plugin buffers
  513. // nothing
  514. // -------------------------------------------------------------------
  515. bool init(const CarlaPluginPtr plugin,
  516. const char* const filename, const char* const name, const char* const label, const uint options)
  517. {
  518. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  519. // ---------------------------------------------------------------
  520. // first checks
  521. if (pData->client != nullptr)
  522. {
  523. pData->engine->setLastError("Plugin client is already registered");
  524. return false;
  525. }
  526. if (filename == nullptr || filename[0] == '\0')
  527. {
  528. pData->engine->setLastError("null filename");
  529. return false;
  530. }
  531. for (int i = 128; --i >=0;)
  532. fSynth.addVoice(new sfzero::Voice());
  533. // ---------------------------------------------------------------
  534. // Init SFZero stuff
  535. fSynth.setCurrentPlaybackSampleRate(pData->engine->getSampleRate());
  536. File file(filename);
  537. sfzero::Sound* const sound = new sfzero::Sound(file);
  538. sfzero::Sound::LoadingIdleCallback cb = {
  539. loadingIdleCallbackFunction,
  540. pData->engine,
  541. };
  542. sound->loadRegions();
  543. sound->loadSamples(cb);
  544. if (fSynth.addSound(sound) == nullptr)
  545. {
  546. pData->engine->setLastError("Failed to allocate SFZ sounds in memory");
  547. return false;
  548. }
  549. sound->dumpToConsole();
  550. // ---------------------------------------------------------------
  551. const String basename(File(filename).getFileNameWithoutExtension());
  552. CarlaString label2(label != nullptr ? label : basename.toRawUTF8());
  553. fLabel = label2.dup();
  554. fRealName = carla_strdup(basename.toRawUTF8());
  555. pData->filename = carla_strdup(filename);
  556. if (name != nullptr && name[0] != '\0')
  557. pData->name = pData->engine->getUniquePluginName(name);
  558. else if (fRealName[0] != '\0')
  559. pData->name = pData->engine->getUniquePluginName(fRealName);
  560. else
  561. pData->name = pData->engine->getUniquePluginName(fLabel);
  562. // ---------------------------------------------------------------
  563. // register client
  564. pData->client = pData->engine->addClient(plugin);
  565. if (pData->client == nullptr || ! pData->client->isOk())
  566. {
  567. pData->engine->setLastError("Failed to register plugin client");
  568. return false;
  569. }
  570. // ---------------------------------------------------------------
  571. // set options
  572. pData->options = 0x0;
  573. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  574. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  575. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  576. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  577. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  578. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  579. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  580. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  581. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  582. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  583. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  584. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  585. return true;
  586. }
  587. // -------------------------------------------------------------------
  588. private:
  589. sfzero::Synth fSynth;
  590. float fNumVoices;
  591. const char* fLabel;
  592. const char* fRealName;
  593. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginSFZero)
  594. };
  595. CARLA_BACKEND_END_NAMESPACE
  596. #endif // HAVE_SFZ
  597. CARLA_BACKEND_START_NAMESPACE
  598. // -------------------------------------------------------------------------------------------------------------------
  599. CarlaPluginPtr CarlaPlugin::newSFZero(const Initializer& init)
  600. {
  601. carla_debug("CarlaPluginSFZero::newSFZero({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})",
  602. init.engine, init.filename, init.name, init.label, init.uniqueId);
  603. #ifdef HAVE_SFZ
  604. // -------------------------------------------------------------------
  605. // Check if file exists
  606. if (! water::File(init.filename).existsAsFile())
  607. {
  608. init.engine->setLastError("Requested file is not valid or does not exist");
  609. return nullptr;
  610. }
  611. std::shared_ptr<CarlaPluginSFZero> plugin(new CarlaPluginSFZero(init.engine, init.id));
  612. if (! plugin->init(plugin, init.filename, init.name, init.label, init.options))
  613. return nullptr;
  614. return plugin;
  615. #else
  616. init.engine->setLastError("SFZ support not available");
  617. return nullptr;
  618. #endif
  619. }
  620. // -------------------------------------------------------------------------------------------------------------------
  621. CARLA_BACKEND_END_NAMESPACE