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

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