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.

992 lines
31KB

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