|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- /*
- * 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 GPL.txt file
- */
-
- #include "CarlaPluginInternal.hpp"
-
- #ifdef WANT_LINUXSAMPLER
-
- #include "linuxsampler/EngineFactory.h"
- #include <linuxsampler/Sampler.h>
-
- #include <QtCore/QFileInfo>
-
- namespace LinuxSampler {
-
- // -----------------------------------------------------------------------
- // 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*>()),
- kEngine(engine),
- kPlugin(plugin)
- {
- CARLA_ASSERT(engine != nullptr);
- CARLA_ASSERT(plugin != nullptr);
- }
-
- // -------------------------------------------------------------------
- // LinuxSampler virtual methods
-
- void Play() override
- {
- }
-
- bool IsPlaying() override
- {
- return (kEngine->isRunning() && kPlugin->enabled());
- }
-
- void Stop() override
- {
- }
-
- uint MaxSamplesPerCycle() override
- {
- return kEngine->getBufferSize();
- }
-
- uint SampleRate() override
- {
- return kEngine->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:
- CarlaBackend::CarlaEngine* const kEngine;
- CarlaBackend::CarlaPlugin* const kPlugin;
- };
-
- // -----------------------------------------------------------------------
- // 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()");
-
- kData->singleMutex.lock();
- kData->masterMutex.lock();
-
- if (kData->active)
- {
- deactivate();
- kData->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 type() const override
- {
- return kIsGIG ? PLUGIN_GIG : PLUGIN_SFZ;
- }
-
- PluginCategory category() override
- {
- return PLUGIN_CATEGORY_SYNTH;
- }
-
- // -------------------------------------------------------------------
- // Information (count)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Information (current data)
-
- // nothing
-
- // -------------------------------------------------------------------
- // Information (per-plugin data)
-
- unsigned int availableOptions() 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) override
- {
- std::strncpy(strBuf, (const char*)fLabel, STR_MAX);
- }
-
- void getMaker(char* const strBuf) override
- {
- std::strncpy(strBuf, (const char*)fMaker, STR_MAX);
- }
-
- void getCopyright(char* const strBuf) override
- {
- getMaker(strBuf);
- }
-
- void getRealName(char* const strBuf) 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>(kData->midiprog.count));
-
- if (index < -1)
- index = -1;
- else if (index > static_cast<int32_t>(kData->midiprog.count))
- return;
-
- if (kData->ctrlChannel < 0 || kData->ctrlChannel >= 16)
- return;
-
- if (index >= 0)
- {
- const uint32_t bank = kData->midiprog.data[index].bank;
- const uint32_t program = kData->midiprog.data[index].program;
- const uint32_t rIndex = bank*128 + program;
-
- const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
-
- if (kData->engine->isOffline())
- {
- fEngineChannel->PrepareLoadInstrument((const char*)fFilename, rIndex);
- fEngineChannel->LoadInstrument();
- }
- else
- {
- fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], fEngineChannel);
- }
- }
-
- CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
- }
-
- // -------------------------------------------------------------------
- // Plugin state
-
- void reload() override
- {
- carla_debug("LinuxSamplerPlugin::reload() - start");
- CARLA_ASSERT(kData->engine != nullptr);
- CARLA_ASSERT(fInstrument != nullptr);
-
- if (kData->engine == nullptr)
- return;
- if (fInstrument == nullptr)
- return;
-
- const ProcessMode processMode(kData->engine->getProccessMode());
-
- // Safely disable plugin for reload
- const ScopedDisabler sd(this);
-
- if (kData->active)
- deactivate();
-
- clearBuffers();
-
- uint32_t aOuts;
- aOuts = 2;
-
- kData->audioOut.createNew(aOuts);
-
- const int portNameSize = kData->engine->maxPortNameSize();
- CarlaString portName;
-
- // ---------------------------------------
- // Audio Outputs
-
- {
- // out-left
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = fName;
- portName += ":";
- }
-
- portName += "out-left";
- portName.truncate(portNameSize);
-
- kData->audioOut.ports[0].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
- kData->audioOut.ports[0].rindex = 0;
-
- // out-right
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = fName;
- portName += ":";
- }
-
- portName += "out-right";
- portName.truncate(portNameSize);
-
- kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
- kData->audioOut.ports[1].rindex = 1;
- }
-
- // ---------------------------------------
- // Event Input
-
- {
- portName.clear();
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- portName = fName;
- portName += ":";
- }
-
- portName += "event-in";
- portName.truncate(portNameSize);
-
- kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
- }
-
- // ---------------------------------------
-
- // plugin hints
- fHints = 0x0;
- fHints |= PLUGIN_IS_SYNTH;
- fHints |= PLUGIN_CAN_VOLUME;
- fHints |= PLUGIN_CAN_BALANCE;
-
- // extra plugin hints
- kData->extraHints = 0x0;
- kData->extraHints |= PLUGIN_HINT_HAS_MIDI_IN;
- kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
-
- bufferSizeChanged(kData->engine->getBufferSize());
- reloadPrograms(true);
-
- if (kData->active)
- activate();
-
- carla_debug("LinuxSamplerPlugin::reload() - end");
- }
-
- void reloadPrograms(bool init) override
- {
- carla_debug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(init));
-
- // Delete old programs
- kData->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;
-
- kData->midiprog.createNew(count);
-
- LinuxSampler::InstrumentManager::instrument_info_t info;
-
- for (i=0; i < kData->midiprog.count; ++i)
- {
- kData->midiprog.data[i].bank = i / 128;
- kData->midiprog.data[i].program = i % 128;
-
- try {
- info = fInstrument->GetInstrumentInfo(fInstrumentIds[i]);
- }
- catch (const LinuxSampler::InstrumentManagerException&)
- {
- continue;
- }
-
- kData->midiprog.data[i].name = carla_strdup(info.InstrumentName.c_str());
- }
-
- #ifndef BUILD_BRIDGE
- // Update OSC Names
- if (kData->engine->isOscControlRegistered())
- {
- kData->engine->osc_send_control_set_midi_program_count(fId, count);
-
- for (i=0; i < count; ++i)
- kData->engine->osc_send_control_set_midi_program_data(fId, i, kData->midiprog.data[i].bank, kData->midiprog.data[i].program, kData->midiprog.data[i].name);
- }
- #endif
-
- if (init)
- {
- setMidiProgram(0, false, false, false);
- }
- else
- {
- kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 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 (! kData->active)
- {
- // disable any output sound
- for (i=0; i < kData->audioOut.count; ++i)
- carla_zeroFloat(outBuffer[i], frames);
-
- return;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Check if needs reset
-
- if (kData->needsReset)
- {
- if (fOptions & 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 (kData->ctrlChannel >= 0 && kData->ctrlChannel < MAX_MIDI_CHANNELS)
- {
- for (k=0; k < MAX_MIDI_NOTE; ++k)
- fMidiInputPort->DispatchNoteOff(k, 0, kData->ctrlChannel);
- }
-
- kData->needsReset = false;
- }
-
- // --------------------------------------------------------------------------------------------------------
- // Event Input and Processing
-
- {
- // ----------------------------------------------------------------------------------------------------
- // MIDI Input (External)
-
- if (kData->extNotes.mutex.tryLock())
- {
- while (! kData->extNotes.data.isEmpty())
- {
- const ExternalMidiNote& note(kData->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);
- }
-
- kData->extNotes.mutex.unlock();
-
- } // End of MIDI Input (External)
-
- // ----------------------------------------------------------------------------------------------------
- // Event Input (System)
-
- bool allNotesOffSent = false;
- bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
-
- uint32_t time, nEvents = kData->event.portIn->getEventCount();
- uint32_t startTime = 0;
- uint32_t timeOffset = 0;
- uint32_t nextBankId = 0;
-
- if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
- nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
-
- for (i=0; i < nEvents; ++i)
- {
- const EngineEvent& event(kData->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 (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
- nextBankId = kData->midiprog.data[kData->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 == kData->ctrlChannel)
- {
- double value;
-
- if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
- {
- value = ctrlEvent.value;
- setDryWet(value, false, false);
- postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
- continue;
- }
-
- if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
- {
- value = ctrlEvent.value*127/100;
- setVolume(value, false, false);
- postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
- continue;
- }
-
- if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
- {
- double left, right;
- value = ctrlEvent.value/0.5 - 1.0;
-
- if (value < 0.0)
- {
- left = -1.0;
- right = (value*2)+1.0;
- }
- else if (value > 0.0)
- {
- left = (value*2)-1.0;
- right = 1.0;
- }
- else
- {
- left = -1.0;
- right = 1.0;
- }
-
- setBalanceLeft(left, false, false);
- setBalanceRight(right, false, false);
- postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
- postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
- continue;
- }
- }
- #endif
-
- // Control plugin parameters
- for (k=0; k < kData->param.count; ++k)
- {
- if (kData->param.data[k].midiChannel != event.channel)
- continue;
- if (kData->param.data[k].midiCC != ctrlEvent.param)
- continue;
- if (kData->param.data[k].type != PARAMETER_INPUT)
- continue;
- if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
- continue;
-
- double value;
-
- if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
- {
- value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
- }
- else
- {
- value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
-
- if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
- value = std::rint(value);
- }
-
- setParameterValue(k, value, false, false, false);
- postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
- }
-
- break;
- }
-
- case kEngineControlEventTypeMidiBank:
- if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
- nextBankId = ctrlEvent.param;
- break;
-
- case kEngineControlEventTypeMidiProgram:
- if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
- {
- const uint32_t nextProgramId = ctrlEvent.param;
-
- for (k=0; k < kData->midiprog.count; ++k)
- {
- if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
- {
- setMidiProgram(k, false, false, false);
- postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
- break;
- }
- }
- }
- break;
-
- case kEngineControlEventTypeAllSoundOff:
- if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, sampleAccurate ? startTime : time);
- }
- break;
-
- case kEngineControlEventTypeAllNotesOff:
- if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
- {
- if (event.channel == kData->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);
-
- 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);
-
- postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
- }
- else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & 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) && (fOptions & 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_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
- {
- //const uint8_t pressure = midiEvent.data[1];
-
- // unsupported
- }
- else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & 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;
- }
- }
- }
-
- kData->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 (kData->engine->isOffline())
- {
- kData->singleMutex.lock();
- }
- else if (! kData->singleMutex.tryLock())
- {
- for (i=0; i < kData->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 = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
- const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
-
- float oldBufLeft[doBalance ? frames : 1];
-
- for (i=0; i < kData->audioOut.count; ++i)
- {
- // Balance
- if (doBalance)
- {
- if (i % 2 == 0)
- carla_copyFloat(oldBufLeft, outBuffer[i], frames);
-
- float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
- float balRangeR = (kData->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] *= kData->postProc.volume;
- }
- }
-
- } // End of Post-processing
- #endif
-
- // --------------------------------------------------------------------------------------------------------
-
- kData->singleMutex.unlock();
- return true;
- }
-
- // -------------------------------------------------------------------
- // Plugin buffers
-
- // nothing
-
- // -------------------------------------------------------------------
-
- const void* getExtraStuff() override
- {
- return kUses16Outs ? (const void*)0x1 : nullptr;
- }
-
- bool init(const char* filename, const char* const name, const char* label)
- {
- CARLA_ASSERT(kData->engine != nullptr);
- CARLA_ASSERT(kData->client == nullptr);
- CARLA_ASSERT(filename != nullptr);
- CARLA_ASSERT(label != nullptr);
-
- // ---------------------------------------------------------------
- // first checks
-
- if (kData->engine == nullptr)
- {
- return false;
- }
-
- if (kData->client != nullptr)
- {
- kData->engine->setLastError("Plugin client is already registered");
- return false;
- }
-
- if (filename == nullptr)
- {
- kData->engine->setLastError("null filename");
- return false;
- }
-
- if (label == nullptr)
- {
- kData->engine->setLastError("null label");
- return false;
- }
-
- // ---------------------------------------------------------------
- // Check if file exists
- {
- QFileInfo file(filename);
-
- if (! (file.exists() && file.isFile() && file.isReadable()))
- {
- kData->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)
- {
- kData->engine->setLastError(e.what());
- return false;
- }
-
- // ---------------------------------------------------------------
- // Get the Engine's Instrument Manager
-
- fInstrument = fEngine->GetInstrumentManager();
-
- if (fInstrument == nullptr)
- {
- kData->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)
- {
- kData->engine->setLastError(e.what());
- LinuxSampler::EngineFactory::Destroy(fEngine);
- fEngine = nullptr;
- return false;
- }
-
- // ---------------------------------------------------------------
- // Get info
-
- if (fInstrumentIds.size() == 0)
- {
- kData->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)
- {
- kData->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();
- fFilename = filename;
-
- if (name != nullptr)
- fName = kData->engine->getUniquePluginName(name);
- else
- fName = kData->engine->getUniquePluginName((const char*)fRealName);
-
- // ---------------------------------------------------------------
- // Register client
-
- kData->client = kData->engine->addClient(this);
-
- if (kData->client == nullptr || ! kData->client->isOk())
- {
- kData->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
- fOptions = 0x0;
-
- fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
- fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
- fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
-
- // load settings
- kData->idStr = kIsGIG ? "GIG" : "SFZ";
- kData->idStr += "/";
- kData->idStr += label;
- fOptions = kData->loadSettings(fOptions, availableOptions());
- }
-
- 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() == 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
-
- #else // WANT_LINUXSAMPLER
- # warning linuxsampler not available (no GIG and SFZ support)
- #endif
-
- 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;
- #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;
- #endif
- }
-
- CARLA_BACKEND_END_NAMESPACE
|