|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135 |
- /*
- * 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/JackBridge1.cpp"
- # include "jackbridge/JackBridge2.cpp"
- #endif
-
- #include <cmath>
- #include <QtCore/QStringList>
-
- #define URI_CANVAS_ICON "http://kxstudio.sf.net/ns/canvas/icon"
-
- CARLA_BACKEND_START_NAMESPACE
-
- #if 0
- } // Fix editor indentation
- #endif
-
- // -------------------------------------------------------------------------------------------------------------------
- // Fallback data
-
- static const EngineEvent kFallbackJackEngineEvent;
-
- // -------------------------------------------------------------------------------------------------------------------
- // 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)
- return;
-
- if (kPort == nullptr)
- return CarlaEngineAudioPort::initBuffer(engine);
-
- const uint32_t bufferSize(engine->getBufferSize());
-
- fBuffer = (float*)jackbridge_port_get_buffer(kPort, bufferSize);
-
- if (! kIsInput)
- carla_zeroFloat(fBuffer, bufferSize);
- }
-
- 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-CV port
-
- class CarlaEngineJackCVPort : public CarlaEngineCVPort
- {
- public:
- CarlaEngineJackCVPort(const bool isInput, const ProcessMode processMode, const uint32_t bufferSize, jack_client_t* const client, jack_port_t* const port)
- : CarlaEngineCVPort(isInput, processMode, bufferSize),
- kClient(client),
- kPort(port)
- {
- carla_debug("CarlaEngineJackCVPort::CarlaEngineJackCVPort(%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);
- }
- }
-
- ~CarlaEngineJackCVPort() override
- {
- carla_debug("CarlaEngineJackCVPort::~CarlaEngineJackCVPort()");
-
- if (kClient != nullptr && kPort != nullptr)
- jackbridge_port_unregister(kClient, kPort);
- }
-
- void initBuffer(CarlaEngine* const engine) override
- {
- CARLA_ASSERT(engine != nullptr);
-
- if (engine == nullptr)
- return;
-
- if (kPort == nullptr)
- return CarlaEngineCVPort::initBuffer(engine);
-
- CARLA_ASSERT(engine->getBufferSize() == fBufferSize);
-
- if (kIsInput)
- {
- float* const jackBuffer = (float*)jackbridge_port_get_buffer(kPort, fBufferSize);
- carla_copyFloat(fBuffer, jackBuffer, fBufferSize);
- }
- else
- {
- carla_zeroFloat(fBuffer, fBufferSize);
- }
- }
-
- void writeBuffer(CarlaEngine* const engine) override
- {
- CARLA_ASSERT(! kIsInput);
- CARLA_ASSERT(engine != nullptr);
-
- if (kIsInput)
- return;
- if (engine == nullptr)
- return;
-
- CARLA_ASSERT(engine->getBufferSize() == fBufferSize);
-
- float* const jackBuffer = (float*)jackbridge_port_get_buffer(kPort, fBufferSize);
- carla_copyFloat(jackBuffer, fBuffer, fBufferSize);
- }
-
- private:
- jack_client_t* const kClient;
- jack_port_t* const kPort;
-
- friend class CarlaEngineJack;
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackCVPort)
- };
-
- // -------------------------------------------------------------------------------------------------------------------
- // Carla Engine JACK-Event port
-
- 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)
- 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() const 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;
- carla_zeroStruct<jack_midi_event_t>(jackEvent);
-
- if (! jackbridge_midi_event_get(&jackEvent, fJackBuffer, index))
- return kFallbackJackEngineEvent;
- if (jackEvent.size == 0 || 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))
- {
- CARLA_ASSERT(jackEvent.size == 2 || jackEvent.size == 3);
-
- 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
- {
- CARLA_ASSERT(jackEvent.size == 3);
-
- 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))
- {
- CARLA_ASSERT(jackEvent.size == 2);
-
- 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.size = static_cast<uint8_t>(jackEvent.size);
-
- if (jackEvent.size > 1)
- carla_copy<uint8_t>(fRetEvent.midi.data+1, jackEvent.buffer+1, jackEvent.size-1);
- }
-
- 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));
-
- size_t size = 0;
- jack_midi_data_t data[4] = { 0 };
-
- switch (type)
- {
- case kEngineControlEventTypeNull:
- break;
- case kEngineControlEventTypeParameter:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = param;
- data[2] = fixedValue * 127.0f;
- size = 3;
- break;
- case kEngineControlEventTypeMidiBank:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_BANK_SELECT;
- data[2] = param;
- size = 3;
- break;
- case kEngineControlEventTypeMidiProgram:
- data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel;
- data[1] = param;
- size = 2;
- break;
- case kEngineControlEventTypeAllSoundOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
- data[2] = 0;
- size = 3;
- break;
- case kEngineControlEventTypeAllNotesOff:
- data[0] = MIDI_STATUS_CONTROL_CHANGE + channel;
- data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
- data[2] = 0;
- size = 3;
- 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] = MIDI_GET_STATUS_FROM_DATA(data) + 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 CarlaEngine& engine, jack_client_t* const client)
- : CarlaEngineClient(engine),
- kClient(client),
- kUseClient(engine.getProccessMode() == PROCESS_MODE_SINGLE_CLIENT || engine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(name:\"%s\", %p)", engine.getName(), client);
-
- if (kUseClient)
- {
- CARLA_ASSERT(kClient != nullptr);
- }
- else
- {
- CARLA_ASSERT(kClient == nullptr);
- }
- }
-
- ~CarlaEngineJackClient() override
- {
- carla_debug("CarlaEngineClient::~CarlaEngineClient()");
-
- if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS && kClient != nullptr)
- jackbridge_client_close(kClient);
- }
-
- void activate() override
- {
- carla_debug("CarlaEngineJackClient::activate()");
-
- if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(kClient != nullptr && ! fActive);
-
- if (kClient != nullptr && ! fActive)
- jackbridge_activate(kClient);
- }
-
- CarlaEngineClient::activate();
- }
-
- void deactivate() override
- {
- carla_debug("CarlaEngineJackClient::deactivate()");
-
- if (kEngine.getProccessMode() == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CARLA_ASSERT(kClient != nullptr && fActive);
-
- if (kClient != nullptr && 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 kEnginePortTypeCV:
- port = jackbridge_port_register(kClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsControlVoltage | (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, kEngine.getProccessMode(), kClient, port);
- case kEnginePortTypeCV:
- return new CarlaEngineJackCVPort(isInput, kEngine.getProccessMode(), kEngine.getBufferSize(), kClient, port);
- case kEnginePortTypeEvent:
- return new CarlaEngineJackEventPort(isInput, kEngine.getProccessMode(), 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
- # ifdef CARLA_PROPER_CPP11_SUPPORT
- fRackPorts{nullptr},
- # endif
- fLastGroupId(0),
- fLastPortId(0),
- fLastConnectionId(0)
- #endif
- {
- carla_debug("CarlaEngineJack::CarlaEngineJack()");
-
- #ifdef BUILD_BRIDGE
- fOptions.processMode = PROCESS_MODE_MULTIPLE_CLIENTS;
- #else
- # ifndef CARLA_PROPER_CPP11_SUPPORT
- carla_fill<jack_port_t*>(fRackPorts, kRackPortCount, nullptr);
- # endif
- #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_custom_publish_data(fClient, URI_CANVAS_ICON, "carla", 6);
-
- 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[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;
- #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[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]);
- }
-
- 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
- {
- const char* const iconName(plugin->iconName());
- 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_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_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);
-
- CARLA_ASSERT(client != nullptr);
-
- if (client == nullptr)
- return nullptr;
-
- jackbridge_custom_publish_data(client, URI_CANVAS_ICON, iconName, std::strlen(iconName)+1);
-
- 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(*this, 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);
-
- bool needsReinit = (fOptions.processMode == PROCESS_MODE_SINGLE_CLIENT);
- const char* name = getUniquePluginName(newName);
-
- // TODO - use rename port if single-client
-
- // JACK client rename
- if (fOptions.processMode == PROCESS_MODE_MULTIPLE_CLIENTS)
- {
- CarlaEngineJackClient* const client((CarlaEngineJackClient*)CarlaPluginGetEngineClient(plugin));
-
- if (bridge.client_rename_ptr != nullptr)
- {
- name = bridge.client_rename_ptr(client->kClient, name);
- }
- else
- {
- // we should not be able to do this, jack really needs to allow client rename
- needsReinit = true;
-
- if (jack_client_t* jclient = jackbridge_client_open(name, JackNullOption, nullptr))
- {
- const char* const iconName(plugin->iconName());
- 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->kClient);
-
- // set new client data
- name = jackbridge_get_client_name(jclient);
-
- jackbridge_set_process_callback(jclient, carla_jack_process_callback_plugin, plugin);
- # if 0
- jackbridge_set_latency_callback(jclient, carla_jack_latency_callback_plugin, plugin);
- # endif
-
- // this is supposed to be constant...
- std::memcpy((jack_client_t**)&client->kClient, &jclient, sizeof(jack_client_t**));
- }
- }
- }
-
- 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_ASSERT(fClient != nullptr);
-
- 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(int connectionId) override
- {
- CARLA_ASSERT(fClient != nullptr);
-
- if (fClient == nullptr)
- {
- setLastError("Invalid JACK client");
- return false;
- }
-
- foreach (const ConnectionToId& connectionToId, fUsedConnections)
- {
- 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;
- }
-
- 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[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);
-
- 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 != nullptr && 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 != nullptr && 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[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>(kData->bufEvents.in, INTERNAL_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->bufEvents.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 >= INTERNAL_EVENT_COUNT)
- break;
- }
- }
-
- // process rack
- processRack(inBuf, outBuf, nframes);
-
- // output control
- {
- jackbridge_midi_clear_buffer(eventOut);
-
- for (unsigned short i=0; i < INTERNAL_EVENT_COUNT; ++i)
- {
- EngineEvent* const engineEvent = &kData->bufEvents.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)
- {
- // do nothing on client registration, wait for first port
- if (reg) return;
-
- const int id(getGroupId(name)); // also checks name nullness
-
- if (id == -1)
- return;
-
- GroupNameToId groupNameId(id, name);
- fUsedGroupNames.removeAll(groupNameId);
-
- callback(CALLBACK_PATCHBAY_CLIENT_REMOVED, 0, id, 0, 0.0f, nullptr);
- }
-
- void handleJackPortRegistrationCallback(jack_port_id_t port, 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_ASSERT(jackPort != nullptr);
- CARLA_ASSERT(portName != nullptr);
- CARLA_ASSERT(fullPortName != nullptr);
-
- if (jackPort == nullptr)
- return;
- if (portName == nullptr)
- return;
- if (fullPortName == nullptr)
- return;
-
- const size_t groupNameSize(std::strstr(fullPortName, portName) - fullPortName - 1);
- char groupName[groupNameSize+1];
-
- carla_copy<char>(groupName, fullPortName, groupNameSize);
- groupName[groupNameSize] = '\0';
-
- int groupId = getGroupId(groupName);
-
- if (reg)
- {
- const int jackPortFlags(jackbridge_port_flags(jackPort));
-
- if (groupId == -1)
- {
- groupId = fLastGroupId++;
-
- GroupNameToId groupNameToId(groupId, groupName);
- fUsedGroupNames.append(groupNameToId);
-
- PatchbayIconType groupIcon = PATCHBAY_ICON_APPLICATION;
-
- void* data = nullptr;
- size_t 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);
-
- 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;
- }
- else if (jackPortFlags & JackPortIsPhysical)
- groupIcon = PATCHBAY_ICON_HARDWARE;
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, groupIcon, 0.0f, groupName);
- }
-
- bool portIsInput = (jackPortFlags & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
- bool portIsCV = (jackPortFlags & JackPortIsControlVoltage);
-
- unsigned int canvasPortFlags = 0x0;
- canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
- canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
-
- if (portIsAudio && portIsCV)
- canvasPortFlags |= PATCHBAY_PORT_IS_CV;
-
- PortNameToId portNameToId(groupId, fLastPortId++, portName, fullPortName);
- fUsedPortNames.append(portNameToId);
-
- callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, portNameToId.portId, canvasPortFlags, portName);
- }
- else
- {
- const int portId(getPortId(fullPortName));
-
- CARLA_ASSERT(groupId != -1);
- CARLA_ASSERT(portId != -1);
-
- if (groupId == -1 || portId == -1)
- return;
-
- PortNameToId portNameId(groupId, portId, portName, fullPortName);
- fUsedPortNames.removeOne(portNameId);
-
- callback(CALLBACK_PATCHBAY_PORT_REMOVED, 0, portId, 0, 0.0f, nullptr);
- }
- }
-
- void handleJackPortConnectCallback(jack_port_id_t a, jack_port_id_t b, 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_ASSERT(jackPortA != nullptr);
- CARLA_ASSERT(jackPortB != nullptr);
- CARLA_ASSERT(fullPortNameA != nullptr);
- CARLA_ASSERT(fullPortNameB != nullptr);
-
- if (jackPortA == nullptr)
- return;
- if (jackPortB == nullptr)
- return;
- if (fullPortNameA == nullptr)
- return;
- if (fullPortNameB == nullptr)
- return;
-
- const int portIdA(getPortId(fullPortNameA));
- const int portIdB(getPortId(fullPortNameB));
-
- if (portIdA == -1 || portIdB == -1)
- return;
-
- if (connect)
- {
- ConnectionToId connectionToId(fLastConnectionId++, portIdA, portIdB);
- fUsedConnections.append(connectionToId);
-
- callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, connectionToId.id, portIdA, portIdB, nullptr);
- }
- else
- {
- for (int i=0, count=fUsedConnections.count(); i < count; ++i)
- {
- const ConnectionToId& connectionToId(fUsedConnections[i]);
-
- if (connectionToId.portOut == portIdA && connectionToId.portIn == portIdB)
- {
- callback(CALLBACK_PATCHBAY_CONNECTION_REMOVED, 0, connectionToId.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* const jackPort(jackbridge_port_by_id(fClient, port));
- const char* const portName(jackbridge_port_short_name(jackPort));
-
- CARLA_ASSERT(jackPort != nullptr);
- CARLA_ASSERT(portName != nullptr);
-
- if (jackPort == nullptr)
- return;
- if (portName == nullptr)
- return;
-
- const size_t groupNameSize(std::strstr(newName, portName) - newName - 1);
- char groupName[groupNameSize+1];
-
- carla_copy<char>(groupName, newName, groupNameSize);
- groupName[groupNameSize] = '\0';
-
- const int groupId(getGroupId(groupName));
-
- CARLA_ASSERT(groupId != -1);
-
- if (groupId == -1)
- return;
-
- for (int i=0, count=fUsedPortNames.count(); i < count; ++i)
- {
- PortNameToId& portNameId(fUsedPortNames[i]);
-
- if (/*portNameId.groupId == groupId &&*/ std::strcmp(portNameId.fullName, oldName) == 0)
- {
- CARLA_ASSERT(portNameId.groupId == groupId);
- portNameId.rename(portName, newName);
- callback(CALLBACK_PATCHBAY_PORT_RENAMED, 0, portNameId.portId, 0, 0.0f, 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 {
- 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];
-
- GroupNameToId()
- {
- id = -1;
- name[0] = '\0';
- }
-
- GroupNameToId(const int id, const char name[])
- {
- this->id = id;
- std::strncpy(this->name, name, STR_MAX);
- this->name[STR_MAX] = '\0';
- }
-
- bool operator==(const GroupNameToId& groupNameId)
- {
- if (groupNameId.id != id)
- return false;
- if (std::strcmp(groupNameId.name, name) != 0)
- return false;
- return true;
- }
- };
-
- struct PortNameToId {
- int groupId;
- int portId;
- char name[STR_MAX+1];
- char fullName[STR_MAX+1]; // unique
-
- PortNameToId()
- {
- groupId = -1;
- portId = -1;
- name[0] = '\0';
- fullName[0] = '\0';
- }
-
- PortNameToId(const int groupId, const int portId, const char name[], const char fullName[])
- {
- this->groupId = groupId;
- this->portId = portId;
- std::strncpy(this->name, name, STR_MAX);
- this->name[STR_MAX] = '\0';
- std::strncpy(this->fullName, fullName, STR_MAX);
- this->fullName[STR_MAX] = '\0';
- }
-
- void rename(const char name[], const char fullName[])
- {
- std::strncpy(this->name, name, STR_MAX);
- this->name[STR_MAX] = '\0';
- std::strncpy(this->fullName, fullName, STR_MAX);
- this->fullName[STR_MAX] = '\0';
- }
-
- bool operator==(const PortNameToId& portNameId)
- {
- 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;
- }
- };
-
- struct ConnectionToId {
- int id;
- int portOut;
- int portIn;
-
- ConnectionToId()
- {
- id = -1;
- portOut = -1;
- portIn = -1;
- }
-
- ConnectionToId(const int id, const int portOut, const int portIn)
- {
- this->id = id;
- this->portOut = portOut;
- this->portIn = portIn;
- }
-
- bool operator==(const ConnectionToId& connectionId)
- {
- if (connectionId.id != id)
- return false;
- if (connectionId.portOut != portOut)
- return false;
- if (connectionId.portIn != portIn)
- return false;
- return true;
- }
- };
-
- int fLastGroupId;
- int fLastPortId;
- int fLastConnectionId;
-
- QList<GroupNameToId> fUsedGroupNames;
- QList<PortNameToId> fUsedPortNames;
- QList<ConnectionToId> fUsedConnections;
-
- int getGroupId(const char* const name)
- {
- CARLA_ASSERT(name != nullptr);
-
- if (name == nullptr)
- return -1;
-
- foreach (const GroupNameToId& groupNameId, fUsedGroupNames)
- {
- if (std::strcmp(groupNameId.name, name) == 0)
- return groupNameId.id;
- }
-
- return -1;
- }
-
- int getPortId(const char* const fullName)
- {
- CARLA_ASSERT(fullName != nullptr);
-
- if (fullName == nullptr)
- return -1;
-
- foreach (const PortNameToId& portNameId, fUsedPortNames)
- {
- if (std::strcmp(portNameId.fullName, fullName) == 0)
- return portNameId.portId;
- }
-
- return -1;
- }
-
- void getFullPortName(const int portId, char nameBuf[STR_MAX+1])
- {
- foreach (const PortNameToId& portNameId, fUsedPortNames)
- {
- 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_ASSERT(fLastGroupId == 0);
- CARLA_ASSERT(fLastPortId == 0);
- CARLA_ASSERT(fLastConnectionId == 0);
- CARLA_ASSERT(ourName != nullptr);
-
- // query initial jack ports
- QStringList parsedGroups;
-
- // our client
- if (ourName != nullptr)
- {
- parsedGroups.append(QString(ourName));
-
- GroupNameToId groupNameToId(fLastGroupId++, ourName);
- fUsedGroupNames.append(groupNameToId);
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupNameToId.id, PATCHBAY_ICON_CARLA, 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_ASSERT(jackPort != nullptr);
- CARLA_ASSERT(portName != nullptr);
-
- if (jackPort == nullptr)
- continue;
- if (portName == nullptr)
- continue;
-
- const int jackPortFlags(jackbridge_port_flags(jackPort));
-
- const size_t groupNameSize(std::strstr(fullPortName, portName) - fullPortName - 1);
- char groupName[groupNameSize+1];
- int groupId = -1;
-
- carla_copy<char>(groupName, fullPortName, groupNameSize);
- groupName[groupNameSize] = '\0';
-
- if (parsedGroups.contains(QString(groupName)))
- {
- groupId = getGroupId(groupName);
- CARLA_ASSERT(groupId != -1);
- }
- else
- {
- groupId = fLastGroupId++;
- parsedGroups.append(groupName);
-
- GroupNameToId groupNameToId(groupId, groupName);
- fUsedGroupNames.append(groupNameToId);
-
- PatchbayIconType groupIcon = PATCHBAY_ICON_APPLICATION;
-
- void* data = nullptr;
- size_t 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);
-
- 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;
- }
- else if (jackPortFlags & JackPortIsPhysical)
- groupIcon = PATCHBAY_ICON_HARDWARE;
-
- callback(CALLBACK_PATCHBAY_CLIENT_ADDED, 0, groupId, groupIcon, 0.0f, groupName);
- }
-
- bool portIsInput = (jackPortFlags & JackPortIsInput);
- bool portIsAudio = (std::strcmp(jackbridge_port_type(jackPort), JACK_DEFAULT_AUDIO_TYPE) == 0);
- bool portIsCV = (jackPortFlags & JackPortIsControlVoltage);
-
- unsigned int canvasPortFlags = 0x0;
- canvasPortFlags |= portIsInput ? PATCHBAY_PORT_IS_INPUT : PATCHBAY_PORT_IS_OUTPUT;
- canvasPortFlags |= portIsAudio ? PATCHBAY_PORT_IS_AUDIO : PATCHBAY_PORT_IS_MIDI;
-
- if (portIsAudio && portIsCV)
- canvasPortFlags |= PATCHBAY_PORT_IS_CV;
-
- PortNameToId portNameToId(groupId, fLastPortId++, portName, fullPortName);
- fUsedPortNames.append(portNameToId);
-
- callback(CALLBACK_PATCHBAY_PORT_ADDED, 0, groupId, portNameToId.portId, canvasPortFlags, portName);
- }
-
- 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* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
- const char* const fullPortName(ports[i]);
-
- CARLA_ASSERT(jackPort != nullptr);
-
- if (jackPort == nullptr)
- continue;
-
- const int thisPortId(getPortId(fullPortName));
-
- if (thisPortId == -1)
- continue;
-
- if (const char** connections = jackbridge_port_get_connections(jackPort))
- {
- for (int j=0; connections[j] != nullptr; ++j)
- {
- const int targetPortId(getPortId(connections[j]));
-
- ConnectionToId connectionToId(fLastConnectionId++, thisPortId, targetPortId);
- fUsedConnections.append(connectionToId);
-
- callback(CALLBACK_PATCHBAY_CONNECTION_ADDED, 0, connectionToId.id, thisPortId, targetPortId, nullptr);
- }
-
- jackbridge_free(connections);
- }
- }
-
- 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[2] = { 0.0f };
- float outPeaks[2] = { 0.0f };
-
- 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 < 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;
- }
- }
-
- 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
|