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

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