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.

990 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. // Safely disable plugin for reload
  207. const ScopedDisabler m(this);
  208. if (x_client->isActive())
  209. x_client->deactivate();
  210. // Remove client ports
  211. removeClientPorts();
  212. // Delete old data
  213. deleteBuffers();
  214. uint32_t aOuts;
  215. aOuts = 2;
  216. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  217. aOut.rindexes = new uint32_t[aOuts];
  218. const int portNameSize = x_engine->maxPortNameSize();
  219. CarlaString portName;
  220. // ---------------------------------------
  221. // Audio Outputs
  222. {
  223. portName.clear();
  224. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  225. {
  226. portName = m_name;
  227. portName += ":";
  228. }
  229. portName += "out-left";
  230. portName.truncate(portNameSize);
  231. aOut.ports[0] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  232. aOut.rindexes[0] = 0;
  233. }
  234. {
  235. portName.clear();
  236. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  237. {
  238. portName = m_name;
  239. portName += ":";
  240. }
  241. portName += "out-right";
  242. portName.truncate(portNameSize);
  243. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  244. aOut.rindexes[1] = 1;
  245. }
  246. // ---------------------------------------
  247. // Control Input
  248. {
  249. portName.clear();
  250. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  251. {
  252. portName = m_name;
  253. portName += ":";
  254. }
  255. portName += "control-in";
  256. portName.truncate(portNameSize);
  257. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  258. }
  259. // ---------------------------------------
  260. // MIDI Input
  261. {
  262. portName.clear();
  263. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  264. {
  265. portName = m_name;
  266. portName += ":";
  267. }
  268. portName += "midi-in";
  269. portName.truncate(portNameSize);
  270. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  271. }
  272. // ---------------------------------------
  273. aOut.count = aOuts;
  274. // plugin checks
  275. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  276. m_hints |= PLUGIN_IS_SYNTH;
  277. m_hints |= PLUGIN_CAN_VOLUME;
  278. m_hints |= PLUGIN_CAN_BALANCE;
  279. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  280. reloadPrograms(true);
  281. x_client->activate();
  282. qDebug("LinuxSamplerPlugin::reload() - end");
  283. }
  284. void reloadPrograms(bool init)
  285. {
  286. qDebug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(init));
  287. // Delete old programs
  288. if (midiprog.count > 0)
  289. {
  290. for (uint32_t i=0; i < midiprog.count; i++)
  291. free((void*)midiprog.data[i].name);
  292. delete[] midiprog.data;
  293. }
  294. midiprog.count = 0;
  295. midiprog.data = nullptr;
  296. // Query new programs
  297. uint32_t i = 0;
  298. midiprog.count += instrumentIds.size();
  299. // sound kits must always have at least 1 midi-program
  300. CARLA_ASSERT(midiprog.count > 0);
  301. if (midiprog.count > 0)
  302. midiprog.data = new MidiProgramData[midiprog.count];
  303. // Update data
  304. for (i=0; i < midiprog.count; i++)
  305. {
  306. LinuxSampler::InstrumentManager::instrument_info_t info = instrument->GetInstrumentInfo(instrumentIds[i]);
  307. // FIXME - use % 128 stuff
  308. midiprog.data[i].bank = 0;
  309. midiprog.data[i].program = i;
  310. midiprog.data[i].name = strdup(info.InstrumentName.c_str());
  311. }
  312. // Update OSC Names
  313. if (x_engine->isOscControlRegisted())
  314. {
  315. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  316. for (i=0; i < midiprog.count; i++)
  317. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  318. }
  319. if (init)
  320. {
  321. if (midiprog.count > 0)
  322. setMidiProgram(0, false, false, false, true);
  323. }
  324. else
  325. {
  326. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  327. }
  328. }
  329. // -------------------------------------------------------------------
  330. // Plugin processing
  331. void process(float** const, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  332. {
  333. uint32_t i, k;
  334. uint32_t midiEventCount = 0;
  335. double aOutsPeak[2] = { 0.0 };
  336. CARLA_PROCESS_CONTINUE_CHECK;
  337. // --------------------------------------------------------------------------------------------------------
  338. // Parameters Input [Automation]
  339. if (m_active && m_activeBefore)
  340. {
  341. bool allNotesOffSent = false;
  342. const CarlaEngineControlEvent* cinEvent;
  343. uint32_t time, nEvents = param.portCin->getEventCount();
  344. uint32_t nextBankId = 0;
  345. if (midiprog.current >= 0 && midiprog.count > 0)
  346. nextBankId = midiprog.data[midiprog.current].bank;
  347. for (i=0; i < nEvents; i++)
  348. {
  349. cinEvent = param.portCin->getEvent(i);
  350. if (! cinEvent)
  351. continue;
  352. time = cinEvent->time - framesOffset;
  353. if (time >= frames)
  354. continue;
  355. // Control change
  356. switch (cinEvent->type)
  357. {
  358. case CarlaEngineNullEvent:
  359. break;
  360. case CarlaEngineParameterChangeEvent:
  361. {
  362. double value;
  363. // Control backend stuff
  364. if (cinEvent->channel == m_ctrlInChannel)
  365. {
  366. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  367. {
  368. value = cinEvent->value;
  369. setDryWet(value, false, false);
  370. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  371. continue;
  372. }
  373. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  374. {
  375. value = cinEvent->value*127/100;
  376. setVolume(value, false, false);
  377. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  378. continue;
  379. }
  380. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  381. {
  382. double left, right;
  383. value = cinEvent->value/0.5 - 1.0;
  384. if (value < 0.0)
  385. {
  386. left = -1.0;
  387. right = (value*2)+1.0;
  388. }
  389. else if (value > 0.0)
  390. {
  391. left = (value*2)-1.0;
  392. right = 1.0;
  393. }
  394. else
  395. {
  396. left = -1.0;
  397. right = 1.0;
  398. }
  399. setBalanceLeft(left, false, false);
  400. setBalanceRight(right, false, false);
  401. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  402. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  403. continue;
  404. }
  405. }
  406. #if 0
  407. // Control plugin parameters
  408. for (k=0; k < param.count; k++)
  409. {
  410. if (param.data[k].midiChannel != cinEvent->channel)
  411. continue;
  412. if (param.data[k].midiCC != cinEvent->parameter)
  413. continue;
  414. if (param.data[k].type != PARAMETER_INPUT)
  415. continue;
  416. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  417. {
  418. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  419. {
  420. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  421. }
  422. else
  423. {
  424. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  425. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  426. value = rint(value);
  427. }
  428. setParameterValue(k, value, false, false, false);
  429. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  430. }
  431. }
  432. #endif
  433. break;
  434. }
  435. case CarlaEngineMidiBankChangeEvent:
  436. if (cinEvent->channel == m_ctrlInChannel)
  437. nextBankId = rint(cinEvent->value);
  438. break;
  439. case CarlaEngineMidiProgramChangeEvent:
  440. if (cinEvent->channel == m_ctrlInChannel)
  441. {
  442. uint32_t nextProgramId = rint(cinEvent->value);
  443. for (k=0; k < midiprog.count; k++)
  444. {
  445. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  446. {
  447. setMidiProgram(k, false, false, false, false);
  448. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  449. break;
  450. }
  451. }
  452. }
  453. break;
  454. case CarlaEngineAllSoundOffEvent:
  455. if (cinEvent->channel == m_ctrlInChannel)
  456. {
  457. if (midi.portMin && ! allNotesOffSent)
  458. sendMidiAllNotesOff();
  459. audioOutputDevice->Stop();
  460. audioOutputDevice->Play();
  461. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  462. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  463. allNotesOffSent = true;
  464. }
  465. break;
  466. case CarlaEngineAllNotesOffEvent:
  467. if (cinEvent->channel == m_ctrlInChannel)
  468. {
  469. if (midi.portMin && ! allNotesOffSent)
  470. sendMidiAllNotesOff();
  471. allNotesOffSent = true;
  472. }
  473. break;
  474. }
  475. }
  476. } // End of Parameters Input
  477. CARLA_PROCESS_CONTINUE_CHECK;
  478. // --------------------------------------------------------------------------------------------------------
  479. // MIDI Input
  480. if (m_active && m_activeBefore)
  481. {
  482. // ----------------------------------------------------------------------------------------------------
  483. // MIDI Input (External)
  484. {
  485. engineMidiLock();
  486. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  487. {
  488. if (extMidiNotes[i].channel < 0)
  489. break;
  490. if (extMidiNotes[i].velo)
  491. midiInputPort->DispatchNoteOn(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  492. else
  493. midiInputPort->DispatchNoteOff(extMidiNotes[i].note, extMidiNotes[i].velo, m_ctrlInChannel, 0);
  494. extMidiNotes[i].channel = -1; // mark as invalid
  495. midiEventCount += 1;
  496. }
  497. engineMidiUnlock();
  498. } // End of MIDI Input (External)
  499. CARLA_PROCESS_CONTINUE_CHECK;
  500. // ----------------------------------------------------------------------------------------------------
  501. // MIDI Input (System)
  502. {
  503. const CarlaEngineMidiEvent* minEvent;
  504. uint32_t time, nEvents = midi.portMin->getEventCount();
  505. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  506. {
  507. minEvent = midi.portMin->getEvent(i);
  508. if (! minEvent)
  509. continue;
  510. time = minEvent->time - framesOffset;
  511. if (time >= frames)
  512. continue;
  513. uint8_t status = minEvent->data[0];
  514. uint8_t channel = status & 0x0F;
  515. // Fix bad note-off
  516. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  517. status -= 0x10;
  518. if (MIDI_IS_STATUS_NOTE_OFF(status))
  519. {
  520. uint8_t note = minEvent->data[1];
  521. midiInputPort->DispatchNoteOff(note, 0, channel, time);
  522. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  523. }
  524. else if (MIDI_IS_STATUS_NOTE_ON(status))
  525. {
  526. uint8_t note = minEvent->data[1];
  527. uint8_t velo = minEvent->data[2];
  528. midiInputPort->DispatchNoteOn(note, velo, channel, time);
  529. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  530. }
  531. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  532. {
  533. //uint8_t note = minEvent->data[1];
  534. //uint8_t pressure = minEvent->data[2];
  535. // TODO, not in linuxsampler API?
  536. }
  537. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  538. {
  539. uint8_t pressure = minEvent->data[1];
  540. midiInputPort->DispatchControlChange(MIDI_STATUS_AFTERTOUCH, pressure, channel, time);
  541. }
  542. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  543. {
  544. uint8_t lsb = minEvent->data[1];
  545. uint8_t msb = minEvent->data[2];
  546. midiInputPort->DispatchPitchbend(((msb << 7) | lsb) - 8192, channel, time);
  547. }
  548. else
  549. continue;
  550. midiEventCount += 1;
  551. }
  552. } // End of MIDI Input (System)
  553. } // End of MIDI Input
  554. CARLA_PROCESS_CONTINUE_CHECK;
  555. // --------------------------------------------------------------------------------------------------------
  556. // Plugin processing
  557. if (m_active)
  558. {
  559. if (! m_activeBefore)
  560. {
  561. for (int c=0; c < MAX_MIDI_CHANNELS; c++)
  562. {
  563. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, c);
  564. midiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, c);
  565. }
  566. audioOutputDevice->Play();
  567. }
  568. audioOutputDevice->Channel(0)->SetBuffer(outBuffer[0]);
  569. audioOutputDevice->Channel(1)->SetBuffer(outBuffer[1]);
  570. // QUESTION: Need to clear it before?
  571. audioOutputDevice->Render(frames);
  572. }
  573. else
  574. {
  575. if (m_activeBefore)
  576. audioOutputDevice->Stop();
  577. }
  578. CARLA_PROCESS_CONTINUE_CHECK;
  579. // --------------------------------------------------------------------------------------------------------
  580. // Post-processing (dry/wet, volume and balance)
  581. if (m_active)
  582. {
  583. bool do_volume = x_volume != 1.0;
  584. bool do_balance = (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  585. double bal_rangeL, bal_rangeR;
  586. float oldBufLeft[do_balance ? frames : 0];
  587. for (i=0; i < aOut.count; i++)
  588. {
  589. // Balance
  590. if (do_balance)
  591. {
  592. if (i%2 == 0)
  593. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  594. bal_rangeL = (x_balanceLeft+1.0)/2;
  595. bal_rangeR = (x_balanceRight+1.0)/2;
  596. for (k=0; k < frames; k++)
  597. {
  598. if (i%2 == 0)
  599. {
  600. // left output
  601. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  602. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  603. }
  604. else
  605. {
  606. // right
  607. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  608. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  609. }
  610. }
  611. }
  612. // Volume
  613. if (do_volume)
  614. {
  615. for (k=0; k < frames; k++)
  616. outBuffer[i][k] *= x_volume;
  617. }
  618. // Output VU
  619. if (x_engine->processMode() != PROCESS_MODE_CONTINUOUS_RACK)
  620. {
  621. for (k=0; i < 2 && k < frames; k++)
  622. {
  623. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  624. aOutsPeak[i] = abs(outBuffer[i][k]);
  625. }
  626. }
  627. }
  628. }
  629. else
  630. {
  631. // disable any output sound if not active
  632. for (i=0; i < aOut.count; i++)
  633. carla_zeroF(outBuffer[i], frames);
  634. aOutsPeak[0] = 0.0;
  635. aOutsPeak[1] = 0.0;
  636. } // End of Post-processing
  637. CARLA_PROCESS_CONTINUE_CHECK;
  638. // --------------------------------------------------------------------------------------------------------
  639. // Peak Values
  640. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  641. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  642. m_activeBefore = m_active;
  643. }
  644. // -------------------------------------------------------------------
  645. bool init(const char* filename, const char* const name, const char* label)
  646. {
  647. QFileInfo file(filename);
  648. if (file.exists() && file.isFile() && file.isReadable())
  649. {
  650. const char* stype = m_isGIG ? "gig" : "sfz";
  651. try {
  652. engine = LinuxSampler::EngineFactory::Create(stype);
  653. }
  654. catch (LinuxSampler::Exception& e)
  655. {
  656. x_engine->setLastError(e.what());
  657. return false;
  658. }
  659. try {
  660. instrument = engine->GetInstrumentManager();
  661. }
  662. catch (LinuxSampler::Exception& e)
  663. {
  664. x_engine->setLastError(e.what());
  665. return false;
  666. }
  667. try {
  668. instrumentIds = instrument->GetInstrumentFileContent(filename);
  669. }
  670. catch (LinuxSampler::Exception& e)
  671. {
  672. x_engine->setLastError(e.what());
  673. return false;
  674. }
  675. if (instrumentIds.size() > 0)
  676. {
  677. LinuxSampler::InstrumentManager::instrument_info_t info = instrument->GetInstrumentInfo(instrumentIds[0]);
  678. m_label = strdup(info.Product.c_str());
  679. m_maker = strdup(info.Artists.c_str());
  680. m_filename = strdup(filename);
  681. if (name)
  682. m_name = x_engine->getUniquePluginName(name);
  683. else
  684. m_name = x_engine->getUniquePluginName(label && label[0] ? label : info.InstrumentName.c_str());
  685. sampler_channel = sampler->AddSamplerChannel();
  686. sampler_channel->SetEngineType(stype);
  687. sampler_channel->SetAudioOutputDevice(audioOutputDevice);
  688. //sampler_channel->SetMidiInputDevice(midiInputDevice);
  689. //sampler_channel->SetMidiInputChannel(LinuxSampler::midi_chan_1);
  690. midiInputPort->Connect(sampler_channel->GetEngineChannel(), LinuxSampler::midi_chan_all);
  691. engine_channel = sampler_channel->GetEngineChannel();
  692. engine_channel->Connect(audioOutputDevice);
  693. engine_channel->PrepareLoadInstrument(filename, 0); // todo - find instrument from label
  694. engine_channel->LoadInstrument();
  695. engine_channel->Volume(LinuxSampler::VOLUME_MAX);
  696. x_client = x_engine->addClient(this);
  697. if (x_client->isOk())
  698. return true;
  699. else
  700. x_engine->setLastError("Failed to register plugin client");
  701. }
  702. else
  703. x_engine->setLastError("Failed to find any instruments");
  704. }
  705. else
  706. x_engine->setLastError("Requested file is not valid or does not exist");
  707. return false;
  708. }
  709. // -------------------------------------------------------------------
  710. static CarlaPlugin* newLinuxSampler(const initializer& init, bool isGIG);
  711. private:
  712. LinuxSampler::Sampler* sampler;
  713. LinuxSampler::SamplerChannel* sampler_channel;
  714. LinuxSampler::Engine* engine;
  715. LinuxSampler::EngineChannel* engine_channel;
  716. LinuxSampler::InstrumentManager* instrument;
  717. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> instrumentIds;
  718. LinuxSampler::AudioOutputDevicePlugin* audioOutputDevice;
  719. LinuxSampler::MidiInputDevicePlugin* midiInputDevice;
  720. LinuxSampler::MidiInputPort* midiInputPort;
  721. bool m_isGIG;
  722. const char* m_label;
  723. const char* m_maker;
  724. };
  725. CarlaPlugin* LinuxSamplerPlugin::newLinuxSampler(const initializer& init, bool isGIG)
  726. {
  727. qDebug("LinuxSamplerPlugin::newLinuxSampler(%p, \"%s\", \"%s\", \"%s\", %s)", init.engine, init.filename, init.name, init.label, bool2str(isGIG));
  728. short id = init.engine->getNewPluginId();
  729. if (id < 0 || id > init.engine->maxPluginNumber())
  730. {
  731. init.engine->setLastError("Maximum number of plugins reached");
  732. return nullptr;
  733. }
  734. LinuxSamplerPlugin* const plugin = new LinuxSamplerPlugin(init.engine, id, isGIG);
  735. if (! plugin->init(init.filename, init.name, init.label))
  736. {
  737. delete plugin;
  738. return nullptr;
  739. }
  740. plugin->reload();
  741. plugin->registerToOscControl();
  742. return plugin;
  743. }
  744. /**@}*/
  745. CARLA_BACKEND_END_NAMESPACE
  746. #else // WANT_LINUXSAMPLER
  747. # warning linuxsampler not available (no GIG and SFZ support)
  748. #endif
  749. CARLA_BACKEND_START_NAMESPACE
  750. CarlaPlugin* CarlaPlugin::newGIG(const initializer& init)
  751. {
  752. qDebug("CarlaPlugin::newGIG(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  753. #ifdef WANT_LINUXSAMPLER
  754. return LinuxSamplerPlugin::newLinuxSampler(init, true);
  755. #else
  756. init.engine->setLastError("linuxsampler support not available");
  757. return nullptr;
  758. #endif
  759. }
  760. CarlaPlugin* CarlaPlugin::newSFZ(const initializer& init)
  761. {
  762. qDebug("CarlaPlugin::newSFZ(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  763. #ifdef WANT_LINUXSAMPLER
  764. return LinuxSamplerPlugin::newLinuxSampler(init, false);
  765. #else
  766. init.engine->setLastError("linuxsampler support not available");
  767. return nullptr;
  768. #endif
  769. }
  770. CARLA_BACKEND_END_NAMESPACE