|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107 |
- /*
- * Carla Plugin Host
- * Copyright (C) 2011-2014 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 "CarlaEngineInternal.hpp"
- #include "CarlaPlugin.hpp"
-
- #include "CarlaBackendUtils.hpp"
- #include "CarlaEngineUtils.hpp"
- #include "CarlaMathUtils.hpp"
- #include "CarlaMIDI.h"
-
- #include "jackbridge/JackBridge.hpp"
-
- #include <QtCore/QStringList>
-
- #define URI_CANVAS_ICON "http://kxstudio.sf.net/ns/canvas/icon"
-
- CARLA_BACKEND_START_NAMESPACE
-
- #if 0
- } // Fix editor indentation
- #endif
-
- class CarlaEngineJack;
-
- // -----------------------------------------------------------------------
- // Fallback data
-
- static const EngineEvent kFallbackJackEngineEvent = { kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, 0.0f }} };
-
- // -----------------------------------------------------------------------
- // Carla Engine JACK-Audio port
-
- class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
- {
- public:
- CarlaEngineJackAudioPort(const CarlaEngine& engine, const bool isInputPort, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineAudioPort(engine, isInputPort),
- fClient(client),
- fPort(port)
- {
- carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %p, %p)", bool2str(isInputPort), client, port);
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT || fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(client != nullptr && port != nullptr);
-
- if (jack_uuid_t uuid = jackbridge_port_uuid(port))
- jackbridge_set_property(client, uuid, "urn:jack:IsControlVoltage", "NO", "text/plain");
- }
- else
- {
- CARLA_ASSERT(client == nullptr && port == nullptr);
- }
- }
-
- ~CarlaEngineJackAudioPort() override
- {
- carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
-
- if (fClient != nullptr && fPort != nullptr)
- {
- jackbridge_port_unregister(fClient, fPort);
- fClient = nullptr;
- fPort = nullptr;
- }
- }
-
- void initBuffer() noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineAudioPort::initBuffer();
-
- const uint32_t bufferSize(fEngine.getBufferSize());
-
- fBuffer = (float*)jackbridge_port_get_buffer(fPort, bufferSize);
-
- if (! fIsInput)
- FLOAT_CLEAR(fBuffer, bufferSize);
- }
-
- private:
- jack_client_t* fClient;
- jack_port_t* fPort;
-
- friend class CarlaEngineJack;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
- };
-
- // -----------------------------------------------------------------------
- // Carla Engine JACK-CV port
-
- class CarlaEngineJackCVPort : public CarlaEngineCVPort
- {
- public:
- CarlaEngineJackCVPort(const CarlaEngine& engine, const bool isInputPort, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineCVPort(engine, isInputPort),
- fClient(client),
- fPort(port)
- {
- carla_debug("CarlaEngineJackCVPort::CarlaEngineJackCVPort(%s, %p, %p)", bool2str(isInputPort), client, port);
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT || fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(client != nullptr && port != nullptr);
-
- if (jack_uuid_t uuid = jackbridge_port_uuid(port))
- jackbridge_set_property(client, uuid, "urn:jack:IsControlVoltage", "YES", "text/plain");
- }
- else
- {
- CARLA_ASSERT(client == nullptr && port == nullptr);
- }
- }
-
- ~CarlaEngineJackCVPort() override
- {
- carla_debug("CarlaEngineJackCVPort::~CarlaEngineJackCVPort()");
-
- if (fClient != nullptr && fPort != nullptr)
- {
- jackbridge_port_unregister(fClient, fPort);
- fClient = nullptr;
- fPort = nullptr;
- }
- }
-
- void initBuffer() noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineCVPort::initBuffer();
-
- const uint32_t bufferSize(fEngine.getBufferSize());
-
- fBuffer = (float*)jackbridge_port_get_buffer(fPort, bufferSize);
-
- if (! fIsInput)
- FLOAT_CLEAR(fBuffer, bufferSize);
- }
-
- private:
- jack_client_t* fClient;
- jack_port_t* fPort;
-
- friend class CarlaEngineJack;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackCVPort)
- };
-
- // -----------------------------------------------------------------------
- // Carla Engine JACK-Event port
-
- class CarlaEngineJackEventPort : public CarlaEngineEventPort
- {
- public:
- CarlaEngineJackEventPort(const CarlaEngine& engine, const bool isInputPort, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineEventPort(engine, isInputPort),
- fClient(client),
- fPort(port),
- fJackBuffer(nullptr)
- {
- carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %p, %p)", bool2str(isInputPort), client, port);
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT || fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(client != nullptr && port != nullptr);
- }
- else
- {
- CARLA_ASSERT(client == nullptr && port == nullptr);
- }
- }
-
- ~CarlaEngineJackEventPort() override
- {
- carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
-
- if (fClient != nullptr && fPort != nullptr)
- {
- jackbridge_port_unregister(fClient, fPort);
- fClient = nullptr;
- fPort = nullptr;
- }
- }
-
- void initBuffer() noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineEventPort::initBuffer();
-
- fJackBuffer = jackbridge_port_get_buffer(fPort, fEngine.getBufferSize());
-
- if (! fIsInput)
- jackbridge_midi_clear_buffer(fJackBuffer);
- }
-
- uint32_t getEventCount() const noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineEventPort::getEventCount();
-
- CARLA_SAFE_ASSERT_RETURN(fIsInput, 0);
- CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, 0);
-
- return jackbridge_midi_get_event_count(fJackBuffer);
- }
-
- const EngineEvent& getEvent(const uint32_t index) const noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineEventPort::getEvent(index);
-
- CARLA_SAFE_ASSERT_RETURN(fIsInput, kFallbackJackEngineEvent);
- CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, kFallbackJackEngineEvent);
-
- return getEventUnchecked(index);
- }
-
- const EngineEvent& getEventUnchecked(const uint32_t index) const noexcept override
- {
- jack_midi_event_t jackEvent;
-
- if (! jackbridge_midi_event_get(&jackEvent, fJackBuffer, index))
- return kFallbackJackEngineEvent;
-
- CARLA_SAFE_ASSERT_RETURN(jackEvent.size <= 0xFF /* uint8_t max */, kFallbackJackEngineEvent);
-
- fRetEvent.time = jackEvent.time;
- fRetEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer);
-
- return fRetEvent;
- }
-
- bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
-
- CARLA_SAFE_ASSERT_RETURN(! fIsInput, false);
- CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
- CARLA_SAFE_ASSERT_RETURN(param < 0x5F, false);
- CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
-
- if (type == kEngineControlEventTypeParameter) {
- CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
- }
-
- uint8_t size = 0;
- uint8_t data[3] = { 0, 0, 0 };
-
- EngineControlEvent ctrlEvent = { type, param, value };
- ctrlEvent.dumpToMidiData(channel, size, data);
-
- if (size == 0)
- return false;
-
- return jackbridge_midi_event_write(fJackBuffer, time, data, size);
- }
-
- bool writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t size, const uint8_t* const data) noexcept override
- {
- if (fPort == nullptr)
- return CarlaEngineEventPort::writeMidiEvent(time, channel, port, size, data);
-
- CARLA_SAFE_ASSERT_RETURN(! fIsInput, false);
- CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
- CARLA_SAFE_ASSERT_RETURN(size > 0, false);
- CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
-
- jack_midi_data_t jdata[size];
-
- jdata[0] = static_cast<jack_midi_data_t>(MIDI_GET_STATUS_FROM_DATA(data) + channel);
-
- for (uint8_t i=1; i < size; ++i)
- jdata[i] = data[i];
-
- return jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
- }
-
- private:
- jack_client_t* fClient;
- jack_port_t* fPort;
- void* fJackBuffer;
-
- mutable EngineEvent fRetEvent;
-
- friend class CarlaEngineJack;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
- };
-
- // -----------------------------------------------------------------------
- // Jack Engine client
-
- class CarlaEngineJackClient : public CarlaEngineClient
- {
- public:
- CarlaEngineJackClient(const CarlaEngine& engine, jack_client_t* const client)
- : CarlaEngineClient(engine),
- fClient(client),
- fUseClient(engine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT || engine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(%p)", client);
-
- if (fUseClient)
- {
- CARLA_ASSERT(fClient != nullptr);
- }
- else
- {
- CARLA_ASSERT(fClient == nullptr);
- }
- }
-
- ~CarlaEngineJackClient() override
- {
- carla_debug("CarlaEngineClient::~CarlaEngineClient()");
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS && fClient != nullptr)
- jackbridge_client_close(fClient);
- }
-
- void activate() noexcept override
- {
- carla_debug("CarlaEngineJackClient::activate()");
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr && ! fActive,);
-
- try {
- jackbridge_activate(fClient);
- } catch(...) {}
- }
-
- CarlaEngineClient::activate();
- }
-
- void deactivate() noexcept override
- {
- carla_debug("CarlaEngineJackClient::deactivate()");
-
- if (fEngine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr && fActive,);
-
- try {
- jackbridge_deactivate(fClient);
- } catch(...) {}
- }
-
- CarlaEngineClient::deactivate();
- }
-
- bool isOk() const noexcept override
- {
- if (fUseClient)
- return (fClient != nullptr);
-
- return CarlaEngineClient::isOk();
- }
-
- #if 0
- void setLatency(const uint32_t samples) noexcept override
- {
- CarlaEngineClient::setLatency(samples);
-
- if (fUseClient && fClient != nullptr)
- {
- // try etc
- jackbridge_recompute_total_latencies(fClient);
- }
- }
- #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 (fUseClient && fClient != nullptr)
- {
- switch (portType)
- {
- case kEnginePortTypeNull:
- break;
- case kEnginePortTypeAudio:
- port = jackbridge_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
- break;
- case kEnginePortTypeCV:
- port = jackbridge_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
- break;
- case kEnginePortTypeEvent:
- port = jackbridge_port_register(fClient, name, JACK_DEFAULT_MIDI_TYPE, isInput ? JackPortIsInput : JackPortIsOutput, 0);
- break;
- }
- }
-
- if (port == nullptr)
- {
- carla_stderr("CarlaEngineJackClient::addPort(%s, \"%s\", %s) - failed to create JACK port", EnginePortType2Str(portType), name, bool2str(isInput));
- return nullptr;
- }
-
- // Create Engine port
- switch (portType)
- {
- case kEnginePortTypeNull:
- break;
- case kEnginePortTypeAudio:
- return new CarlaEngineJackAudioPort(fEngine, isInput, fClient, port);
- case kEnginePortTypeCV:
- return new CarlaEngineJackCVPort(fEngine, isInput, fClient, port);
- case kEnginePortTypeEvent:
- return new CarlaEngineJackEventPort(fEngine, isInput, fClient, port);
- }
-
- carla_stderr("CarlaEngineJackClient::addPort(%s, \"%s\", %s) - invalid type", EnginePortType2Str(portType), name, bool2str(isInput));
- return nullptr;
- }
-
- private:
- jack_client_t* fClient;
- const bool fUseClient;
-
- 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
- fIsRunning(false)
- #else
- fLastGroupId(0),
- fLastPortId(0),
- fLastConnectionId(0)
- #endif
- {
- carla_debug("CarlaEngineJack::CarlaEngineJack()");
-
- #ifdef BUILD_BRIDGE
- pData->options.processMode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS;
- #else
- carla_fill<jack_port_t*>(fRackPorts, kRackPortCount, nullptr);
- #endif
-
- // FIXME: Always enable JACK transport for now
- pData->options.transportMode = ENGINE_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();
- //fGroupIconsChanged.clear();
- #endif
- }
-
- // -------------------------------------------------------------------
- // Maximum values
-
- unsigned int getMaxClientNameSize() const noexcept override
- {
- if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT || pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- unsigned int ret = 0;
-
- try {
- ret = static_cast<unsigned int>(jackbridge_client_name_size());
- }
- catch (...) {}
-
- return ret;
- }
-
- return CarlaEngine::getMaxClientNameSize();
- }
-
- unsigned int getMaxPortNameSize() const noexcept override
- {
- if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT || pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- unsigned int ret = 0;
-
- try {
- ret = static_cast<unsigned int>(jackbridge_port_name_size());
- }
- catch (...) {}
-
- return ret;
- }
-
- return CarlaEngine::getMaxPortNameSize();
- }
-
- // -------------------------------------------------------------------
- // 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);
-
- #ifdef BUILD_BRIDGE
- if (pData->bufferSize == 0 || pData->sampleRate == 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))
- {
- pData->bufferSize = jackbridge_get_buffer_size(tmpClient);
- pData->sampleRate = jackbridge_get_sample_rate(tmpClient);
-
- jackbridge_client_close(tmpClient);
- }
- }
-
- fIsRunning = true;
-
- return CarlaEngine::init(clientName);
- #else
- fLastGroupId = 0;
- fLastPortId = 0;
- fLastConnectionId = 0;
-
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
- //fGroupIconsChanged.clear();
-
- fClient = jackbridge_client_open(clientName, JackNullOption, nullptr);
-
- if (fClient != nullptr)
- {
- pData->bufferSize = jackbridge_get_buffer_size(fClient);
- pData->sampleRate = jackbridge_get_sample_rate(fClient);
-
- //jackbridge_custom_publish_data(fClient, URI_CANVAS_ICON, "carla", 6);
- //jackbridge_custom_set_data_appearance_callback(fClient, carla_jack_custom_appearance_callback, this);
-
- 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_latency_callback(fClient, carla_jack_latency_callback, this);
- jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
- jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
-
- 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 (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- fRackPorts[kRackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
- fRackPorts[kRackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
- fRackPorts[kRackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
- fRackPorts[kRackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
- fRackPorts[kRackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
- fRackPorts[kRackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
- }
-
- if (jackbridge_activate(fClient))
- {
- CarlaEngine::init(jackClientName);
- return true;
- }
- else
- {
- setLastError("Failed to activate the JACK client");
- jackbridge_client_close(fClient);
- fClient = nullptr;
- }
- }
- else
- setLastError("Failed to create new JACK client");
-
- return false;
- #endif
- }
-
- bool close() override
- {
- carla_debug("CarlaEngineJack::close()");
- CarlaEngine::close();
-
- #ifdef BUILD_BRIDGE
- fClient = nullptr;
- fIsRunning = false;
- return true;
- #else
- if (jackbridge_deactivate(fClient))
- {
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn1]);
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioIn2]);
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut1]);
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortAudioOut2]);
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventIn]);
- jackbridge_port_unregister(fClient, fRackPorts[kRackPortEventOut]);
- carla_fill<jack_port_t*>(fRackPorts, kRackPortCount, nullptr);
- }
-
- 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();
- //fGroupIconsChanged.clear();
-
- return false;
- #endif
- }
-
- #if 0 //ndef BUILD_BRIDGE
- void idle() override
- {
- CarlaEngine::idle();
-
- if (fGroupIconsChanged.count() == 0)
- return;
-
- static bool checkIcons = false;
-
- if (! checkIcons)
- {
- checkIcons = true; // check them next time
- return;
- }
-
- checkIcons = false;
-
- void* data;
- size_t dataSize;
-
- List<int> groupIconsCopy;
- fGroupIconsChanged.spliceAppend(groupIconsCopy, true);
-
- for (List<int>::Itenerator it = groupIconsCopy.begin(); it.valid(); it.next())
- {
- const int& groupId(*it);
- const char* const groupName(getGroupName(groupId));
-
- data = nullptr;
- dataSize = 0;
-
- if (jackbridge_custom_get_data(fClient, groupName, URI_CANVAS_ICON, &data, &dataSize) && data != nullptr && dataSize != 0)
- {
- const char* const icon((const char*)data);
- CARLA_ASSERT(std::strlen(icon)+1 == dataSize);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ICON_CHANGED, 0, groupId, 0, 0.0f, icon);
-
- jackbridge_free(data);
- }
- }
-
- groupIconsCopy.clear();
- }
- #endif
-
- bool isRunning() const noexcept override
- {
- #ifdef BUILD_BRIDGE
- return (fClient != nullptr || fIsRunning);
- #else
- return (fClient != nullptr);
- #endif
- }
-
- bool isOffline() const noexcept override
- {
- return fFreewheel;
- }
-
- EngineType getType() const noexcept override
- {
- return kEngineTypeJack;
- }
-
- const char* getCurrentDriverName() const noexcept override
- {
- return "JACK";
- }
-
- CarlaEngineClient* addClient(CarlaPlugin* const plugin) override
- {
- //const char* const iconName(plugin->getIconName());
- jack_client_t* client = nullptr;
-
- #ifdef BUILD_BRIDGE
- client = fClient = jackbridge_client_open(plugin->getName(), JackNullOption, nullptr);
-
- CARLA_SAFE_ASSERT_RETURN(client != nullptr, nullptr);
-
- pData->bufferSize = jackbridge_get_buffer_size(client);
- pData->sampleRate = jackbridge_get_sample_rate(client);
-
- //jackbridge_custom_publish_data(client, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
-
- 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_latency_callback(client, carla_jack_latency_callback, this);
- jackbridge_set_process_callback(client, carla_jack_process_callback, this);
- jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
- #else
- if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- client = fClient;
- }
- else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- client = jackbridge_client_open(plugin->getName(), JackNullOption, nullptr);
-
- CARLA_SAFE_ASSERT_RETURN(client != nullptr, nullptr);
-
- //jackbridge_custom_publish_data(client, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
-
- //jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, plugin);
- jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, plugin);
- }
- #endif
- return new CarlaEngineJackClient(*this, client);
- }
-
- #ifndef BUILD_BRIDGE
- const char* renamePlugin(const unsigned int id, const char* const newName) override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount > 0, nullptr);
- CARLA_SAFE_ASSERT_RETURN(id < pData->curPluginCount, nullptr);
- CARLA_SAFE_ASSERT_RETURN(pData->plugins != nullptr, nullptr);
- CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', nullptr);
-
- CarlaPlugin* const plugin(pData->plugins[id].plugin);
-
- if (plugin == nullptr)
- {
- carla_stderr("CarlaEngine::clonePlugin(%i) - could not find plugin", id);
- return nullptr;
- }
-
- CARLA_SAFE_ASSERT(plugin->getId() == id);
-
- bool needsReinit = (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT);
- const char* name = getUniquePluginName(newName);
-
- // TODO - use rename port if single-client and JACK2
-
- // JACK client rename
- if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
-
- #if 0
- if (bridge.client_rename_ptr != nullptr)
- {
- name = bridge.client_rename_ptr(client->fClient, name);
- }
- else
- #endif
- {
- // we should not be able to do this, jack really needs to allow client rename
- needsReinit = true;
-
- if (jack_client_t* const jclient = jackbridge_client_open(name, JackNullOption, nullptr))
- {
- //const char* const iconName(plugin->getIconName());
- //jackbridge_custom_publish_data(jclient, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
-
- // close old client
- plugin->setEnabled(false);
-
- if (client->isActive())
- client->deactivate();
-
- plugin->clearBuffers();
-
- jackbridge_client_close(client->fClient);
-
- // set new client data
- name = jackbridge_get_client_name(jclient);
-
- jackbridge_set_process_callback(jclient, carla_jack_process_callback_plugin, plugin);
- //jackbridge_set_latency_callback(jclient, carla_jack_latency_callback_plugin, plugin);
-
- client->fClient = jclient;
- }
- }
- }
-
- if (name == nullptr)
- return nullptr;
-
- // Rename
- plugin->setName(name);
-
- if (needsReinit)
- {
- // 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_SAFE_ASSERT_RETURN(fClient != nullptr, false);
-
- if (fClient == nullptr)
- {
- setLastError("Invalid JACK client");
- return false;
- }
-
- char portNameA[STR_MAX+1];
- char portNameB[STR_MAX+1];
- getFullPortName(portA, portNameA);
- getFullPortName(portB, portNameB);
-
- if (! jackbridge_connect(fClient, portNameA, portNameB))
- {
- setLastError("JACK operation failed");
- return false;
- }
-
- return true;
- }
-
- bool patchbayDisconnect(uint connectionId) override
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
-
- for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.begin(); it.valid(); it.next())
- {
- const ConnectionToId& connectionToId(it.getValue());
-
- if (connectionToId.id == connectionId)
- {
- char portNameOut[STR_MAX+1];
- char portNameIn[STR_MAX+1];
-
- getFullPortName(connectionToId.portOut, portNameOut);
- getFullPortName(connectionToId.portIn, portNameIn);
-
- if (! jackbridge_disconnect(fClient, portNameOut, portNameIn))
- {
- setLastError("JACK operation failed");
- return false;
- }
-
- return true;
- }
- }
-
- setLastError("Failed to find the requested connection");
- return false;
- }
-
- bool patchbayRefresh() override
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
-
- fLastGroupId = 0;
- fLastPortId = 0;
- fLastConnectionId = 0;
-
- fUsedGroupNames.clear();
- fUsedPortNames.clear();
- fUsedConnections.clear();
- //fGroupIconsChanged.clear();
-
- initJackPatchbay(jackbridge_get_client_name(fClient));
-
- return true;
- }
-
- // -------------------------------------------------------------------
- // Transport
-
- void transportPlay() noexcept override
- {
- if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
- return CarlaEngine::transportPlay();
-
- if (fClient != nullptr)
- {
- try {
- jackbridge_transport_start(fClient);
- } catch(...) {}
- }
- }
-
- void transportPause() noexcept override
- {
- if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
- return CarlaEngine::transportPause();
-
- if (fClient != nullptr)
- {
- try {
- jackbridge_transport_stop(fClient);
- } catch(...) {}
- }
- }
-
- void transportRelocate(const uint64_t frame) noexcept override
- {
- if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
- return CarlaEngine::transportRelocate(frame);
-
- if (fClient != nullptr)
- {
- try {
- jackbridge_transport_locate(fClient, static_cast<jack_nframes_t>(frame));
- } catch(...) {}
- }
- }
-
- // -------------------------------------------------------------------
- // Patchbay stuff
-
- const char* const* getPatchbayConnections() const override
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, nullptr);
- carla_debug("CarlaEngineJack::getPatchbayConnections()");
-
- LinkedList<const char*> connList;
-
- if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
- {
- for (int i=0; ports[i] != nullptr; ++i)
- {
- jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
- const char* const fullPortName(ports[i]);
-
- CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
-
- if (const char** connections = jackbridge_port_get_all_connections(fClient, jackPort))
- {
- for (int j=0; connections[j] != nullptr; ++j)
- {
- connList.append(carla_strdup(fullPortName));
- connList.append(carla_strdup(connections[j]));
- }
-
- jackbridge_free(connections);
- }
- }
-
- jackbridge_free(ports);
- }
-
- const size_t connCount(connList.count());
-
- if (connCount == 0)
- return nullptr;
-
- const char** const retConns = new const char*[connCount+1];
-
- for (size_t i=0; i < connCount; ++i)
- retConns[i] = connList.getAt(i);
-
- retConns[connCount] = nullptr;
- connList.clear();
-
- return retConns;
- }
-
- void restorePatchbayConnection(const char* const connSource, const char* const connTarget) override
- {
- CARLA_SAFE_ASSERT_RETURN(fClient != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
- carla_debug("CarlaEngineJack::restorePatchbayConnection(\"%s\", \"%s\")", connSource, connTarget);
-
- if (jack_port_t* const port = jackbridge_port_by_name(fClient, connSource))
- {
- if (jackbridge_port_by_name(fClient, connTarget) == nullptr)
- return;
-
- if (! jackbridge_port_connected_to(port, connTarget))
- jackbridge_connect(fClient, connSource, connTarget);
- }
- }
- #endif
-
- // -------------------------------------------------------------------
-
- protected:
- void handleJackBufferSizeCallback(const uint32_t newBufferSize)
- {
- if (pData->bufferSize == newBufferSize)
- return;
-
- pData->bufferSize = newBufferSize;
- bufferSizeChanged(newBufferSize);
- }
-
- void handleJackSampleRateCallback(const double newSampleRate)
- {
- if (pData->sampleRate == newSampleRate)
- return;
-
- pData->sampleRate = newSampleRate;
- sampleRateChanged(newSampleRate);
- }
-
- void handleJackFreewheelCallback(const bool isFreewheel)
- {
- if (fFreewheel == isFreewheel)
- return;
-
- fFreewheel = isFreewheel;
- offlineModeChanged(isFreewheel);
- }
-
- void saveTransportInfo()
- {
- if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
- return;
-
- fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate
-
- fTransportState = jackbridge_transport_query(fClient, &fTransportPos);
-
- pData->timeInfo.playing = (fTransportState == JackTransportRolling);
-
- if (fTransportPos.unique_1 == fTransportPos.unique_2)
- {
- pData->timeInfo.frame = fTransportPos.frame;
- pData->timeInfo.usecs = fTransportPos.usecs;
-
- if (fTransportPos.valid & JackPositionBBT)
- {
- pData->timeInfo.valid = EngineTimeInfo::kValidBBT;
- pData->timeInfo.bbt.bar = fTransportPos.bar;
- pData->timeInfo.bbt.beat = fTransportPos.beat;
- pData->timeInfo.bbt.tick = fTransportPos.tick;
- pData->timeInfo.bbt.barStartTick = fTransportPos.bar_start_tick;
- pData->timeInfo.bbt.beatsPerBar = fTransportPos.beats_per_bar;
- pData->timeInfo.bbt.beatType = fTransportPos.beat_type;
- pData->timeInfo.bbt.ticksPerBeat = fTransportPos.ticks_per_beat;
- pData->timeInfo.bbt.beatsPerMinute = fTransportPos.beats_per_minute;
- }
- else
- pData->timeInfo.valid = 0x0;
- }
- else
- {
- pData->timeInfo.frame = 0;
- pData->timeInfo.valid = 0x0;
- }
- }
-
- void handleJackProcessCallback(const uint32_t nframes)
- {
- saveTransportInfo();
-
- if (pData->curPluginCount == 0)
- {
- #ifndef BUILD_BRIDGE
- // pass-through
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
- float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
- float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
- float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
- void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
-
- FLOAT_COPY(audioOut1, audioIn1, nframes);
- FLOAT_COPY(audioOut2, audioIn2, nframes);
-
- jackbridge_midi_clear_buffer(eventOut);
- }
- #endif
-
- return runPendingRtEvents();
- }
-
- #ifdef BUILD_BRIDGE
- CarlaPlugin* const plugin(pData->plugins[0].plugin);
-
- if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
- {
- plugin->initBuffers();
- processPlugin(plugin, nframes);
- plugin->unlock();
- }
-
- return runPendingRtEvents();
- #else
- if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- {
- for (unsigned int i=0; i < pData->curPluginCount; ++i)
- {
- CarlaPlugin* const plugin(pData->plugins[i].plugin);
-
- if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
- {
- plugin->initBuffers();
- processPlugin(plugin, nframes);
- plugin->unlock();
- }
- }
-
- return runPendingRtEvents();
- }
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- {
- // get buffers from jack
- float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
- float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
- float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
- float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
- void* const eventIn = jackbridge_port_get_buffer(fRackPorts[kRackPortEventIn], nframes);
- void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], 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>(pData->bufEvents.in, kMaxEngineEventInternalCount);
- {
- 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))
- continue;
-
- CARLA_SAFE_ASSERT_CONTINUE(jackEvent.size <= 0xFF /* uint8_t max */);
-
- EngineEvent& engineEvent(pData->bufEvents.in[engineEventIndex++]);
-
- engineEvent.time = jackEvent.time;
- engineEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer);
-
- if (engineEventIndex >= kMaxEngineEventInternalCount)
- break;
- }
- }
-
- // process rack
- pData->processRack(inBuf, outBuf, nframes, fFreewheel);
-
- // output control
- {
- jackbridge_midi_clear_buffer(eventOut);
-
- for (unsigned short i=0; i < kMaxEngineEventInternalCount; ++i)
- {
- const EngineEvent& engineEvent(pData->bufEvents.out[i]);
-
- uint8_t size = 0;
- uint8_t data[3] = { 0, 0, 0 };
- const uint8_t* dataPtr = data;
-
- switch (engineEvent.type)
- {
- case kEngineEventTypeNull:
- break;
-
- case kEngineEventTypeControl:
- {
- const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
- ctrlEvent.dumpToMidiData(engineEvent.channel, size, data);
- break;
- }
-
- case kEngineEventTypeMidi:
- {
- const EngineMidiEvent& midiEvent(engineEvent.midi);
-
- size = midiEvent.size;
-
- if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
- dataPtr = midiEvent.dataExt;
- else
- dataPtr = midiEvent.dataExt;
-
- break;
- }
- }
-
- if (size > 0)
- jackbridge_midi_event_write(eventOut, engineEvent.time, dataPtr, size);
- }
- }
-
- return runPendingRtEvents();
- }
- #endif // ! BUILD_BRIDGE
-
- runPendingRtEvents();
- }
-
- #if 0
- void handleJackLatencyCallback(const jack_latency_callback_mode_t mode)
- {
- if (pData->options.processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT)
- return;
-
- for (unsigned int i=0; i < pData->curPluginCount; ++i)
- {
- CarlaPlugin* const plugin(pData->plugins[i].plugin);
-
- if (plugin != nullptr && plugin->isEnabled())
- latencyPlugin(plugin, mode);
- }
- }
- #endif
-
- #ifndef BUILD_BRIDGE
- # if 0
- void handleCustomAppearanceCallback(const char* client_name, const char* key, jack_custom_change_t change)
- {
- if ((change == JackCustomAdded || change == JackCustomReplaced) && std::strcmp(key, URI_CANVAS_ICON) == 0)
- {
- const int groupId (getGroupId(client_name));
-
- if (groupId == -1)
- return;
-
- fGroupIconsChanged.append(groupId);
- }
- }
- # endif
-
- void handleJackClientRegistrationCallback(const char* const name, const bool reg)
- {
- // do nothing on client registration, wait for first port
- if (reg) return;
-
- const int id(getGroupId(name)); // also checks name nullness
-
- if (id < 0)
- return;
-
- GroupNameToId groupNameToId;
- groupNameToId.setData(id, name);
- fUsedGroupNames.removeAll(groupNameToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, static_cast<uint>(id), 0, 0, 0.0f, nullptr);
- }
-
- void handleJackPortRegistrationCallback(const jack_port_id_t port, const bool reg)
- {
- jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
- const char* const portName(jackbridge_port_short_name(jackPort));
- const char* const fullPortName(jackbridge_port_name(jackPort));
-
- CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr,);
-
- CarlaString groupName(fullPortName);
- groupName.truncate(groupName.rfind(portName)-1);
-
- int groupId = getGroupId(groupName);
-
- if (reg)
- {
- const int jackPortFlags(jackbridge_port_flags(jackPort));
-
- if (groupId < 0)
- {
- groupId = fLastGroupId++;
- CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
-
- GroupNameToId groupNameToId;
- groupNameToId.setData(groupId, groupName);
- fUsedGroupNames.append(groupNameToId);
-
- if (jackPortFlags & JackPortIsPhysical)
- {
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, static_cast<uint>(groupId), PATCHBAY_ICON_HARDWARE, 0, 0.0f, groupName);
- // hardware
- }
- else
- {
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, static_cast<uint>(groupId), 0, 0, 0.0f, groupName);
- //fGroupIconsChanged.append(groupId);
- // "application"
- }
- }
-
- bool portIsInput = (jackPortFlags & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
- bool portIsCV = false;
-
- if (jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
- {
- char* value = nullptr;
- char* type = nullptr;
-
- if (jackbridge_get_property(uuid, "urn:jack:IsControlVoltage", &value, &type) && value != nullptr && type != nullptr && std::strcmp(type, "text/plain") == 0)
- portIsCV = (std::strcmp(value, "YES") == 0);
- }
-
- unsigned int canvasPortFlags = 0x0;
- canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
- canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_TYPE_AUDIO : PATCHBAY_PORT_TYPE_MIDI;
-
- if (portIsAudio && portIsCV)
- canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
-
- PortNameToId portNameToId;
- portNameToId.setData(groupId, fLastPortId++, portName, fullPortName);
- fUsedPortNames.append(portNameToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, static_cast<uint>(groupId), portNameToId.portId, static_cast<int>(canvasPortFlags), 0.0f, portName);
- }
- else
- {
- const int portId(getPortId(fullPortName));
-
- CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
- CARLA_SAFE_ASSERT_RETURN(portId >= 0,);
-
- PortNameToId portNameToId;
- portNameToId.setData(groupId, portId, portName, fullPortName);
- fUsedPortNames.removeOne(portNameToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, static_cast<uint>(groupId), portId, 0, 0.0f, nullptr);
- }
- }
-
- void handleJackPortConnectCallback(const jack_port_id_t a, const jack_port_id_t b, const bool connect)
- {
- jack_port_t* const jackPortA(jackbridge_port_by_id(fClient, a));
- jack_port_t* const jackPortB(jackbridge_port_by_id(fClient, b));
- const char* const fullPortNameA(jackbridge_port_name(jackPortA));
- const char* const fullPortNameB(jackbridge_port_name(jackPortB));
-
- CARLA_SAFE_ASSERT_RETURN(jackPortA != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(jackPortB != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr,);
-
- const int portIdA(getPortId(fullPortNameA));
- const int portIdB(getPortId(fullPortNameB));
-
- if (portIdA == -1 || portIdB == -1)
- return;
-
- if (connect)
- {
- ConnectionToId connectionToId;
- connectionToId.setData(fLastConnectionId++, portIdA, portIdB);
- fUsedConnections.append(connectionToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
- }
- else
- {
- for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.begin(); it.valid(); it.next())
- {
- const ConnectionToId& connectionToId(it.getValue());
-
- if (connectionToId.portOut == portIdA && connectionToId.portIn == portIdB)
- {
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, static_cast<uint>(connectionToId.id), connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
- fUsedConnections.remove(it);
- break;
- }
- }
- }
- }
-
- void handleJackClientRenameCallback(const char* const oldName, const char* const newName)
- {
- for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
- {
- GroupNameToId& groupNameToId(it.getValue());
-
- if (std::strcmp(groupNameToId.name, oldName) == 0)
- {
- groupNameToId.rename(newName);
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, static_cast<uint>(groupNameToId.id), 0, 0, 0.0f, newName);
- break;
- }
- }
- }
-
- void handleJackPortRenameCallback(const jack_port_id_t port, const char* const oldName, const char* const newName)
- {
- jack_port_t* const jackPort(jackbridge_port_by_id(fClient, port));
- const char* const portName(jackbridge_port_short_name(jackPort));
-
- CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr,);
-
- CarlaString groupName(newName);
- groupName.truncate(groupName.rfind(portName)-1);
-
- const int groupId(getGroupId(groupName));
-
- CARLA_SAFE_ASSERT_RETURN(groupId >= 0,);
-
- for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
- {
- PortNameToId& portNameId(it.getValue());
-
- if (std::strcmp(portNameId.fullName, oldName) == 0)
- {
- CARLA_SAFE_ASSERT(portNameId.groupId == groupId);
- portNameId.rename(portName, newName);
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_RENAMED, static_cast<uint>(groupId), portNameId.portId, 0, 0.0f, newName);
- break;
- }
- }
- }
- #endif
-
- void handleJackShutdownCallback()
- {
- for (unsigned int i=0; i < pData->curPluginCount; ++i)
- {
- //CarlaPlugin* const plugin(pData->plugins[i].plugin);
-
- //if (plugin)
- // plugin->x_client = nullptr;
- }
-
- fClient = nullptr;
- callback(ENGINE_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 fIsRunning;
- #else
- enum RackPorts {
- kRackPortAudioIn1 = 0,
- kRackPortAudioIn2 = 1,
- kRackPortAudioOut1 = 2,
- kRackPortAudioOut2 = 3,
- kRackPortEventIn = 4,
- kRackPortEventOut = 5,
- kRackPortCount = 6
- };
-
- jack_port_t* fRackPorts[kRackPortCount];
-
- struct GroupNameToId {
- int id;
- char name[STR_MAX+1];
-
- void clear() noexcept
- {
- id = -1;
- name[0] = '\0';
- }
-
- void setData(const int i, const char n[]) noexcept
- {
- id = i;
- rename(n);
- }
-
- void rename(const char n[]) noexcept
- {
- std::strncpy(name, n, STR_MAX);
- name[STR_MAX] = '\0';
- }
-
- bool operator==(const GroupNameToId& groupNameId) const noexcept
- {
- if (groupNameId.id != id)
- return false;
- if (std::strcmp(groupNameId.name, name) != 0)
- return false;
- return true;
- }
-
- bool operator!=(const GroupNameToId& groupNameId) const noexcept
- {
- return !operator==(groupNameId);
- }
- };
-
- struct PortNameToId {
- int groupId;
- int portId;
- char name[STR_MAX+1];
- char fullName[STR_MAX+1]; // unique
-
- void clear() noexcept
- {
- groupId = -1;
- portId = -1;
- name[0] = '\0';
- fullName[0] = '\0';
- }
-
- void setData(const int gId, const int pId, const char n[], const char fn[]) noexcept
- {
- groupId = gId;
- portId = pId;
- rename(n, fn);
- }
-
- void rename(const char n[], const char fn[]) noexcept
- {
- std::strncpy(name, n, STR_MAX);
- name[STR_MAX] = '\0';
-
- std::strncpy(fullName, fn, STR_MAX);
- fullName[STR_MAX] = '\0';
- }
-
- bool operator==(const PortNameToId& portNameId) noexcept
- {
- if (portNameId.groupId != groupId)
- return false;
- if (portNameId.portId != portId)
- return false;
- if (std::strcmp(portNameId.name, name) != 0)
- return false;
- if (std::strcmp(portNameId.fullName, fullName) != 0)
- return false;
- return true;
- }
-
- bool operator!=(const PortNameToId& portNameId) noexcept
- {
- return !operator==(portNameId);
- }
- };
-
- struct ConnectionToId {
- uint id;
- int portOut;
- int portIn;
-
- void clear() noexcept
- {
- id = 0;
- portOut = -1;
- portIn = -1;
- }
-
- void setData(const uint i, const int out, const int in) noexcept
- {
- id = i;
- portOut = out;
- portIn = in;
- }
-
- bool operator==(const ConnectionToId& connectionId) const noexcept
- {
- if (connectionId.id != id)
- return false;
- if (connectionId.portOut != portOut)
- return false;
- if (connectionId.portIn != portIn)
- return false;
- return true;
- }
-
- bool operator!=(const ConnectionToId& connectionId) const noexcept
- {
- return !operator==(connectionId);
- }
- };
-
- int fLastGroupId;
- int fLastPortId;
- uint fLastConnectionId;
-
- LinkedList<GroupNameToId> fUsedGroupNames;
- LinkedList<PortNameToId> fUsedPortNames;
- LinkedList<ConnectionToId> fUsedConnections;
- //LinkedList<int> fGroupIconsChanged;
-
- int getGroupId(const char* const name)
- {
- CARLA_SAFE_ASSERT_RETURN(name != nullptr, -1);
-
- for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
- {
- const GroupNameToId& groupNameId(it.getValue());
-
- if (std::strcmp(groupNameId.name, name) == 0)
- return groupNameId.id;
- }
-
- return -1;
- }
-
- const char* getGroupName(const int groupId)
- {
- static const char fallback[1] = { '\0' };
-
- CARLA_SAFE_ASSERT_RETURN(groupId >= 0, fallback);
-
- for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroupNames.begin(); it.valid(); it.next())
- {
- const GroupNameToId& groupNameId(it.getValue());
-
- if (groupNameId.id == groupId)
- return groupNameId.name;
- }
-
- return fallback;
- }
-
- int getPortId(const char* const fullName)
- {
- CARLA_SAFE_ASSERT_RETURN(fullName != nullptr, -1);
-
- for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
- {
- const PortNameToId& portNameId(it.getValue());
-
- if (std::strcmp(portNameId.fullName, fullName) == 0)
- return portNameId.portId;
- }
-
- return -1;
- }
-
- void getFullPortName(const int portId, char nameBuf[STR_MAX+1])
- {
- for (LinkedList<PortNameToId>::Itenerator it = fUsedPortNames.begin(); it.valid(); it.next())
- {
- const PortNameToId& portNameId(it.getValue());
-
- if (portNameId.portId == portId)
- {
- std::strncpy(nameBuf, portNameId.fullName, STR_MAX);
- nameBuf[STR_MAX] = '\0';
- return;
- }
- }
-
- nameBuf[0] = '\0';
- }
-
- void initJackPatchbay(const char* const ourName)
- {
- CARLA_SAFE_ASSERT_RETURN(fLastGroupId == 0,);
- CARLA_SAFE_ASSERT_RETURN(fLastPortId == 0,);
- CARLA_SAFE_ASSERT_RETURN(fLastConnectionId == 0,);
- CARLA_SAFE_ASSERT_RETURN(ourName != nullptr,);
-
- // query initial jack ports
- QStringList parsedGroups;
-
- // our client
- {
- parsedGroups.append(QString(ourName));
-
- GroupNameToId groupNameToId;
- groupNameToId.setData(fLastGroupId++, ourName);
- fUsedGroupNames.append(groupNameToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, 0 /* our client */, PATCHBAY_ICON_CARLA, 0, 0.0f, ourName);
- }
-
- if (const char** ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0))
- {
- for (int i=0; ports[i] != nullptr; ++i)
- {
- jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
- const char* const portName(jackbridge_port_short_name(jackPort));
- const char* const fullPortName(ports[i]);
-
- CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
- CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr);
-
- const int jackPortFlags(jackbridge_port_flags(jackPort));
-
- int groupId = -1;
-
- bool found;
- CarlaString groupName(fullPortName);
- groupName.truncate(groupName.rfind(portName, &found)-1);
-
- CARLA_SAFE_ASSERT(found);
-
- QString qGroupName((const char*)groupName);
-
- if (parsedGroups.contains(qGroupName))
- {
- groupId = getGroupId(groupName);
- CARLA_SAFE_ASSERT(groupId >= 0);
- }
- else
- {
- groupId = fLastGroupId++;
- parsedGroups.append(qGroupName);
-
- GroupNameToId groupNameToId;
- groupNameToId.setData(groupId, groupName);
- fUsedGroupNames.append(groupNameToId);
-
- PatchbayIcon groupIcon = PATCHBAY_ICON_APPLICATION;
-
- #if 0
- void* data = nullptr;
- size_t dataSize = 0;
- #endif
-
- if (jackPortFlags & JackPortIsPhysical)
- {
- groupIcon = PATCHBAY_ICON_HARDWARE;
- }
- #if 0
- else if (jackbridge_custom_get_data(fClient, groupName, URI_CANVAS_ICON, &data, &dataSize) && data != nullptr && dataSize != 0)
- {
- const char* const icon((const char*)data);
- CARLA_ASSERT(std::strlen(icon)+1 == dataSize);
-
- if (std::strcmp(icon, "app") == 0 || std::strcmp(icon, "application") == 0)
- groupIcon = PATCHBAY_ICON_APPLICATION;
- else if (std::strcmp(icon, "hardware") == 0)
- groupIcon = PATCHBAY_ICON_HARDWARE;
- else if (std::strcmp(icon, "carla") == 0)
- groupIcon = PATCHBAY_ICON_CARLA;
- else if (std::strcmp(icon, "distrho") == 0)
- groupIcon = PATCHBAY_ICON_DISTRHO;
- else if (std::strcmp(icon, "file") == 0)
- groupIcon = PATCHBAY_ICON_FILE;
- else if (std::strcmp(icon, "plugin") == 0)
- groupIcon = PATCHBAY_ICON_PLUGIN;
- }
- #endif
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, static_cast<uint>(groupId), groupIcon, 0, 0.0f, groupName);
- }
-
- bool portIsInput = (jackPortFlags & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
- bool portIsCV = false;
-
- if (jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
- {
- char* value = nullptr;
- char* type = nullptr;
-
- if (jackbridge_get_property(uuid, "urn:jack:IsControlVoltage", &value, &type) && value != nullptr && type != nullptr && std::strcmp(type, "text/plain") == 0)
- portIsCV = (std::strcmp(value, "YES") == 0);
- }
-
- unsigned int canvasPortFlags = 0x0;
- canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
- canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_TYPE_AUDIO : PATCHBAY_PORT_TYPE_MIDI;
-
- if (portIsCV)
- canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
-
- PortNameToId portNameToId;
- portNameToId.setData(groupId, fLastPortId++, portName, fullPortName);
- fUsedPortNames.append(portNameToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, static_cast<uint>(groupId), portNameToId.portId, static_cast<int>(canvasPortFlags), 0.0f, portName);
- }
-
- // query connections, after all ports are in place
- for (int i=0; ports[i] != nullptr; ++i)
- {
- jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
- const char* const fullPortName(ports[i]);
-
- const int thisPortId(getPortId(fullPortName));
-
- CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
- CARLA_SAFE_ASSERT_CONTINUE(thisPortId != -1);
-
- if (const char** connections = jackbridge_port_get_all_connections(fClient, jackPort))
- {
- for (int j=0; connections[j] != nullptr; ++j)
- {
- const int targetPortId(getPortId(connections[j]));
-
- ConnectionToId connectionToId;
- connectionToId.setData(fLastConnectionId++, thisPortId, targetPortId);
- fUsedConnections.append(connectionToId);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
- }
-
- jackbridge_free(connections);
- }
- }
-
- jackbridge_free(ports);
- }
- }
- #endif
-
- // -------------------------------------
-
- void processPlugin(CarlaPlugin* const plugin, const uint32_t nframes)
- {
- const uint32_t inCount(plugin->getAudioInCount());
- const uint32_t outCount(plugin->getAudioOutCount());
-
- float* inBuffer[inCount];
- float* outBuffer[outCount];
-
- float inPeaks[2] = { 0.0f };
- float outPeaks[2] = { 0.0f };
-
- for (uint32_t i=0; i < inCount; ++i)
- {
- CarlaEngineAudioPort* const port(plugin->getAudioInPort(i));
- inBuffer[i] = port->getBuffer();
- }
-
- for (uint32_t i=0; i < outCount; ++i)
- {
- CarlaEngineAudioPort* const port(plugin->getAudioOutPort(i));
- outBuffer[i] = port->getBuffer();
- }
-
- for (uint32_t i=0; i < inCount && i < 2; ++i)
- {
- for (uint32_t j=0; j < nframes; ++j)
- {
- const float absV(std::abs(inBuffer[i][j]));
-
- if (absV > inPeaks[i])
- inPeaks[i] = absV;
- }
- }
-
- plugin->process(inBuffer, outBuffer, nframes);
-
- for (uint32_t i=0; i < outCount && i < 2; ++i)
- {
- for (uint32_t j=0; j < nframes; ++j)
- {
- const float absV(std::abs(outBuffer[i][j]));
-
- if (absV > outPeaks[i])
- outPeaks[i] = absV;
- }
- }
-
- setPluginPeaks(plugin->getId(), 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());
- const uint32_t latency(plugin->getLatencyInFrames());
-
- if (latency == 0)
- return;
-
- //jack_latency_range_t range;
-
- // TODO
-
- 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 += latency;
- range.max += latency;
- 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 += latency;
- range.max += latency;
- jackbridge_port_set_latency_range(portIn, mode, &range);
- }
- }
- }
- #endif
-
- // -------------------------------------
-
- #define handlePtr ((CarlaEngineJack*)arg)
-
- static int carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
- {
- handlePtr->handleJackBufferSizeCallback(newBufferSize);
- return 0;
- }
-
- static int carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
- {
- handlePtr->handleJackSampleRateCallback(newSampleRate);
- 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
- # if 0
- static void carla_jack_custom_appearance_callback(const char* client_name, const char* key, jack_custom_change_t change, void* arg)
- {
- handlePtr->handleCustomAppearanceCallback(client_name, key, change);
- }
- # endif
-
- 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_client_rename_callback(const char* oldName, const char* newName, void* arg)
- {
- handlePtr->handleJackClientRenameCallback(oldName, newName);
- return 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->isEnabled())
- {
- CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
- CARLA_SAFE_ASSERT_RETURN(engine != nullptr,0);
-
- if (plugin->tryLock(engine->fFreewheel))
- {
- plugin->initBuffers();
- engine->saveTransportInfo();
- engine->processPlugin(plugin, nframes);
- plugin->unlock();
- }
- }
-
- 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->isEnabled())
- {
- CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
- CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
-
- engine->latencyPlugin(plugin, mode);
- }
- }
- # endif
- #endif
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
- };
-
- // -----------------------------------------------------------------------
-
- CarlaEngine* CarlaEngine::newJack()
- {
- carla_debug("CarlaEngine::newJack()");
- return new CarlaEngineJack();
- }
-
- // -----------------------------------------------------------------------
-
- CARLA_BACKEND_END_NAMESPACE
|