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.

CarlaPluginSFZero.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. if (midiEvent.size > EngineMidiEvent::kDataSize)
  397. continue;
  398. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  399. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  400. continue;
  401. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  402. continue;
  403. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  404. continue;
  405. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  406. continue;
  407. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  408. continue;
  409. // Fix bad note-off
  410. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  411. status = MIDI_STATUS_NOTE_OFF;
  412. // put back channel in data
  413. uint8_t midiData2[EngineMidiEvent::kDataSize];
  414. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  415. std::memcpy(midiData2 + 1, midiEvent.data + 1, static_cast<std::size_t>(midiEvent.size - 1));
  416. const MidiMessage midiMessage(midiData2, static_cast<int>(midiEvent.size), 0.0);
  417. fSynth.handleMidiEvent(midiMessage);
  418. if (status == MIDI_STATUS_NOTE_ON)
  419. {
  420. pData->postponeNoteOnRtEvent(true, event.channel, midiEvent.data[1], midiEvent.data[2]);
  421. }
  422. else if (status == MIDI_STATUS_NOTE_OFF)
  423. {
  424. pData->postponeNoteOffRtEvent(true, event.channel, midiEvent.data[1]);
  425. }
  426. } break;
  427. }
  428. }
  429. pData->postRtEvents.trySplice();
  430. if (frames > timeOffset)
  431. processSingle(audioOutBuffer, frames - timeOffset, timeOffset);
  432. } // End of Event Input and Processing
  433. // --------------------------------------------------------------------------------------------------------
  434. // Parameter outputs
  435. fNumVoices = static_cast<float>(fSynth.numVoicesUsed());
  436. }
  437. bool processSingle(AudioSampleBuffer& audioOutBuffer, const uint32_t frames, const uint32_t timeOffset)
  438. {
  439. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  440. // --------------------------------------------------------------------------------------------------------
  441. // Try lock, silence otherwise
  442. #ifndef STOAT_TEST_BUILD
  443. if (pData->engine->isOffline())
  444. {
  445. pData->singleMutex.lock();
  446. }
  447. else
  448. #endif
  449. if (! pData->singleMutex.tryLock())
  450. {
  451. audioOutBuffer.clear(timeOffset, frames);
  452. return false;
  453. }
  454. // --------------------------------------------------------------------------------------------------------
  455. // Run plugin
  456. fSynth.renderVoices(audioOutBuffer, static_cast<int>(timeOffset), static_cast<int>(frames));
  457. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  458. // --------------------------------------------------------------------------------------------------------
  459. // Post-processing (dry/wet, volume and balance)
  460. {
  461. const bool doVolume = carla_isNotEqual(pData->postProc.volume, 1.0f);
  462. //const bool doBalance = carla_isNotEqual(pData->postProc.balanceLeft, -1.0f) || carla_isNotEqual(pData->postProc.balanceRight, 1.0f);
  463. float* outBufferL = audioOutBuffer.getWritePointer(0, timeOffset);
  464. float* outBufferR = audioOutBuffer.getWritePointer(1, timeOffset);
  465. #if 0
  466. if (doBalance)
  467. {
  468. float* const oldBufLeft = pData->postProc.extraBuffer;
  469. // there was a loop here
  470. {
  471. if (i % 2 == 0)
  472. carla_copyFloats(oldBufLeft, outBuffer[i], frames);
  473. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  474. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  475. for (uint32_t k=0; k < frames; ++k)
  476. {
  477. if (i % 2 == 0)
  478. {
  479. // left
  480. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  481. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  482. }
  483. else
  484. {
  485. // right
  486. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  487. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  488. }
  489. }
  490. }
  491. }
  492. #endif
  493. if (doVolume)
  494. {
  495. const float volume = pData->postProc.volume;
  496. for (uint32_t k=0; k < frames; ++k)
  497. {
  498. *outBufferL++ *= volume;
  499. *outBufferR++ *= volume;
  500. }
  501. }
  502. } // End of Post-processing
  503. #endif
  504. // --------------------------------------------------------------------------------------------------------
  505. pData->singleMutex.unlock();
  506. return true;
  507. }
  508. void sampleRateChanged(const double newSampleRate) override
  509. {
  510. fSynth.setCurrentPlaybackSampleRate(newSampleRate);
  511. }
  512. // -------------------------------------------------------------------
  513. // Plugin buffers
  514. // nothing
  515. // -------------------------------------------------------------------
  516. bool init(const CarlaPluginPtr plugin,
  517. const char* const filename, const char* const name, const char* const label, const uint options)
  518. {
  519. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  520. // ---------------------------------------------------------------
  521. // first checks
  522. if (pData->client != nullptr)
  523. {
  524. pData->engine->setLastError("Plugin client is already registered");
  525. return false;
  526. }
  527. if (filename == nullptr || filename[0] == '\0')
  528. {
  529. pData->engine->setLastError("null filename");
  530. return false;
  531. }
  532. for (int i = 128; --i >=0;)
  533. fSynth.addVoice(new sfzero::Voice());
  534. // ---------------------------------------------------------------
  535. // Init SFZero stuff
  536. fSynth.setCurrentPlaybackSampleRate(pData->engine->getSampleRate());
  537. File file(filename);
  538. sfzero::Sound* const sound = new sfzero::Sound(file);
  539. sfzero::Sound::LoadingIdleCallback cb = {
  540. loadingIdleCallbackFunction,
  541. pData->engine,
  542. };
  543. sound->loadRegions();
  544. sound->loadSamples(cb);
  545. if (fSynth.addSound(sound) == nullptr)
  546. {
  547. pData->engine->setLastError("Failed to allocate SFZ sounds in memory");
  548. return false;
  549. }
  550. sound->dumpToConsole();
  551. // ---------------------------------------------------------------
  552. const String basename(File(filename).getFileNameWithoutExtension());
  553. CarlaString label2(label != nullptr ? label : basename.toRawUTF8());
  554. fLabel = label2.dup();
  555. fRealName = carla_strdup(basename.toRawUTF8());
  556. pData->filename = carla_strdup(filename);
  557. if (name != nullptr && name[0] != '\0')
  558. pData->name = pData->engine->getUniquePluginName(name);
  559. else if (fRealName[0] != '\0')
  560. pData->name = pData->engine->getUniquePluginName(fRealName);
  561. else
  562. pData->name = pData->engine->getUniquePluginName(fLabel);
  563. // ---------------------------------------------------------------
  564. // register client
  565. pData->client = pData->engine->addClient(plugin);
  566. if (pData->client == nullptr || ! pData->client->isOk())
  567. {
  568. pData->engine->setLastError("Failed to register plugin client");
  569. return false;
  570. }
  571. // ---------------------------------------------------------------
  572. // set options
  573. pData->options = 0x0;
  574. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  575. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  576. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  577. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  578. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  579. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  580. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  581. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  582. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  583. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  584. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  585. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  586. return true;
  587. }
  588. // -------------------------------------------------------------------
  589. private:
  590. sfzero::Synth fSynth;
  591. float fNumVoices;
  592. const char* fLabel;
  593. const char* fRealName;
  594. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginSFZero)
  595. };
  596. CARLA_BACKEND_END_NAMESPACE
  597. #endif // HAVE_SFZ
  598. CARLA_BACKEND_START_NAMESPACE
  599. // -------------------------------------------------------------------------------------------------------------------
  600. CarlaPluginPtr CarlaPlugin::newSFZero(const Initializer& init)
  601. {
  602. carla_debug("CarlaPluginSFZero::newSFZero({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})",
  603. init.engine, init.filename, init.name, init.label, init.uniqueId);
  604. #ifdef HAVE_SFZ
  605. // -------------------------------------------------------------------
  606. // Check if file exists
  607. if (! water::File(init.filename).existsAsFile())
  608. {
  609. init.engine->setLastError("Requested file is not valid or does not exist");
  610. return nullptr;
  611. }
  612. std::shared_ptr<CarlaPluginSFZero> plugin(new CarlaPluginSFZero(init.engine, init.id));
  613. if (! plugin->init(plugin, init.filename, init.name, init.label, init.options))
  614. return nullptr;
  615. return plugin;
  616. #else
  617. init.engine->setLastError("SFZ support not available");
  618. return nullptr;
  619. #endif
  620. }
  621. // -------------------------------------------------------------------------------------------------------------------
  622. CARLA_BACKEND_END_NAMESPACE