|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198 |
- /*
- * Carla LinuxSampler Plugin
- * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaPluginInternal.hpp"
-
- #ifdef WANT_LINUXSAMPLER
-
- #include "linuxsampler/EngineFactory.h"
- #include <linuxsampler/Sampler.h>
-
- namespace LinuxSampler {
-
- using CarlaBackend::CarlaEngine;
- using CarlaBackend::CarlaPlugin;
-
- // -----------------------------------------------------------------------
- // LinuxSampler static values
-
- static const float VOLUME_MAX = 3.16227766f; // +10 dB
- static const float VOLUME_MIN = 0.0f; // -inf dB
-
- // -----------------------------------------------------------------------
- // LinuxSampler AudioOutputDevice Plugin
-
- class AudioOutputDevicePlugin : public AudioOutputDevice
- {
- public:
- AudioOutputDevicePlugin(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin)
- : AudioOutputDevice(std::map<String, DeviceCreationParameter*>()),
- fEngine(engine),
- fPlugin(plugin)
- {
- CARLA_ASSERT(engine != nullptr);
- CARLA_ASSERT(plugin != nullptr);
- }
-
- // -------------------------------------------------------------------
- // LinuxSampler virtual methods
-
- void Play() override
- {
- }
-
- bool IsPlaying() override
- {
- return (fEngine->isRunning() && fPlugin->isEnabled());
- }
-
- void Stop() override
- {
- }
-
- uint MaxSamplesPerCycle() override
- {
- return fEngine->getBufferSize();
- }
-
- uint SampleRate() override
- {
- return fEngine->getSampleRate();
- }
-
- String Driver() override
- {
- return "AudioOutputDevicePlugin";
- }
-
- AudioChannel* CreateChannel(uint channelNr) override
- {
- return new AudioChannel(channelNr, nullptr, 0);
- }
-
- // -------------------------------------------------------------------
- // Give public access to the RenderAudio call
-
- int Render(const uint samples)
- {
- return RenderAudio(samples);
- }
-
- private:
- CarlaEngine* const fEngine;
- CarlaPlugin* const fPlugin;
- };
-
- // -----------------------------------------------------------------------
- // LinuxSampler MidiInputDevice Plugin
-
- class MidiInputDevicePlugin : public MidiInputDevice
- {
- public:
- MidiInputDevicePlugin(Sampler* const sampler)
- : MidiInputDevice(std::map<String, DeviceCreationParameter*>(), sampler)
- {
- }
-
- // -------------------------------------------------------------------
- // LinuxSampler virtual methods
-
- void Listen() override
- {
- }
-
- void StopListen() override
- {
- }
-
- String Driver() override
- {
- return "MidiInputDevicePlugin";
- }
-
- MidiInputPort* CreateMidiPort() override
- {
- return new MidiInputPortPlugin(this, Ports.size());
- }
-
- // -------------------------------------------------------------------
- // Properly delete port (destructor is protected)
-
- void DeleteMidiPort(MidiInputPort* const port)
- {
- delete (MidiInputPortPlugin*)port;
- }
-
- // -------------------------------------------------------------------
- // MIDI Port implementation for this plugin MIDI input driver
- // (Constructor and destructor are protected)
-
- class MidiInputPortPlugin : public MidiInputPort
- {
- protected:
- MidiInputPortPlugin(MidiInputDevicePlugin* const device, const int portNumber)
- : MidiInputPort(device, portNumber) {}
- friend class MidiInputDevicePlugin;
- };
- };
-
- } // namespace LinuxSampler
-
- // -----------------------------------------------------------------------
-
- CARLA_BACKEND_START_NAMESPACE
-
- #if 0
- }
- #endif
-
- class LinuxSamplerPlugin : public CarlaPlugin
- {
- public:
- LinuxSamplerPlugin(CarlaEngine* const engine, const unsigned short id, const bool isGIG, const bool use16Outs)
- : CarlaPlugin(engine, id),
- kIsGIG(isGIG),
- kUses16Outs(use16Outs),
- fSampler(new LinuxSampler::Sampler()),
- fSamplerChannel(nullptr),
- fEngine(nullptr),
- fEngineChannel(nullptr),
- fAudioOutputDevice(new LinuxSampler::AudioOutputDevicePlugin(engine, this)),
- fMidiInputDevice(new LinuxSampler::MidiInputDevicePlugin(fSampler)),
- fMidiInputPort(fMidiInputDevice->CreateMidiPort()),
- fInstrument(nullptr)
- {
- carla_debug("LinuxSamplerPlugin::LinuxSamplerPlugin(%p, %i, %s)", engine, id, bool2str(isGIG));
- }
-
- ~LinuxSamplerPlugin() override
- {
- carla_debug("LinuxSamplerPlugin::~LinuxSamplerPlugin()");
-
- pData->singleMutex.lock();
- pData->masterMutex.lock();
-
- if (pData->client != nullptr && pData->client->isActive())
- pData->client->deactivate();
-
- if (pData->active)
- {
- deactivate();
- pData->active = false;
- }
-
- if (fEngine != nullptr)
- {
- if (fSamplerChannel != nullptr)
- {
- fMidiInputPort->Disconnect(fSamplerChannel->GetEngineChannel());
- fEngineChannel->DisconnectAudioOutputDevice();
- fSampler->RemoveSamplerChannel(fSamplerChannel);
- }
-
- LinuxSampler::EngineFactory::Destroy(fEngine);
- }
-
- // destructor is private
- fMidiInputDevice->DeleteMidiPort(fMidiInputPort);
-
- delete fMidiInputDevice;
- delete fAudioOutputDevice;
- delete fSampler;
-
- fInstrumentIds.clear();
-
- clearBuffers();
- }
-
- // -------------------------------------------------------------------
- // Information (base)
-
- PluginType getType() const noexcept override
- {
- return kIsGIG ? PLUGIN_GIG : PLUGIN_SFZ;
- }
-
- PluginCategory getCategory() const override
- {
- return PLUGIN_CATEGORY_SYNTH;
- }
-
- // -------------------------------------------------------------------
- // Information (count)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Information (current data)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- unsigned int getOptionsAvailable() const override
- {
- unsigned int options = 0x0;
-
- options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
- options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
- options |= PLUGIN_OPTION_SEND_PITCHBEND;
- options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
-
- return options;
- }
-
- void getLabel(char* const strBuf) const override
- {
- std::strncpy(strBuf, (const char*)fLabel, STR_MAX);
- }
-
- void getMaker(char* const strBuf) const override
- {
- std::strncpy(strBuf, (const char*)fMaker, STR_MAX);
- }
-
- void getCopyright(char* const strBuf) const override
- {
- getMaker(strBuf);
- }
-
- void getRealName(char* const strBuf) const override
- {
- std::strncpy(strBuf, (const char*)fRealName, STR_MAX);
- }
-
- // -------------------------------------------------------------------
- // Set data (state)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Set data (internal stuff)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Set data (plugin-specific stuff)
-
- void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
- {
- CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
-
- if (index < -1)
- index = -1;
- else if (index > static_cast<int32_t>(pData->midiprog.count))
- return;
-
- if (pData->ctrlChannel < 0 || pData->ctrlChannel >= 16)
- return;
-
- if (index >= 0)
- {
- const uint32_t bank = pData->midiprog.data[index].bank;
- const uint32_t program = pData->midiprog.data[index].program;
- const uint32_t rIndex = bank*128 + program;
-
- const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
-
- if (pData->engine->isOffline())
- {
- fEngineChannel->PrepareLoadInstrument((const char*)pData->filename, rIndex);
- fEngineChannel->LoadInstrument();
- }
- else
- {
- fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], fEngineChannel);
- }
- }
-
- CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fInstrument != nullptr,);
- carla_debug("LinuxSamplerPlugin::reload() - start");
-
- const EngineProcessMode processMode(pData->engine->getProccessMode());
-
- // Safely disable plugin for reload
- const ScopedDisabler sd(this);
-
- if (pData->active)
- deactivate();
-
- clearBuffers();
-
- uint32_t aOuts;
- aOuts = 2;
-
- pData->audioOut.createNew(aOuts);
-
- const int portNameSize = pData->engine->getMaxPortNameSize();
- CarlaString portName;
-
- // ---------------------------------------
- // Audio Outputs
-
- {
- // out-left
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "out-left";
- portName.truncate(portNameSize);
-
- pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
- pData->audioOut.ports[0].rindex = 0;
-
- // out-right
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "out-right";
- portName.truncate(portNameSize);
-
- pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
- pData->audioOut.ports[1].rindex = 1;
- }
-
- // ---------------------------------------
- // Event Input
-
- {
- portName.clear();
-
- if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = pData->name;
- portName += ":";
- }
-
- portName += "event-in";
- portName.truncate(portNameSize);
-
- pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
- }
-
- // ---------------------------------------
-
- // plugin hints
- pData->hints = 0x0;
- //pData->hints |= PLUGIN_IS_SYNTH;
- pData->hints |= PLUGIN_CAN_VOLUME;
- pData->hints |= PLUGIN_CAN_BALANCE;
-
- // extra plugin hints
- pData->extraHints = 0x0;
- pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
- pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
-
- bufferSizeChanged(pData->engine->getBufferSize());
- reloadPrograms(true);
-
- if (pData->active)
- activate();
-
- carla_debug("LinuxSamplerPlugin::reload() - end");
- }
-
- void reloadPrograms(bool init) override
- {
- carla_debug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(init));
-
- // Delete old programs
- pData->midiprog.clear();
-
- // Query new programs
- uint32_t i, count = fInstrumentIds.size();
-
- // sound kits must always have at least 1 midi-program
- CARLA_ASSERT(count > 0);
-
- if (count == 0)
- return;
-
- pData->midiprog.createNew(count);
-
- LinuxSampler::InstrumentManager::instrument_info_t info;
-
- for (i=0; i < pData->midiprog.count; ++i)
- {
- pData->midiprog.data[i].bank = i / 128;
- pData->midiprog.data[i].program = i % 128;
-
- try {
- info = fInstrument->GetInstrumentInfo(fInstrumentIds[i]);
- }
- catch (const LinuxSampler::InstrumentManagerException&)
- {
- continue;
- }
-
- pData->midiprog.data[i].name = carla_strdup(info.InstrumentName.c_str());
- }
-
- #ifndef BUILD_BRIDGE
- // Update OSC Names
- if (pData->engine->isOscControlRegistered())
- {
- pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
-
- for (i=0; i < count; ++i)
- pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
- }
- #endif
-
- if (init)
- {
- setMidiProgram(0, false, false, false);
- }
- else
- {
- pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
- }
- }
-
- // -------------------------------------------------------------------
- // Plugin processing
-
- void activate() override
- {
- CARLA_ASSERT(fAudioOutputDevice != nullptr);
-
- fAudioOutputDevice->Play();
- }
-
- void deactivate() override
- {
- CARLA_ASSERT(fAudioOutputDevice != nullptr);
-
- fAudioOutputDevice->Stop();
- }
-
- void process(float** const, float** const outBuffer, const uint32_t frames) override
- {
- uint32_t i, k;
-
- // --------------------------------------------------------------------------------------------------------
- // Check if active
-
- if (! pData->active)
- {
- // disable any output sound
- for (i=0; i < pData->audioOut.count; ++i)
- {
- #ifdef HAVE_JUCE
- FloatVectorOperations::clear(outBuffer[i], frames);
- #else
- #endif
- }
-
- return;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Check if needs reset
-
- if (pData->needsReset)
- {
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
- {
- fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, k);
- fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, k);
- }
- }
- else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
- {
- for (k=0; k < MAX_MIDI_NOTE; ++k)
- fMidiInputPort->DispatchNoteOff(k, 0, pData->ctrlChannel);
- }
-
- pData->needsReset = false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Event Input and Processing
-
- {
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (External)
-
- if (pData->extNotes.mutex.tryLock())
- {
- while (! pData->extNotes.data.isEmpty())
- {
- const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
-
- CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
-
- if (note.velo > 0)
- fMidiInputPort->DispatchNoteOn(note.note, note.velo, note.channel, 0);
- else
- fMidiInputPort->DispatchNoteOff(note.note, note.velo, note.channel, 0);
- }
-
- pData->extNotes.mutex.unlock();
-
- } // End of MIDI Input (External)
-
- // ----------------------------------------------------------------------------------------------------
- // Event Input (System)
-
- bool allNotesOffSent = false;
- bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
-
- uint32_t time, nEvents = pData->event.portIn->getEventCount();
- uint32_t startTime = 0;
- uint32_t timeOffset = 0;
- uint32_t nextBankId = 0;
-
- if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
- nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
-
- for (i=0; i < nEvents; ++i)
- {
- const EngineEvent& event(pData->event.portIn->getEvent(i));
-
- time = event.time;
-
- if (time >= frames)
- continue;
-
- CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
-
- if (time > timeOffset && sampleAccurate)
- {
- if (processSingle(outBuffer, time - timeOffset, timeOffset))
- {
- startTime = 0;
- timeOffset = time;
-
- if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
- nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
- else
- nextBankId = 0;
- }
- else
- startTime += timeOffset;
- }
-
- // Control change
- switch (event.type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl:
- {
- const EngineControlEvent& ctrlEvent = event.ctrl;
-
- switch (ctrlEvent.type)
- {
- case kEngineControlEventTypeNull:
- break;
-
- case kEngineControlEventTypeParameter:
- {
- #ifndef BUILD_BRIDGE
- // Control backend stuff
- if (event.channel == pData->ctrlChannel)
- {
- float value;
-
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
- {
- value = ctrlEvent.value;
- setDryWet(value, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
- }
-
- if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
- {
- value = ctrlEvent.value*127.0f/100.0f;
- setVolume(value, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
- }
-
- if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
- {
- float left, right;
- value = ctrlEvent.value/0.5f - 1.0f;
-
- if (value < 0.0f)
- {
- left = -1.0f;
- right = (value*2.0f)+1.0f;
- }
- else if (value > 0.0f)
- {
- left = (value*2.0f)-1.0f;
- right = 1.0f;
- }
- else
- {
- left = -1.0f;
- right = 1.0f;
- }
-
- setBalanceLeft(left, false, false);
- setBalanceRight(right, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
- }
- }
- #endif
-
- // Control plugin parameters
- for (k=0; k < pData->param.count; ++k)
- {
- if (pData->param.data[k].midiChannel != event.channel)
- continue;
- if (pData->param.data[k].midiCC != ctrlEvent.param)
- continue;
- if ((pData->param.data[k].hints & PARAMETER_IS_INPUT) == 0)
- continue;
- if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
- continue;
-
- double value;
-
- if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
- {
- value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
- }
- else
- {
- value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
-
- if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
- value = std::rint(value);
- }
-
- setParameterValue(k, value, false, false, false);
- pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
- }
-
- if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
- {
- fMidiInputPort->DispatchControlChange(ctrlEvent.param, ctrlEvent.value*127.0f, event.channel, sampleAccurate ? startTime : time);
- }
-
- break;
- }
-
- case kEngineControlEventTypeMidiBank:
- if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
- nextBankId = ctrlEvent.param;
- break;
-
- case kEngineControlEventTypeMidiProgram:
- if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
- {
- const uint32_t nextProgramId = ctrlEvent.param;
-
- for (k=0; k < pData->midiprog.count; ++k)
- {
- if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
- {
- setMidiProgram(k, false, false, false);
- pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
- break;
- }
- }
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, sampleAccurate ? startTime : time);
- }
- break;
-
- case kEngineControlEventTypeAllNotesOff:
- if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
- {
- allNotesOffSent = true;
- sendMidiAllNotesOffToCallback();
- }
-
- fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, sampleAccurate ? startTime : time);
- }
- break;
- }
-
- break;
- }
-
- case kEngineEventTypeMidi:
- {
- const EngineMidiEvent& midiEvent(event.midi);
-
- uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
- uint8_t channel = event.channel;
-
- // Fix bad note-off (per DSSI spec)
- if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
- status -= 0x10;
-
- int32_t fragmentPos = sampleAccurate ? startTime : time;
-
- if (MIDI_IS_STATUS_NOTE_OFF(status))
- {
- const uint8_t note = midiEvent.data[1];
-
- fMidiInputPort->DispatchNoteOff(note, 0, channel, fragmentPos);
-
- pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
- }
- else if (MIDI_IS_STATUS_NOTE_ON(status))
- {
- const uint8_t note = midiEvent.data[1];
- const uint8_t velo = midiEvent.data[2];
-
- fMidiInputPort->DispatchNoteOn(note, velo, channel, fragmentPos);
-
- pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
- }
- else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
- {
- //const uint8_t note = midiEvent.data[1];
- //const uint8_t pressure = midiEvent.data[2];
-
- // unsupported
- }
- else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
- {
- const uint8_t control = midiEvent.data[1];
- const uint8_t value = midiEvent.data[2];
-
- fMidiInputPort->DispatchControlChange(control, value, channel, fragmentPos);
- }
- else if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
- {
- //const uint8_t pressure = midiEvent.data[1];
-
- // unsupported
- }
- else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
- {
- const uint8_t lsb = midiEvent.data[1];
- const uint8_t msb = midiEvent.data[2];
-
- fMidiInputPort->DispatchPitchbend(((msb << 7) | lsb) - 8192, channel, fragmentPos);
- }
-
- break;
- }
- }
- }
-
- pData->postRtEvents.trySplice();
-
- if (frames > timeOffset)
- processSingle(outBuffer, frames - timeOffset, timeOffset);
-
- } // End of Event Input and Processing
- }
-
- bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
- {
- CARLA_ASSERT(outBuffer != nullptr);
- CARLA_ASSERT(frames > 0);
-
- if (outBuffer == nullptr)
- return false;
- if (frames == 0)
- return false;
-
- uint32_t i, k;
-
- // --------------------------------------------------------------------------------------------------------
- // Try lock, silence otherwise
-
- if (pData->engine->isOffline())
- {
- pData->singleMutex.lock();
- }
- else if (! pData->singleMutex.tryLock())
- {
- for (i=0; i < pData->audioOut.count; ++i)
- {
- for (k=0; k < frames; ++k)
- outBuffer[i][k+timeOffset] = 0.0f;
- }
-
- return false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Run plugin
-
- fAudioOutputDevice->Channel(0)->SetBuffer(outBuffer[0] + timeOffset);
- fAudioOutputDevice->Channel(1)->SetBuffer(outBuffer[1] + timeOffset);
- // QUESTION: Need to clear it before?
- fAudioOutputDevice->Render(frames);
-
- #ifndef BUILD_BRIDGE
- // --------------------------------------------------------------------------------------------------------
- // Post-processing (dry/wet, volume and balance)
-
- {
- const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
- const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
-
- float oldBufLeft[doBalance ? frames : 1];
-
- for (i=0; i < pData->audioOut.count; ++i)
- {
- // Balance
- if (doBalance)
- {
- if (i % 2 == 0)
- {
- #ifdef HAVE_JUCE
- FloatVectorOperations::copy(oldBufLeft, outBuffer[i], frames);
- #else
- #endif
- }
-
- float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
- float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
-
- for (k=0; k < frames; ++k)
- {
- if (i % 2 == 0)
- {
- // left
- outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
- outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
- }
- else
- {
- // right
- outBuffer[i][k] = outBuffer[i][k] * balRangeR;
- outBuffer[i][k] += oldBufLeft[k] * balRangeL;
- }
- }
- }
-
- // Volume
- if (doVolume)
- {
- for (k=0; k < frames; ++k)
- outBuffer[i][k+timeOffset] *= pData->postProc.volume;
- }
- }
-
- } // End of Post-processing
- #endif
-
- // --------------------------------------------------------------------------------------------------------
-
- pData->singleMutex.unlock();
- return true;
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- // nothing
-
- // -------------------------------------------------------------------
-
- const void* getExtraStuff() const noexcept override
- {
- return kUses16Outs ? (const void*)0x1 : nullptr;
- }
-
- bool init(const char* filename, const char* const name, const char* label)
- {
- CARLA_ASSERT(pData->engine != nullptr);
- CARLA_ASSERT(pData->client == nullptr);
- CARLA_ASSERT(filename != nullptr);
- CARLA_ASSERT(label != nullptr);
-
- // ---------------------------------------------------------------
- // first checks
-
- if (pData->engine == nullptr)
- {
- return false;
- }
-
- if (pData->client != nullptr)
- {
- pData->engine->setLastError("Plugin client is already registered");
- return false;
- }
-
- if (filename == nullptr)
- {
- pData->engine->setLastError("null filename");
- return false;
- }
-
- if (label == nullptr)
- {
- pData->engine->setLastError("null label");
- return false;
- }
-
- // ---------------------------------------------------------------
- // Check if file exists
- {
- // QFileInfo file(filename);
- //
- // if (! (file.exists() && file.isFile() && file.isReadable()))
- // {
- // pData->engine->setLastError("Requested file is not valid or does not exist");
- // return false;
- // }
- }
-
- // ---------------------------------------------------------------
- // Create the LinuxSampler Engine
-
- const char* const stype = kIsGIG ? "gig" : "sfz";
-
- try {
- fEngine = LinuxSampler::EngineFactory::Create(stype);
- }
- catch (LinuxSampler::Exception& e)
- {
- pData->engine->setLastError(e.what());
- return false;
- }
-
- // ---------------------------------------------------------------
- // Get the Engine's Instrument Manager
-
- fInstrument = fEngine->GetInstrumentManager();
-
- if (fInstrument == nullptr)
- {
- pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- // ---------------------------------------------------------------
- // Load the Instrument via filename
-
- try {
- fInstrumentIds = fInstrument->GetInstrumentFileContent(filename);
- }
- catch (const LinuxSampler::InstrumentManagerException& e)
- {
- pData->engine->setLastError(e.what());
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- // ---------------------------------------------------------------
- // Get info
-
- if (fInstrumentIds.size() == 0)
- {
- pData->engine->setLastError("Failed to find any instruments");
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- LinuxSampler::InstrumentManager::instrument_info_t info;
-
- try {
- info = fInstrument->GetInstrumentInfo(fInstrumentIds[0]);
- }
- catch (const LinuxSampler::InstrumentManagerException& e)
- {
- pData->engine->setLastError(e.what());
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- fRealName = info.InstrumentName.c_str();
- fLabel = info.Product.c_str();
- fMaker = info.Artists.c_str();
- pData->filename = filename;
-
- if (kUses16Outs && ! fLabel.endsWith(" (16 outs)"))
- fLabel += " (16 outs)";
-
- if (name != nullptr)
- pData->name = pData->engine->getUniquePluginName(name);
- else
- pData->name = pData->engine->getUniquePluginName((const char*)fRealName);
-
- // ---------------------------------------------------------------
- // Register client
-
- pData->client = pData->engine->addClient(this);
-
- if (pData->client == nullptr || ! pData->client->isOk())
- {
- pData->engine->setLastError("Failed to register plugin client");
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- // ---------------------------------------------------------------
- // Init LinuxSampler stuff
-
- fSamplerChannel = fSampler->AddSamplerChannel();
- fSamplerChannel->SetEngineType(stype);
- fSamplerChannel->SetAudioOutputDevice(fAudioOutputDevice);
-
- fEngineChannel = fSamplerChannel->GetEngineChannel();
- fEngineChannel->Connect(fAudioOutputDevice);
- fEngineChannel->Volume(LinuxSampler::VOLUME_MAX);
-
- fMidiInputPort->Connect(fSamplerChannel->GetEngineChannel(), LinuxSampler::midi_chan_all);
-
- // ---------------------------------------------------------------
- // load plugin settings
-
- {
- // set default options
- pData->options = 0x0;
-
- pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
- pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
- pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
-
- // load settings
- pData->idStr = kIsGIG ? "GIG" : "SFZ";
- pData->idStr += "/";
- pData->idStr += label;
- pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
- }
-
- return true;
- }
-
- // -------------------------------------------------------------------
-
- static CarlaPlugin* newLinuxSampler(const Initializer& init, bool isGIG, const bool use16Outs);
-
- private:
- const bool kIsGIG; // sfz if false
- const bool kUses16Outs;
-
- CarlaString fRealName;
- CarlaString fLabel;
- CarlaString fMaker;
-
- LinuxSampler::Sampler* fSampler;
- LinuxSampler::SamplerChannel* fSamplerChannel;
-
- LinuxSampler::Engine* fEngine;
- LinuxSampler::EngineChannel* fEngineChannel;
-
- LinuxSampler::AudioOutputDevicePlugin* fAudioOutputDevice;
- LinuxSampler::MidiInputDevicePlugin* fMidiInputDevice;
- LinuxSampler::MidiInputPort* fMidiInputPort;
-
- LinuxSampler::InstrumentManager* fInstrument;
- std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LinuxSamplerPlugin)
- };
-
- CarlaPlugin* LinuxSamplerPlugin::newLinuxSampler(const Initializer& init, const bool isGIG, const bool use16Outs)
- {
- carla_debug("LinuxSamplerPlugin::newLinuxSampler({%p, \"%s\", \"%s\", \"%s\"}, %s, %s)", init.engine, init.filename, init.name, init.label, bool2str(isGIG), bool2str(use16Outs));
-
- if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
- {
- init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
- return nullptr;
- }
-
- LinuxSamplerPlugin* const plugin(new LinuxSamplerPlugin(init.engine, init.id, isGIG, use16Outs));
-
- if (! plugin->init(init.filename, init.name, init.label))
- {
- delete plugin;
- return nullptr;
- }
-
- plugin->reload();
-
- return plugin;
- }
-
- CARLA_BACKEND_END_NAMESPACE
-
- #endif // WANT_LINUXSAMPLER
-
- CARLA_BACKEND_START_NAMESPACE
-
- CarlaPlugin* CarlaPlugin::newGIG(const Initializer& init, const bool use16Outs)
- {
- carla_debug("CarlaPlugin::newGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
- #ifdef WANT_LINUXSAMPLER
- return LinuxSamplerPlugin::newLinuxSampler(init, true, use16Outs);
- #else
- init.engine->setLastError("linuxsampler support not available");
- return nullptr;
-
- // unused
- (void)use16Outs;
- #endif
- }
-
- CarlaPlugin* CarlaPlugin::newSFZ(const Initializer& init, const bool use16Outs)
- {
- carla_debug("CarlaPlugin::newSFZ({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
- #ifdef WANT_LINUXSAMPLER
- return LinuxSamplerPlugin::newLinuxSampler(init, false, use16Outs);
- #else
- init.engine->setLastError("linuxsampler support not available");
- return nullptr;
-
- // unused
- (void)use16Outs;
- #endif
- }
-
- CARLA_BACKEND_END_NAMESPACE
|