|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- /*
- * 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 "CarlaEngineGraph.hpp"
- #include "CarlaEngineInternal.hpp"
- #include "CarlaBackendUtils.hpp"
- #include "CarlaMathUtils.hpp"
- #include "CarlaStringList.hpp"
-
- #include "RtLinkedList.hpp"
-
- #include "jackbridge/JackBridge.hpp"
- #include "juce_audio_basics.h"
-
- #include "rtaudio/RtAudio.h"
- #include "rtmidi/RtMidi.h"
-
- using juce::FloatVectorOperations;
-
- CARLA_BACKEND_START_NAMESPACE
-
- // -------------------------------------------------------------------------------------------------------------------
- // Global static data
-
- static CharStringListPtr gDeviceNames;
- static std::vector<RtAudio::Api> gRtAudioApis;
-
- // -------------------------------------------------------------------------------------------------------------------
-
- static void initRtAudioAPIsIfNeeded()
- {
- static bool needsInit = true;
-
- if (! needsInit)
- return;
-
- needsInit = false;
-
- // get APIs in a local var, and pass wanted ones into gRtAudioApis
-
- std::vector<RtAudio::Api> apis;
- RtAudio::getCompiledApi(apis);
-
- for (std::vector<RtAudio::Api>::iterator it = apis.begin(), end = apis.end(); it != end; ++it)
- {
- const RtAudio::Api& api(*it);
-
- if (api == RtAudio::MACOSX_CORE)
- continue;
- if (api == RtAudio::WINDOWS_ASIO)
- continue;
- if (api == RtAudio::WINDOWS_DS)
- continue;
- if (api == RtAudio::WINDOWS_WASAPI)
- continue;
- if (api == RtAudio::UNIX_JACK && ! jackbridge_is_ok())
- continue;
-
- gRtAudioApis.push_back(api);
- }
- }
-
- static const char* getRtAudioApiName(const RtAudio::Api api) noexcept
- {
- switch (api)
- {
- case RtAudio::UNSPECIFIED:
- return "Unspecified";
- case RtAudio::LINUX_ALSA:
- return "ALSA";
- case RtAudio::LINUX_PULSE:
- return "PulseAudio";
- case RtAudio::LINUX_OSS:
- return "OSS";
- case RtAudio::UNIX_JACK:
- #if defined(CARLA_OS_WIN)
- return "JACK with WinMM";
- #elif defined(CARLA_OS_MAC)
- return "JACK with CoreMidi";
- #elif defined(CARLA_OS_LINUX)
- return "JACK with ALSA-MIDI";
- #else
- return "JACK (RtAudio)";
- #endif
- case RtAudio::MACOSX_CORE:
- return "CoreAudio";
- case RtAudio::WINDOWS_ASIO:
- return "ASIO";
- case RtAudio::WINDOWS_DS:
- return "DirectSound";
- case RtAudio::WINDOWS_WASAPI:
- return "WASAPI";
- case RtAudio::RTAUDIO_DUMMY:
- return "Dummy";
- }
-
- carla_stderr("CarlaBackend::getRtAudioApiName(%i) - invalid API", api);
- return nullptr;
- }
-
- static RtMidi::Api getMatchedAudioMidiAPI(const RtAudio::Api rtApi) noexcept
- {
- switch (rtApi)
- {
- case RtAudio::UNSPECIFIED:
- return RtMidi::UNSPECIFIED;
-
- case RtAudio::LINUX_ALSA:
- case RtAudio::LINUX_OSS:
- case RtAudio::LINUX_PULSE:
- return RtMidi::LINUX_ALSA;
-
- case RtAudio::UNIX_JACK:
- #if defined(CARLA_OS_WIN)
- return RtMidi::WINDOWS_MM;
- #elif defined(CARLA_OS_MAC)
- return RtMidi::MACOSX_CORE;
- #elif defined(CARLA_OS_LINUX)
- return RtMidi::LINUX_ALSA;
- #else
- return RtMidi::UNIX_JACK;
- #endif
-
- case RtAudio::MACOSX_CORE:
- return RtMidi::MACOSX_CORE;
-
- case RtAudio::WINDOWS_ASIO:
- case RtAudio::WINDOWS_DS:
- case RtAudio::WINDOWS_WASAPI:
- return RtMidi::WINDOWS_MM;
-
- case RtAudio::RTAUDIO_DUMMY:
- return RtMidi::RTMIDI_DUMMY;
- }
-
- return RtMidi::UNSPECIFIED;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
- // RtAudio Engine
-
- class CarlaEngineRtAudio : public CarlaEngine
- {
- public:
- CarlaEngineRtAudio(const RtAudio::Api api)
- : CarlaEngine(),
- fAudio(api),
- fAudioInCount(0),
- fAudioOutCount(0),
- fLastEventTime(0),
- fDeviceName(),
- fMidiIns(),
- fMidiInEvents(),
- fMidiOuts(),
- fMidiOutMutex(),
- fMidiOutVector(3),
- leakDetector_CarlaEngineRtAudio()
- {
- carla_debug("CarlaEngineRtAudio::CarlaEngineRtAudio(%i)", api);
-
- // just to make sure
- pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
- }
-
- ~CarlaEngineRtAudio() override
- {
- CARLA_SAFE_ASSERT(fAudioInCount == 0);
- CARLA_SAFE_ASSERT(fAudioOutCount == 0);
- CARLA_SAFE_ASSERT(fLastEventTime == 0);
- carla_debug("CarlaEngineRtAudio::~CarlaEngineRtAudio()");
- }
-
- // -------------------------------------
-
- bool init(const char* const clientName) override
- {
- CARLA_SAFE_ASSERT_RETURN(fAudioInCount == 0, false);
- CARLA_SAFE_ASSERT_RETURN(fAudioOutCount == 0, false);
- CARLA_SAFE_ASSERT_RETURN(fLastEventTime == 0, false);
- CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
- carla_debug("CarlaEngineRtAudio::init(\"%s\")", clientName);
-
- if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
- {
- setLastError("Invalid process mode");
- return false;
- }
-
- const uint devCount(fAudio.getDeviceCount());
-
- if (devCount == 0)
- {
- setLastError("No audio devices available for this driver");
- return false;
- }
-
- RtAudio::StreamParameters iParams, oParams;
- bool deviceSet = false;
-
- if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
- {
- for (uint i=0; i < devCount; ++i)
- {
- RtAudio::DeviceInfo devInfo(fAudio.getDeviceInfo(i));
-
- if (devInfo.probed && devInfo.outputChannels > 0 && devInfo.name == pData->options.audioDevice)
- {
- deviceSet = true;
- fDeviceName = devInfo.name.c_str();
- iParams.deviceId = i;
- oParams.deviceId = i;
- iParams.nChannels = devInfo.inputChannels;
- oParams.nChannels = devInfo.outputChannels;
- break;
- }
- }
- }
-
- if (! deviceSet)
- {
- iParams.deviceId = fAudio.getDefaultInputDevice();
- oParams.deviceId = fAudio.getDefaultOutputDevice();
- iParams.nChannels = fAudio.getDeviceInfo(iParams.deviceId).inputChannels;
- oParams.nChannels = fAudio.getDeviceInfo(oParams.deviceId).outputChannels;
-
- carla_stdout("No device set, using %i inputs and %i outputs", iParams.nChannels, oParams.nChannels);
- }
-
- if (oParams.nChannels == 0)
- {
- setLastError("Current audio setup has no outputs, cannot continue");
- return false;
- }
-
- iParams.nChannels = carla_fixValue(0U, 128U, iParams.nChannels);
- oParams.nChannels = carla_fixValue(0U, 128U, oParams.nChannels);
-
- RtAudio::StreamOptions rtOptions;
- rtOptions.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_HOG_DEVICE | RTAUDIO_SCHEDULE_REALTIME | RTAUDIO_NONINTERLEAVED;
- rtOptions.streamName = clientName;
- rtOptions.priority = 85;
-
- if (fAudio.getCurrentApi() == RtAudio::LINUX_ALSA && ! deviceSet)
- rtOptions.flags |= RTAUDIO_ALSA_USE_DEFAULT;
-
- uint bufferFrames = pData->options.audioBufferSize;
-
- try {
- fAudio.openStream(&oParams, (iParams.nChannels > 0) ? &iParams : nullptr, RTAUDIO_FLOAT32, pData->options.audioSampleRate, &bufferFrames, carla_rtaudio_process_callback, this, &rtOptions);
- }
- catch (const RtAudioError& e) {
- setLastError(e.what());
- return false;
- }
-
- pData->bufferSize = bufferFrames;
- pData->sampleRate = fAudio.getStreamSampleRate();
-
- fAudioInCount = iParams.nChannels;
- fAudioOutCount = oParams.nChannels;
- fLastEventTime = 0;
-
- pData->graph.create(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, pData->sampleRate, pData->bufferSize, fAudioInCount, fAudioOutCount);
-
- try {
- fAudio.startStream();
- }
- catch (const RtAudioError& e)
- {
- close();
- setLastError(e.what());
- return false;
- }
-
- CarlaEngine::init(clientName);
- patchbayRefresh(false);
-
- return true;
- }
-
- bool close() override
- {
- CARLA_SAFE_ASSERT(fAudioOutCount != 0);
- carla_debug("CarlaEngineRtAudio::close()");
-
- bool hasError = !CarlaEngine::close();
-
- if (fAudio.isStreamOpen())
- {
- if (fAudio.isStreamRunning())
- {
- try {
- fAudio.stopStream();
- }
- catch (const RtAudioError& e)
- {
- if (! hasError)
- {
- setLastError(e.what());
- hasError = true;
- }
- }
- }
-
- fAudio.closeStream();
- }
-
- pData->graph.destroy();
-
- for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin(); it.valid(); it.next())
- {
- MidiInPort& inPort(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
-
- inPort.port->cancelCallback();
- inPort.port->closePort();
- delete inPort.port;
- }
-
- fMidiIns.clear();
- fMidiInEvents.clear();
-
- fMidiOutMutex.lock();
-
- for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin(); it.valid(); it.next())
- {
- MidiOutPort& outPort(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
-
- outPort.port->closePort();
- delete outPort.port;
- }
-
- fMidiOuts.clear();
- fMidiOutMutex.unlock();
-
- fAudioInCount = 0;
- fAudioOutCount = 0;
- fLastEventTime = 0;
- fDeviceName.clear();
-
- return !hasError;
- }
-
- bool isRunning() const noexcept override
- {
- return fAudio.isStreamRunning();
- }
-
- bool isOffline() const noexcept override
- {
- return false;
- }
-
- EngineType getType() const noexcept override
- {
- return kEngineTypeRtAudio;
- }
-
- const char* getCurrentDriverName() const noexcept override
- {
- return CarlaBackend::getRtAudioApiName(fAudio.getCurrentApi());
- }
-
- // -------------------------------------------------------------------
- // Patchbay
-
- bool patchbayRefresh(const bool /*external*/) override
- {
- CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
-
- if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
- patchbayRefreshRack();
- else
- patchbayRefreshPatchbay();
-
- return true;
- }
-
- void patchbayRefreshRack()
- {
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
-
- graph->connections.clear();
-
- char strBuf[STR_MAX+1];
- strBuf[STR_MAX] = '\0';
-
- // Main
- {
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_CARLA, PATCHBAY_ICON_CARLA, -1, 0.0f, getName());
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_IN, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_OUT, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
- }
-
- // Audio In
- {
- if (fDeviceName.isNotEmpty())
- std::snprintf(strBuf, STR_MAX, "Capture (%s)", fDeviceName.buffer());
- else
- std::strncpy(strBuf, "Capture", STR_MAX);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_AUDIO_IN, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
-
- for (uint i=0; i < fAudioInCount; ++i)
- {
- std::snprintf(strBuf, STR_MAX, "capture_%i", i+1);
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_AUDIO_IN, static_cast<int>(i), PATCHBAY_PORT_TYPE_AUDIO, 0.0f, strBuf);
- }
- }
-
- // Audio Out
- {
- if (fDeviceName.isNotEmpty())
- std::snprintf(strBuf, STR_MAX, "Playback (%s)", fDeviceName.buffer());
- else
- std::strncpy(strBuf, "Playback", STR_MAX);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_AUDIO_OUT, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
-
- for (uint i=0; i < fAudioOutCount; ++i)
- {
- std::snprintf(strBuf, STR_MAX, "playback_%i", i+1);
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_AUDIO_OUT, static_cast<int>(i), PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, strBuf);
- }
- }
-
- // MIDI In
- {
- RtMidiIn midiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery");
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_MIDI_IN, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
-
- for (uint i=0, count = midiIn.getPortCount(); i < count; ++i)
- {
- std::string portName(midiIn.getPortName(i));
-
- std::snprintf(strBuf, STR_MAX, "Readable MIDI ports:%s", portName.c_str());
-
- PortNameToId portNameToId;
- portNameToId.setData(RACK_GRAPH_GROUP_MIDI_IN, i, portName.c_str(), strBuf);
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, portNameToId.group, static_cast<int>(portNameToId.port), PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
-
- graph->midi.ins.append(portNameToId);
- }
- }
-
- // MIDI Out
- {
- RtMidiOut midiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery");
-
- callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_MIDI_OUT, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
-
- for (uint i=0, count = midiOut.getPortCount(); i < count; ++i)
- {
- std::string portName(midiOut.getPortName(i));
-
- std::snprintf(strBuf, STR_MAX, "Writable MIDI ports:%s", portName.c_str());
-
- PortNameToId portNameToId;
- portNameToId.setData(RACK_GRAPH_GROUP_MIDI_OUT, i, portName.c_str(), strBuf);
-
- callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, portNameToId.group, static_cast<int>(portNameToId.port), PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
-
- graph->midi.outs.append(portNameToId);
- }
- }
-
- // Connections
- graph->audio.mutex.lock();
-
- for (LinkedList<uint>::Itenerator it = graph->audio.connectedIn1.begin(); it.valid(); it.next())
- {
- const uint& portId(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioInCount);
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_AUDIO_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN1);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = graph->audio.connectedIn2.begin(); it.valid(); it.next())
- {
- const uint& portId(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioInCount);
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_AUDIO_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN2);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = graph->audio.connectedOut1.begin(); it.valid(); it.next())
- {
- const uint& portId(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioOutCount);
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT1, RACK_GRAPH_GROUP_AUDIO_OUT, portId);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- for (LinkedList<uint>::Itenerator it = graph->audio.connectedOut2.begin(); it.valid(); it.next())
- {
- const uint& portId(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioOutCount);
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT2, RACK_GRAPH_GROUP_AUDIO_OUT, portId);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- graph->audio.mutex.unlock();
-
- for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
- {
- const MidiInPort& inPort(it.getValue());
-
- const uint portId(graph->midi.getPortId(true, inPort.name));
- CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midi.ins.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_MIDI_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_IN);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- fMidiOutMutex.lock();
-
- for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
- {
- const MidiOutPort& outPort(it.getValue());
-
- const uint portId(graph->midi.getPortId(false, outPort.name));
- CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midi.outs.count());
-
- ConnectionToId connectionToId;
- connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_OUT, RACK_GRAPH_GROUP_MIDI_OUT, portId);
-
- std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
-
- callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
-
- graph->connections.list.append(connectionToId);
- }
-
- fMidiOutMutex.unlock();
- }
-
- void patchbayRefreshPatchbay() noexcept
- {
- PatchbayGraph* const graph(pData->graph.getPatchbayGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
-
- graph->refreshConnections(this);
- }
-
- // -------------------------------------------------------------------
-
- protected:
- void handleAudioProcessCallback(void* outputBuffer, void* inputBuffer, uint nframes, double streamTime, RtAudioStreamStatus status)
- {
- const PendingRtEventsRunner prt(this);
-
- // get buffers from RtAudio
- const float* const insPtr = (const float*)inputBuffer;
- float* const outsPtr = (float*)outputBuffer;
-
- // assert rtaudio buffers
- CARLA_SAFE_ASSERT_RETURN(outputBuffer != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(pData->bufferSize == nframes,);
-
- // initialize rtaudio input
- const float* inBuf[fAudioInCount];
-
- for (uint i=0; i < fAudioInCount; ++i)
- inBuf[i] = insPtr+(nframes*i);
-
- // initialize rtaudio output
- float* outBuf[fAudioOutCount];
-
- for (uint i=0; i < fAudioOutCount; ++i)
- outBuf[i] = outsPtr+(nframes*i);
-
- FloatVectorOperations::clear(outsPtr, static_cast<int>(nframes*fAudioOutCount));
-
- // initialize events
- carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
- carla_zeroStruct<EngineEvent>(pData->events.out, kMaxEngineEventInternalCount);
-
- if (fMidiInEvents.mutex.tryLock())
- {
- uint32_t engineEventIndex = 0;
- fMidiInEvents.splice();
-
- for (LinkedList<RtMidiEvent>::Itenerator it = fMidiInEvents.data.begin(); it.valid(); it.next())
- {
- const RtMidiEvent& midiEvent(it.getValue());
- EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
-
- if (midiEvent.time < pData->timeInfo.frame)
- {
- engineEvent.time = 0;
- }
- else if (midiEvent.time >= pData->timeInfo.frame + nframes)
- {
- carla_stderr("MIDI Event in the future!, %i vs %i", engineEvent.time, pData->timeInfo.frame);
- engineEvent.time = static_cast<uint32_t>(pData->timeInfo.frame) + nframes - 1;
- }
- else
- engineEvent.time = static_cast<uint32_t>(midiEvent.time - pData->timeInfo.frame);
-
- engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
-
- if (engineEventIndex >= kMaxEngineEventInternalCount)
- break;
- }
-
- fMidiInEvents.data.clear();
- fMidiInEvents.mutex.unlock();
- }
-
- pData->graph.process(pData, inBuf, outBuf, nframes);
-
- fMidiOutMutex.lock();
-
- if (fMidiOuts.count() > 0)
- {
- uint8_t size = 0;
- uint8_t data[3] = { 0, 0, 0 };
- const uint8_t* dataPtr = data;
-
- for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
- {
- const EngineEvent& engineEvent(pData->events.out[i]);
-
- if (engineEvent.type == kEngineEventTypeNull)
- break;
-
- else if (engineEvent.type == kEngineEventTypeControl)
- {
- const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
- ctrlEvent.convertToMidiData(engineEvent.channel, size, data);
- dataPtr = data;
- }
- else if (engineEvent.type == kEngineEventTypeMidi)
- {
- const EngineMidiEvent& midiEvent(engineEvent.midi);
-
- size = midiEvent.size;
-
- if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
- dataPtr = midiEvent.dataExt;
- else
- dataPtr = midiEvent.data;
- }
- else
- {
- continue;
- }
-
- if (size > 0)
- {
- fMidiOutVector.assign(dataPtr, dataPtr + size);
-
- for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
- {
- MidiOutPort& outPort(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
-
- outPort.port->sendMessage(&fMidiOutVector);
- }
- }
- }
- }
-
- fMidiOutMutex.unlock();
-
- return; // unused
- (void)streamTime; (void)status;
- }
-
- void handleMidiCallback(double timeStamp, std::vector<uchar>* const message)
- {
- const size_t messageSize(message->size());
-
- if (messageSize == 0 || messageSize > EngineMidiEvent::kDataSize)
- return;
-
- timeStamp /= 2;
-
- if (timeStamp > 0.95)
- timeStamp = 0.95;
- else if (timeStamp < 0.0)
- timeStamp = 0.0;
-
- RtMidiEvent midiEvent;
- midiEvent.time = pData->timeInfo.frame + uint64_t(timeStamp * (double)pData->bufferSize);
-
- if (midiEvent.time < fLastEventTime)
- midiEvent.time = fLastEventTime;
- else
- fLastEventTime = midiEvent.time;
-
- midiEvent.size = static_cast<uint8_t>(messageSize);
-
- size_t i=0;
- for (; i < messageSize; ++i)
- midiEvent.data[i] = message->at(i);
- for (; i < EngineMidiEvent::kDataSize; ++i)
- midiEvent.data[i] = 0;
-
- fMidiInEvents.append(midiEvent);
- }
-
- // -------------------------------------------------------------------
-
- bool connectRackMidiInPort(const char* const portName) override
- {
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
- carla_debug("CarlaEngineRtAudio::connectRackMidiInPort(\"%s\")", portName);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
-
- CarlaString newRtMidiPortName;
- newRtMidiPortName += getName();
- newRtMidiPortName += ":";
- newRtMidiPortName += portName;
-
- RtMidiIn* const rtMidiIn(new RtMidiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer(), 512));
- rtMidiIn->ignoreTypes();
- rtMidiIn->setCallback(carla_rtmidi_callback, this);
-
- bool found = false;
- uint rtMidiPortIndex;
-
- for (uint i=0, count=rtMidiIn->getPortCount(); i < count; ++i)
- {
- if (rtMidiIn->getPortName(i) == portName)
- {
- found = true;
- rtMidiPortIndex = i;
- break;
- }
- }
-
- if (! found)
- {
- delete rtMidiIn;
- return false;
- }
-
- try {
- rtMidiIn->openPort(rtMidiPortIndex, portName);
- }
- catch(...) {
- delete rtMidiIn;
- return false;
- };
-
- MidiInPort midiPort;
- midiPort.port = rtMidiIn;
-
- std::strncpy(midiPort.name, portName, STR_MAX);
- midiPort.name[STR_MAX] = '\0';
-
- fMidiIns.append(midiPort);
- return true;
- }
-
- bool connectRackMidiOutPort(const char* const portName) override
- {
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
- carla_debug("CarlaEngineRtAudio::connectRackMidiOutPort(\"%s\")", portName);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
-
- CarlaString newRtMidiPortName;
- newRtMidiPortName += getName();
- newRtMidiPortName += ":";
- newRtMidiPortName += portName;
-
- RtMidiOut* const rtMidiOut(new RtMidiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer()));
-
- bool found = false;
- uint rtMidiPortIndex;
-
- for (uint i=0, count=rtMidiOut->getPortCount(); i < count; ++i)
- {
- if (rtMidiOut->getPortName(i) == portName)
- {
- found = true;
- rtMidiPortIndex = i;
- break;
- }
- }
-
- if (! found)
- {
- delete rtMidiOut;
- return false;
- }
-
- try {
- rtMidiOut->openPort(rtMidiPortIndex, portName);
- }
- catch(...) {
- delete rtMidiOut;
- return false;
- };
-
- MidiOutPort midiPort;
- midiPort.port = rtMidiOut;
-
- std::strncpy(midiPort.name, portName, STR_MAX);
- midiPort.name[STR_MAX] = '\0';
-
- const CarlaMutexLocker cml(fMidiOutMutex);
-
- fMidiOuts.append(midiPort);
- return true;
- }
-
- bool disconnectRackMidiInPort(const char* const portName) override
- {
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
- carla_debug("CarlaEngineRtAudio::disconnectRackMidiInPort(\"%s\")", portName);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
-
- for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
- {
- MidiInPort& inPort(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
-
- if (std::strcmp(inPort.name, portName) != 0)
- continue;
-
- inPort.port->cancelCallback();
- inPort.port->closePort();
- delete inPort.port;
-
- fMidiIns.remove(it);
- return true;
- }
-
- return false;
- }
-
- bool disconnectRackMidiOutPort(const char* const portName) override
- {
- CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
- carla_debug("CarlaEngineRtAudio::disconnectRackMidiOutPort(\"%s\")", portName);
-
- RackGraph* const graph(pData->graph.getRackGraph());
- CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(graph->midi.outs.count() > 0, false);
-
- const CarlaMutexLocker cml(fMidiOutMutex);
-
- for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
- {
- MidiOutPort& outPort(it.getValue());
- CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
-
- if (std::strcmp(outPort.name, portName) != 0)
- continue;
-
- outPort.port->closePort();
- delete outPort.port;
-
- fMidiOuts.remove(it);
- return true;
- }
-
- return false;
- }
-
- // -------------------------------------------------------------------
-
- private:
- RtAudio fAudio;
-
- // useful info
- uint fAudioInCount;
- uint fAudioOutCount;
- uint64_t fLastEventTime;
-
- // current device name
- CarlaString fDeviceName;
-
- struct MidiInPort {
- RtMidiIn* port;
- char name[STR_MAX+1];
- };
-
- struct MidiOutPort {
- RtMidiOut* port;
- char name[STR_MAX+1];
- };
-
- struct RtMidiEvent {
- uint64_t time; // needs to compare to internal time
- uint8_t size;
- uint8_t data[EngineMidiEvent::kDataSize];
- };
-
- struct RtMidiEvents {
- CarlaMutex mutex;
- RtLinkedList<RtMidiEvent>::Pool dataPool;
- RtLinkedList<RtMidiEvent> data;
- RtLinkedList<RtMidiEvent> dataPending;
-
- RtMidiEvents()
- : mutex(),
- dataPool(512, 512),
- data(dataPool),
- dataPending(dataPool) {}
-
- ~RtMidiEvents()
- {
- clear();
- }
-
- void append(const RtMidiEvent& event)
- {
- mutex.lock();
- dataPending.append(event);
- mutex.unlock();
- }
-
- void clear()
- {
- mutex.lock();
- data.clear();
- dataPending.clear();
- mutex.unlock();
- }
-
- void splice()
- {
- dataPending.spliceAppendTo(data);
- }
- };
-
- LinkedList<MidiInPort> fMidiIns;
- RtMidiEvents fMidiInEvents;
-
- LinkedList<MidiOutPort> fMidiOuts;
- CarlaMutex fMidiOutMutex;
- std::vector<uint8_t> fMidiOutVector;
-
- #define handlePtr ((CarlaEngineRtAudio*)userData)
-
- static int carla_rtaudio_process_callback(void* outputBuffer, void* inputBuffer, uint nframes, double streamTime, RtAudioStreamStatus status, void* userData)
- {
- handlePtr->handleAudioProcessCallback(outputBuffer, inputBuffer, nframes, streamTime, status);
- return 0;
- }
-
- static void carla_rtmidi_callback(double timeStamp, std::vector<uchar>* message, void* userData)
- {
- handlePtr->handleMidiCallback(timeStamp, message);
- }
-
- #undef handlePtr
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineRtAudio)
- };
-
- // -----------------------------------------
-
- CarlaEngine* CarlaEngine::newRtAudio(const AudioApi api)
- {
- initRtAudioAPIsIfNeeded();
-
- RtAudio::Api rtApi(RtAudio::UNSPECIFIED);
-
- switch (api)
- {
- case AUDIO_API_NULL:
- rtApi = RtAudio::RTAUDIO_DUMMY;
- break;
- case AUDIO_API_JACK:
- rtApi = RtAudio::UNIX_JACK;
- break;
- case AUDIO_API_ALSA:
- rtApi = RtAudio::LINUX_ALSA;
- break;
- case AUDIO_API_OSS:
- rtApi = RtAudio::LINUX_OSS;
- break;
- case AUDIO_API_PULSE:
- rtApi = RtAudio::LINUX_PULSE;
- break;
- case AUDIO_API_CORE:
- rtApi = RtAudio::MACOSX_CORE;
- break;
- case AUDIO_API_ASIO:
- rtApi = RtAudio::WINDOWS_ASIO;
- break;
- case AUDIO_API_DS:
- rtApi = RtAudio::WINDOWS_DS;
- break;
- }
-
- return new CarlaEngineRtAudio(rtApi);
- }
-
- uint CarlaEngine::getRtAudioApiCount()
- {
- initRtAudioAPIsIfNeeded();
-
- return static_cast<uint>(gRtAudioApis.size());
- }
-
- const char* CarlaEngine::getRtAudioApiName(const uint index)
- {
- initRtAudioAPIsIfNeeded();
-
- CARLA_SAFE_ASSERT_RETURN(index < gRtAudioApis.size(), nullptr);
-
- return CarlaBackend::getRtAudioApiName(gRtAudioApis[index]);
- }
-
- const char* const* CarlaEngine::getRtAudioApiDeviceNames(const uint index)
- {
- initRtAudioAPIsIfNeeded();
-
- if (index >= gRtAudioApis.size())
- return nullptr;
-
- const RtAudio::Api& api(gRtAudioApis[index]);
-
- RtAudio rtAudio(api);
-
- const uint devCount(rtAudio.getDeviceCount());
-
- if (devCount == 0)
- return nullptr;
-
- CarlaStringList devNames;
-
- for (uint i=0; i < devCount; ++i)
- {
- RtAudio::DeviceInfo devInfo(rtAudio.getDeviceInfo(i));
-
- if (devInfo.probed && devInfo.outputChannels > 0 /*&& (devInfo.nativeFormats & RTAUDIO_FLOAT32) != 0*/)
- devNames.append(devInfo.name.c_str());
- }
-
- gDeviceNames = devNames.toCharStringListPtr();
-
- return gDeviceNames;
- }
-
- const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const uint index, const char* const deviceName)
- {
- initRtAudioAPIsIfNeeded();
-
- if (index >= gRtAudioApis.size())
- return nullptr;
-
- const RtAudio::Api& api(gRtAudioApis[index]);
-
- RtAudio rtAudio(api);
-
- const uint devCount(rtAudio.getDeviceCount());
-
- if (devCount == 0)
- return nullptr;
-
- uint i;
- RtAudio::DeviceInfo rtAudioDevInfo;
-
- for (i=0; i < devCount; ++i)
- {
- rtAudioDevInfo = rtAudio.getDeviceInfo(i);
-
- if (rtAudioDevInfo.name == deviceName)
- break;
- }
-
- if (i == devCount)
- return nullptr;
-
- static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
- static uint32_t dummyBufferSizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
- static double dummySampleRates[] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
-
- // reset
- devInfo.hints = 0x0;
- devInfo.bufferSizes = dummyBufferSizes;
-
- // cleanup
- if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
- {
- delete[] devInfo.sampleRates;
- devInfo.sampleRates = nullptr;
- }
-
- if (size_t sampleRatesCount = rtAudioDevInfo.sampleRates.size())
- {
- double* const sampleRates(new double[sampleRatesCount+1]);
-
- for (size_t j=0; j < sampleRatesCount; ++j)
- sampleRates[j] = rtAudioDevInfo.sampleRates[j];
- sampleRates[sampleRatesCount] = 0.0;
-
- devInfo.sampleRates = sampleRates;
- }
- else
- {
- devInfo.sampleRates = dummySampleRates;
- }
-
- return &devInfo;
- }
-
- // -----------------------------------------
-
- CARLA_BACKEND_END_NAMESPACE
|