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.

307 lines
8.8KB

  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 juce;
  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. const int iframes(static_cast<int>(frames));
  104. float* const out1(outBuffer[0]);
  105. float* const out2(outBuffer[1]);
  106. if (fLength == 0 || ! fDoProcess)
  107. {
  108. //carla_stderr("P: no process");
  109. FloatVectorOperations::clear(out1, iframes);
  110. FloatVectorOperations::clear(out2, iframes);
  111. return;
  112. }
  113. const int64_t nextReadPos(fLoopMode ? (static_cast<int64_t>(timePos->frame) % fLength) : static_cast<int64_t>(timePos->frame));
  114. // not playing
  115. if (! timePos->playing)
  116. {
  117. //carla_stderr("P: not playing");
  118. FloatVectorOperations::clear(out1, iframes);
  119. FloatVectorOperations::clear(out2, iframes);
  120. const CarlaMutexLocker cml(fReaderMutex);
  121. if (fReaderSource != nullptr)
  122. fReaderSource->setNextReadPosition(nextReadPos);
  123. return;
  124. }
  125. const CarlaMutexLocker cml(fReaderMutex);
  126. if (fReaderSource != nullptr)
  127. fReaderSource->setNextReadPosition(nextReadPos);
  128. if (fReader == nullptr)
  129. return;
  130. fReader->read(&fReaderBuffer, 0, iframes, nextReadPos, true, true);
  131. FloatVectorOperations::copy(out1, fReaderBuffer.getReadPointer(0), iframes);
  132. FloatVectorOperations::copy(out2, fReaderBuffer.getReadPointer(1), iframes);
  133. }
  134. // -------------------------------------------------------------------
  135. // Plugin UI calls
  136. void uiShow(const bool show) override
  137. {
  138. if (! show)
  139. return;
  140. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  141. uiCustomDataChanged("file", filename);
  142. uiClosed();
  143. }
  144. // -------------------------------------------------------------------
  145. // Plugin dispatcher calls
  146. void bufferSizeChanged(const uint32_t bufferSize) override
  147. {
  148. fReaderBuffer.setSize(2, static_cast<int>(bufferSize));
  149. }
  150. private:
  151. bool fLoopMode;
  152. bool fDoProcess;
  153. int64_t fLength;
  154. //TimeSliceThread fThread;
  155. AudioSampleBuffer fReaderBuffer;
  156. CarlaMutex fReaderMutex;
  157. ScopedPointer<AudioFormatReader> fReader;
  158. ScopedPointer<AudioFormatReaderSource> fReaderSource;
  159. void _loadAudioFile(const char* const filename)
  160. {
  161. carla_stdout("AudioFilePlugin::loadFilename(\"%s\")", filename);
  162. fDoProcess = false;
  163. fLength = 0;
  164. //fThread.stopThread(-1);
  165. {
  166. fReaderMutex.lock();
  167. AudioFormatReader* const reader(fReader.release());
  168. AudioFormatReaderSource* const readerSource(fReaderSource.release());
  169. fReaderMutex.unlock();
  170. delete readerSource;
  171. delete reader;
  172. }
  173. const String jfilename = String(CharPointer_UTF8(filename));
  174. File file(jfilename);
  175. if (! file.existsAsFile())
  176. return;
  177. AudioFormatManager& afm(getAudioFormatManagerInstance());
  178. AudioFormat* const format(afm.findFormatForFileExtension(file.getFileExtension()));
  179. CARLA_SAFE_ASSERT_RETURN(format != nullptr,);
  180. if (MemoryMappedAudioFormatReader* const memReader = format->createMemoryMappedReader(file))
  181. {
  182. memReader->mapEntireFile();
  183. fReader = memReader;
  184. carla_stdout("Using memory mapped read file");
  185. }
  186. else
  187. {
  188. AudioFormatReader* const reader(afm.createReaderFor(file));
  189. CARLA_SAFE_ASSERT_RETURN(reader != nullptr,);
  190. // this code can be used for very large files
  191. //fThread.startThread();
  192. //BufferingAudioReader* const bufferingReader(new BufferingAudioReader(reader, fThread, getSampleRate()*2));
  193. //bufferingReader->setReadTimeout(50);
  194. AudioFormatReaderSource* const readerSource(new AudioFormatReaderSource(/*bufferingReader*/reader, false));
  195. readerSource->setLooping(fLoopMode);
  196. fReaderSource = readerSource;
  197. fReader = reader;
  198. carla_stdout("Using regular read file");
  199. }
  200. fLength = fReader->lengthInSamples;
  201. fDoProcess = true;
  202. }
  203. PluginClassEND(AudioFilePlugin)
  204. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  205. };
  206. // -----------------------------------------------------------------------
  207. static const NativePluginDescriptor audiofileDesc = {
  208. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  209. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_HAS_UI
  210. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE),
  211. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  212. /* audioIns */ 0,
  213. /* audioOuts */ 2,
  214. /* midiIns */ 0,
  215. /* midiOuts */ 0,
  216. /* paramIns */ 1,
  217. /* paramOuts */ 0,
  218. /* name */ "Audio File",
  219. /* label */ "audiofile",
  220. /* maker */ "falkTX",
  221. /* copyright */ "GNU GPL v2+",
  222. PluginDescriptorFILL(AudioFilePlugin)
  223. };
  224. // -----------------------------------------------------------------------
  225. #endif
  226. CARLA_EXPORT
  227. void carla_register_native_plugin_audiofile();
  228. CARLA_EXPORT
  229. void carla_register_native_plugin_audiofile()
  230. {
  231. //carla_register_native_plugin(&audiofileDesc);
  232. }
  233. // -----------------------------------------------------------------------