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.

974 lines
30KB

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