|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780 |
- /*
- * Carla JACK Engine
- * Copyright (C) 2012-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 "CarlaEngineInternal.hpp"
- #include "CarlaBackendUtils.hpp"
- #include "CarlaMIDI.h"
-
- #ifdef JACKBRIDGE_EXPORT
- # include "jackbridge/JackBridge.hpp"
- #else
- # include "jackbridge/JackBridge.cpp"
- #endif
-
- #include <cmath>
- #include <QtCore/QStringList>
-
- CARLA_BACKEND_START_NAMESPACE
-
- #if 0
- } // Fix editor indentation
- #endif
-
- // -------------------------------------------------------------------
- // Plugin Helpers, defined in CarlaPlugin.cpp
-
- extern CarlaEngine* CarlaPluginGetEngine(CarlaPlugin* const plugin);
- extern CarlaEngineClient* CarlaPluginGetEngineClient(CarlaPlugin* const plugin);
- extern CarlaEngineAudioPort* CarlaPluginGetAudioInPort(CarlaPlugin* const plugin, uint32_t index);
- extern CarlaEngineAudioPort* CarlaPluginGetAudioOutPort(CarlaPlugin* const plugin, uint32_t index);
-
- // -------------------------------------------------------------------------------------------------------------------
- // Carla Engine JACK-Audio port
-
- class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
- {
- public:
- CarlaEngineJackAudioPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineAudioPort(isInput, processMode),
- kClient(client),
- kPort(port)
- {
- carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(client != nullptr && port != nullptr);
- }
- else
- {
- CARLA_ASSERT(client == nullptr && port == nullptr);
- }
- }
-
- ~CarlaEngineJackAudioPort() override
- {
- carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
-
- if (kClient != nullptr && kPort != nullptr)
- jackbridge_port_unregister(kClient, kPort);
- }
-
- void initBuffer(CarlaEngine* const engine) override
- {
- CARLA_ASSERT(engine != nullptr);
-
- if (engine == nullptr)
- {
- fBuffer = nullptr;
- return;
- }
-
- if (kPort == nullptr)
- return CarlaEngineAudioPort::initBuffer(engine);
-
- fBuffer = (float*)jackbridge_port_get_buffer(kPort, engine->getBufferSize());
-
- if (! kIsInput)
- carla_zeroFloat(fBuffer, engine->getBufferSize());
- }
-
- private:
- jack_client_t* const kClient;
- jack_port_t* const kPort;
-
- friend class CarlaEngineJack;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
- // Carla Engine JACK-Event port
-
- static const EngineEvent kFallbackJackEngineEvent;
-
- class CarlaEngineJackEventPort : public CarlaEngineEventPort
- {
- public:
- CarlaEngineJackEventPort(const bool isInput, const ProcessMode processMode, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineEventPort(isInput, processMode),
- kClient(client),
- kPort(port),
- fJackBuffer(nullptr)
- {
- carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port);
-
- if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(client != nullptr && port != nullptr);
- }
- else
- {
- CARLA_ASSERT(client == nullptr && port == nullptr);
- }
- }
-
- ~CarlaEngineJackEventPort() override
- {
- carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
-
- if (kClient != nullptr && kPort != nullptr)
- jackbridge_port_unregister(kClient, kPort);
- }
-
- void initBuffer(CarlaEngine* const engine) override
- {
- CARLA_ASSERT(engine != nullptr);
-
- if (engine == nullptr)
- {
- fJackBuffer = nullptr;
- return;
- }
-
- if (kPort == nullptr)
- return CarlaEngineEventPort::initBuffer(engine);
-
- fJackBuffer = jackbridge_port_get_buffer(kPort, engine->getBufferSize());
-
- if (! kIsInput)
- jackbridge_midi_clear_buffer(fJackBuffer);
- }
-
- uint32_t getEventCount() override
- {
- if (kPort == nullptr)
- return CarlaEngineEventPort::getEventCount();
-
- CARLA_ASSERT(kIsInput);
- CARLA_ASSERT(fJackBuffer != nullptr);
-
- if (! kIsInput)
- return 0;
- if (fJackBuffer == nullptr)
- return 0;
-
- return jackbridge_midi_get_event_count(fJackBuffer);
- }
-
- const EngineEvent& getEvent(const uint32_t index) override
- {
- if (kPort == nullptr)
- return CarlaEngineEventPort::getEvent(index);
-
- CARLA_ASSERT(kIsInput);
- CARLA_ASSERT(fJackBuffer != nullptr);
-
- if (! kIsInput)
- return kFallbackJackEngineEvent;
- if (fJackBuffer == nullptr)
- return kFallbackJackEngineEvent;
-
- jack_midi_event_t jackEvent;
-
- if ((! jackbridge_midi_event_get(&jackEvent, fJackBuffer, index)) || jackEvent.size > 4)
- return kFallbackJackEngineEvent;
-
- fRetEvent.clear();
-
- const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
- const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
-
- fRetEvent.time = jackEvent.time;
- fRetEvent.channel = midiChannel;
-
- if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
- {
- const uint8_t midiControl = jackEvent.buffer[1];
- fRetEvent.type = kEngineEventTypeControl;
-
- if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
- {
- const uint8_t midiBank = jackEvent.buffer[2];
-
- fRetEvent.ctrl.type = kEngineControlEventTypeMidiBank;
- fRetEvent.ctrl.param = midiBank;
- fRetEvent.ctrl.value = 0.0f;
- }
- else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
- {
- fRetEvent.ctrl.type = kEngineControlEventTypeAllSoundOff;
- fRetEvent.ctrl.param = 0;
- fRetEvent.ctrl.value = 0.0f;
- }
- else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
- {
- fRetEvent.ctrl.type = kEngineControlEventTypeAllNotesOff;
- fRetEvent.ctrl.param = 0;
- fRetEvent.ctrl.value = 0.0f;
- }
- else
- {
- const uint8_t midiValue = jackEvent.buffer[2];
-
- fRetEvent.ctrl.type = kEngineControlEventTypeParameter;
- fRetEvent.ctrl.param = midiControl;
- fRetEvent.ctrl.value = float(midiValue)/127.0f;
- }
- }
- else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
- {
- const uint8_t midiProgram = jackEvent.buffer[1];
- fRetEvent.type = kEngineEventTypeControl;
-
- fRetEvent.ctrl.type = kEngineControlEventTypeMidiProgram;
- fRetEvent.ctrl.param = midiProgram;
- fRetEvent.ctrl.value = 0.0f;
- }
- else
- {
- fRetEvent.type = kEngineEventTypeMidi;
-
- fRetEvent.midi.data[0] = midiStatus;
- fRetEvent.midi.data[1] = jackEvent.buffer[1];
- fRetEvent.midi.data[2] = jackEvent.buffer[2];
- fRetEvent.midi.size = static_cast<uint8_t>(jackEvent.size);
- }
-
- return fRetEvent;
- }
-
- void writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) override
- {
- if (kPort == nullptr)
- return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
-
- CARLA_ASSERT(! kIsInput);
- CARLA_ASSERT(fJackBuffer != nullptr);
- CARLA_ASSERT(type != kEngineControlEventTypeNull);
- CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
- CARLA_ASSERT(param < MAX_MIDI_VALUE);
- CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
-
- if (kIsInput)
- return;
- if (fJackBuffer == nullptr)
- return;
- if (type == kEngineControlEventTypeNull)
- return;
- if (channel >= MAX_MIDI_CHANNELS)
- return;
- if (param >= MAX_MIDI_VALUE)
- return;
- if (type == kEngineControlEventTypeParameter)
- {
- CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
- }
-
- const float fixedValue = carla_fixValue<float>(0.0f, 1.0f, value);
-
- uint8_t data[4] = { 0 };
- uint8_t size = 0;
-
- switch (type)
- {
- case kEngineControlEventTypeNull:
- break;
- case kEngineControlEventTypeParameter:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = static_cast<uint8_t>(param);
- data[2] = uint8_t(fixedValue * 127.0f);
- size = 3;
- break;
- case kEngineControlEventTypeMidiBank:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_BANK_SELECT;
- data[2] = static_cast<uint8_t>(param);
- size = 3;
- break;
- case kEngineControlEventTypeMidiProgram:
- data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
- data[1] = static_cast<uint8_t>(param);
- size = 2;
- break;
- case kEngineControlEventTypeAllSoundOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- size = 2;
- break;
- case kEngineControlEventTypeAllNotesOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- size = 2;
- break;
- }
-
- if (size > 0)
- jackbridge_midi_event_write(fJackBuffer, time, data, size);
- }
-
- void writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t* const data, const uint8_t size) override
- {
- if (kPort == nullptr)
- return CarlaEngineEventPort::writeMidiEvent(time, channel, port, data, size);
-
- CARLA_ASSERT(! kIsInput);
- CARLA_ASSERT(fJackBuffer != nullptr);
- CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
- CARLA_ASSERT(data != nullptr);
- CARLA_ASSERT(size > 0);
-
- if (kIsInput)
- return;
- if (fJackBuffer == nullptr)
- return;
- if (channel >= MAX_MIDI_CHANNELS)
- return;
- if (data == nullptr)
- return;
- if (size == 0)
- return;
-
- uint8_t jdata[size];
- carla_copy<uint8_t>(jdata, data, size);
-
- jdata[0] = data[0] + channel;
-
- jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
- }
-
- private:
- jack_client_t* const kClient;
- jack_port_t* const kPort;
-
- void* fJackBuffer;
- EngineEvent fRetEvent;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
- // Jack Engine client
-
- class CarlaEngineJackClient : public CarlaEngineClient
- {
- public:
- CarlaEngineJackClient(const EngineType engineType, const ProcessMode processMode, jack_client_t* const client)
- : CarlaEngineClient(engineType, processMode),
- kClient(client),
- kUseClient(processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(%s, %s, %p)", EngineType2Str(engineType), ProcessMode2Str(processMode), client);
-
- if (kUseClient)
- {
- CARLA_ASSERT(kClient != nullptr);
- }
- else
- {
- CARLA_ASSERT(kClient == nullptr);
- }
- }
-
- ~CarlaEngineJackClient() override
- {
- carla_debug("CarlaEngineClient::~CarlaEngineClient()");
-
- if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- if (kClient)
- jackbridge_client_close(kClient);
- }
- }
-
- void activate() override
- {
- carla_debug("CarlaEngineJackClient::activate()");
-
- if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(kClient && ! fActive);
-
- if (kClient && ! fActive)
- jackbridge_activate(kClient);
- }
-
- CarlaEngineClient::activate();
- }
-
- void deactivate() override
- {
- carla_debug("CarlaEngineJackClient::deactivate()");
-
- if (kProcessMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(kClient && fActive);
-
- if (kClient && fActive)
- jackbridge_deactivate(kClient);
- }
-
- CarlaEngineClient::deactivate();
- }
-
- bool isOk() const override
- {
- carla_debug("CarlaEngineJackClient::isOk()");
-
- if (kUseClient)
- return (kClient != nullptr);
-
- return CarlaEngineClient::isOk();
- }
-
- #if 0
- void setLatency(const uint32_t samples) override
- {
- CarlaEngineClient::setLatency(samples);
-
- if (kUseClient)
- jackbridge_recompute_total_latencies(kClient);
- }
- #endif
-
- CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput) override
- {
- carla_debug("CarlaEngineJackClient::addPort(%s, \"%s\", %s)", EnginePortType2Str(portType), name, bool2str(isInput));
-
- jack_port_t* port = nullptr;
-
- // Create JACK port first, if needed
- if (kUseClient)
- {
- switch (portType)
- {
- case kEnginePortTypeNull:
- break;
- case kEnginePortTypeAudio:
- port = jackbridge_port_register(kClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
- break;
- case kEnginePortTypeEvent:
- port = jackbridge_port_register(kClient, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
- break;
- }
- }
-
- // Create Engine port
- switch (portType)
- {
- case kEnginePortTypeNull:
- break;
- case kEnginePortTypeAudio:
- return new CarlaEngineJackAudioPort(isInput, kProcessMode, kClient, port);
- case kEnginePortTypeEvent:
- return new CarlaEngineJackEventPort(isInput, kProcessMode, kClient, port);
- }
-
- carla_stderr("CarlaEngineJackClient::addPort(%s, \"%s\", %s) - invalid type", EnginePortType2Str(portType), name, bool2str(isInput));
- return nullptr;
- }
-
- private:
- jack_client_t* const kClient;
- const bool kUseClient;
-
- friend class CarlaEngineJack;
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
- // Jack Engine
-
- class CarlaEngineJack : public CarlaEngine
- {
- public:
- CarlaEngineJack()
- : CarlaEngine(),
- fClient(nullptr),
- fTransportState(JackTransportStopped),
- fFreewheel(false),
- #ifdef BUILD_BRIDGE
- fHasQuit(false)
- #else
- # ifndef QTCREATOR_TEST
- fRackPorts{nullptr},
- fLastGroupId(0),
- # endif
- fLastPortId(0),
- fLastConnectionId(0)
- #endif
- {
- carla_debug("CarlaEngineJack::CarlaEngineJack()");
-
- #ifdef BUILD_BRIDGE
- fOptions.processMode = PROCESS_MODE_MULTIPLE_CLIENTS;
- #endif
- // FIXME: Always enable JACK transport for now
- fOptions.transportMode = TRANSPORT_MODE_JACK;
-
- carla_zeroStruct<jack_position_t>(fTransportPos);
- }
-
- ~CarlaEngineJack() override
- {
- carla_debug("CarlaEngineJack::~CarlaEngineJack()");
- CARLA_ASSERT(fClient == nullptr);
-
- #ifndef BUILD_BRIDGE
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
- #endif
- }
-
- // -------------------------------------------------------------------
- // Maximum values
-
- unsigned int maxClientNameSize() const override
- {
- if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- return static_cast<unsigned int>(jackbridge_client_name_size());
-
- return CarlaEngine::maxClientNameSize();
- }
-
- unsigned int maxPortNameSize() const override
- {
- if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT || fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- return static_cast<unsigned int>(jackbridge_port_name_size());
-
- return CarlaEngine::maxPortNameSize();
- }
-
- // -------------------------------------------------------------------
- // Virtual, per-engine type calls
-
- bool init(const char* const clientName) override
- {
- carla_debug("CarlaEngineJack::init(\"%s\")", clientName);
-
- fFreewheel = false;
- fTransportState = JackTransportStopped;
-
- carla_zeroStruct<jack_position_t>(fTransportPos);
-
- #ifndef BUILD_BRIDGE
- fLastGroupId = 0;
- fLastPortId = 0;
- fLastConnectionId = 0;
-
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
-
- fClient = jackbridge_client_open(clientName, JackNullOption, nullptr);
-
- if (fClient != nullptr)
- {
- fBufferSize = jackbridge_get_buffer_size(fClient);
- fSampleRate = jackbridge_get_sample_rate(fClient);
-
- jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
- jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
- jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
- jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
- jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
- # if 0
- jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
- # endif
-
- const char* const jackClientName(jackbridge_get_client_name(fClient));
-
- initJackPatchbay(jackClientName);
-
- jackbridge_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
- jackbridge_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
- jackbridge_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
- jackbridge_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
-
- if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- fRackPorts[rackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
- fRackPorts[rackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
- fRackPorts[rackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
- fRackPorts[rackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
- fRackPorts[rackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
- fRackPorts[rackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
- }
-
- if (jackbridge_activate(fClient))
- {
- return CarlaEngine::init(jackClientName);
- }
- else
- {
- setLastError("Failed to activate the JACK client");
- jackbridge_client_close(fClient);
- fClient = nullptr;
- }
- }
- else
- setLastError("Failed to create new JACK client");
-
- return false;
- #else
- if (fBufferSize == 0 || fSampleRate == 0.0)
- {
- // open temp client to get initial buffer-size and sample-rate values
- if (jack_client_t* tmpClient = jackbridge_client_open(clientName, JackNullOption, nullptr))
- {
- fBufferSize = jackbridge_get_buffer_size(tmpClient);
- fSampleRate = jackbridge_get_sample_rate(tmpClient);
-
- jackbridge_client_close(tmpClient);
- }
- }
-
- return CarlaEngine::init(clientName);
- #endif
- }
-
- bool close() override
- {
- carla_debug("CarlaEngineJack::close()");
- CarlaEngine::close();
-
- #ifdef BUILD_BRIDGE
- fClient = nullptr;
- fHasQuit = true;
- return true;
- #else
- if (jackbridge_deactivate(fClient))
- {
- if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioIn1]);
- jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioIn2]);
- jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioOut1]);
- jackbridge_port_unregister(fClient, fRackPorts[rackPortAudioOut2]);
- jackbridge_port_unregister(fClient, fRackPorts[rackPortEventIn]);
- jackbridge_port_unregister(fClient, fRackPorts[rackPortEventOut]);
- }
-
- if (jackbridge_client_close(fClient))
- {
- fClient = nullptr;
- return true;
- }
- else
- setLastError("Failed to close the JACK client");
- }
- else
- setLastError("Failed to deactivate the JACK client");
-
- fClient = nullptr;
-
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
- #endif
- return false;
- }
-
- bool isRunning() const override
- {
- #ifdef BUILD_BRIDGE
- return (fClient != nullptr || ! fHasQuit);
- #else
- return (fClient != nullptr);
- #endif
- }
-
- bool isOffline() const override
- {
- return fFreewheel;
- }
-
- EngineType type() const override
- {
- return kEngineTypeJack;
- }
-
- CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
- {
- jack_client_t* client = nullptr;
-
- #ifdef BUILD_BRIDGE
- client = fClient = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
-
- fBufferSize = jackbridge_get_buffer_size(client);
- fSampleRate = jackbridge_get_sample_rate(client);
-
- jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
- jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
- jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
- jackbridge_set_process_callback(client, carla_jack_process_callback, this);
- jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
- # if 0
- jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
- # endif
- #else
- if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- client = fClient;
- }
- else if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- client = jackbridge_client_open(plugin->name(), JackNullOption, nullptr);
- jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
- # if 0
- jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
- # endif
- }
- #endif
-
- return new CarlaEngineJackClient(kEngineTypeJack, fOptions.processMode, client);
- }
-
- #ifndef BUILD_BRIDGE
- const char* renamePlugin(const unsigned int id, const char* const newName) override
- {
- CARLA_ASSERT(kData->curPluginCount > 0);
- CARLA_ASSERT(id < kData->curPluginCount);
- CARLA_ASSERT(kData->plugins != nullptr);
- CARLA_ASSERT(newName != nullptr);
-
- if (kData->plugins == nullptr)
- {
- setLastError("Critical error: no plugins are currently loaded!");
- return nullptr;
- }
-
- CarlaPlugin* const plugin = kData->plugins[id].plugin;
-
- if (plugin == nullptr)
- {
- carla_stderr("CarlaEngine::clonePlugin(%i) - could not find plugin", id);
- return nullptr;
- }
-
- CARLA_ASSERT(plugin->id() == id);
-
- const char* name = getUniquePluginName(newName);
-
- // JACK client rename
- if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- if (bridge.client_rename_ptr != nullptr)
- {
- jack_client_t* const client = ((CarlaEngineJackClient*)CarlaPluginGetEngineClient(plugin))->kClient;
- name = bridge.client_rename_ptr(client, name);
- }
- else
- {
- setLastError("Your current JACK version does not allow renaming of clients");
- return nullptr;
- }
- }
-
- if (name == nullptr)
- return nullptr;
-
- // Rename
- plugin->setName(name);
-
- if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- // reload plugin to recreate its ports
- const SaveState& saveState(plugin->getSaveState());
- plugin->reload();
- plugin->loadSaveState(saveState);
- }
-
- return name;
- }
-
- // -------------------------------------------------------------------
- // Patchbay
-
- bool patchbayConnect(int portA, int portB) override
- {
- CARLA_ASSERT(fClient != nullptr);
-
- if (fClient == nullptr)
- {
- setLastError("Invalid JACK client");
- return false;
- }
-
- const char* const portNameA = getFullPortName(portA).toUtf8().constData();
- const char* const portNameB = getFullPortName(portB).toUtf8().constData();
-
- if (! jackbridge_connect(fClient, portNameA, portNameB))
- {
- setLastError("JACK operation failed");
- return false;
- }
-
- return true;
- }
-
- bool patchbayDisconnect(int connectionId) override
- {
- CARLA_ASSERT(fClient != nullptr);
-
- if (fClient == nullptr)
- {
- setLastError("Invalid JACK client");
- return false;
- }
-
- for (int i=0, count=fUsedConnections.count(); i < count; ++i)
- {
- if (fUsedConnections[i].id == connectionId)
- {
- const char* const portNameA = getFullPortName(fUsedConnections[i].portOut).toUtf8().constData();
- const char* const portNameB = getFullPortName(fUsedConnections[i].portIn).toUtf8().constData();
-
- if (! jackbridge_disconnect(fClient, portNameA, portNameB))
- {
- setLastError("JACK operation failed");
- return false;
- }
-
- return true;
- }
- }
-
- setLastError("Failed to find the requested connection");
- return false;
- }
-
- void patchbayRefresh() override
- {
- CARLA_ASSERT(fClient != nullptr);
-
- if (fClient == nullptr)
- return;
-
- fLastGroupId = 0;
- fLastPortId = 0;
- fLastConnectionId = 0;
-
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
-
- initJackPatchbay(jackbridge_get_client_name(fClient));
- }
- #endif
-
- // -------------------------------------------------------------------
- // Transport
-
- void transportPlay() override
- {
- if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
- CarlaEngine::transportPlay();
- else if (fClient != nullptr)
- jackbridge_transport_start(fClient);
- }
-
- void transportPause() override
- {
- if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
- CarlaEngine::transportPause();
- else if (fClient != nullptr)
- jackbridge_transport_stop(fClient);
- }
-
- void transportRelocate(const uint32_t frame) override
- {
- if (fOptions.transportMode == TRANSPORT_MODE_INTERNAL)
- CarlaEngine::transportRelocate(frame);
- else if (fClient != nullptr)
- jackbridge_transport_locate(fClient, frame);
- }
-
- // -------------------------------------
-
- protected:
- void handleJackBufferSizeCallback(const uint32_t newBufferSize)
- {
- if (fBufferSize == newBufferSize)
- return;
-
- fBufferSize = newBufferSize;
- bufferSizeChanged(newBufferSize);
- }
-
- void handleJackSampleRateCallback(const double newSampleRate)
- {
- if (fSampleRate == newSampleRate)
- return;
-
- fSampleRate = newSampleRate;
- sampleRateChanged(newSampleRate);
- }
-
- void handleJackFreewheelCallback(const bool isFreewheel)
- {
- fFreewheel = isFreewheel;
- }
-
- void saveTransportInfo()
- {
- if (fOptions.transportMode != TRANSPORT_MODE_JACK)
- return;
-
- fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate
-
- fTransportState = jackbridge_transport_query(fClient, &fTransportPos);
-
- fTimeInfo.playing = (fTransportState == JackTransportRolling);
-
- if (fTransportPos.unique_1 == fTransportPos.unique_2)
- {
- fTimeInfo.frame = fTransportPos.frame;
- fTimeInfo.usecs = fTransportPos.usecs;
-
- if (fTransportPos.valid & JackPositionBBT)
- {
- fTimeInfo.valid = EngineTimeInfo::ValidBBT;
- fTimeInfo.bbt.bar = fTransportPos.bar;
- fTimeInfo.bbt.beat = fTransportPos.beat;
- fTimeInfo.bbt.tick = fTransportPos.tick;
- fTimeInfo.bbt.barStartTick = fTransportPos.bar_start_tick;
- fTimeInfo.bbt.beatsPerBar = fTransportPos.beats_per_bar;
- fTimeInfo.bbt.beatType = fTransportPos.beat_type;
- fTimeInfo.bbt.ticksPerBeat = fTransportPos.ticks_per_beat;
- fTimeInfo.bbt.beatsPerMinute = fTransportPos.beats_per_minute;
- }
- else
- fTimeInfo.valid = 0x0;
- }
- else
- {
- fTimeInfo.frame = 0;
- fTimeInfo.valid = 0x0;
- }
- }
-
- void handleJackProcessCallback(const uint32_t nframes)
- {
- saveTransportInfo();
-
- #ifndef BUILD_BRIDGE
- if (kData->curPluginCount == 0)
- {
- // pass-through
- if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn1], nframes);
- float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn2], nframes);
- float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut1], nframes);
- float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut2], nframes);
- void* const eventOut = jackbridge_port_get_buffer(fRackPorts[rackPortEventOut], nframes);
-
- CARLA_ASSERT(audioIn1 != nullptr);
- CARLA_ASSERT(audioIn2 != nullptr);
- CARLA_ASSERT(audioOut1 != nullptr);
- CARLA_ASSERT(audioOut2 != nullptr);
- CARLA_ASSERT(eventOut != nullptr);
-
- carla_copyFloat(audioOut1, audioIn1, nframes);
- carla_copyFloat(audioOut2, audioIn2, nframes);
- jackbridge_midi_clear_buffer(eventOut);
- }
-
- return proccessPendingEvents();
- }
- #endif
-
- #ifdef BUILD_BRIDGE
- CarlaPlugin* const plugin = getPluginUnchecked(0);
-
- if (plugin && plugin->enabled() && plugin->tryLock())
- {
- plugin->initBuffers();
- processPlugin(plugin, nframes);
- plugin->unlock();
- }
- #else
- if (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT)
- {
- for (unsigned int i=0; i < kData->curPluginCount; ++i)
- {
- CarlaPlugin* const plugin = getPluginUnchecked(i);
-
- if (plugin && plugin->enabled() && plugin->tryLock())
- {
- plugin->initBuffers();
- processPlugin(plugin, nframes);
- plugin->unlock();
- }
- }
- }
- else if (fOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
- {
- // get buffers from jack
- float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn1], nframes);
- float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioIn2], nframes);
- float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut1], nframes);
- float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[rackPortAudioOut2], nframes);
- void* const eventIn = jackbridge_port_get_buffer(fRackPorts[rackPortEventIn], nframes);
- void* const eventOut = jackbridge_port_get_buffer(fRackPorts[rackPortEventOut], nframes);
-
- // assert buffers
- CARLA_ASSERT(audioIn1 != nullptr);
- CARLA_ASSERT(audioIn2 != nullptr);
- CARLA_ASSERT(audioOut1 != nullptr);
- CARLA_ASSERT(audioOut2 != nullptr);
- CARLA_ASSERT(eventIn != nullptr);
- CARLA_ASSERT(eventOut != nullptr);
-
- // create audio buffers
- float* inBuf[2] = { audioIn1, audioIn2 };
- float* outBuf[2] = { audioOut1, audioOut2 };
-
- // initialize input events
- carla_zeroStruct<EngineEvent>(kData->rack.in, RACK_EVENT_COUNT);
- {
- uint32_t engineEventIndex = 0;
-
- jack_midi_event_t jackEvent;
- const uint32_t jackEventCount = jackbridge_midi_get_event_count(eventIn);
-
- for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; ++jackEventIndex)
- {
- if (jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex) != 0)
- continue;
-
- EngineEvent* const engineEvent = &kData->rack.in[engineEventIndex++];
- engineEvent->clear();
-
- const uint8_t midiStatus = MIDI_GET_STATUS_FROM_DATA(jackEvent.buffer);
- const uint8_t midiChannel = MIDI_GET_CHANNEL_FROM_DATA(jackEvent.buffer);
-
- engineEvent->time = jackEvent.time;
- engineEvent->channel = midiChannel;
-
- if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus))
- {
- const uint8_t midiControl = jackEvent.buffer[1];
- engineEvent->type = kEngineEventTypeControl;
-
- if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
- {
- const uint8_t midiBank = jackEvent.buffer[2];
-
- engineEvent->ctrl.type = kEngineControlEventTypeMidiBank;
- engineEvent->ctrl.param = midiBank;
- engineEvent->ctrl.value = 0.0f;
- }
- else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
- {
- engineEvent->ctrl.type = kEngineControlEventTypeAllSoundOff;
- engineEvent->ctrl.param = 0;
- engineEvent->ctrl.value = 0.0f;
- }
- else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
- {
- engineEvent->ctrl.type = kEngineControlEventTypeAllNotesOff;
- engineEvent->ctrl.param = 0;
- engineEvent->ctrl.value = 0.0f;
- }
- else
- {
- const uint8_t midiValue = jackEvent.buffer[2];
-
- engineEvent->ctrl.type = kEngineControlEventTypeParameter;
- engineEvent->ctrl.param = midiControl;
- engineEvent->ctrl.value = float(midiValue)/127.0f;
- }
- }
- else if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus))
- {
- const uint8_t midiProgram = jackEvent.buffer[1];
- engineEvent->type = kEngineEventTypeControl;
-
- engineEvent->ctrl.type = kEngineControlEventTypeMidiProgram;
- engineEvent->ctrl.param = midiProgram;
- engineEvent->ctrl.value = 0.0f;
- }
- else if (jackEvent.size <= 4)
- {
- engineEvent->type = kEngineEventTypeMidi;
-
- carla_copy<uint8_t>(engineEvent->midi.data, jackEvent.buffer, jackEvent.size);
-
- engineEvent->midi.data[0] = midiStatus;
- engineEvent->midi.size = static_cast<uint8_t>(jackEvent.size);
- }
-
- if (engineEventIndex >= RACK_EVENT_COUNT)
- break;
- }
- }
-
- // process rack
- processRack(inBuf, outBuf, nframes);
-
- // output control
- {
- jackbridge_midi_clear_buffer(eventOut);
-
- for (unsigned short i=0; i < RACK_EVENT_COUNT; ++i)
- {
- EngineEvent* const engineEvent = &kData->rack.out[i];
-
- uint8_t data[3] = { 0 };
- uint8_t size = 0;
-
- switch (engineEvent->type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl:
- {
- EngineControlEvent* const ctrlEvent = &engineEvent->ctrl;
-
- if (ctrlEvent->type == kEngineControlEventTypeParameter && MIDI_IS_CONTROL_BANK_SELECT(ctrlEvent->param))
- {
- // FIXME?
- ctrlEvent->type = kEngineControlEventTypeMidiBank;
- ctrlEvent->param = ctrlEvent->value;
- ctrlEvent->value = 0.0f;
- }
-
- switch (ctrlEvent->type)
- {
- case kEngineControlEventTypeNull:
- break;
- case kEngineControlEventTypeParameter:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
- data[1] = static_cast<uint8_t>(ctrlEvent->param);
- data[2] = uint8_t(ctrlEvent->value * 127.0f);
- size = 3;
- break;
- case kEngineControlEventTypeMidiBank:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
- data[1] = MIDI_CONTROL_BANK_SELECT;
- data[2] = static_cast<uint8_t>(ctrlEvent->param);
- size = 3;
- break;
- case kEngineControlEventTypeMidiProgram:
- data[0] = MIDI_STATUS_PROGRAM_CHANGE + engineEvent->channel;
- data[1] = static_cast<uint8_t>(ctrlEvent->param);
- size = 2;
- break;
- case kEngineControlEventTypeAllSoundOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
- data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- size = 2;
- break;
- case kEngineControlEventTypeAllNotesOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + engineEvent->channel;
- data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- size = 2;
- break;
- }
- break;
- }
-
- case kEngineEventTypeMidi:
- {
- EngineMidiEvent* const midiEvent = &engineEvent->midi;
-
- data[0] = midiEvent->data[0];
- data[1] = midiEvent->data[1];
- data[2] = midiEvent->data[2];
- size = midiEvent->size;
- break;
- }
- }
-
- if (size > 0)
- jackbridge_midi_event_write(eventOut, engineEvent->time, data, size);
- }
- }
- }
- #endif // ! BUILD_BRIDGE
-
- proccessPendingEvents();
- }
-
- #if 0
- void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
- {
- if (fOptions.processMode != PROCESS_MODE_SINGLE_CLIENT)
- return;
-
- for (unsigned int i=0; i < kData->curPluginCount; ++i)
- {
- CarlaPlugin* const plugin = getPluginUnchecked(i);
-
- if (plugin && plugin->enabled())
- latencyPlugin(plugin, mode);
- }
- }
- #endif
-
- #ifndef BUILD_BRIDGE
- void handleJackClientRegistrationCallback(const char* name, bool reg)
- {
- if (reg)
- {
- GroupNameToId groupNameToId;
- groupNameToId.id = fLastGroupId;
- groupNameToId.name = name;
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, fLastGroupId, 0, 0.0f, name);
- fUsedGroupNames.append(groupNameToId);
- fLastGroupId++;
- }
- else
- {
- for (int i=0, count=fUsedGroupNames.count(); i < count; ++i)
- {
- if (fUsedGroupNames[i].name == name)
- {
- callback(CALLBACK_PATCHBAY_CLIENT_REMOVED, 0, fUsedGroupNames[i].id, 0, 0.0f, nullptr);
- fUsedGroupNames.takeAt(i);
- break;
- }
- }
- }
- }
-
- void handleJackPortRegistrationCallback(jack_port_id_t port, bool reg)
- {
- jack_port_t* jackPort = jackbridge_port_by_id(fClient, port);
-
- QString fullName(jackbridge_port_name(jackPort));
- QString groupName = fullName.split(":").at(0);
- int groupId = getGroupId(groupName);
-
- const char* portName = jackbridge_port_short_name(jackPort);
-
- if (reg)
- {
- bool portIsInput = (jackbridge_port_flags(jackPort) & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
-
- unsigned int portFlags = 0x0;
- portFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
- portFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
-
- PortNameToId portNameToId;
- portNameToId.groupId = groupId;
- portNameToId.portId = fLastPortId;
- portNameToId.name = portName;
- portNameToId.fullName = fullName;
-
- fUsedPortNames.append(portNameToId);
- callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, fLastPortId, portFlags, portName);
- fLastPortId++;
- }
- else
- {
- for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
- {
- if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
- {
- callback(CALLBACK_PATCHBAY_PORT_REMOVED, 0, fUsedPortNames[i].portId, 0, 0.0f, nullptr);
- fUsedPortNames.takeAt(i);
- break;
- }
- }
- }
- }
-
- void handleJackPortConnectCallback(jack_port_id_t a, jack_port_id_t b, bool connect)
- {
- jack_port_t* jackPortA = jackbridge_port_by_id(fClient, a);
- jack_port_t* jackPortB = jackbridge_port_by_id(fClient, b);
-
- int portIdA = getPortId(QString(jackbridge_port_name(jackPortA)));
- int portIdB = getPortId(QString(jackbridge_port_name(jackPortB)));
-
- if (connect)
- {
- ConnectionToId connectionToId;
- connectionToId.id = fLastConnectionId;
- connectionToId.portOut = portIdA;
- connectionToId.portIn = portIdB;
-
- fUsedConnections.append(connectionToId);
- callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, fLastConnectionId, portIdA, portIdB, nullptr);
- fLastConnectionId++;
- }
- else
- {
- for (int i=0, count=fUsedConnections.count(); i < count; ++i)
- {
- if (fUsedConnections[i].portOut == portIdA && fUsedConnections[i].portIn == portIdB)
- {
- callback(CALLBACK_PATCHBAY_CONNECTION_REMOVED, 0, fUsedConnections[i].id, 0, 0.0f, nullptr);
- fUsedConnections.takeAt(i);
- break;
- }
- }
- }
- }
-
- void handleJackPortRenameCallback(jack_port_id_t port, const char* oldName, const char* newName)
- {
- jack_port_t* jackPort = jackbridge_port_by_id(fClient, port);
-
- QString fullName(oldName);
- QString groupName = fullName.split(":").at(0);
- int groupId = getGroupId(groupName);
-
- const char* portName = jackbridge_port_short_name(jackPort);
-
- for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
- {
- if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
- {
- callback(CALLBACK_PATCHBAY_PORT_RENAMED, 0, fUsedPortNames[i].portId, 0, 0.0f, newName);
- fUsedPortNames[i].name = newName;
- break;
- }
- }
- }
- #endif
-
- void handleJackShutdownCallback()
- {
- for (unsigned int i=0; i < kData->curPluginCount; ++i)
- {
- //CarlaPlugin* const plugin = getPluginUnchecked(i);
-
- //if (plugin)
- // plugin->x_client = nullptr;
- }
-
- fClient = nullptr;
- callback(CALLBACK_QUIT, 0, 0, 0, 0.0f, nullptr);
- }
-
- // -------------------------------------
-
- private:
- jack_client_t* fClient;
- jack_position_t fTransportPos;
- jack_transport_state_t fTransportState;
- bool fFreewheel;
-
- // -------------------------------------
-
- #ifdef BUILD_BRIDGE
- bool fHasQuit;
- #else
- enum RackPorts {
- rackPortAudioIn1 = 0,
- rackPortAudioIn2 = 1,
- rackPortAudioOut1 = 2,
- rackPortAudioOut2 = 3,
- rackPortEventIn = 4,
- rackPortEventOut = 5,
- rackPortCount = 8
- };
-
- jack_port_t* fRackPorts[rackPortCount];
-
- struct GroupNameToId {
- int id;
- QString name;
- };
-
- struct PortNameToId {
- int groupId;
- int portId;
- QString name;
- QString fullName;
- };
-
- struct ConnectionToId {
- int id;
- int portOut;
- int portIn;
- };
-
- int fLastGroupId;
- int fLastPortId;
- int fLastConnectionId;
-
- QList<GroupNameToId> fUsedGroupNames;
- QList<PortNameToId> fUsedPortNames;
- QList<ConnectionToId> fUsedConnections;
-
- int getGroupId(QString groupName)
- {
- for (int i=0, count=fUsedGroupNames.count(); i < count; ++i)
- {
- if (fUsedGroupNames[i].name == groupName)
- {
- return fUsedGroupNames[i].id;
- }
- }
- return -1;
- }
-
- int getPortId(QString fullPortName)
- {
- QString groupName = fullPortName.split(":").at(0);
- QString portName = fullPortName.replace(groupName+":", "");
-
- int groupId = getGroupId(groupName);
-
- for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
- {
- if (fUsedPortNames[i].groupId == groupId && fUsedPortNames[i].name == portName)
- {
- return fUsedPortNames[i].portId;
- }
- }
-
- return -1;
- }
-
- QString& getFullPortName(int portId)
- {
- static QString fallbackString;
-
- for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
- {
- if (fUsedPortNames[i].portId == portId)
- {
- return fUsedPortNames[i].fullName;
- }
- }
-
- return fallbackString;
- }
-
- void initJackPatchbay(const char* const ourName)
- {
- // query initial jack ports
- QList<QString> parsedGroups;
-
- // our client
- {
- GroupNameToId groupNameToId;
- groupNameToId.id = fLastGroupId;
- groupNameToId.name = ourName;
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, fLastGroupId, 0, 0.0f, ourName);
- fUsedGroupNames.append(groupNameToId);
- fLastGroupId++;
-
- parsedGroups.append(QString(ourName));
- }
-
- if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0))
- {
- for (int i=0; ports[i] != nullptr; ++i)
- {
- jack_port_t* jackPort = jackbridge_port_by_name(fClient, ports[i]);
- const char* portName = jackbridge_port_short_name(jackPort);
-
- QString fullName(ports[i]);
- QString groupName(fullName.split(":").at(0));
- int groupId = -1;
-
- //if (groupName == ourName)
- // continue;
-
- if (parsedGroups.contains(groupName))
- {
- groupId = getGroupId(groupName);
- }
- else
- {
- groupId = fLastGroupId++;
-
- GroupNameToId groupNameToId;
- groupNameToId.id = groupId;
- groupNameToId.name = groupName;
-
- fUsedGroupNames.append(groupNameToId);
- parsedGroups.append(groupName);
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, 0, 0.0f, groupName.toUtf8().constData());
- }
-
- bool portIsInput = (jackbridge_port_flags(jackPort) & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
-
- unsigned int portFlags = 0x0;
- portFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
- portFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
-
- PortNameToId portNameToId;
- portNameToId.groupId = groupId;
- portNameToId.portId = fLastPortId;
- portNameToId.name = portName;
- portNameToId.fullName = fullName;
-
- fUsedPortNames.append(portNameToId);
- callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, fLastPortId, portFlags, portName);
- fLastPortId++;
- }
-
- jackbridge_free(ports);
- }
-
- // query connections, after all ports are in place
- if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
- {
- for (int i=0; ports[i] != nullptr; ++i)
- {
- jack_port_t* jackPort = jackbridge_port_by_name(fClient, ports[i]);
-
- int thisPortId = getPortId(QString(ports[i]));
-
- if (const char** jackConnections = jackbridge_port_get_connections(jackPort))
- {
- for (int j=0; jackConnections[j] != nullptr; ++j)
- {
- int targetPortId = getPortId(QString(jackConnections[j]));
-
- ConnectionToId connectionToId;
- connectionToId.id = fLastConnectionId;
- connectionToId.portOut = thisPortId;
- connectionToId.portIn = targetPortId;
-
- fUsedConnections.append(connectionToId);
- callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, fLastConnectionId, thisPortId, targetPortId, nullptr);
- fLastConnectionId++;
- }
-
- jackbridge_free(jackConnections);
- }
- }
-
- jackbridge_free(ports);
- }
- }
- #endif
-
- // -------------------------------------
-
- void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
- {
- const uint32_t inCount = plugin->audioInCount();
- const uint32_t outCount = plugin->audioOutCount();
-
- float* inBuffer[inCount];
- float* outBuffer[outCount];
-
- float inPeaks[inCount];
- float outPeaks[outCount];
-
- if (inCount > 0)
- carla_zeroFloat(inPeaks, inCount);
- if (outCount > 0)
- carla_zeroFloat(outPeaks, outCount);
-
- for (uint32_t i=0; i < inCount; ++i)
- {
- CarlaEngineAudioPort* const port = CarlaPluginGetAudioInPort(plugin, i);
- inBuffer[i] = port->getBuffer();
- }
-
- for (uint32_t i=0; i < outCount; ++i)
- {
- CarlaEngineAudioPort* const port = CarlaPluginGetAudioOutPort(plugin, i);
- outBuffer[i] = port->getBuffer();
- }
-
- for (uint32_t i=0; i < inCount; ++i)
- {
- for (uint32_t j=0; j < nframes; ++j)
- {
- const float absV = std::fabs(inBuffer[i][j]);
-
- if (absV > inPeaks[i])
- inPeaks[i] = absV;
- }
- }
-
- plugin->process(inBuffer, outBuffer, nframes);
-
- for (uint32_t i=0; i < outCount; ++i)
- {
- for (uint32_t j=0; j < nframes; ++j)
- {
- const float absV = std::fabs(outBuffer[i][j]);
-
- if (absV > outPeaks[i])
- outPeaks[i] = absV;
- }
- }
-
- setPeaks(plugin->id(), inPeaks, outPeaks);
- }
-
- #if 0
- void latencyPlugin(CarlaPlugin* const plugin, jack_latency_callback_mode_t mode)
- {
- const uint32_t inCount = plugin->audioInCount();
- const uint32_t outCount = plugin->audioOutCount();
-
- jack_latency_range_t range;
- uint32_t pluginLatency = plugin->latency();
-
- if (pluginLatency == 0)
- return;
-
- if (mode == JackCaptureLatency)
- {
- for (uint32_t i=0; i < inCount; ++i)
- {
- uint32_t aOutI = (i >= outCount) ? outCount : i;
- jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, i))->kPort;
- jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, aOutI))->kPort;
-
- jackbridge_port_get_latency_range(portIn, mode, &range);
- range.min += pluginLatency;
- range.max += pluginLatency;
- jackbridge_port_set_latency_range(portOut, mode, &range);
- }
- }
- else
- {
- for (uint32_t i=0; i < outCount; ++i)
- {
- uint32_t aInI = (i >= inCount) ? inCount : i;
- jack_port_t* const portIn = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioInPort(plugin, aInI))->kPort;
- jack_port_t* const portOut = ((CarlaEngineJackAudioPort*)CarlaPluginGetAudioOutPort(plugin, i))->kPort;
-
- jackbridge_port_get_latency_range(portOut, mode, &range);
- range.min += pluginLatency;
- range.max += pluginLatency;
- jackbridge_port_set_latency_range(portIn, mode, &range);
- }
- }
- }
- #endif
-
- // -------------------------------------
-
- #define handlePtr ((CarlaEngineJack*)arg)
-
- static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
- {
- handlePtr->handleJackSampleRateCallback(newSampleRate);
- return 0;
- }
-
- static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
- {
- handlePtr->handleJackBufferSizeCallback(newBufferSize);
- return 0;
- }
-
- static void carla_jack_freewheel_callback(int starting, void* arg)
- {
- handlePtr->handleJackFreewheelCallback(bool(starting));
- }
-
- static int carla_jack_process_callback(jack_nframes_t nframes, void* arg)
- {
- handlePtr->handleJackProcessCallback(nframes);
- return 0;
- }
-
- #if 0
- static void carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
- {
- handlePtr->handleJackLatencyCallback(mode);
- }
- #endif
-
- #ifndef BUILD_BRIDGE
- static void carla_jack_client_registration_callback(const char* name, int reg, void* arg)
- {
- handlePtr->handleJackClientRegistrationCallback(name, (reg != 0));
- }
-
- static void carla_jack_port_registration_callback(jack_port_id_t port, int reg, void* arg)
- {
- handlePtr->handleJackPortRegistrationCallback(port, (reg != 0));
- }
-
- static void carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
- {
- handlePtr->handleJackPortConnectCallback(a, b, (connect != 0));
- }
-
- static int carla_jack_port_rename_callback(jack_port_id_t port, const char* oldName, const char* newName, void* arg)
- {
- handlePtr->handleJackPortRenameCallback(port, oldName, newName);
- return 0;
- }
- #endif
-
- static void carla_jack_shutdown_callback(void* arg)
- {
- handlePtr->handleJackShutdownCallback();
- }
-
- #undef handlePtr
-
- // -------------------------------------
-
- #ifndef BUILD_BRIDGE
- static int carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg)
- {
- CarlaPlugin* const plugin = (CarlaPlugin*)arg;
-
- if (plugin != nullptr && plugin->enabled() && plugin->tryLock())
- {
- CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
-
- plugin->initBuffers();
- engine->saveTransportInfo();
- engine->processPlugin(plugin, nframes);
- plugin->unlock();
- }
- else
- carla_stdout("Plugin not enabled or locked");
-
- return 0;
- }
-
- # if 0
- static void carla_jack_latency_callback_plugin(jack_latency_callback_mode_t mode, void* arg)
- {
- CarlaPlugin* const plugin = (CarlaPlugin*)arg;
-
- if (plugin != nullptr && plugin->enabled())
- {
- CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin);
-
- engine->latencyPlugin(plugin, mode);
- }
- }
- # endif
- #endif
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
- };
-
- // -----------------------------------------
-
- CarlaEngine* CarlaEngine::newJack()
- {
- return new CarlaEngineJack();
- }
-
- // -----------------------------------------
-
- CARLA_BACKEND_END_NAMESPACE
|