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.

590 lines
17KB

  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 GPL.txt file
  16. */
  17. #ifndef HAVE_JUCE
  18. # error This file should not be compiled if Juce is disabled
  19. #endif
  20. #include "CarlaEngineInternal.hpp"
  21. #include "CarlaBackendUtils.hpp"
  22. // #include "RtLinkedList.hpp"
  23. #include "juce_audio_devices.h"
  24. using namespace juce;
  25. CARLA_BACKEND_START_NAMESPACE
  26. #if 0
  27. } // Fix editor indentation
  28. #endif
  29. // -------------------------------------------------------------------------------------------------------------------
  30. static const char** gRetNames = nullptr;
  31. static OwnedArray<AudioIODeviceType> gJuceDeviceTypes;
  32. static void initJuceDevices()
  33. {
  34. static AudioDeviceManager manager;
  35. if (gJuceDeviceTypes.size() == 0)
  36. manager.createAudioDeviceTypes(gJuceDeviceTypes);
  37. }
  38. // -------------------------------------------------------------------------------------------------------------------
  39. // Cleanup
  40. static struct JuceCleanup {
  41. JuceCleanup() {}
  42. ~JuceCleanup()
  43. {
  44. if (gRetNames != nullptr)
  45. {
  46. delete[] gRetNames;
  47. gRetNames = nullptr;
  48. }
  49. gJuceDeviceTypes.clear(true);
  50. }
  51. } sJuceCleanup;
  52. // -------------------------------------------------------------------------------------------------------------------
  53. // Juce Engine
  54. class CarlaEngineJuce : public CarlaEngine,
  55. public AudioIODeviceCallback
  56. {
  57. public:
  58. CarlaEngineJuce(AudioIODeviceType* const devType)
  59. : CarlaEngine(),
  60. AudioIODeviceCallback(),
  61. fDeviceType(devType)
  62. {
  63. carla_debug("CarlaEngineJuce::CarlaEngineJuce(%p)", devType);
  64. // just to make sure
  65. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  66. }
  67. ~CarlaEngineJuce() override
  68. {
  69. carla_debug("CarlaEngineJuce::~CarlaEngineJuce()");
  70. }
  71. // -------------------------------------
  72. bool init(const char* const clientName) override
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  75. carla_debug("CarlaEngineJuce::init(\"%s\")", clientName);
  76. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  77. {
  78. setLastError("Invalid process mode");
  79. return false;
  80. }
  81. pData->bufAudio.usePatchbay = (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY);
  82. String deviceName;
  83. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  84. {
  85. deviceName = pData->options.audioDevice;
  86. }
  87. else
  88. {
  89. const int defaultIndex(fDeviceType->getDefaultDeviceIndex(false));
  90. StringArray deviceNames(fDeviceType->getDeviceNames());
  91. if (defaultIndex >= 0 && defaultIndex < deviceNames.size())
  92. deviceName = deviceNames[defaultIndex];
  93. }
  94. if (deviceName.isEmpty())
  95. {
  96. setLastError("Audio device has not been selected yet and a default one is not available");
  97. return false;
  98. }
  99. fDevice = fDeviceType->createDevice(deviceName, deviceName);
  100. if (fDevice == nullptr)
  101. {
  102. setLastError("Failed to create device");
  103. return false;
  104. }
  105. StringArray inputNames(fDevice->getInputChannelNames());
  106. StringArray outputNames(fDevice->getOutputChannelNames());
  107. BigInteger inputChannels;
  108. inputChannels.setRange(0, inputNames.size(), true);
  109. BigInteger outputChannels;
  110. outputChannels.setRange(0, outputNames.size(), true);
  111. String error = fDevice->open(inputChannels, outputChannels, pData->options.audioSampleRate, static_cast<int>(pData->options.audioBufferSize));
  112. if (error.isNotEmpty())
  113. {
  114. fDevice = nullptr;
  115. setLastError(error.toUTF8());
  116. return false;
  117. }
  118. pData->bufferSize = static_cast<uint32_t>(fDevice->getCurrentBufferSizeSamples());
  119. pData->sampleRate = fDevice->getCurrentSampleRate();
  120. pData->bufAudio.inCount = static_cast<uint32_t>(inputChannels.countNumberOfSetBits());
  121. pData->bufAudio.outCount = static_cast<uint32_t>(outputChannels.countNumberOfSetBits());
  122. CARLA_SAFE_ASSERT(pData->bufAudio.outCount > 0);
  123. pData->bufAudio.create(pData->bufferSize);
  124. fDevice->start(this);
  125. CarlaEngine::init(clientName);
  126. patchbayRefresh();
  127. return true;
  128. }
  129. bool close() override
  130. {
  131. carla_debug("CarlaEngineJuce::close()");
  132. pData->bufAudio.isReady = false;
  133. bool hasError = !CarlaEngine::close();
  134. if (fDevice != nullptr)
  135. {
  136. if (fDevice->isPlaying())
  137. fDevice->stop();
  138. if (fDevice->isOpen())
  139. fDevice->close();
  140. fDevice = nullptr;
  141. }
  142. pData->bufAudio.clear();
  143. return !hasError;
  144. }
  145. bool isRunning() const noexcept override
  146. {
  147. return fDevice != nullptr && fDevice->isPlaying();
  148. }
  149. bool isOffline() const noexcept override
  150. {
  151. return false;
  152. }
  153. EngineType getType() const noexcept override
  154. {
  155. return kEngineTypeJuce;
  156. }
  157. const char* getCurrentDriverName() const noexcept override
  158. {
  159. return fDeviceType->getTypeName().toRawUTF8();
  160. }
  161. // -------------------------------------------------------------------
  162. // Patchbay
  163. bool patchbayRefresh() override
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(pData->bufAudio.isReady, false);
  166. pData->bufAudio.initPatchbay();
  167. if (pData->bufAudio.usePatchbay)
  168. {
  169. // not implemented yet
  170. return false;
  171. }
  172. char strBuf[STR_MAX+1];
  173. strBuf[STR_MAX] = '\0';
  174. //EngineRackBuffers* const rack(pData->bufAudio.rack);
  175. // Main
  176. {
  177. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_CARLA, PATCHBAY_ICON_CARLA, -1, 0.0f, getName());
  178. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_IN1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
  179. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_IN2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
  180. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_OUT1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  181. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_OUT2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  182. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_MIDI_IN, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
  183. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_MIDI_OUT, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  184. }
  185. const String& deviceName(fDevice->getName());
  186. // Audio In
  187. {
  188. if (deviceName.isNotEmpty())
  189. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName.toRawUTF8());
  190. else
  191. std::strncpy(strBuf, "Capture", STR_MAX);
  192. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_IN, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  193. StringArray inputNames(fDevice->getInputChannelNames());
  194. CARLA_SAFE_ASSERT(inputNames.size() == static_cast<int>(pData->bufAudio.inCount));
  195. for (uint i=0; i < pData->bufAudio.inCount; ++i)
  196. {
  197. String inputName(inputNames[static_cast<int>(i)]);
  198. if (inputName.trim().isNotEmpty())
  199. std::snprintf(strBuf, STR_MAX, "%s", inputName.toRawUTF8());
  200. else
  201. std::snprintf(strBuf, STR_MAX, "capture_%i", i+1);
  202. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_IN, static_cast<int>(i), PATCHBAY_PORT_TYPE_AUDIO, 0.0f, strBuf);
  203. }
  204. }
  205. // Audio Out
  206. {
  207. if (deviceName.isNotEmpty())
  208. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName.toRawUTF8());
  209. else
  210. std::strncpy(strBuf, "Playback", STR_MAX);
  211. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_OUT, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  212. StringArray outputNames(fDevice->getOutputChannelNames());
  213. CARLA_SAFE_ASSERT(outputNames.size() == static_cast<int>(pData->bufAudio.outCount));
  214. for (uint i=0; i < pData->bufAudio.outCount; ++i)
  215. {
  216. String outputName(outputNames[static_cast<int>(i)]);
  217. if (outputName.trim().isNotEmpty())
  218. std::snprintf(strBuf, STR_MAX, "%s", outputName.toRawUTF8());
  219. else
  220. std::snprintf(strBuf, STR_MAX, "playback_%i", i+1);
  221. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_OUT, static_cast<int>(i), static_cast<int>(PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT), 0.0f, strBuf);
  222. }
  223. }
  224. // MIDI In
  225. // MIDI Out
  226. // Connections
  227. return true;
  228. }
  229. // -------------------------------------------------------------------
  230. protected:
  231. void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override
  232. {
  233. // assert juce buffers
  234. CARLA_SAFE_ASSERT_RETURN(numInputChannels == static_cast<int>(pData->bufAudio.inCount),);
  235. CARLA_SAFE_ASSERT_RETURN(numOutputChannels == static_cast<int>(pData->bufAudio.outCount),);
  236. CARLA_SAFE_ASSERT_RETURN(outputChannelData != nullptr,);
  237. CARLA_SAFE_ASSERT_RETURN(numSamples == static_cast<int>(pData->bufferSize),);
  238. if (numOutputChannels == 0 || ! pData->bufAudio.isReady)
  239. return runPendingRtEvents();
  240. // initialize input events
  241. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  242. // TODO - get events from juce
  243. if (pData->bufAudio.usePatchbay)
  244. {
  245. }
  246. else
  247. {
  248. pData->processRackFull(const_cast<float**>(inputChannelData), static_cast<uint32_t>(numInputChannels),
  249. outputChannelData, static_cast<uint32_t>(numOutputChannels),
  250. static_cast<uint32_t>(numSamples), false);
  251. }
  252. // output events
  253. {
  254. // TODO
  255. //fMidiOutEvents...
  256. }
  257. runPendingRtEvents();
  258. return;
  259. // unused
  260. (void)inputChannelData;
  261. (void)numInputChannels;
  262. }
  263. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  264. {
  265. }
  266. void audioDeviceStopped() override
  267. {
  268. }
  269. void audioDeviceError(const String& errorMessage) override
  270. {
  271. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  272. }
  273. // -------------------------------------------------------------------
  274. bool connectRackMidiInPort(const int) override
  275. {
  276. return false;
  277. }
  278. bool connectRackMidiOutPort(const int) override
  279. {
  280. return false;
  281. }
  282. bool disconnectRackMidiInPort(const int) override
  283. {
  284. return false;
  285. }
  286. bool disconnectRackMidiOutPort(const int) override
  287. {
  288. return false;
  289. }
  290. // -------------------------------------
  291. private:
  292. ScopedPointer<AudioIODevice> fDevice;
  293. AudioIODeviceType* const fDeviceType;
  294. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  295. };
  296. // -----------------------------------------
  297. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  298. {
  299. initJuceDevices();
  300. String juceApi;
  301. switch (api)
  302. {
  303. case AUDIO_API_NULL:
  304. case AUDIO_API_OSS:
  305. case AUDIO_API_PULSE:
  306. break;
  307. case AUDIO_API_JACK:
  308. juceApi = "JACK";
  309. break;
  310. case AUDIO_API_ALSA:
  311. juceApi = "ALSA";
  312. break;
  313. case AUDIO_API_CORE:
  314. juceApi = "CoreAudio";
  315. break;
  316. case AUDIO_API_ASIO:
  317. juceApi = "ASIO";
  318. break;
  319. case AUDIO_API_DS:
  320. juceApi = "DirectSound";
  321. break;
  322. }
  323. if (juceApi.isEmpty())
  324. return nullptr;
  325. AudioIODeviceType* deviceType = nullptr;
  326. for (int i=0, count=gJuceDeviceTypes.size(); i < count; ++i)
  327. {
  328. deviceType = gJuceDeviceTypes[i];
  329. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  330. break;
  331. }
  332. if (deviceType == nullptr)
  333. return nullptr;
  334. deviceType->scanForDevices();
  335. return new CarlaEngineJuce(deviceType);
  336. }
  337. unsigned int CarlaEngine::getJuceApiCount()
  338. {
  339. return 0; // TODO
  340. initJuceDevices();
  341. return static_cast<unsigned int>(gJuceDeviceTypes.size());
  342. }
  343. const char* CarlaEngine::getJuceApiName(const unsigned int index)
  344. {
  345. initJuceDevices();
  346. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  347. return nullptr;
  348. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  349. if (deviceType == nullptr)
  350. return nullptr;
  351. return deviceType->getTypeName().toRawUTF8();
  352. }
  353. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int index)
  354. {
  355. initJuceDevices();
  356. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  357. return nullptr;
  358. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  359. if (deviceType == nullptr)
  360. return nullptr;
  361. deviceType->scanForDevices();
  362. StringArray deviceNames(deviceType->getDeviceNames());
  363. const int deviceNameCount(deviceNames.size());
  364. if (deviceNameCount <= 0)
  365. return nullptr;
  366. if (gRetNames != nullptr)
  367. {
  368. for (int i=0; gRetNames[i] != nullptr; ++i)
  369. delete[] gRetNames[i];
  370. delete[] gRetNames;
  371. }
  372. gRetNames = new const char*[deviceNameCount+1];
  373. for (int i=0; i < deviceNameCount; ++i)
  374. gRetNames[i] = carla_strdup(deviceNames[i].toRawUTF8());
  375. gRetNames[deviceNameCount] = nullptr;
  376. return gRetNames;
  377. }
  378. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int index, const char* const deviceName)
  379. {
  380. initJuceDevices();
  381. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  382. {
  383. carla_stderr("here 001");
  384. return nullptr;
  385. }
  386. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  387. if (deviceType == nullptr)
  388. return nullptr;
  389. deviceType->scanForDevices();
  390. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  391. if (device == nullptr)
  392. return nullptr;
  393. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  394. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  395. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  396. // reset
  397. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  398. // cleanup
  399. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  400. {
  401. delete[] devInfo.bufferSizes;
  402. devInfo.bufferSizes = nullptr;
  403. }
  404. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  405. {
  406. delete[] devInfo.sampleRates;
  407. devInfo.sampleRates = nullptr;
  408. }
  409. if (device->hasControlPanel())
  410. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  411. Array<int> juceBufferSizes = device->getAvailableBufferSizes();
  412. if (int bufferSizesCount = juceBufferSizes.size())
  413. {
  414. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  415. for (int i=0; i < bufferSizesCount; ++i)
  416. bufferSizes[i] = static_cast<uint32_t>(juceBufferSizes[i]);
  417. bufferSizes[bufferSizesCount] = 0;
  418. devInfo.bufferSizes = bufferSizes;
  419. }
  420. else
  421. {
  422. devInfo.bufferSizes = dummyBufferSizes;
  423. }
  424. Array<double> juceSampleRates = device->getAvailableSampleRates();
  425. if (int sampleRatesCount = juceSampleRates.size())
  426. {
  427. double* const sampleRates(new double[sampleRatesCount+1]);
  428. for (int i=0; i < sampleRatesCount; ++i)
  429. sampleRates[i] = juceSampleRates[i];
  430. sampleRates[sampleRatesCount] = 0.0;
  431. devInfo.sampleRates = sampleRates;
  432. }
  433. else
  434. {
  435. devInfo.sampleRates = dummySampleRates;
  436. }
  437. return &devInfo;
  438. }
  439. // -----------------------------------------
  440. CARLA_BACKEND_END_NAMESPACE