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.

834 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 midi_program_t [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. allNotesOffSent = true;
  349. }
  350. break;
  351. case CarlaEngineEventAllNotesOff:
  352. if (cinEvent->channel == m_ctrlInChannel)
  353. {
  354. if (midi.portMin && ! allNotesOffSent)
  355. sendMidiAllNotesOff();
  356. allNotesOffSent = true;
  357. }
  358. break;
  359. }
  360. }
  361. } // End of Parameters Input
  362. CARLA_PROCESS_CONTINUE_CHECK;
  363. // --------------------------------------------------------------------------------------------------------
  364. // MIDI Input
  365. if (m_active && m_activeBefore)
  366. {
  367. // ----------------------------------------------------------------------------------------------------
  368. // MIDI Input (External)
  369. {
  370. engineMidiLock();
  371. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  372. {
  373. if (extMidiNotes[i].channel < 0)
  374. break;
  375. if (extMidiNotes[i].velo)
  376. midiInputPort->DispatchNoteOn(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  377. else
  378. midiInputPort->DispatchNoteOff(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  379. extMidiNotes[i].channel = -1; // mark as invalid
  380. midiEventCount += 1;
  381. }
  382. engineMidiUnlock();
  383. } // End of MIDI Input (External)
  384. CARLA_PROCESS_CONTINUE_CHECK;
  385. // ----------------------------------------------------------------------------------------------------
  386. // MIDI Input (System)
  387. {
  388. const CarlaEngineMidiEvent* minEvent;
  389. uint32_t time, nEvents = midi.portMin->getEventCount();
  390. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  391. {
  392. minEvent = midi.portMin->getEvent(i);
  393. if (! minEvent)
  394. continue;
  395. time = minEvent->time - framesOffset;
  396. if (time >= frames)
  397. continue;
  398. uint8_t status = minEvent->data[0];
  399. uint8_t channel = status & 0x0F;
  400. // Fix bad note-off
  401. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  402. status -= 0x10;
  403. if (MIDI_IS_STATUS_NOTE_OFF(status))
  404. {
  405. uint8_t note = minEvent->data[1];
  406. midiInputPort->DispatchNoteOff(note, 0, channel, time);
  407. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  408. }
  409. else if (MIDI_IS_STATUS_NOTE_ON(status))
  410. {
  411. uint8_t note = minEvent->data[1];
  412. uint8_t velo = minEvent->data[2];
  413. midiInputPort->DispatchNoteOn(note, velo, channel, time);
  414. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  415. }
  416. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  417. {
  418. //uint8_t note = minEvent->data[1];
  419. //uint8_t pressure = minEvent->data[2];
  420. // TODO, not in linuxsampler API?
  421. }
  422. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  423. {
  424. uint8_t pressure = minEvent->data[1];
  425. midiInputPort->DispatchControlChange(MIDI_STATUS_AFTERTOUCH, pressure, channel, time);
  426. }
  427. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  428. {
  429. uint8_t lsb = minEvent->data[1];
  430. uint8_t msb = minEvent->data[2];
  431. midiInputPort->DispatchPitchbend(((msb << 7) | lsb) - 8192, channel, time);
  432. }
  433. else
  434. continue;
  435. midiEventCount += 1;
  436. }
  437. } // End of MIDI Input (System)
  438. } // End of MIDI Input
  439. CARLA_PROCESS_CONTINUE_CHECK;
  440. // --------------------------------------------------------------------------------------------------------
  441. // Plugin processing
  442. if (m_active)
  443. {
  444. if (! m_activeBefore)
  445. {
  446. if (m_ctrlInChannel >= 0 && m_ctrlInChannel < 16)
  447. {
  448. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, m_ctrlInChannel);
  449. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, m_ctrlInChannel);
  450. }
  451. audioOutputDevice->Play();
  452. }
  453. audioOutputDevice->Channel(0)->SetBuffer(outBuffer[0]);
  454. audioOutputDevice->Channel(1)->SetBuffer(outBuffer[1]);
  455. // QUESTION: Need to clear it before?
  456. audioOutputDevice->Render(frames);
  457. }
  458. else
  459. {
  460. if (m_activeBefore)
  461. audioOutputDevice->Stop();
  462. }
  463. CARLA_PROCESS_CONTINUE_CHECK;
  464. // --------------------------------------------------------------------------------------------------------
  465. // Post-processing (dry/wet, volume and balance)
  466. if (m_active)
  467. {
  468. bool do_volume = x_volume != 1.0;
  469. bool do_balance = (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  470. double bal_rangeL, bal_rangeR;
  471. float oldBufLeft[do_balance ? frames : 0];
  472. for (i=0; i < aOut.count; i++)
  473. {
  474. // Balance
  475. if (do_balance)
  476. {
  477. if (i%2 == 0)
  478. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  479. bal_rangeL = (x_balanceLeft+1.0)/2;
  480. bal_rangeR = (x_balanceRight+1.0)/2;
  481. for (k=0; k < frames; k++)
  482. {
  483. if (i%2 == 0)
  484. {
  485. // left output
  486. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  487. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  488. }
  489. else
  490. {
  491. // right
  492. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  493. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  494. }
  495. }
  496. }
  497. // Volume
  498. if (do_volume)
  499. {
  500. for (k=0; k < frames; k++)
  501. outBuffer[i][k] *= x_volume;
  502. }
  503. // Output VU
  504. for (k=0; i < 2 && k < frames; k++)
  505. {
  506. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  507. aOutsPeak[i] = abs(outBuffer[i][k]);
  508. }
  509. }
  510. }
  511. else
  512. {
  513. // disable any output sound if not active
  514. for (i=0; i < aOut.count; i++)
  515. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  516. aOutsPeak[0] = 0.0;
  517. aOutsPeak[1] = 0.0;
  518. } // End of Post-processing
  519. CARLA_PROCESS_CONTINUE_CHECK;
  520. // --------------------------------------------------------------------------------------------------------
  521. // Peak Values
  522. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  523. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  524. m_activeBefore = m_active;
  525. }
  526. // -------------------------------------------------------------------
  527. bool init(const char* filename, const char* const name, const char* label)
  528. {
  529. QFileInfo file(filename);
  530. if (file.exists() && file.isFile() && file.isReadable())
  531. {
  532. const char* stype = m_isGIG ? "gig" : "sfz";
  533. try {
  534. engine = LinuxSampler::EngineFactory::Create(stype);
  535. }
  536. catch (LinuxSampler::Exception& e)
  537. {
  538. setLastError(e.what());
  539. return false;
  540. }
  541. try {
  542. instrument = engine->GetInstrumentManager();
  543. }
  544. catch (LinuxSampler::Exception& e)
  545. {
  546. setLastError(e.what());
  547. return false;
  548. }
  549. try {
  550. instrumentIds = instrument->GetInstrumentFileContent(filename);
  551. }
  552. catch (LinuxSampler::Exception& e)
  553. {
  554. setLastError(e.what());
  555. return false;
  556. }
  557. if (instrumentIds.size() > 0)
  558. {
  559. LinuxSampler::InstrumentManager::instrument_info_t info = instrument->GetInstrumentInfo(instrumentIds[0]);
  560. m_label = strdup(info.Product.c_str());
  561. m_maker = strdup(info.Artists.c_str());
  562. m_filename = strdup(filename);
  563. if (name)
  564. m_name = x_engine->getUniqueName(name);
  565. else
  566. m_name = x_engine->getUniqueName(label && label[0] ? label : info.InstrumentName.c_str());
  567. sampler_channel = sampler->AddSamplerChannel();
  568. sampler_channel->SetEngineType(stype);
  569. sampler_channel->SetAudioOutputDevice(audioOutputDevice);
  570. //sampler_channel->SetMidiInputDevice(midiInputDevice);
  571. //sampler_channel->SetMidiInputChannel(LinuxSampler::midi_chan_1);
  572. midiInputPort->Connect(sampler_channel->GetEngineChannel(), LinuxSampler::midi_chan_all);
  573. engine_channel = sampler_channel->GetEngineChannel();
  574. engine_channel->Connect(audioOutputDevice);
  575. engine_channel->PrepareLoadInstrument(filename, 0); // todo - find instrument from label
  576. engine_channel->LoadInstrument();
  577. engine_channel->Volume(LinuxSampler::VOLUME_MAX);
  578. x_client = x_engine->addClient(this);
  579. if (x_client->isOk())
  580. return true;
  581. else
  582. setLastError("Failed to register plugin client");
  583. }
  584. else
  585. setLastError("Failed to find any instruments");
  586. }
  587. else
  588. setLastError("Requested file is not valid or does not exist");
  589. return false;
  590. }
  591. // -------------------------------------------------------------------
  592. static CarlaPlugin* newLinuxSampler(const initializer& init, bool isGIG);
  593. private:
  594. LinuxSampler::Sampler* sampler;
  595. LinuxSampler::SamplerChannel* sampler_channel;
  596. LinuxSampler::Engine* engine;
  597. LinuxSampler::EngineChannel* engine_channel;
  598. LinuxSampler::InstrumentManager* instrument;
  599. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> instrumentIds;
  600. LinuxSampler::AudioOutputDevicePlugin* audioOutputDevice;
  601. LinuxSampler::MidiInputDevicePlugin* midiInputDevice;
  602. LinuxSampler::MidiInputPort* midiInputPort;
  603. bool m_isGIG;
  604. const char* m_label;
  605. const char* m_maker;
  606. };
  607. CarlaPlugin* LinuxSamplerPlugin::newLinuxSampler(const initializer& init, bool isGIG)
  608. {
  609. qDebug("LinuxSamplerPlugin::newLinuxSampler(%p, \"%s\", \"%s\", \"%s\", %s)", init.engine, init.filename, init.name, init.label, bool2str(isGIG));
  610. short id = init.engine->getNewPluginId();
  611. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  612. {
  613. setLastError("Maximum number of plugins reached");
  614. return nullptr;
  615. }
  616. LinuxSamplerPlugin* const plugin = new LinuxSamplerPlugin(init.engine, id, isGIG);
  617. if (! plugin->init(init.filename, init.name, init.label))
  618. {
  619. delete plugin;
  620. return nullptr;
  621. }
  622. plugin->reload();
  623. plugin->registerToOscControl();
  624. return plugin;
  625. }
  626. /**@}*/
  627. CARLA_BACKEND_END_NAMESPACE
  628. #else // WANT_LINUXSAMPLER
  629. # warning linuxsampler not available (no GIG and SFZ support)
  630. #endif
  631. CARLA_BACKEND_START_NAMESPACE
  632. CarlaPlugin* CarlaPlugin::newGIG(const initializer& init)
  633. {
  634. qDebug("CarlaPlugin::newGIG(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  635. #ifdef WANT_LINUXSAMPLER
  636. return LinuxSamplerPlugin::newLinuxSampler(init, true);
  637. #else
  638. setLastError("linuxsampler support not available");
  639. return nullptr;
  640. #endif
  641. }
  642. CarlaPlugin* CarlaPlugin::newSFZ(const initializer& init)
  643. {
  644. qDebug("CarlaPlugin::newSFZ(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  645. #ifdef WANT_LINUXSAMPLER
  646. return LinuxSamplerPlugin::newLinuxSampler(init, false);
  647. #else
  648. setLastError("linuxsampler support not available");
  649. return nullptr;
  650. #endif
  651. }
  652. CARLA_BACKEND_END_NAMESPACE