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.0KB

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