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.

audio-file.cpp 9.1KB

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