Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaEngineRtAudio.cpp 40KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaStringList.hpp"
  22. #include "RtLinkedList.hpp"
  23. #include "jackbridge/JackBridge.hpp"
  24. #include "juce_audio_basics.h"
  25. #include "rtaudio/RtAudio.h"
  26. #include "rtmidi/RtMidi.h"
  27. using juce::jmax;
  28. using juce::AudioSampleBuffer;
  29. using juce::FloatVectorOperations;
  30. CARLA_BACKEND_START_NAMESPACE
  31. // -------------------------------------------------------------------------------------------------------------------
  32. // Global static data
  33. static CharStringListPtr gDeviceNames;
  34. static std::vector<RtAudio::Api> gRtAudioApis;
  35. // -------------------------------------------------------------------------------------------------------------------
  36. static void initRtAudioAPIsIfNeeded()
  37. {
  38. static bool needsInit = true;
  39. if (! needsInit)
  40. return;
  41. needsInit = false;
  42. // get APIs in a local var, and pass wanted ones into gRtAudioApis
  43. std::vector<RtAudio::Api> apis;
  44. RtAudio::getCompiledApi(apis);
  45. for (std::vector<RtAudio::Api>::iterator it = apis.begin(), end = apis.end(); it != end; ++it)
  46. {
  47. const RtAudio::Api& api(*it);
  48. if (api == RtAudio::MACOSX_CORE)
  49. continue;
  50. if (api == RtAudio::WINDOWS_ASIO)
  51. continue;
  52. if (api == RtAudio::WINDOWS_DS)
  53. continue;
  54. if (api == RtAudio::WINDOWS_WASAPI)
  55. continue;
  56. if (api == RtAudio::UNIX_JACK && ! jackbridge_is_ok())
  57. continue;
  58. gRtAudioApis.push_back(api);
  59. }
  60. }
  61. static const char* getRtAudioApiName(const RtAudio::Api api) noexcept
  62. {
  63. switch (api)
  64. {
  65. case RtAudio::UNSPECIFIED:
  66. return "Unspecified";
  67. case RtAudio::LINUX_ALSA:
  68. return "ALSA";
  69. case RtAudio::LINUX_PULSE:
  70. return "PulseAudio";
  71. case RtAudio::LINUX_OSS:
  72. return "OSS";
  73. case RtAudio::UNIX_JACK:
  74. #if defined(CARLA_OS_WIN)
  75. return "JACK with WinMM";
  76. #elif defined(CARLA_OS_MAC)
  77. return "JACK with CoreMidi";
  78. #elif defined(CARLA_OS_LINUX)
  79. return "JACK with ALSA-MIDI";
  80. #else
  81. return "JACK (RtAudio)";
  82. #endif
  83. case RtAudio::MACOSX_CORE:
  84. return "CoreAudio";
  85. case RtAudio::WINDOWS_ASIO:
  86. return "ASIO";
  87. case RtAudio::WINDOWS_DS:
  88. return "DirectSound";
  89. case RtAudio::WINDOWS_WASAPI:
  90. return "WASAPI";
  91. case RtAudio::RTAUDIO_DUMMY:
  92. return "Dummy";
  93. }
  94. carla_stderr("CarlaBackend::getRtAudioApiName(%i) - invalid API", api);
  95. return nullptr;
  96. }
  97. static RtMidi::Api getMatchedAudioMidiAPI(const RtAudio::Api rtApi) noexcept
  98. {
  99. switch (rtApi)
  100. {
  101. case RtAudio::UNSPECIFIED:
  102. return RtMidi::UNSPECIFIED;
  103. case RtAudio::LINUX_ALSA:
  104. case RtAudio::LINUX_OSS:
  105. case RtAudio::LINUX_PULSE:
  106. return RtMidi::LINUX_ALSA;
  107. case RtAudio::UNIX_JACK:
  108. #if defined(CARLA_OS_WIN)
  109. return RtMidi::WINDOWS_MM;
  110. #elif defined(CARLA_OS_MAC)
  111. return RtMidi::MACOSX_CORE;
  112. #elif defined(CARLA_OS_LINUX)
  113. return RtMidi::LINUX_ALSA;
  114. #else
  115. return RtMidi::UNIX_JACK;
  116. #endif
  117. case RtAudio::MACOSX_CORE:
  118. return RtMidi::MACOSX_CORE;
  119. case RtAudio::WINDOWS_ASIO:
  120. case RtAudio::WINDOWS_DS:
  121. case RtAudio::WINDOWS_WASAPI:
  122. return RtMidi::WINDOWS_MM;
  123. case RtAudio::RTAUDIO_DUMMY:
  124. return RtMidi::RTMIDI_DUMMY;
  125. }
  126. return RtMidi::UNSPECIFIED;
  127. }
  128. // -------------------------------------------------------------------------------------------------------------------
  129. // RtAudio Engine
  130. class CarlaEngineRtAudio : public CarlaEngine
  131. {
  132. public:
  133. CarlaEngineRtAudio(const RtAudio::Api api)
  134. : CarlaEngine(),
  135. fAudio(api),
  136. fAudioInterleaved(false),
  137. fAudioInCount(0),
  138. fAudioOutCount(0),
  139. fLastEventTime(0),
  140. fDeviceName(),
  141. fAudioIntBufIn(),
  142. fAudioIntBufOut(),
  143. fMidiIns(),
  144. fMidiInEvents(),
  145. fMidiOuts(),
  146. fMidiOutMutex(),
  147. fMidiOutVector(3),
  148. leakDetector_CarlaEngineRtAudio()
  149. {
  150. carla_debug("CarlaEngineRtAudio::CarlaEngineRtAudio(%i)", api);
  151. // just to make sure
  152. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  153. }
  154. ~CarlaEngineRtAudio() override
  155. {
  156. CARLA_SAFE_ASSERT(fAudioInCount == 0);
  157. CARLA_SAFE_ASSERT(fAudioOutCount == 0);
  158. CARLA_SAFE_ASSERT(fLastEventTime == 0);
  159. carla_debug("CarlaEngineRtAudio::~CarlaEngineRtAudio()");
  160. }
  161. // -------------------------------------
  162. bool init(const char* const clientName) override
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(fAudioInCount == 0, false);
  165. CARLA_SAFE_ASSERT_RETURN(fAudioOutCount == 0, false);
  166. CARLA_SAFE_ASSERT_RETURN(fLastEventTime == 0, false);
  167. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  168. carla_debug("CarlaEngineRtAudio::init(\"%s\")", clientName);
  169. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  170. {
  171. setLastError("Patchbay process mode is not implemented yet for non-JACK drivers, sorry!");
  172. return false;
  173. }
  174. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  175. {
  176. setLastError("Invalid process mode");
  177. return false;
  178. }
  179. const uint devCount(fAudio.getDeviceCount());
  180. if (devCount == 0)
  181. {
  182. setLastError("No audio devices available for this driver");
  183. return false;
  184. }
  185. RtAudio::StreamParameters iParams, oParams;
  186. bool deviceSet = false;
  187. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  188. {
  189. for (uint i=0; i < devCount; ++i)
  190. {
  191. RtAudio::DeviceInfo devInfo(fAudio.getDeviceInfo(i));
  192. if (devInfo.probed && devInfo.outputChannels > 0 && devInfo.name == pData->options.audioDevice)
  193. {
  194. deviceSet = true;
  195. fDeviceName = devInfo.name.c_str();
  196. iParams.deviceId = i;
  197. oParams.deviceId = i;
  198. iParams.nChannels = devInfo.inputChannels;
  199. oParams.nChannels = devInfo.outputChannels;
  200. break;
  201. }
  202. }
  203. }
  204. if (! deviceSet)
  205. {
  206. iParams.deviceId = fAudio.getDefaultInputDevice();
  207. oParams.deviceId = fAudio.getDefaultOutputDevice();
  208. iParams.nChannels = fAudio.getDeviceInfo(iParams.deviceId).inputChannels;
  209. oParams.nChannels = fAudio.getDeviceInfo(oParams.deviceId).outputChannels;
  210. carla_stdout("No device set, using %i inputs and %i outputs", iParams.nChannels, oParams.nChannels);
  211. }
  212. if (oParams.nChannels == 0)
  213. {
  214. setLastError("Current audio setup has no outputs, cannot continue");
  215. return false;
  216. }
  217. iParams.nChannels = carla_fixValue(0U, 128U, iParams.nChannels);
  218. oParams.nChannels = carla_fixValue(0U, 128U, oParams.nChannels);
  219. fAudioInterleaved = fAudio.getCurrentApi() == RtAudio::LINUX_PULSE;
  220. RtAudio::StreamOptions rtOptions;
  221. rtOptions.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_HOG_DEVICE | RTAUDIO_SCHEDULE_REALTIME;
  222. rtOptions.streamName = clientName;
  223. rtOptions.priority = 85;
  224. if (fAudio.getCurrentApi() == RtAudio::LINUX_ALSA && ! deviceSet)
  225. rtOptions.flags |= RTAUDIO_ALSA_USE_DEFAULT;
  226. if (! fAudioInterleaved)
  227. rtOptions.flags |= RTAUDIO_NONINTERLEAVED;
  228. uint bufferFrames = pData->options.audioBufferSize;
  229. try {
  230. fAudio.openStream(&oParams, (iParams.nChannels > 0) ? &iParams : nullptr, RTAUDIO_FLOAT32, pData->options.audioSampleRate, &bufferFrames, carla_rtaudio_process_callback, this, &rtOptions);
  231. }
  232. catch (const RtAudioError& e) {
  233. setLastError(e.what());
  234. return false;
  235. }
  236. if (! pData->init(clientName))
  237. {
  238. close();
  239. setLastError("Failed to init internal data");
  240. return false;
  241. }
  242. pData->bufferSize = bufferFrames;
  243. pData->sampleRate = fAudio.getStreamSampleRate();
  244. fAudioInCount = iParams.nChannels;
  245. fAudioOutCount = oParams.nChannels;
  246. fLastEventTime = 0;
  247. fAudioIntBufIn.setSize(static_cast<int>(fAudioInCount), static_cast<int>(bufferFrames));
  248. fAudioIntBufOut.setSize(static_cast<int>(fAudioOutCount), static_cast<int>(bufferFrames));
  249. pData->graph.create(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, pData->sampleRate, pData->bufferSize, fAudioInCount, fAudioOutCount);
  250. try {
  251. fAudio.startStream();
  252. }
  253. catch (const RtAudioError& e)
  254. {
  255. close();
  256. setLastError(e.what());
  257. return false;
  258. }
  259. patchbayRefresh(false);
  260. callback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, getCurrentDriverName());
  261. return true;
  262. }
  263. bool close() override
  264. {
  265. CARLA_SAFE_ASSERT(fAudioOutCount != 0);
  266. carla_debug("CarlaEngineRtAudio::close()");
  267. bool hasError = false;
  268. // stop stream first
  269. if (fAudio.isStreamOpen() && fAudio.isStreamRunning())
  270. {
  271. try {
  272. fAudio.stopStream();
  273. }
  274. catch (const RtAudioError& e)
  275. {
  276. setLastError(e.what());
  277. hasError = true;
  278. }
  279. }
  280. // clear engine data
  281. CarlaEngine::close();
  282. pData->graph.destroy();
  283. for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin(); it.valid(); it.next())
  284. {
  285. static MidiInPort fallback = { nullptr, { '\0' } };
  286. MidiInPort& inPort(it.getValue(fallback));
  287. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  288. inPort.port->cancelCallback();
  289. inPort.port->closePort();
  290. delete inPort.port;
  291. }
  292. fMidiIns.clear();
  293. fMidiInEvents.clear();
  294. fMidiOutMutex.lock();
  295. for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin(); it.valid(); it.next())
  296. {
  297. static MidiOutPort fallback = { nullptr, { '\0' } };
  298. MidiOutPort& outPort(it.getValue(fallback));
  299. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  300. outPort.port->closePort();
  301. delete outPort.port;
  302. }
  303. fMidiOuts.clear();
  304. fMidiOutMutex.unlock();
  305. fAudioInCount = 0;
  306. fAudioOutCount = 0;
  307. fLastEventTime = 0;
  308. fDeviceName.clear();
  309. // close stream
  310. if (fAudio.isStreamOpen())
  311. fAudio.closeStream();
  312. return !hasError;
  313. }
  314. bool isRunning() const noexcept override
  315. {
  316. return fAudio.isStreamOpen();
  317. }
  318. bool isOffline() const noexcept override
  319. {
  320. return false;
  321. }
  322. EngineType getType() const noexcept override
  323. {
  324. return kEngineTypeRtAudio;
  325. }
  326. const char* getCurrentDriverName() const noexcept override
  327. {
  328. return CarlaBackend::getRtAudioApiName(fAudio.getCurrentApi());
  329. }
  330. // -------------------------------------------------------------------
  331. // Patchbay
  332. bool patchbayRefresh(const bool /*external*/) override
  333. {
  334. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  335. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  336. patchbayRefreshRack();
  337. else
  338. patchbayRefreshPatchbay();
  339. return true;
  340. }
  341. void patchbayRefreshRack()
  342. {
  343. RackGraph* const graph(pData->graph.getRackGraph());
  344. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  345. graph->connections.clear();
  346. char strBuf[STR_MAX+1];
  347. strBuf[STR_MAX] = '\0';
  348. // Main
  349. {
  350. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_CARLA, PATCHBAY_ICON_CARLA, -1, 0.0f, getName());
  351. 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");
  352. 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");
  353. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  354. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  355. 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");
  356. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_OUT, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  357. }
  358. // Audio In
  359. {
  360. if (fDeviceName.isNotEmpty())
  361. std::snprintf(strBuf, STR_MAX, "Capture (%s)", fDeviceName.buffer());
  362. else
  363. std::strncpy(strBuf, "Capture", STR_MAX);
  364. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_AUDIO_IN, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  365. for (uint i=0; i < fAudioInCount; ++i)
  366. {
  367. std::snprintf(strBuf, STR_MAX, "capture_%i", i+1);
  368. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_AUDIO_IN, static_cast<int>(i)+1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, strBuf);
  369. }
  370. }
  371. // Audio Out
  372. {
  373. if (fDeviceName.isNotEmpty())
  374. std::snprintf(strBuf, STR_MAX, "Playback (%s)", fDeviceName.buffer());
  375. else
  376. std::strncpy(strBuf, "Playback", STR_MAX);
  377. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_AUDIO_OUT, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  378. for (uint i=0; i < fAudioOutCount; ++i)
  379. {
  380. std::snprintf(strBuf, STR_MAX, "playback_%i", i+1);
  381. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_GRAPH_GROUP_AUDIO_OUT, static_cast<int>(i)+1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, strBuf);
  382. }
  383. }
  384. // MIDI In
  385. {
  386. RtMidiIn midiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery");
  387. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_MIDI_IN, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
  388. for (uint i=0, count = midiIn.getPortCount(); i < count; ++i)
  389. {
  390. std::string portName(midiIn.getPortName(i));
  391. std::snprintf(strBuf, STR_MAX, "Readable MIDI ports:%s", portName.c_str());
  392. PortNameToId portNameToId;
  393. portNameToId.setData(RACK_GRAPH_GROUP_MIDI_IN, i+1, portName.c_str(), strBuf);
  394. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, portNameToId.group, static_cast<int>(portNameToId.port), PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
  395. graph->midi.ins.append(portNameToId);
  396. }
  397. }
  398. // MIDI Out
  399. {
  400. RtMidiOut midiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery");
  401. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_GRAPH_GROUP_MIDI_OUT, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
  402. for (uint i=0, count = midiOut.getPortCount(); i < count; ++i)
  403. {
  404. std::string portName(midiOut.getPortName(i));
  405. std::snprintf(strBuf, STR_MAX, "Writable MIDI ports:%s", portName.c_str());
  406. PortNameToId portNameToId;
  407. portNameToId.setData(RACK_GRAPH_GROUP_MIDI_OUT, i+1, portName.c_str(), strBuf);
  408. 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);
  409. graph->midi.outs.append(portNameToId);
  410. }
  411. }
  412. // Connections
  413. graph->audio.mutex.lock();
  414. for (LinkedList<uint>::Itenerator it = graph->audio.connectedIn1.begin(); it.valid(); it.next())
  415. {
  416. const uint& portId(it.getValue(0));
  417. CARLA_SAFE_ASSERT_CONTINUE(portId != 0);
  418. CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioInCount);
  419. ConnectionToId connectionToId;
  420. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_AUDIO_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN1);
  421. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  422. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  423. graph->connections.list.append(connectionToId);
  424. }
  425. for (LinkedList<uint>::Itenerator it = graph->audio.connectedIn2.begin(); it.valid(); it.next())
  426. {
  427. const uint& portId(it.getValue(0));
  428. CARLA_SAFE_ASSERT_CONTINUE(portId != 0);
  429. CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioInCount);
  430. ConnectionToId connectionToId;
  431. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_AUDIO_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_IN2);
  432. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  433. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  434. graph->connections.list.append(connectionToId);
  435. }
  436. for (LinkedList<uint>::Itenerator it = graph->audio.connectedOut1.begin(); it.valid(); it.next())
  437. {
  438. const uint& portId(it.getValue(0));
  439. CARLA_SAFE_ASSERT_CONTINUE(portId != 0);
  440. CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioOutCount);
  441. ConnectionToId connectionToId;
  442. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT1, RACK_GRAPH_GROUP_AUDIO_OUT, portId);
  443. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  444. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  445. graph->connections.list.append(connectionToId);
  446. }
  447. for (LinkedList<uint>::Itenerator it = graph->audio.connectedOut2.begin(); it.valid(); it.next())
  448. {
  449. const uint& portId(it.getValue(0));
  450. CARLA_SAFE_ASSERT_CONTINUE(portId != 0);
  451. CARLA_SAFE_ASSERT_CONTINUE(portId < fAudioOutCount);
  452. ConnectionToId connectionToId;
  453. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_AUDIO_OUT2, RACK_GRAPH_GROUP_AUDIO_OUT, portId);
  454. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  455. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  456. graph->connections.list.append(connectionToId);
  457. }
  458. graph->audio.mutex.unlock();
  459. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  460. {
  461. static const MidiInPort fallback = { nullptr, { '\0' } };
  462. const MidiInPort& inPort(it.getValue(fallback));
  463. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  464. const uint portId(graph->midi.getPortId(true, inPort.name));
  465. CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midi.ins.count());
  466. ConnectionToId connectionToId;
  467. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_MIDI_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_IN);
  468. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  469. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  470. graph->connections.list.append(connectionToId);
  471. }
  472. fMidiOutMutex.lock();
  473. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  474. {
  475. static const MidiOutPort fallback = { nullptr, { '\0' } };
  476. const MidiOutPort& outPort(it.getValue(fallback));
  477. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  478. const uint portId(graph->midi.getPortId(false, outPort.name));
  479. CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midi.outs.count());
  480. ConnectionToId connectionToId;
  481. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_OUT, RACK_GRAPH_GROUP_MIDI_OUT, portId);
  482. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  483. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  484. graph->connections.list.append(connectionToId);
  485. }
  486. fMidiOutMutex.unlock();
  487. }
  488. void patchbayRefreshPatchbay() noexcept
  489. {
  490. PatchbayGraph* const graph(pData->graph.getPatchbayGraph());
  491. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  492. graph->refreshConnections(this);
  493. }
  494. // -------------------------------------------------------------------
  495. protected:
  496. void handleAudioProcessCallback(void* outputBuffer, void* inputBuffer, uint nframes, double streamTime, RtAudioStreamStatus status)
  497. {
  498. const PendingRtEventsRunner prt(this);
  499. // get buffers from RtAudio
  500. const float* const insPtr = (const float*)inputBuffer;
  501. /* */ float* const outsPtr = (float*)outputBuffer;
  502. // assert rtaudio buffers
  503. CARLA_SAFE_ASSERT_RETURN(outputBuffer != nullptr,);
  504. CARLA_SAFE_ASSERT_RETURN(pData->bufferSize == nframes,);
  505. // set rtaudio buffers as non-interleaved
  506. const float* inBuf[fAudioInCount];
  507. /* */ float* outBuf[fAudioOutCount];
  508. if (fAudioInterleaved)
  509. {
  510. float* inBuf2[fAudioInCount];
  511. for (int i=0, count=static_cast<int>(fAudioInCount); i<count; ++i)
  512. {
  513. inBuf [i] = fAudioIntBufIn.getReadPointer(i);
  514. inBuf2[i] = fAudioIntBufIn.getWritePointer(i);
  515. }
  516. for (int i=0, count=static_cast<int>(fAudioOutCount); i<count; ++i)
  517. outBuf[i] = fAudioIntBufOut.getWritePointer(i);
  518. // init input
  519. for (uint i=0; i<nframes; ++i)
  520. for (uint j=0; j<fAudioInCount; ++j)
  521. inBuf2[j][i] = insPtr[i*fAudioInCount+j];
  522. // clear output
  523. fAudioIntBufOut.clear();
  524. }
  525. else
  526. {
  527. for (uint i=0; i < fAudioInCount; ++i)
  528. inBuf[i] = insPtr+(nframes*i);
  529. for (uint i=0; i < fAudioOutCount; ++i)
  530. outBuf[i] = outsPtr+(nframes*i);
  531. // clear output
  532. FloatVectorOperations::clear(outsPtr, static_cast<int>(nframes*fAudioOutCount));
  533. }
  534. // initialize events
  535. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  536. carla_zeroStruct<EngineEvent>(pData->events.out, kMaxEngineEventInternalCount);
  537. if (fMidiInEvents.mutex.tryLock())
  538. {
  539. uint32_t engineEventIndex = 0;
  540. fMidiInEvents.splice();
  541. for (LinkedList<RtMidiEvent>::Itenerator it = fMidiInEvents.data.begin(); it.valid(); it.next())
  542. {
  543. static const RtMidiEvent fallback = { 0, 0, { 0 } };
  544. const RtMidiEvent& midiEvent(it.getValue(fallback));
  545. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.size > 0);
  546. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  547. if (midiEvent.time < pData->timeInfo.frame)
  548. {
  549. engineEvent.time = 0;
  550. }
  551. else if (midiEvent.time >= pData->timeInfo.frame + nframes)
  552. {
  553. carla_stderr("MIDI Event in the future!, %i vs %i", engineEvent.time, pData->timeInfo.frame);
  554. engineEvent.time = static_cast<uint32_t>(pData->timeInfo.frame) + nframes - 1;
  555. }
  556. else
  557. engineEvent.time = static_cast<uint32_t>(midiEvent.time - pData->timeInfo.frame);
  558. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
  559. if (engineEventIndex >= kMaxEngineEventInternalCount)
  560. break;
  561. }
  562. fMidiInEvents.data.clear();
  563. fMidiInEvents.mutex.unlock();
  564. }
  565. pData->graph.process(pData, inBuf, outBuf, nframes);
  566. fMidiOutMutex.lock();
  567. if (fMidiOuts.count() > 0)
  568. {
  569. uint8_t size = 0;
  570. uint8_t data[3] = { 0, 0, 0 };
  571. const uint8_t* dataPtr = data;
  572. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  573. {
  574. const EngineEvent& engineEvent(pData->events.out[i]);
  575. if (engineEvent.type == kEngineEventTypeNull)
  576. break;
  577. else if (engineEvent.type == kEngineEventTypeControl)
  578. {
  579. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  580. ctrlEvent.convertToMidiData(engineEvent.channel, size, data);
  581. dataPtr = data;
  582. }
  583. else if (engineEvent.type == kEngineEventTypeMidi)
  584. {
  585. const EngineMidiEvent& midiEvent(engineEvent.midi);
  586. size = midiEvent.size;
  587. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  588. dataPtr = midiEvent.dataExt;
  589. else
  590. dataPtr = midiEvent.data;
  591. }
  592. else
  593. {
  594. continue;
  595. }
  596. if (size > 0)
  597. {
  598. fMidiOutVector.assign(dataPtr, dataPtr + size);
  599. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  600. {
  601. static MidiOutPort fallback = { nullptr, { '\0' } };
  602. MidiOutPort& outPort(it.getValue(fallback));
  603. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  604. outPort.port->sendMessage(&fMidiOutVector);
  605. }
  606. }
  607. }
  608. }
  609. if (fAudioInterleaved)
  610. {
  611. for (uint i=0; i < nframes; ++i)
  612. for (uint j=0; j<fAudioOutCount; ++j)
  613. outsPtr[i*fAudioOutCount+j] = outBuf[j][i];
  614. }
  615. fMidiOutMutex.unlock();
  616. return; // unused
  617. (void)streamTime; (void)status;
  618. }
  619. void handleMidiCallback(double timeStamp, std::vector<uchar>* const message)
  620. {
  621. const size_t messageSize(message->size());
  622. if (messageSize == 0 || messageSize > EngineMidiEvent::kDataSize)
  623. return;
  624. timeStamp /= 2;
  625. if (timeStamp > 0.95)
  626. timeStamp = 0.95;
  627. else if (timeStamp < 0.0)
  628. timeStamp = 0.0;
  629. RtMidiEvent midiEvent;
  630. midiEvent.time = pData->timeInfo.frame + uint64_t(timeStamp * (double)pData->bufferSize);
  631. if (midiEvent.time < fLastEventTime)
  632. midiEvent.time = fLastEventTime;
  633. else
  634. fLastEventTime = midiEvent.time;
  635. midiEvent.size = static_cast<uint8_t>(messageSize);
  636. size_t i=0;
  637. for (; i < messageSize; ++i)
  638. midiEvent.data[i] = message->at(i);
  639. for (; i < EngineMidiEvent::kDataSize; ++i)
  640. midiEvent.data[i] = 0;
  641. fMidiInEvents.append(midiEvent);
  642. }
  643. // -------------------------------------------------------------------
  644. bool connectRackMidiInPort(const char* const portName) override
  645. {
  646. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  647. carla_debug("CarlaEngineRtAudio::connectRackMidiInPort(\"%s\")", portName);
  648. RackGraph* const graph(pData->graph.getRackGraph());
  649. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  650. CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
  651. CarlaString newRtMidiPortName;
  652. newRtMidiPortName += getName();
  653. newRtMidiPortName += ":";
  654. newRtMidiPortName += portName;
  655. RtMidiIn* const rtMidiIn(new RtMidiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer(), 512));
  656. rtMidiIn->ignoreTypes();
  657. rtMidiIn->setCallback(carla_rtmidi_callback, this);
  658. bool found = false;
  659. uint rtMidiPortIndex;
  660. for (uint i=0, count=rtMidiIn->getPortCount(); i < count; ++i)
  661. {
  662. if (rtMidiIn->getPortName(i) == portName)
  663. {
  664. found = true;
  665. rtMidiPortIndex = i;
  666. break;
  667. }
  668. }
  669. if (! found)
  670. {
  671. delete rtMidiIn;
  672. return false;
  673. }
  674. try {
  675. rtMidiIn->openPort(rtMidiPortIndex, portName);
  676. }
  677. catch(...) {
  678. delete rtMidiIn;
  679. return false;
  680. };
  681. MidiInPort midiPort;
  682. midiPort.port = rtMidiIn;
  683. std::strncpy(midiPort.name, portName, STR_MAX);
  684. midiPort.name[STR_MAX] = '\0';
  685. fMidiIns.append(midiPort);
  686. return true;
  687. }
  688. bool connectRackMidiOutPort(const char* const portName) override
  689. {
  690. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  691. carla_debug("CarlaEngineRtAudio::connectRackMidiOutPort(\"%s\")", portName);
  692. RackGraph* const graph(pData->graph.getRackGraph());
  693. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  694. CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
  695. CarlaString newRtMidiPortName;
  696. newRtMidiPortName += getName();
  697. newRtMidiPortName += ":";
  698. newRtMidiPortName += portName;
  699. RtMidiOut* const rtMidiOut(new RtMidiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer()));
  700. bool found = false;
  701. uint rtMidiPortIndex;
  702. for (uint i=0, count=rtMidiOut->getPortCount(); i < count; ++i)
  703. {
  704. if (rtMidiOut->getPortName(i) == portName)
  705. {
  706. found = true;
  707. rtMidiPortIndex = i;
  708. break;
  709. }
  710. }
  711. if (! found)
  712. {
  713. delete rtMidiOut;
  714. return false;
  715. }
  716. try {
  717. rtMidiOut->openPort(rtMidiPortIndex, portName);
  718. }
  719. catch(...) {
  720. delete rtMidiOut;
  721. return false;
  722. };
  723. MidiOutPort midiPort;
  724. midiPort.port = rtMidiOut;
  725. std::strncpy(midiPort.name, portName, STR_MAX);
  726. midiPort.name[STR_MAX] = '\0';
  727. const CarlaMutexLocker cml(fMidiOutMutex);
  728. fMidiOuts.append(midiPort);
  729. return true;
  730. }
  731. bool disconnectRackMidiInPort(const char* const portName) override
  732. {
  733. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  734. carla_debug("CarlaEngineRtAudio::disconnectRackMidiInPort(\"%s\")", portName);
  735. RackGraph* const graph(pData->graph.getRackGraph());
  736. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  737. CARLA_SAFE_ASSERT_RETURN(graph->midi.ins.count() > 0, false);
  738. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  739. {
  740. static MidiInPort fallback = { nullptr, { '\0' } };
  741. MidiInPort& inPort(it.getValue(fallback));
  742. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  743. if (std::strncmp(inPort.name, portName, STR_MAX) != 0)
  744. continue;
  745. inPort.port->cancelCallback();
  746. inPort.port->closePort();
  747. delete inPort.port;
  748. fMidiIns.remove(it);
  749. return true;
  750. }
  751. return false;
  752. }
  753. bool disconnectRackMidiOutPort(const char* const portName) override
  754. {
  755. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  756. carla_debug("CarlaEngineRtAudio::disconnectRackMidiOutPort(\"%s\")", portName);
  757. RackGraph* const graph(pData->graph.getRackGraph());
  758. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  759. CARLA_SAFE_ASSERT_RETURN(graph->midi.outs.count() > 0, false);
  760. const CarlaMutexLocker cml(fMidiOutMutex);
  761. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  762. {
  763. static MidiOutPort fallback = { nullptr, { '\0' } };
  764. MidiOutPort& outPort(it.getValue(fallback));
  765. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  766. if (std::strncmp(outPort.name, portName, STR_MAX) != 0)
  767. continue;
  768. outPort.port->closePort();
  769. delete outPort.port;
  770. fMidiOuts.remove(it);
  771. return true;
  772. }
  773. return false;
  774. }
  775. // -------------------------------------------------------------------
  776. private:
  777. RtAudio fAudio;
  778. // useful info
  779. bool fAudioInterleaved;
  780. uint fAudioInCount;
  781. uint fAudioOutCount;
  782. uint64_t fLastEventTime;
  783. // current device name
  784. CarlaString fDeviceName;
  785. // temp buffer for interleaved audio
  786. AudioSampleBuffer fAudioIntBufIn;
  787. AudioSampleBuffer fAudioIntBufOut;
  788. struct MidiInPort {
  789. RtMidiIn* port;
  790. char name[STR_MAX+1];
  791. };
  792. struct MidiOutPort {
  793. RtMidiOut* port;
  794. char name[STR_MAX+1];
  795. };
  796. struct RtMidiEvent {
  797. uint64_t time; // needs to compare to internal time
  798. uint8_t size;
  799. uint8_t data[EngineMidiEvent::kDataSize];
  800. };
  801. struct RtMidiEvents {
  802. CarlaMutex mutex;
  803. RtLinkedList<RtMidiEvent>::Pool dataPool;
  804. RtLinkedList<RtMidiEvent> data;
  805. RtLinkedList<RtMidiEvent> dataPending;
  806. RtMidiEvents()
  807. : mutex(),
  808. dataPool(512, 512),
  809. data(dataPool),
  810. dataPending(dataPool) {}
  811. ~RtMidiEvents()
  812. {
  813. clear();
  814. }
  815. void append(const RtMidiEvent& event)
  816. {
  817. mutex.lock();
  818. dataPending.append(event);
  819. mutex.unlock();
  820. }
  821. void clear()
  822. {
  823. mutex.lock();
  824. data.clear();
  825. dataPending.clear();
  826. mutex.unlock();
  827. }
  828. void splice()
  829. {
  830. if (dataPending.count() > 0)
  831. dataPending.moveTo(data, true /* append */);
  832. }
  833. };
  834. LinkedList<MidiInPort> fMidiIns;
  835. RtMidiEvents fMidiInEvents;
  836. LinkedList<MidiOutPort> fMidiOuts;
  837. CarlaMutex fMidiOutMutex;
  838. std::vector<uint8_t> fMidiOutVector;
  839. #define handlePtr ((CarlaEngineRtAudio*)userData)
  840. static int carla_rtaudio_process_callback(void* outputBuffer, void* inputBuffer, uint nframes, double streamTime, RtAudioStreamStatus status, void* userData)
  841. {
  842. handlePtr->handleAudioProcessCallback(outputBuffer, inputBuffer, nframes, streamTime, status);
  843. return 0;
  844. }
  845. static void carla_rtmidi_callback(double timeStamp, std::vector<uchar>* message, void* userData)
  846. {
  847. handlePtr->handleMidiCallback(timeStamp, message);
  848. }
  849. #undef handlePtr
  850. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineRtAudio)
  851. };
  852. // -----------------------------------------
  853. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi api)
  854. {
  855. initRtAudioAPIsIfNeeded();
  856. RtAudio::Api rtApi(RtAudio::UNSPECIFIED);
  857. switch (api)
  858. {
  859. case AUDIO_API_NULL:
  860. rtApi = RtAudio::RTAUDIO_DUMMY;
  861. break;
  862. case AUDIO_API_JACK:
  863. rtApi = RtAudio::UNIX_JACK;
  864. break;
  865. case AUDIO_API_ALSA:
  866. rtApi = RtAudio::LINUX_ALSA;
  867. break;
  868. case AUDIO_API_OSS:
  869. rtApi = RtAudio::LINUX_OSS;
  870. break;
  871. case AUDIO_API_PULSE:
  872. rtApi = RtAudio::LINUX_PULSE;
  873. break;
  874. case AUDIO_API_CORE:
  875. rtApi = RtAudio::MACOSX_CORE;
  876. break;
  877. case AUDIO_API_ASIO:
  878. rtApi = RtAudio::WINDOWS_ASIO;
  879. break;
  880. case AUDIO_API_DS:
  881. rtApi = RtAudio::WINDOWS_DS;
  882. break;
  883. }
  884. return new CarlaEngineRtAudio(rtApi);
  885. }
  886. uint CarlaEngine::getRtAudioApiCount()
  887. {
  888. initRtAudioAPIsIfNeeded();
  889. return static_cast<uint>(gRtAudioApis.size());
  890. }
  891. const char* CarlaEngine::getRtAudioApiName(const uint index)
  892. {
  893. initRtAudioAPIsIfNeeded();
  894. CARLA_SAFE_ASSERT_RETURN(index < gRtAudioApis.size(), nullptr);
  895. return CarlaBackend::getRtAudioApiName(gRtAudioApis[index]);
  896. }
  897. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const uint index)
  898. {
  899. initRtAudioAPIsIfNeeded();
  900. if (index >= gRtAudioApis.size())
  901. return nullptr;
  902. const RtAudio::Api& api(gRtAudioApis[index]);
  903. RtAudio rtAudio(api);
  904. const uint devCount(rtAudio.getDeviceCount());
  905. if (devCount == 0)
  906. return nullptr;
  907. CarlaStringList devNames;
  908. for (uint i=0; i < devCount; ++i)
  909. {
  910. RtAudio::DeviceInfo devInfo(rtAudio.getDeviceInfo(i));
  911. if (devInfo.probed && devInfo.outputChannels > 0 /*&& (devInfo.nativeFormats & RTAUDIO_FLOAT32) != 0*/)
  912. devNames.append(devInfo.name.c_str());
  913. }
  914. gDeviceNames = devNames.toCharStringListPtr();
  915. return gDeviceNames;
  916. }
  917. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const uint index, const char* const deviceName)
  918. {
  919. initRtAudioAPIsIfNeeded();
  920. if (index >= gRtAudioApis.size())
  921. return nullptr;
  922. const RtAudio::Api& api(gRtAudioApis[index]);
  923. RtAudio rtAudio(api);
  924. const uint devCount(rtAudio.getDeviceCount());
  925. if (devCount == 0)
  926. return nullptr;
  927. uint i;
  928. RtAudio::DeviceInfo rtAudioDevInfo;
  929. for (i=0; i < devCount; ++i)
  930. {
  931. rtAudioDevInfo = rtAudio.getDeviceInfo(i);
  932. if (rtAudioDevInfo.name == deviceName)
  933. break;
  934. }
  935. if (i == devCount)
  936. return nullptr;
  937. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  938. static uint32_t dummyBufferSizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  939. static double dummySampleRates[] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  940. // reset
  941. devInfo.hints = 0x0;
  942. devInfo.bufferSizes = dummyBufferSizes;
  943. // cleanup
  944. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  945. {
  946. delete[] devInfo.sampleRates;
  947. devInfo.sampleRates = nullptr;
  948. }
  949. if (size_t sampleRatesCount = rtAudioDevInfo.sampleRates.size())
  950. {
  951. double* const sampleRates(new double[sampleRatesCount+1]);
  952. for (size_t j=0; j < sampleRatesCount; ++j)
  953. sampleRates[j] = rtAudioDevInfo.sampleRates[j];
  954. sampleRates[sampleRatesCount] = 0.0;
  955. devInfo.sampleRates = sampleRates;
  956. }
  957. else
  958. {
  959. devInfo.sampleRates = dummySampleRates;
  960. }
  961. return &devInfo;
  962. }
  963. // -----------------------------------------
  964. CARLA_BACKEND_END_NAMESPACE