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
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
9 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
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
11 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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. #if defined(__clang__)
  25. # pragma clang diagnostic push
  26. # pragma clang diagnostic ignored "-Wconversion"
  27. # pragma clang diagnostic ignored "-Weffc++"
  28. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  29. # pragma GCC diagnostic push
  30. # pragma GCC diagnostic ignored "-Wconversion"
  31. # pragma GCC diagnostic ignored "-Weffc++"
  32. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  33. #endif
  34. #include "rtaudio/RtAudio.h"
  35. #include "rtmidi/RtMidi.h"
  36. #if defined(__clang__)
  37. # pragma clang diagnostic pop
  38. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  39. # pragma GCC diagnostic pop
  40. #endif
  41. CARLA_BACKEND_START_NAMESPACE
  42. // -------------------------------------------------------------------------------------------------------------------
  43. // Global static data
  44. static CharStringListPtr gDeviceNames;
  45. static std::vector<RtAudio::Api> gRtAudioApis;
  46. // -------------------------------------------------------------------------------------------------------------------
  47. static void initRtAudioAPIsIfNeeded()
  48. {
  49. static bool needsInit = true;
  50. if (! needsInit)
  51. return;
  52. needsInit = false;
  53. // get APIs in a local var, and pass wanted ones into gRtAudioApis
  54. std::vector<RtAudio::Api> apis;
  55. RtAudio::getCompiledApi(apis);
  56. for (std::vector<RtAudio::Api>::const_iterator it = apis.begin(), end=apis.end(); it != end; ++it)
  57. {
  58. const RtAudio::Api& api(*it);
  59. if (api == RtAudio::UNIX_JACK)
  60. {
  61. #if defined(CARLA_OS_LINUX) || defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  62. if ( ! jackbridge_is_ok())
  63. continue;
  64. #else
  65. /* NOTE
  66. * RtMidi doesn't have a native MIDI backend for these OSes,
  67. * Using RtAudio JACK funcitonality is only useful when we need to access the native MIDI APIs.
  68. * (JACK audio + ALSA MIDI, or JACK audio + CoreMidi, or JACK audio + Windows MIDI)
  69. * Because RtMidi has no native MIDI support outside of win/mac/linux, we skip these RtAudio APIs.
  70. * Those OSes can use Carla's JACK support directly, which is much better than RtAudio classes.
  71. */
  72. continue;
  73. #endif
  74. }
  75. gRtAudioApis.push_back(api);
  76. }
  77. }
  78. static const char* getRtAudioApiName(const RtAudio::Api api) noexcept
  79. {
  80. switch (api)
  81. {
  82. case RtAudio::UNSPECIFIED:
  83. return "Unspecified";
  84. case RtAudio::LINUX_ALSA:
  85. return "ALSA";
  86. case RtAudio::LINUX_OSS:
  87. return "OSS";
  88. case RtAudio::UNIX_PULSE:
  89. return "PulseAudio";
  90. case RtAudio::UNIX_JACK:
  91. #if defined(CARLA_OS_LINUX) && defined(__LINUX_ALSA__)
  92. return "JACK with ALSA-MIDI";
  93. #elif defined(CARLA_OS_MAC)
  94. return "JACK with CoreMidi";
  95. #elif defined(CARLA_OS_WIN)
  96. return "JACK with WinMM";
  97. #else
  98. return "JACK (RtAudio)";
  99. #endif
  100. case RtAudio::MACOSX_CORE:
  101. return "CoreAudio";
  102. case RtAudio::WINDOWS_ASIO:
  103. return "ASIO";
  104. case RtAudio::WINDOWS_DS:
  105. return "DirectSound";
  106. case RtAudio::WINDOWS_WASAPI:
  107. return "WASAPI";
  108. case RtAudio::RTAUDIO_DUMMY:
  109. return "Dummy";
  110. }
  111. carla_stderr("CarlaBackend::getRtAudioApiName(%i) - invalid API", api);
  112. return nullptr;
  113. }
  114. static RtMidi::Api getMatchedAudioMidiAPI(const RtAudio::Api rtApi) noexcept
  115. {
  116. switch (rtApi)
  117. {
  118. case RtAudio::UNSPECIFIED:
  119. return RtMidi::UNSPECIFIED;
  120. case RtAudio::LINUX_ALSA:
  121. case RtAudio::LINUX_OSS:
  122. return RtMidi::LINUX_ALSA;
  123. case RtAudio::UNIX_PULSE:
  124. case RtAudio::UNIX_JACK:
  125. #if defined(CARLA_OS_LINUX) && defined(__LINUX_ALSA__)
  126. return RtMidi::LINUX_ALSA;
  127. #elif defined(CARLA_OS_MAC)
  128. return RtMidi::MACOSX_CORE;
  129. #elif defined(CARLA_OS_WIN)
  130. return RtMidi::WINDOWS_MM;
  131. #else
  132. return RtMidi::RTMIDI_DUMMY;
  133. #endif
  134. case RtAudio::MACOSX_CORE:
  135. return RtMidi::MACOSX_CORE;
  136. case RtAudio::WINDOWS_ASIO:
  137. case RtAudio::WINDOWS_DS:
  138. case RtAudio::WINDOWS_WASAPI:
  139. return RtMidi::WINDOWS_MM;
  140. case RtAudio::RTAUDIO_DUMMY:
  141. return RtMidi::RTMIDI_DUMMY;
  142. }
  143. return RtMidi::UNSPECIFIED;
  144. }
  145. // -------------------------------------------------------------------------------------------------------------------
  146. // RtAudio Engine
  147. class CarlaEngineRtAudio : public CarlaEngine
  148. {
  149. public:
  150. CarlaEngineRtAudio(const RtAudio::Api api)
  151. : CarlaEngine(),
  152. fAudio(api),
  153. fAudioInterleaved(false),
  154. fAudioInCount(0),
  155. fAudioOutCount(0),
  156. fLastEventTime(0),
  157. fDeviceName(),
  158. fAudioIntBufIn(nullptr),
  159. fAudioIntBufOut(nullptr),
  160. fMidiIns(),
  161. fMidiInEvents(),
  162. fMidiOuts(),
  163. fMidiOutMutex(),
  164. fMidiOutVector(EngineMidiEvent::kDataSize)
  165. {
  166. carla_debug("CarlaEngineRtAudio::CarlaEngineRtAudio(%i)", api);
  167. // just to make sure
  168. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  169. }
  170. ~CarlaEngineRtAudio() override
  171. {
  172. CARLA_SAFE_ASSERT(fAudioInCount == 0);
  173. CARLA_SAFE_ASSERT(fAudioOutCount == 0);
  174. CARLA_SAFE_ASSERT(fLastEventTime == 0);
  175. carla_debug("CarlaEngineRtAudio::~CarlaEngineRtAudio()");
  176. }
  177. // -------------------------------------
  178. bool init(const char* const clientName) override
  179. {
  180. CARLA_SAFE_ASSERT_RETURN(fAudioInCount == 0, false);
  181. CARLA_SAFE_ASSERT_RETURN(fAudioOutCount == 0, false);
  182. CARLA_SAFE_ASSERT_RETURN(fLastEventTime == 0, false);
  183. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  184. carla_debug("CarlaEngineRtAudio::init(\"%s\")", clientName);
  185. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  186. {
  187. setLastError("Invalid process mode");
  188. return false;
  189. }
  190. const bool isDummy(fAudio.getCurrentApi() == RtAudio::RtAudio::RTAUDIO_DUMMY);
  191. bool deviceSet = false;
  192. RtAudio::StreamParameters iParams, oParams;
  193. if (isDummy)
  194. {
  195. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  196. {
  197. setLastError("Cannot use dummy driver in Rack mode");
  198. return false;
  199. }
  200. fDeviceName = "Dummy";
  201. }
  202. else
  203. {
  204. const uint devCount(fAudio.getDeviceCount());
  205. if (devCount == 0)
  206. {
  207. setLastError("No audio devices available for this driver");
  208. return false;
  209. }
  210. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  211. {
  212. for (uint i=0; i < devCount; ++i)
  213. {
  214. RtAudio::DeviceInfo devInfo(fAudio.getDeviceInfo(i));
  215. if (devInfo.probed && devInfo.outputChannels > 0 && devInfo.name == pData->options.audioDevice)
  216. {
  217. deviceSet = true;
  218. fDeviceName = devInfo.name.c_str();
  219. iParams.deviceId = i;
  220. oParams.deviceId = i;
  221. iParams.nChannels = devInfo.inputChannels;
  222. oParams.nChannels = devInfo.outputChannels;
  223. break;
  224. }
  225. }
  226. }
  227. if (! deviceSet)
  228. {
  229. iParams.deviceId = fAudio.getDefaultInputDevice();
  230. oParams.deviceId = fAudio.getDefaultOutputDevice();
  231. iParams.nChannels = fAudio.getDeviceInfo(iParams.deviceId).inputChannels;
  232. oParams.nChannels = fAudio.getDeviceInfo(oParams.deviceId).outputChannels;
  233. carla_stdout("No device set, using %i inputs and %i outputs", iParams.nChannels, oParams.nChannels);
  234. }
  235. if (oParams.nChannels == 0 && pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  236. {
  237. setLastError("Current audio setup has no outputs, cannot continue");
  238. return false;
  239. }
  240. iParams.nChannels = carla_fixedValue(0U, 128U, iParams.nChannels);
  241. oParams.nChannels = carla_fixedValue(0U, 128U, oParams.nChannels);
  242. fAudioInterleaved = fAudio.getCurrentApi() == RtAudio::UNIX_PULSE;
  243. }
  244. RtAudio::StreamOptions rtOptions;
  245. rtOptions.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_SCHEDULE_REALTIME;
  246. rtOptions.numberOfBuffers = pData->options.audioTripleBuffer ? 3 : 2;
  247. rtOptions.streamName = clientName;
  248. rtOptions.priority = 85;
  249. if (fAudio.getCurrentApi() == RtAudio::LINUX_ALSA && ! deviceSet)
  250. rtOptions.flags |= RTAUDIO_ALSA_USE_DEFAULT;
  251. if (! fAudioInterleaved)
  252. rtOptions.flags |= RTAUDIO_NONINTERLEAVED;
  253. uint bufferFrames = pData->options.audioBufferSize;
  254. try {
  255. fAudio.openStream(oParams.nChannels > 0 ? &oParams : nullptr,
  256. iParams.nChannels > 0 ? &iParams : nullptr,
  257. RTAUDIO_FLOAT32, pData->options.audioSampleRate, &bufferFrames,
  258. carla_rtaudio_process_callback, this, &rtOptions,
  259. carla_rtaudio_buffer_size_callback);
  260. }
  261. catch (const RtAudioError& e) {
  262. setLastError(e.what());
  263. return false;
  264. }
  265. if (! pData->init(clientName))
  266. {
  267. close();
  268. setLastError("Failed to init internal data");
  269. return false;
  270. }
  271. pData->bufferSize = bufferFrames;
  272. pData->sampleRate = isDummy ? 44100.0 : fAudio.getStreamSampleRate();
  273. pData->initTime(pData->options.transportExtra);
  274. fAudioInCount = iParams.nChannels;
  275. fAudioOutCount = oParams.nChannels;
  276. fLastEventTime = 0;
  277. if (fAudioInCount > 0)
  278. fAudioIntBufIn = new float[fAudioInCount*bufferFrames];
  279. if (fAudioOutCount > 0)
  280. fAudioIntBufOut = new float[fAudioOutCount*bufferFrames];
  281. pData->graph.create(fAudioInCount, fAudioOutCount, 0, 0);
  282. try {
  283. fAudio.startStream();
  284. }
  285. catch (const RtAudioError& e)
  286. {
  287. close();
  288. setLastError(e.what());
  289. return false;
  290. }
  291. patchbayRefresh(true, false, false);
  292. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  293. refreshExternalGraphPorts<PatchbayGraph>(pData->graph.getPatchbayGraph(), false, false);
  294. callback(true, true,
  295. ENGINE_CALLBACK_ENGINE_STARTED,
  296. 0,
  297. pData->options.processMode,
  298. pData->options.transportMode,
  299. static_cast<int>(pData->bufferSize),
  300. static_cast<float>(pData->sampleRate),
  301. getCurrentDriverName());
  302. return true;
  303. }
  304. bool close() override
  305. {
  306. carla_debug("CarlaEngineRtAudio::close()");
  307. bool hasError = false;
  308. // stop stream first
  309. if (fAudio.isStreamOpen() && fAudio.isStreamRunning())
  310. {
  311. try {
  312. fAudio.stopStream();
  313. }
  314. catch (const RtAudioError& e)
  315. {
  316. setLastError(e.what());
  317. hasError = true;
  318. }
  319. }
  320. // clear engine data
  321. CarlaEngine::close();
  322. pData->graph.destroy();
  323. for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin2(); it.valid(); it.next())
  324. {
  325. static MidiInPort fallback = { nullptr, { '\0' } };
  326. MidiInPort& inPort(it.getValue(fallback));
  327. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  328. inPort.port->cancelCallback();
  329. inPort.port->closePort();
  330. delete inPort.port;
  331. }
  332. fMidiIns.clear();
  333. fMidiInEvents.clear();
  334. fMidiOutMutex.lock();
  335. for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin2(); it.valid(); it.next())
  336. {
  337. static MidiOutPort fallback = { nullptr, { '\0' } };
  338. MidiOutPort& outPort(it.getValue(fallback));
  339. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  340. outPort.port->closePort();
  341. delete outPort.port;
  342. }
  343. fMidiOuts.clear();
  344. fMidiOutMutex.unlock();
  345. fAudioInCount = 0;
  346. fAudioOutCount = 0;
  347. fLastEventTime = 0;
  348. fDeviceName.clear();
  349. if (fAudioIntBufIn != nullptr)
  350. {
  351. delete[] fAudioIntBufIn;
  352. fAudioIntBufIn = nullptr;
  353. }
  354. if (fAudioIntBufOut != nullptr)
  355. {
  356. delete[] fAudioIntBufOut;
  357. fAudioIntBufOut = nullptr;
  358. }
  359. // close stream
  360. if (fAudio.isStreamOpen())
  361. fAudio.closeStream();
  362. return !hasError;
  363. }
  364. bool isRunning() const noexcept override
  365. {
  366. return fAudio.isStreamOpen();
  367. }
  368. bool isOffline() const noexcept override
  369. {
  370. return false;
  371. }
  372. EngineType getType() const noexcept override
  373. {
  374. return kEngineTypeRtAudio;
  375. }
  376. const char* getCurrentDriverName() const noexcept override
  377. {
  378. return CarlaBackend::getRtAudioApiName(fAudio.getCurrentApi());
  379. }
  380. // -------------------------------------------------------------------
  381. // Patchbay
  382. template<class Graph>
  383. bool refreshExternalGraphPorts(Graph* const graph, const bool sendHost, const bool sendOSC)
  384. {
  385. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  386. char strBuf[STR_MAX+1U];
  387. strBuf[STR_MAX] = '\0';
  388. ExternalGraph& extGraph(graph->extGraph);
  389. // ---------------------------------------------------------------
  390. // clear last ports
  391. extGraph.clear();
  392. // ---------------------------------------------------------------
  393. // fill in new ones
  394. // Audio In
  395. for (uint i=0; i < fAudioInCount; ++i)
  396. {
  397. std::snprintf(strBuf, STR_MAX, "capture_%i", i+1);
  398. PortNameToId portNameToId;
  399. portNameToId.setData(kExternalGraphGroupAudioIn, i+1, strBuf, "");
  400. extGraph.audioPorts.ins.append(portNameToId);
  401. }
  402. // Audio Out
  403. for (uint i=0; i < fAudioOutCount; ++i)
  404. {
  405. std::snprintf(strBuf, STR_MAX, "playback_%i", i+1);
  406. PortNameToId portNameToId;
  407. portNameToId.setData(kExternalGraphGroupAudioOut, i+1, strBuf, "");
  408. extGraph.audioPorts.outs.append(portNameToId);
  409. }
  410. // MIDI In
  411. try
  412. {
  413. RtMidiIn midiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery-in");
  414. for (uint i=0, count = midiIn.getPortCount(); i < count; ++i)
  415. {
  416. PortNameToId portNameToId;
  417. portNameToId.setData(kExternalGraphGroupMidiIn, i+1, midiIn.getPortName(i).c_str(), "");
  418. extGraph.midiPorts.ins.append(portNameToId);
  419. }
  420. } CARLA_SAFE_EXCEPTION("RtMidiIn discovery");
  421. // MIDI Out
  422. try
  423. {
  424. RtMidiOut midiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), "carla-discovery-out");
  425. for (uint i=0, count = midiOut.getPortCount(); i < count; ++i)
  426. {
  427. PortNameToId portNameToId;
  428. portNameToId.setData(kExternalGraphGroupMidiOut, i+1, midiOut.getPortName(i).c_str(), "");
  429. extGraph.midiPorts.outs.append(portNameToId);
  430. }
  431. } CARLA_SAFE_EXCEPTION("RtMidiOut discovery");
  432. // ---------------------------------------------------------------
  433. // now refresh
  434. if (sendHost || sendOSC)
  435. graph->refresh(sendHost, sendOSC, true, fDeviceName.buffer());
  436. // ---------------------------------------------------------------
  437. // add midi connections
  438. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin2(); it.valid(); it.next())
  439. {
  440. static const MidiInPort fallback = { nullptr, { '\0' } };
  441. const MidiInPort& inPort(it.getValue(fallback));
  442. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  443. const uint portId(extGraph.midiPorts.getPortId(true, inPort.name));
  444. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.ins.count());
  445. ConnectionToId connectionToId;
  446. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupMidiIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn);
  447. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  448. extGraph.connections.list.append(connectionToId);
  449. callback(sendHost, sendOSC,
  450. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  451. connectionToId.id,
  452. 0, 0, 0, 0.0f,
  453. strBuf);
  454. }
  455. fMidiOutMutex.lock();
  456. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin2(); it.valid(); it.next())
  457. {
  458. static const MidiOutPort fallback = { nullptr, { '\0' } };
  459. const MidiOutPort& outPort(it.getValue(fallback));
  460. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  461. const uint portId(extGraph.midiPorts.getPortId(false, outPort.name));
  462. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.outs.count());
  463. ConnectionToId connectionToId;
  464. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, kExternalGraphGroupMidiOut, portId);
  465. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  466. extGraph.connections.list.append(connectionToId);
  467. callback(sendHost, sendOSC,
  468. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  469. connectionToId.id,
  470. 0, 0, 0, 0.0f,
  471. strBuf);
  472. }
  473. fMidiOutMutex.unlock();
  474. return true;
  475. }
  476. bool patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external) override
  477. {
  478. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  479. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  480. return refreshExternalGraphPorts<RackGraph>(pData->graph.getRackGraph(), sendHost, sendOSC);
  481. if (sendHost)
  482. pData->graph.setUsingExternalHost(external);
  483. if (sendOSC)
  484. pData->graph.setUsingExternalOSC(external);
  485. if (external)
  486. return refreshExternalGraphPorts<PatchbayGraph>(pData->graph.getPatchbayGraph(), sendHost, sendOSC);
  487. return CarlaEngine::patchbayRefresh(sendHost, sendOSC, false);
  488. }
  489. // -------------------------------------------------------------------
  490. protected:
  491. void handleAudioProcessCallback(void* outputBuffer, void* inputBuffer,
  492. uint nframes, double streamTime, RtAudioStreamStatus status)
  493. {
  494. const PendingRtEventsRunner prt(this, nframes, true);
  495. if (status & RTAUDIO_INPUT_OVERFLOW)
  496. ++pData->xruns;
  497. if (status & RTAUDIO_OUTPUT_UNDERFLOW)
  498. ++pData->xruns;
  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. // set rtaudio buffers as non-interleaved
  505. const float* inBuf[fAudioInCount];
  506. /* */ float* outBuf[fAudioOutCount];
  507. if (fAudioInterleaved)
  508. {
  509. // FIXME - this looks completely wrong!
  510. float* inBuf2[fAudioInCount];
  511. for (uint i=0, count=fAudioInCount; i<count; ++i)
  512. {
  513. inBuf [i] = fAudioIntBufIn + (nframes*i);
  514. inBuf2[i] = fAudioIntBufIn + (nframes*i);
  515. }
  516. for (uint i=0, count=fAudioOutCount; i<count; ++i)
  517. outBuf[i] = fAudioIntBufOut + (nframes*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. carla_zeroFloats(fAudioIntBufOut, fAudioOutCount*nframes);
  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. carla_zeroFloats(outsPtr, nframes*fAudioOutCount);
  533. }
  534. // initialize events
  535. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  536. carla_zeroStructs(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.begin2(); 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, 0);
  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 mdata[3] = { 0, 0, 0 };
  571. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  572. const uint8_t* mdataPtr;
  573. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  574. {
  575. const EngineEvent& engineEvent(pData->events.out[i]);
  576. /**/ if (engineEvent.type == kEngineEventTypeNull)
  577. {
  578. break;
  579. }
  580. else if (engineEvent.type == kEngineEventTypeControl)
  581. {
  582. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  583. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  584. mdataPtr = mdata;
  585. }
  586. else if (engineEvent.type == kEngineEventTypeMidi)
  587. {
  588. const EngineMidiEvent& midiEvent(engineEvent.midi);
  589. size = midiEvent.size;
  590. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  591. if (size > EngineMidiEvent::kDataSize)
  592. {
  593. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  594. mdataPtr = midiEvent.dataExt;
  595. }
  596. else
  597. {
  598. // set first byte
  599. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  600. // copy rest
  601. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1U);
  602. // done
  603. mdataPtr = mdataTmp;
  604. }
  605. }
  606. else
  607. {
  608. continue;
  609. }
  610. if (size > 0)
  611. {
  612. fMidiOutVector.assign(mdataPtr, mdataPtr + size);
  613. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin2(); it.valid(); it.next())
  614. {
  615. static MidiOutPort fallback = { nullptr, { '\0' } };
  616. MidiOutPort& outPort(it.getValue(fallback));
  617. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  618. outPort.port->sendMessage(&fMidiOutVector);
  619. }
  620. }
  621. }
  622. }
  623. fMidiOutMutex.unlock();
  624. if (fAudioInterleaved)
  625. {
  626. for (uint i=0; i < nframes; ++i)
  627. for (uint j=0; j<fAudioOutCount; ++j)
  628. outsPtr[i*fAudioOutCount+j] = outBuf[j][i];
  629. }
  630. return; // unused
  631. (void)streamTime;
  632. }
  633. void handleBufferSizeCallback(const uint newBufferSize)
  634. {
  635. carla_stdout("bufferSize callback %u %u", pData->bufferSize, newBufferSize);
  636. if (pData->bufferSize == newBufferSize)
  637. return;
  638. if (fAudioInCount > 0)
  639. {
  640. delete[] fAudioIntBufIn;
  641. fAudioIntBufIn = new float[fAudioInCount*newBufferSize];
  642. }
  643. if (fAudioOutCount > 0)
  644. {
  645. delete[] fAudioIntBufOut;
  646. fAudioIntBufOut = new float[fAudioOutCount*newBufferSize];
  647. }
  648. pData->bufferSize = newBufferSize;
  649. bufferSizeChanged(newBufferSize);
  650. }
  651. void handleMidiCallback(double timeStamp, std::vector<uchar>* const message)
  652. {
  653. const size_t messageSize(message->size());
  654. if (messageSize == 0 || messageSize > EngineMidiEvent::kDataSize)
  655. return;
  656. timeStamp /= 2;
  657. if (timeStamp > 0.95)
  658. timeStamp = 0.95;
  659. else if (timeStamp < 0.0)
  660. timeStamp = 0.0;
  661. RtMidiEvent midiEvent;
  662. midiEvent.time = pData->timeInfo.frame + uint64_t(timeStamp * (double)pData->bufferSize);
  663. if (midiEvent.time < fLastEventTime)
  664. midiEvent.time = fLastEventTime;
  665. else
  666. fLastEventTime = midiEvent.time;
  667. midiEvent.size = static_cast<uint8_t>(messageSize);
  668. size_t i=0;
  669. for (; i < messageSize; ++i)
  670. midiEvent.data[i] = message->at(i);
  671. for (; i < EngineMidiEvent::kDataSize; ++i)
  672. midiEvent.data[i] = 0;
  673. fMidiInEvents.append(midiEvent);
  674. }
  675. // -------------------------------------------------------------------
  676. bool connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName) override
  677. {
  678. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  679. carla_debug("CarlaEngineRtAudio::connectExternalGraphPort(%u, %u, \"%s\")", connectionType, portId, portName);
  680. switch (connectionType)
  681. {
  682. case kExternalGraphConnectionAudioIn1:
  683. case kExternalGraphConnectionAudioIn2:
  684. case kExternalGraphConnectionAudioOut1:
  685. case kExternalGraphConnectionAudioOut2:
  686. return CarlaEngine::connectExternalGraphPort(connectionType, portId, portName);
  687. case kExternalGraphConnectionMidiInput: {
  688. CarlaString newRtMidiPortName;
  689. newRtMidiPortName += getName();
  690. newRtMidiPortName += ":";
  691. newRtMidiPortName += portName;
  692. RtMidiIn* rtMidiIn;
  693. try {
  694. rtMidiIn = new RtMidiIn(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer(), 512);
  695. } CARLA_SAFE_EXCEPTION_RETURN("new RtMidiIn", false);
  696. rtMidiIn->ignoreTypes();
  697. rtMidiIn->setCallback(carla_rtmidi_callback, this);
  698. bool found = false;
  699. uint rtMidiPortIndex;
  700. for (uint i=0, count=rtMidiIn->getPortCount(); i < count; ++i)
  701. {
  702. if (rtMidiIn->getPortName(i) == portName)
  703. {
  704. found = true;
  705. rtMidiPortIndex = i;
  706. break;
  707. }
  708. }
  709. if (! found)
  710. {
  711. delete rtMidiIn;
  712. return false;
  713. }
  714. try {
  715. rtMidiIn->openPort(rtMidiPortIndex, portName);
  716. }
  717. catch(...) {
  718. delete rtMidiIn;
  719. return false;
  720. };
  721. MidiInPort midiPort;
  722. midiPort.port = rtMidiIn;
  723. std::strncpy(midiPort.name, portName, STR_MAX);
  724. midiPort.name[STR_MAX] = '\0';
  725. fMidiIns.append(midiPort);
  726. return true;
  727. } break;
  728. case kExternalGraphConnectionMidiOutput: {
  729. CarlaString newRtMidiPortName;
  730. newRtMidiPortName += getName();
  731. newRtMidiPortName += ":";
  732. newRtMidiPortName += portName;
  733. RtMidiOut* rtMidiOut;
  734. try {
  735. rtMidiOut = new RtMidiOut(getMatchedAudioMidiAPI(fAudio.getCurrentApi()), newRtMidiPortName.buffer());
  736. } CARLA_SAFE_EXCEPTION_RETURN("new RtMidiOut", false);
  737. bool found = false;
  738. uint rtMidiPortIndex;
  739. for (uint i=0, count=rtMidiOut->getPortCount(); i < count; ++i)
  740. {
  741. if (rtMidiOut->getPortName(i) == portName)
  742. {
  743. found = true;
  744. rtMidiPortIndex = i;
  745. break;
  746. }
  747. }
  748. if (! found)
  749. {
  750. delete rtMidiOut;
  751. return false;
  752. }
  753. try {
  754. rtMidiOut->openPort(rtMidiPortIndex, portName);
  755. }
  756. catch(...) {
  757. delete rtMidiOut;
  758. return false;
  759. };
  760. MidiOutPort midiPort;
  761. midiPort.port = rtMidiOut;
  762. std::strncpy(midiPort.name, portName, STR_MAX);
  763. midiPort.name[STR_MAX] = '\0';
  764. const CarlaMutexLocker cml(fMidiOutMutex);
  765. fMidiOuts.append(midiPort);
  766. return true;
  767. } break;
  768. }
  769. return false;
  770. }
  771. bool disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName) override
  772. {
  773. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  774. carla_debug("CarlaEngineRtAudio::disconnectExternalGraphPort(%u, %u, \"%s\")", connectionType, portId, portName);
  775. switch (connectionType)
  776. {
  777. case kExternalGraphConnectionAudioIn1:
  778. case kExternalGraphConnectionAudioIn2:
  779. case kExternalGraphConnectionAudioOut1:
  780. case kExternalGraphConnectionAudioOut2:
  781. return CarlaEngine::disconnectExternalGraphPort(connectionType, portId, portName);
  782. case kExternalGraphConnectionMidiInput:
  783. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin2(); it.valid(); it.next())
  784. {
  785. static MidiInPort fallback = { nullptr, { '\0' } };
  786. MidiInPort& inPort(it.getValue(fallback));
  787. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  788. if (std::strncmp(inPort.name, portName, STR_MAX) != 0)
  789. continue;
  790. inPort.port->cancelCallback();
  791. inPort.port->closePort();
  792. delete inPort.port;
  793. fMidiIns.remove(it);
  794. return true;
  795. }
  796. break;
  797. case kExternalGraphConnectionMidiOutput: {
  798. const CarlaMutexLocker cml(fMidiOutMutex);
  799. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin2(); it.valid(); it.next())
  800. {
  801. static MidiOutPort fallback = { nullptr, { '\0' } };
  802. MidiOutPort& outPort(it.getValue(fallback));
  803. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  804. if (std::strncmp(outPort.name, portName, STR_MAX) != 0)
  805. continue;
  806. outPort.port->closePort();
  807. delete outPort.port;
  808. fMidiOuts.remove(it);
  809. return true;
  810. }
  811. } break;
  812. }
  813. return false;
  814. }
  815. // -------------------------------------------------------------------
  816. private:
  817. RtAudio fAudio;
  818. // useful info
  819. bool fAudioInterleaved;
  820. uint fAudioInCount;
  821. uint fAudioOutCount;
  822. uint64_t fLastEventTime;
  823. // current device name
  824. CarlaString fDeviceName;
  825. // temp buffer for interleaved audio
  826. float* fAudioIntBufIn;
  827. float* fAudioIntBufOut;
  828. struct MidiInPort {
  829. RtMidiIn* port;
  830. char name[STR_MAX+1];
  831. };
  832. struct MidiOutPort {
  833. RtMidiOut* port;
  834. char name[STR_MAX+1];
  835. };
  836. struct RtMidiEvent {
  837. uint64_t time; // needs to compare to internal time
  838. uint8_t size;
  839. uint8_t data[EngineMidiEvent::kDataSize];
  840. };
  841. struct RtMidiEvents {
  842. CarlaMutex mutex;
  843. RtLinkedList<RtMidiEvent>::Pool dataPool;
  844. RtLinkedList<RtMidiEvent> data;
  845. RtLinkedList<RtMidiEvent> dataPending;
  846. RtMidiEvents()
  847. : mutex(),
  848. dataPool(512, 512),
  849. data(dataPool),
  850. dataPending(dataPool) {}
  851. ~RtMidiEvents()
  852. {
  853. clear();
  854. }
  855. void append(const RtMidiEvent& event)
  856. {
  857. mutex.lock();
  858. dataPending.append(event);
  859. mutex.unlock();
  860. }
  861. void clear()
  862. {
  863. mutex.lock();
  864. data.clear();
  865. dataPending.clear();
  866. mutex.unlock();
  867. }
  868. void splice()
  869. {
  870. if (dataPending.count() > 0)
  871. dataPending.moveTo(data, true /* append */);
  872. }
  873. };
  874. LinkedList<MidiInPort> fMidiIns;
  875. RtMidiEvents fMidiInEvents;
  876. LinkedList<MidiOutPort> fMidiOuts;
  877. CarlaMutex fMidiOutMutex;
  878. std::vector<uint8_t> fMidiOutVector;
  879. #define handlePtr ((CarlaEngineRtAudio*)userData)
  880. static int carla_rtaudio_process_callback(void* outputBuffer, void* inputBuffer, uint nframes, double streamTime, RtAudioStreamStatus status, void* userData)
  881. {
  882. handlePtr->handleAudioProcessCallback(outputBuffer, inputBuffer, nframes, streamTime, status);
  883. return 0;
  884. }
  885. static bool carla_rtaudio_buffer_size_callback(unsigned int bufferSize, void* userData)
  886. {
  887. handlePtr->handleBufferSizeCallback(bufferSize);
  888. return true;
  889. }
  890. static void carla_rtmidi_callback(double timeStamp, std::vector<uchar>* message, void* userData)
  891. {
  892. handlePtr->handleMidiCallback(timeStamp, message);
  893. }
  894. #undef handlePtr
  895. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineRtAudio)
  896. };
  897. // -----------------------------------------
  898. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi api)
  899. {
  900. initRtAudioAPIsIfNeeded();
  901. RtAudio::Api rtApi = RtAudio::UNSPECIFIED;
  902. switch (api)
  903. {
  904. case AUDIO_API_NULL:
  905. rtApi = RtAudio::RTAUDIO_DUMMY;
  906. break;
  907. case AUDIO_API_JACK:
  908. rtApi = RtAudio::UNIX_JACK;
  909. break;
  910. case AUDIO_API_OSS:
  911. rtApi = RtAudio::LINUX_OSS;
  912. break;
  913. case AUDIO_API_ALSA:
  914. rtApi = RtAudio::LINUX_ALSA;
  915. break;
  916. case AUDIO_API_PULSEAUDIO:
  917. rtApi = RtAudio::UNIX_PULSE;
  918. break;
  919. case AUDIO_API_COREAUDIO:
  920. rtApi = RtAudio::MACOSX_CORE;
  921. break;
  922. case AUDIO_API_ASIO:
  923. rtApi = RtAudio::WINDOWS_ASIO;
  924. break;
  925. case AUDIO_API_DIRECTSOUND:
  926. rtApi = RtAudio::WINDOWS_DS;
  927. break;
  928. case AUDIO_API_WASAPI:
  929. rtApi = RtAudio::WINDOWS_WASAPI;
  930. break;
  931. }
  932. return new CarlaEngineRtAudio(rtApi);
  933. }
  934. uint CarlaEngine::getRtAudioApiCount()
  935. {
  936. initRtAudioAPIsIfNeeded();
  937. return static_cast<uint>(gRtAudioApis.size());
  938. }
  939. const char* CarlaEngine::getRtAudioApiName(const uint index)
  940. {
  941. initRtAudioAPIsIfNeeded();
  942. CARLA_SAFE_ASSERT_RETURN(index < gRtAudioApis.size(), nullptr);
  943. return CarlaBackend::getRtAudioApiName(gRtAudioApis[index]);
  944. }
  945. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const uint index)
  946. {
  947. initRtAudioAPIsIfNeeded();
  948. if (index >= gRtAudioApis.size())
  949. return nullptr;
  950. const RtAudio::Api& api(gRtAudioApis[index]);
  951. CarlaStringList devNames;
  952. try {
  953. RtAudio rtAudio(api);
  954. const uint devCount(rtAudio.getDeviceCount());
  955. if (devCount == 0)
  956. return nullptr;
  957. for (uint i=0; i < devCount; ++i)
  958. {
  959. RtAudio::DeviceInfo devInfo(rtAudio.getDeviceInfo(i));
  960. if (devInfo.probed && devInfo.outputChannels > 0 /*&& (devInfo.nativeFormats & RTAUDIO_FLOAT32) != 0*/)
  961. devNames.append(devInfo.name.c_str());
  962. }
  963. } CARLA_SAFE_EXCEPTION_RETURN("RtAudio device names", nullptr);
  964. gDeviceNames = devNames.toCharStringListPtr();
  965. return gDeviceNames;
  966. }
  967. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const uint index, const char* const deviceName)
  968. {
  969. initRtAudioAPIsIfNeeded();
  970. if (index >= gRtAudioApis.size())
  971. return nullptr;
  972. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  973. static uint32_t dummyBufferSizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  974. static double dummySampleRates[] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  975. // reset
  976. devInfo.hints = 0x0;
  977. // cleanup
  978. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  979. {
  980. delete[] devInfo.bufferSizes;
  981. devInfo.bufferSizes = nullptr;
  982. }
  983. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  984. {
  985. delete[] devInfo.sampleRates;
  986. devInfo.sampleRates = nullptr;
  987. }
  988. const RtAudio::Api& api(gRtAudioApis[index]);
  989. if (api == RtAudio::UNIX_JACK)
  990. {
  991. devInfo.bufferSizes = nullptr;
  992. devInfo.sampleRates = nullptr;
  993. return &devInfo;
  994. }
  995. RtAudio::DeviceInfo rtAudioDevInfo;
  996. try {
  997. RtAudio rtAudio(api);
  998. const uint devCount(rtAudio.getDeviceCount());
  999. if (devCount == 0)
  1000. return nullptr;
  1001. uint i;
  1002. for (i=0; i < devCount; ++i)
  1003. {
  1004. rtAudioDevInfo = rtAudio.getDeviceInfo(i);
  1005. if (rtAudioDevInfo.name == deviceName)
  1006. break;
  1007. }
  1008. if (i == devCount)
  1009. rtAudioDevInfo = rtAudio.getDeviceInfo(rtAudio.getDefaultOutputDevice());
  1010. } CARLA_SAFE_EXCEPTION_RETURN("RtAudio device discovery", nullptr);
  1011. // a few APIs can do triple buffer
  1012. switch (api)
  1013. {
  1014. case RtAudio::LINUX_ALSA:
  1015. case RtAudio::LINUX_OSS:
  1016. case RtAudio::WINDOWS_DS:
  1017. devInfo.hints |= ENGINE_DRIVER_DEVICE_CAN_TRIPLE_BUFFER;
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. // always use default buffer sizes
  1023. devInfo.bufferSizes = dummyBufferSizes;
  1024. // valid sample rates
  1025. if (const size_t sampleRatesCount = rtAudioDevInfo.sampleRates.size())
  1026. {
  1027. double* const sampleRates(new double[sampleRatesCount+1]);
  1028. for (size_t i=0; i < sampleRatesCount; ++i)
  1029. sampleRates[i] = rtAudioDevInfo.sampleRates[i];
  1030. sampleRates[sampleRatesCount] = 0.0;
  1031. devInfo.sampleRates = sampleRates;
  1032. }
  1033. else
  1034. {
  1035. devInfo.sampleRates = dummySampleRates;
  1036. }
  1037. return &devInfo;
  1038. }
  1039. // -----------------------------------------
  1040. CARLA_BACKEND_END_NAMESPACE