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