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.

306 lines
8.7KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2017 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 "CarlaNative.hpp"
  18. #if 0
  19. #include "CarlaMutex.hpp"
  20. #include "CarlaString.hpp"
  21. using namespace juce2;
  22. // -----------------------------------------------------------------------
  23. class AudioFilePlugin : public NativePluginClass
  24. {
  25. public:
  26. AudioFilePlugin(const NativeHostDescriptor* const host)
  27. : NativePluginClass(host),
  28. fLoopMode(false),
  29. fDoProcess(false),
  30. fLength(0),
  31. //fThread("AudioFilePluginThread"),
  32. fReaderBuffer(),
  33. fReaderMutex(),
  34. fReader(),
  35. fReaderSource()
  36. {
  37. fReaderBuffer.setSize(2, static_cast<int>(getBufferSize()));
  38. }
  39. ~AudioFilePlugin() override
  40. {
  41. //fThread.stopThread(-1);
  42. fReader = nullptr;
  43. fReaderSource = nullptr;
  44. }
  45. protected:
  46. // -------------------------------------------------------------------
  47. // Plugin parameter calls
  48. uint32_t getParameterCount() const override
  49. {
  50. return 1;
  51. }
  52. const NativeParameter* getParameterInfo(const uint32_t index) const override
  53. {
  54. if (index != 0)
  55. return nullptr;
  56. static NativeParameter param;
  57. param.name = "Loop Mode";
  58. param.unit = nullptr;
  59. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_BOOLEAN);
  60. param.ranges.def = 1.0f;
  61. param.ranges.min = 0.0f;
  62. param.ranges.max = 1.0f;
  63. param.ranges.step = 1.0f;
  64. param.ranges.stepSmall = 1.0f;
  65. param.ranges.stepLarge = 1.0f;
  66. param.scalePointCount = 0;
  67. param.scalePoints = nullptr;
  68. return &param;
  69. }
  70. float getParameterValue(const uint32_t index) const override
  71. {
  72. if (index != 0)
  73. return 0.0f;
  74. return fLoopMode ? 1.0f : 0.0f;
  75. }
  76. // -------------------------------------------------------------------
  77. // Plugin state calls
  78. void setParameterValue(const uint32_t index, const float value) override
  79. {
  80. if (index != 0)
  81. return;
  82. const bool loopMode(value > 0.5f);
  83. if (fLoopMode == loopMode)
  84. return;
  85. fLoopMode = loopMode;
  86. const CarlaMutexLocker cml(fReaderMutex);
  87. if (fReaderSource != nullptr)
  88. fReaderSource->setLooping(loopMode);
  89. }
  90. void setCustomData(const char* const key, const char* const value) override
  91. {
  92. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  93. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  94. if (std::strcmp(key, "file") != 0)
  95. return;
  96. _loadAudioFile(value);
  97. }
  98. // -------------------------------------------------------------------
  99. // Plugin process calls
  100. void process(float**, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  101. {
  102. const NativeTimeInfo* const timePos(getTimeInfo());
  103. float* const out1(outBuffer[0]);
  104. float* const out2(outBuffer[1]);
  105. if (fLength == 0 || ! fDoProcess)
  106. {
  107. //carla_stderr("P: no process");
  108. carla_zeroFloats(out1, iframes);
  109. carla_zeroFloats(out2, iframes);
  110. return;
  111. }
  112. const int64_t nextReadPos(fLoopMode ? (static_cast<int64_t>(timePos->frame) % fLength) : static_cast<int64_t>(timePos->frame));
  113. // not playing
  114. if (! timePos->playing)
  115. {
  116. //carla_stderr("P: not playing");
  117. carla_zeroFloats(out1, iframes);
  118. carla_zeroFloats(out2, iframes);
  119. const CarlaMutexLocker cml(fReaderMutex);
  120. if (fReaderSource != nullptr)
  121. fReaderSource->setNextReadPosition(nextReadPos);
  122. return;
  123. }
  124. const CarlaMutexLocker cml(fReaderMutex);
  125. if (fReaderSource != nullptr)
  126. fReaderSource->setNextReadPosition(nextReadPos);
  127. if (fReader == nullptr)
  128. return;
  129. fReader->read(&fReaderBuffer, 0, iframes, nextReadPos, true, true);
  130. carla_copyFloats(out1, fReaderBuffer.getReadPointer(0), frames);
  131. carla_copyFloats(out2, fReaderBuffer.getReadPointer(1), frames);
  132. }
  133. // -------------------------------------------------------------------
  134. // Plugin UI calls
  135. void uiShow(const bool show) override
  136. {
  137. if (! show)
  138. return;
  139. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  140. uiCustomDataChanged("file", filename);
  141. uiClosed();
  142. }
  143. // -------------------------------------------------------------------
  144. // Plugin dispatcher calls
  145. void bufferSizeChanged(const uint32_t bufferSize) override
  146. {
  147. fReaderBuffer.setSize(2, static_cast<int>(bufferSize));
  148. }
  149. private:
  150. bool fLoopMode;
  151. bool fDoProcess;
  152. int64_t fLength;
  153. //TimeSliceThread fThread;
  154. AudioSampleBuffer fReaderBuffer;
  155. CarlaMutex fReaderMutex;
  156. ScopedPointer<AudioFormatReader> fReader;
  157. ScopedPointer<AudioFormatReaderSource> fReaderSource;
  158. void _loadAudioFile(const char* const filename)
  159. {
  160. carla_stdout("AudioFilePlugin::loadFilename(\"%s\")", filename);
  161. fDoProcess = false;
  162. fLength = 0;
  163. //fThread.stopThread(-1);
  164. {
  165. fReaderMutex.lock();
  166. AudioFormatReader* const reader(fReader.release());
  167. AudioFormatReaderSource* const readerSource(fReaderSource.release());
  168. fReaderMutex.unlock();
  169. delete readerSource;
  170. delete reader;
  171. }
  172. const String jfilename = String(CharPointer_UTF8(filename));
  173. File file(jfilename);
  174. if (! file.existsAsFile())
  175. return;
  176. AudioFormatManager& afm(getAudioFormatManagerInstance());
  177. AudioFormat* const format(afm.findFormatForFileExtension(file.getFileExtension()));
  178. CARLA_SAFE_ASSERT_RETURN(format != nullptr,);
  179. if (MemoryMappedAudioFormatReader* const memReader = format->createMemoryMappedReader(file))
  180. {
  181. memReader->mapEntireFile();
  182. fReader = memReader;
  183. carla_stdout("Using memory mapped read file");
  184. }
  185. else
  186. {
  187. AudioFormatReader* const reader(afm.createReaderFor(file));
  188. CARLA_SAFE_ASSERT_RETURN(reader != nullptr,);
  189. // this code can be used for very large files
  190. //fThread.startThread();
  191. //BufferingAudioReader* const bufferingReader(new BufferingAudioReader(reader, fThread, getSampleRate()*2));
  192. //bufferingReader->setReadTimeout(50);
  193. AudioFormatReaderSource* const readerSource(new AudioFormatReaderSource(/*bufferingReader*/reader, false));
  194. readerSource->setLooping(fLoopMode);
  195. fReaderSource = readerSource;
  196. fReader = reader;
  197. carla_stdout("Using regular read file");
  198. }
  199. fLength = fReader->lengthInSamples;
  200. fDoProcess = true;
  201. }
  202. PluginClassEND(AudioFilePlugin)
  203. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  204. };
  205. // -----------------------------------------------------------------------
  206. static const NativePluginDescriptor audiofileDesc = {
  207. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  208. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_HAS_UI
  209. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE),
  210. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  211. /* audioIns */ 0,
  212. /* audioOuts */ 2,
  213. /* midiIns */ 0,
  214. /* midiOuts */ 0,
  215. /* paramIns */ 1,
  216. /* paramOuts */ 0,
  217. /* name */ "Audio File",
  218. /* label */ "audiofile",
  219. /* maker */ "falkTX",
  220. /* copyright */ "GNU GPL v2+",
  221. PluginDescriptorFILL(AudioFilePlugin)
  222. };
  223. // -----------------------------------------------------------------------
  224. #endif
  225. CARLA_EXPORT
  226. void carla_register_native_plugin_audiofile();
  227. CARLA_EXPORT
  228. void carla_register_native_plugin_audiofile()
  229. {
  230. //carla_register_native_plugin(&audiofileDesc);
  231. }
  232. // -----------------------------------------------------------------------