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.

988 lines
31KB

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