Collection of tools useful for audio production
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.

837 lines
27KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. // TODO - setMidiProgram()
  18. #ifdef BUILD_BRIDGE
  19. # error Should not use linuxsampler for bridges!
  20. #endif
  21. #include "carla_plugin.h"
  22. #ifdef WANT_LINUXSAMPLER
  23. #include "carla_linuxsampler.h"
  24. #include <QtCore/QFileInfo>
  25. CARLA_BACKEND_START_NAMESPACE
  26. /*!
  27. * @defgroup CarlaBackendLinuxSamplerPlugin Carla Backend LinuxSampler Plugin
  28. *
  29. * The Carla Backend LinuxSampler Plugin.\n
  30. * http://www.linuxsampler.org/
  31. * @{
  32. */
  33. class LinuxSamplerPlugin : public CarlaPlugin
  34. {
  35. public:
  36. LinuxSamplerPlugin(CarlaEngine* const engine_, const unsigned short id, const bool isGIG)
  37. : CarlaPlugin(engine_, id)
  38. {
  39. qDebug("LinuxSamplerPlugin::LinuxSamplerPlugin()");
  40. m_type = isGIG ? PLUGIN_GIG : PLUGIN_SFZ;
  41. sampler = new LinuxSampler::Sampler;
  42. sampler_channel = nullptr;
  43. engine = nullptr;
  44. engine_channel = nullptr;
  45. instrument = nullptr;
  46. audioOutputDevice = new LinuxSampler::AudioOutputDevicePlugin(engine_, this);
  47. midiInputDevice = new LinuxSampler::MidiInputDevicePlugin(sampler);
  48. midiInputPort = midiInputDevice->CreateMidiPort();
  49. m_isGIG = isGIG;
  50. m_label = nullptr;
  51. m_maker = nullptr;
  52. }
  53. ~LinuxSamplerPlugin()
  54. {
  55. qDebug("LinuxSamplerPlugin::~LinuxSamplerPlugin()");
  56. if (m_activeBefore)
  57. audioOutputDevice->Stop();
  58. if (sampler_channel)
  59. {
  60. midiInputPort->Disconnect(sampler_channel->GetEngineChannel());
  61. sampler->RemoveSamplerChannel(sampler_channel);
  62. }
  63. midiInputDevice->DeleteMidiPort(midiInputPort);
  64. delete audioOutputDevice;
  65. delete midiInputDevice;
  66. delete sampler;
  67. instrumentIds.clear();
  68. if (m_label)
  69. free((void*)m_label);
  70. if (m_maker)
  71. free((void*)m_maker);
  72. }
  73. // -------------------------------------------------------------------
  74. // Information (base)
  75. PluginCategory category()
  76. {
  77. return PLUGIN_CATEGORY_SYNTH;
  78. }
  79. // -------------------------------------------------------------------
  80. // Information (per-plugin data)
  81. void getLabel(char* const strBuf)
  82. {
  83. strncpy(strBuf, m_label, STR_MAX);
  84. }
  85. void getMaker(char* const strBuf)
  86. {
  87. strncpy(strBuf, m_maker, STR_MAX);
  88. }
  89. void getCopyright(char* const strBuf)
  90. {
  91. getMaker(strBuf);
  92. }
  93. void getRealName(char* const strBuf)
  94. {
  95. strncpy(strBuf, m_name, STR_MAX);
  96. }
  97. // -------------------------------------------------------------------
  98. // Plugin state
  99. void reload()
  100. {
  101. qDebug("LinuxSamplerPlugin::reload() - start");
  102. CARLA_ASSERT(instrument);
  103. // Safely disable plugin for reload
  104. const ScopedDisabler m(this);
  105. if (x_client->isActive())
  106. x_client->deactivate();
  107. // Remove client ports
  108. removeClientPorts();
  109. // Delete old data
  110. deleteBuffers();
  111. uint32_t aOuts;
  112. aOuts = 2;
  113. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  114. aOut.rindexes = new uint32_t[aOuts];
  115. const int portNameSize = CarlaEngine::maxPortNameSize() - 1;
  116. char portName[portNameSize];
  117. // ---------------------------------------
  118. // Audio Outputs
  119. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  120. {
  121. strcpy(portName, m_name);
  122. strcat(portName, ":out-left");
  123. }
  124. else
  125. strcpy(portName, "out-left");
  126. aOut.ports[0] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  127. aOut.rindexes[0] = 0;
  128. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  129. {
  130. strcpy(portName, m_name);
  131. strcat(portName, ":out-right");
  132. }
  133. else
  134. strcpy(portName, "out-right");
  135. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  136. aOut.rindexes[1] = 1;
  137. // ---------------------------------------
  138. // Control Input
  139. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  140. {
  141. strcpy(portName, m_name);
  142. strcat(portName, ":control-in");
  143. }
  144. else
  145. strcpy(portName, "control-in");
  146. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  147. // ---------------------------------------
  148. // MIDI Input
  149. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  150. {
  151. strcpy(portName, m_name);
  152. strcat(portName, ":midi-in");
  153. }
  154. else
  155. strcpy(portName, "midi-in");
  156. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  157. // ---------------------------------------
  158. aOut.count = aOuts;
  159. // plugin checks
  160. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  161. m_hints |= PLUGIN_IS_SYNTH;
  162. m_hints |= PLUGIN_CAN_VOLUME;
  163. m_hints |= PLUGIN_CAN_BALANCE;
  164. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  165. reloadPrograms(true);
  166. x_client->activate();
  167. qDebug("LinuxSamplerPlugin::reload() - end");
  168. }
  169. void reloadPrograms(bool init)
  170. {
  171. qDebug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(init));
  172. // Delete old programs
  173. if (midiprog.count > 0)
  174. {
  175. for (uint32_t i=0; i < midiprog.count; i++)
  176. free((void*)midiprog.data[i].name);
  177. delete[] midiprog.data;
  178. }
  179. midiprog.count = 0;
  180. midiprog.data = nullptr;
  181. // Query new programs
  182. uint32_t i = 0;
  183. midiprog.count += instrumentIds.size();
  184. // sound kits must always have at least 1 midi-program
  185. CARLA_ASSERT(midiprog.count > 0);
  186. if (midiprog.count > 0)
  187. midiprog.data = new MidiProgramData[midiprog.count];
  188. // Update data
  189. for (i=0; i < midiprog.count; i++)
  190. {
  191. LinuxSampler::InstrumentManager::instrument_info_t info = instrument->GetInstrumentInfo(instrumentIds[i]);
  192. // FIXME - use % 128 stuff
  193. midiprog.data[i].bank = 0;
  194. midiprog.data[i].program = i;
  195. midiprog.data[i].name = strdup(info.InstrumentName.c_str());
  196. }
  197. #ifndef BUILD_BRIDGE
  198. // Update OSC Names
  199. if (x_engine->isOscControlRegisted())
  200. {
  201. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  202. for (i=0; i < midiprog.count; i++)
  203. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  204. }
  205. #endif
  206. if (init)
  207. {
  208. if (midiprog.count > 0)
  209. setMidiProgram(0, false, false, false, true);
  210. }
  211. else
  212. {
  213. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  214. }
  215. }
  216. // -------------------------------------------------------------------
  217. // Plugin processing
  218. void process(float** const, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  219. {
  220. uint32_t i, k;
  221. uint32_t midiEventCount = 0;
  222. double aOutsPeak[2] = { 0.0 };
  223. CARLA_PROCESS_CONTINUE_CHECK;
  224. // --------------------------------------------------------------------------------------------------------
  225. // Parameters Input [Automation]
  226. if (m_active && m_activeBefore)
  227. {
  228. bool allNotesOffSent = false;
  229. const CarlaEngineControlEvent* cinEvent;
  230. uint32_t time, nEvents = param.portCin->getEventCount();
  231. uint32_t nextBankId = 0;
  232. if (midiprog.current >= 0 && midiprog.count > 0)
  233. nextBankId = midiprog.data[midiprog.current].bank;
  234. for (i=0; i < nEvents; i++)
  235. {
  236. cinEvent = param.portCin->getEvent(i);
  237. if (! cinEvent)
  238. continue;
  239. time = cinEvent->time - framesOffset;
  240. if (time >= frames)
  241. continue;
  242. // Control change
  243. switch (cinEvent->type)
  244. {
  245. case CarlaEngineEventNull:
  246. break;
  247. case CarlaEngineEventControlChange:
  248. {
  249. double value;
  250. // Control backend stuff
  251. if (cinEvent->channel == m_ctrlInChannel)
  252. {
  253. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  254. {
  255. value = cinEvent->value;
  256. setDryWet(value, false, false);
  257. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  258. continue;
  259. }
  260. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  261. {
  262. value = cinEvent->value*127/100;
  263. setVolume(value, false, false);
  264. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  265. continue;
  266. }
  267. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  268. {
  269. double left, right;
  270. value = cinEvent->value/0.5 - 1.0;
  271. if (value < 0.0)
  272. {
  273. left = -1.0;
  274. right = (value*2)+1.0;
  275. }
  276. else if (value > 0.0)
  277. {
  278. left = (value*2)-1.0;
  279. right = 1.0;
  280. }
  281. else
  282. {
  283. left = -1.0;
  284. right = 1.0;
  285. }
  286. setBalanceLeft(left, false, false);
  287. setBalanceRight(right, false, false);
  288. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  289. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  290. continue;
  291. }
  292. }
  293. #if 0
  294. // Control plugin parameters
  295. for (k=0; k < param.count; k++)
  296. {
  297. if (param.data[k].midiChannel != cinEvent->channel)
  298. continue;
  299. if (param.data[k].midiCC != cinEvent->controller)
  300. continue;
  301. if (param.data[k].type != PARAMETER_INPUT)
  302. continue;
  303. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  304. {
  305. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  306. {
  307. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  308. }
  309. else
  310. {
  311. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  312. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  313. value = rint(value);
  314. }
  315. setParameterValue(k, value, false, false, false);
  316. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  317. }
  318. }
  319. #endif
  320. break;
  321. }
  322. case CarlaEngineEventMidiBankChange:
  323. if (cinEvent->channel == m_ctrlInChannel)
  324. nextBankId = rint(cinEvent->value);
  325. break;
  326. case CarlaEngineEventMidiProgramChange:
  327. if (cinEvent->channel == m_ctrlInChannel)
  328. {
  329. uint32_t nextProgramId = rint(cinEvent->value);
  330. for (k=0; k < midiprog.count; k++)
  331. {
  332. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  333. {
  334. setMidiProgram(k, false, false, false, false);
  335. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  336. break;
  337. }
  338. }
  339. }
  340. break;
  341. case CarlaEngineEventAllSoundOff:
  342. if (cinEvent->channel == m_ctrlInChannel)
  343. {
  344. if (midi.portMin && ! allNotesOffSent)
  345. sendMidiAllNotesOff();
  346. audioOutputDevice->Stop();
  347. audioOutputDevice->Play();
  348. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  349. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  350. allNotesOffSent = true;
  351. }
  352. break;
  353. case CarlaEngineEventAllNotesOff:
  354. if (cinEvent->channel == m_ctrlInChannel)
  355. {
  356. if (midi.portMin && ! allNotesOffSent)
  357. sendMidiAllNotesOff();
  358. allNotesOffSent = true;
  359. }
  360. break;
  361. }
  362. }
  363. } // End of Parameters Input
  364. CARLA_PROCESS_CONTINUE_CHECK;
  365. // --------------------------------------------------------------------------------------------------------
  366. // MIDI Input
  367. if (m_active && m_activeBefore)
  368. {
  369. // ----------------------------------------------------------------------------------------------------
  370. // MIDI Input (External)
  371. {
  372. engineMidiLock();
  373. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  374. {
  375. if (extMidiNotes[i].channel < 0)
  376. break;
  377. if (extMidiNotes[i].velo)
  378. midiInputPort->DispatchNoteOn(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  379. else
  380. midiInputPort->DispatchNoteOff(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  381. extMidiNotes[i].channel = -1; // mark as invalid
  382. midiEventCount += 1;
  383. }
  384. engineMidiUnlock();
  385. } // End of MIDI Input (External)
  386. CARLA_PROCESS_CONTINUE_CHECK;
  387. // ----------------------------------------------------------------------------------------------------
  388. // MIDI Input (System)
  389. {
  390. const CarlaEngineMidiEvent* minEvent;
  391. uint32_t time, nEvents = midi.portMin->getEventCount();
  392. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  393. {
  394. minEvent = midi.portMin->getEvent(i);
  395. if (! minEvent)
  396. continue;
  397. time = minEvent->time - framesOffset;
  398. if (time >= frames)
  399. continue;
  400. uint8_t status = minEvent->data[0];
  401. uint8_t channel = status & 0x0F;
  402. // Fix bad note-off
  403. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  404. status -= 0x10;
  405. if (MIDI_IS_STATUS_NOTE_OFF(status))
  406. {
  407. uint8_t note = minEvent->data[1];
  408. midiInputPort->DispatchNoteOff(note, 0, channel, time);
  409. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  410. }
  411. else if (MIDI_IS_STATUS_NOTE_ON(status))
  412. {
  413. uint8_t note = minEvent->data[1];
  414. uint8_t velo = minEvent->data[2];
  415. midiInputPort->DispatchNoteOn(note, velo, channel, time);
  416. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  417. }
  418. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  419. {
  420. //uint8_t note = minEvent->data[1];
  421. //uint8_t pressure = minEvent->data[2];
  422. // TODO, not in linuxsampler API?
  423. }
  424. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  425. {
  426. uint8_t pressure = minEvent->data[1];
  427. midiInputPort->DispatchControlChange(MIDI_STATUS_AFTERTOUCH, pressure, channel, time);
  428. }
  429. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  430. {
  431. uint8_t lsb = minEvent->data[1];
  432. uint8_t msb = minEvent->data[2];
  433. midiInputPort->DispatchPitchbend(((msb << 7) | lsb) - 8192, channel, time);
  434. }
  435. else
  436. continue;
  437. midiEventCount += 1;
  438. }
  439. } // End of MIDI Input (System)
  440. } // End of MIDI Input
  441. CARLA_PROCESS_CONTINUE_CHECK;
  442. // --------------------------------------------------------------------------------------------------------
  443. // Plugin processing
  444. if (m_active)
  445. {
  446. if (! m_activeBefore)
  447. {
  448. for (int c=0; c < MAX_MIDI_CHANNELS; c++)
  449. {
  450. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, c);
  451. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, c);
  452. }
  453. audioOutputDevice->Play();
  454. }
  455. audioOutputDevice->Channel(0)->SetBuffer(outBuffer[0]);
  456. audioOutputDevice->Channel(1)->SetBuffer(outBuffer[1]);
  457. // QUESTION: Need to clear it before?
  458. audioOutputDevice->Render(frames);
  459. }
  460. else
  461. {
  462. if (m_activeBefore)
  463. audioOutputDevice->Stop();
  464. }
  465. CARLA_PROCESS_CONTINUE_CHECK;
  466. // --------------------------------------------------------------------------------------------------------
  467. // Post-processing (dry/wet, volume and balance)
  468. if (m_active)
  469. {
  470. bool do_volume = x_volume != 1.0;
  471. bool do_balance = (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  472. double bal_rangeL, bal_rangeR;
  473. float oldBufLeft[do_balance ? frames : 0];
  474. for (i=0; i < aOut.count; i++)
  475. {
  476. // Balance
  477. if (do_balance)
  478. {
  479. if (i%2 == 0)
  480. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  481. bal_rangeL = (x_balanceLeft+1.0)/2;
  482. bal_rangeR = (x_balanceRight+1.0)/2;
  483. for (k=0; k < frames; k++)
  484. {
  485. if (i%2 == 0)
  486. {
  487. // left output
  488. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  489. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  490. }
  491. else
  492. {
  493. // right
  494. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  495. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  496. }
  497. }
  498. }
  499. // Volume
  500. if (do_volume)
  501. {
  502. for (k=0; k < frames; k++)
  503. outBuffer[i][k] *= x_volume;
  504. }
  505. // Output VU
  506. for (k=0; i < 2 && k < frames; k++)
  507. {
  508. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  509. aOutsPeak[i] = abs(outBuffer[i][k]);
  510. }
  511. }
  512. }
  513. else
  514. {
  515. // disable any output sound if not active
  516. for (i=0; i < aOut.count; i++)
  517. zeroF(outBuffer[i], frames);
  518. aOutsPeak[0] = 0.0;
  519. aOutsPeak[1] = 0.0;
  520. } // End of Post-processing
  521. CARLA_PROCESS_CONTINUE_CHECK;
  522. // --------------------------------------------------------------------------------------------------------
  523. // Peak Values
  524. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  525. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  526. m_activeBefore = m_active;
  527. }
  528. // -------------------------------------------------------------------
  529. bool init(const char* filename, const char* const name, const char* label)
  530. {
  531. QFileInfo file(filename);
  532. if (file.exists() && file.isFile() && file.isReadable())
  533. {
  534. const char* stype = m_isGIG ? "gig" : "sfz";
  535. try {
  536. engine = LinuxSampler::EngineFactory::Create(stype);
  537. }
  538. catch (LinuxSampler::Exception& e)
  539. {
  540. setLastError(e.what());
  541. return false;
  542. }
  543. try {
  544. instrument = engine->GetInstrumentManager();
  545. }
  546. catch (LinuxSampler::Exception& e)
  547. {
  548. setLastError(e.what());
  549. return false;
  550. }
  551. try {
  552. instrumentIds = instrument->GetInstrumentFileContent(filename);
  553. }
  554. catch (LinuxSampler::Exception& e)
  555. {
  556. setLastError(e.what());
  557. return false;
  558. }
  559. if (instrumentIds.size() > 0)
  560. {
  561. LinuxSampler::InstrumentManager::instrument_info_t info = instrument->GetInstrumentInfo(instrumentIds[0]);
  562. m_label = strdup(info.Product.c_str());
  563. m_maker = strdup(info.Artists.c_str());
  564. m_filename = strdup(filename);
  565. if (name)
  566. m_name = x_engine->getUniqueName(name);
  567. else
  568. m_name = x_engine->getUniqueName(label && label[0] ? label : info.InstrumentName.c_str());
  569. sampler_channel = sampler->AddSamplerChannel();
  570. sampler_channel->SetEngineType(stype);
  571. sampler_channel->SetAudioOutputDevice(audioOutputDevice);
  572. //sampler_channel->SetMidiInputDevice(midiInputDevice);
  573. //sampler_channel->SetMidiInputChannel(LinuxSampler::midi_chan_1);
  574. midiInputPort->Connect(sampler_channel->GetEngineChannel(), LinuxSampler::midi_chan_all);
  575. engine_channel = sampler_channel->GetEngineChannel();
  576. engine_channel->Connect(audioOutputDevice);
  577. engine_channel->PrepareLoadInstrument(filename, 0); // todo - find instrument from label
  578. engine_channel->LoadInstrument();
  579. engine_channel->Volume(LinuxSampler::VOLUME_MAX);
  580. x_client = x_engine->addClient(this);
  581. if (x_client->isOk())
  582. return true;
  583. else
  584. setLastError("Failed to register plugin client");
  585. }
  586. else
  587. setLastError("Failed to find any instruments");
  588. }
  589. else
  590. setLastError("Requested file is not valid or does not exist");
  591. return false;
  592. }
  593. // -------------------------------------------------------------------
  594. static CarlaPlugin* newLinuxSampler(const initializer& init, bool isGIG);
  595. private:
  596. LinuxSampler::Sampler* sampler;
  597. LinuxSampler::SamplerChannel* sampler_channel;
  598. LinuxSampler::Engine* engine;
  599. LinuxSampler::EngineChannel* engine_channel;
  600. LinuxSampler::InstrumentManager* instrument;
  601. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> instrumentIds;
  602. LinuxSampler::AudioOutputDevicePlugin* audioOutputDevice;
  603. LinuxSampler::MidiInputDevicePlugin* midiInputDevice;
  604. LinuxSampler::MidiInputPort* midiInputPort;
  605. bool m_isGIG;
  606. const char* m_label;
  607. const char* m_maker;
  608. };
  609. CarlaPlugin* LinuxSamplerPlugin::newLinuxSampler(const initializer& init, bool isGIG)
  610. {
  611. qDebug("LinuxSamplerPlugin::newLinuxSampler(%p, \"%s\", \"%s\", \"%s\", %s)", init.engine, init.filename, init.name, init.label, bool2str(isGIG));
  612. short id = init.engine->getNewPluginId();
  613. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  614. {
  615. setLastError("Maximum number of plugins reached");
  616. return nullptr;
  617. }
  618. LinuxSamplerPlugin* const plugin = new LinuxSamplerPlugin(init.engine, id, isGIG);
  619. if (! plugin->init(init.filename, init.name, init.label))
  620. {
  621. delete plugin;
  622. return nullptr;
  623. }
  624. plugin->reload();
  625. plugin->registerToOscControl();
  626. return plugin;
  627. }
  628. /**@}*/
  629. CARLA_BACKEND_END_NAMESPACE
  630. #else // WANT_LINUXSAMPLER
  631. # warning linuxsampler not available (no GIG and SFZ support)
  632. #endif
  633. CARLA_BACKEND_START_NAMESPACE
  634. CarlaPlugin* CarlaPlugin::newGIG(const initializer& init)
  635. {
  636. qDebug("CarlaPlugin::newGIG(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  637. #ifdef WANT_LINUXSAMPLER
  638. return LinuxSamplerPlugin::newLinuxSampler(init, true);
  639. #else
  640. setLastError("linuxsampler support not available");
  641. return nullptr;
  642. #endif
  643. }
  644. CarlaPlugin* CarlaPlugin::newSFZ(const initializer& init)
  645. {
  646. qDebug("CarlaPlugin::newSFZ(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  647. #ifdef WANT_LINUXSAMPLER
  648. return LinuxSamplerPlugin::newLinuxSampler(init, false);
  649. #else
  650. setLastError("linuxsampler support not available");
  651. return nullptr;
  652. #endif
  653. }
  654. CARLA_BACKEND_END_NAMESPACE