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.

285 lines
7.1KB

  1. /*
  2. * Carla Juce Engine
  3. * Copyright (C) 2013 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 "CarlaMIDI.h"
  23. // #include "RtList.hpp"
  24. #include "juce_audio_devices.h"
  25. using namespace juce;
  26. CARLA_BACKEND_START_NAMESPACE
  27. #if 0
  28. } // Fix editor indentation
  29. #endif
  30. // -------------------------------------------------------------------------------------------------------------------
  31. static const char** gRetNames = nullptr;
  32. static OwnedArray<AudioIODeviceType> gJuceDeviceTypes;
  33. static void initJuceDevices()
  34. {
  35. static AudioDeviceManager manager;
  36. if (gJuceDeviceTypes.size() == 0)
  37. manager.createAudioDeviceTypes(gJuceDeviceTypes);
  38. }
  39. // -------------------------------------------------------------------------------------------------------------------
  40. // Juce Engine
  41. class CarlaEngineJuce : public CarlaEngine/*,
  42. public AudioIODeviceCallback*/
  43. {
  44. public:
  45. CarlaEngineJuce()
  46. : CarlaEngine()
  47. {
  48. carla_debug("CarlaEngineJuce::CarlaEngineJuce()");
  49. }
  50. ~CarlaEngineJuce() override
  51. {
  52. if (gRetNames != nullptr)
  53. {
  54. delete[] gRetNames;
  55. gRetNames = nullptr;
  56. }
  57. }
  58. // -------------------------------------
  59. bool init(const char* const clientName) override
  60. {
  61. carla_debug("CarlaEngineJuce::init(\"%s\")", clientName);
  62. CarlaEngine::init(clientName);
  63. return true;
  64. }
  65. bool close() override
  66. {
  67. carla_debug("CarlaEngineJuce::close()");
  68. return CarlaEngine::close();
  69. }
  70. bool isRunning() const noexcept override
  71. {
  72. return false;
  73. }
  74. bool isOffline() const noexcept override
  75. {
  76. return false;
  77. }
  78. EngineType getType() const noexcept override
  79. {
  80. return kEngineTypeJuce;
  81. }
  82. const char* getCurrentDriverName() const noexcept override
  83. {
  84. return nullptr;
  85. }
  86. // -------------------------------------------------------------------
  87. protected:
  88. // void audioDeviceIOCallback (const float** inputChannelData,
  89. // int numInputChannels,
  90. // float** outputChannelData,
  91. // int numOutputChannels,
  92. // int numSamples)
  93. // {
  94. // }
  95. //
  96. // void audioDeviceAboutToStart (juce::AudioIODevice* device)
  97. // {
  98. // }
  99. //
  100. // void audioDeviceStopped()
  101. // {
  102. // }
  103. //
  104. // void audioDeviceError (const juce::String& errorMessage)
  105. // {
  106. // }
  107. // -------------------------------------
  108. private:
  109. //juce::AudioIODeviceType* fDeviceType;
  110. //JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  111. };
  112. // -----------------------------------------
  113. CarlaEngine* CarlaEngine::newJuce(const AudioApi /*api*/)
  114. {
  115. return new CarlaEngineJuce();
  116. }
  117. unsigned int CarlaEngine::getJuceApiCount()
  118. {
  119. initJuceDevices();
  120. return static_cast<unsigned int>(gJuceDeviceTypes.size());
  121. }
  122. const char* CarlaEngine::getJuceApiName(const unsigned int index)
  123. {
  124. initJuceDevices();
  125. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  126. return nullptr;
  127. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  128. if (deviceType == nullptr)
  129. return nullptr;
  130. return deviceType->getTypeName().toRawUTF8();
  131. }
  132. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int index)
  133. {
  134. initJuceDevices();
  135. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  136. return nullptr;
  137. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  138. if (deviceType == nullptr)
  139. return nullptr;
  140. deviceType->scanForDevices();
  141. StringArray deviceNames(deviceType->getDeviceNames());
  142. const int deviceNameCount(deviceNames.size());
  143. if (deviceNameCount <= 0)
  144. return nullptr;
  145. if (gRetNames != nullptr)
  146. {
  147. for (int i=0; gRetNames[i] != nullptr; ++i)
  148. delete[] gRetNames[i];
  149. delete[] gRetNames;
  150. }
  151. gRetNames = new const char*[deviceNameCount+1];
  152. for (int i=0; i < deviceNameCount; ++i)
  153. gRetNames[i] = carla_strdup(deviceNames[i].toRawUTF8());
  154. gRetNames[deviceNameCount] = nullptr;
  155. return gRetNames;
  156. }
  157. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int index, const char* const deviceName)
  158. {
  159. initJuceDevices();
  160. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  161. return nullptr;
  162. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  163. if (deviceType == nullptr)
  164. return nullptr;
  165. deviceType->scanForDevices();
  166. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  167. if (device == nullptr)
  168. return nullptr;
  169. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  170. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  171. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  172. // reset
  173. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  174. // cleanup
  175. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  176. {
  177. delete[] devInfo.bufferSizes;
  178. devInfo.bufferSizes = nullptr;
  179. }
  180. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  181. {
  182. delete[] devInfo.sampleRates;
  183. devInfo.sampleRates = nullptr;
  184. }
  185. if (device->hasControlPanel())
  186. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  187. if (int bufferSizesCount = device->getNumBufferSizesAvailable())
  188. {
  189. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  190. for (int i=0; i < bufferSizesCount; ++i)
  191. bufferSizes[i] = device->getBufferSizeSamples(i);
  192. bufferSizes[bufferSizesCount] = 0;
  193. devInfo.bufferSizes = bufferSizes;
  194. }
  195. else
  196. {
  197. devInfo.bufferSizes = dummyBufferSizes;
  198. }
  199. if (int sampleRatesCount = device->getNumSampleRates())
  200. {
  201. double* const sampleRates(new double[sampleRatesCount+1]);
  202. for (int i=0; i < sampleRatesCount; ++i)
  203. sampleRates[i] = device->getSampleRate(i);
  204. sampleRates[sampleRatesCount] = 0.0;
  205. devInfo.sampleRates = sampleRates;
  206. }
  207. else
  208. {
  209. devInfo.sampleRates = dummySampleRates;
  210. }
  211. return &devInfo;
  212. }
  213. // -----------------------------------------
  214. CARLA_BACKEND_END_NAMESPACE