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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Carla Native Plugins
  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 doc/GPL.txt file.
  16. */
  17. #include "CarlaNative.hpp"
  18. #include "CarlaString.hpp"
  19. #include "audio-base.hpp"
  20. #define PROGRAM_COUNT 16
  21. class AudioFilePlugin : public NativePluginClass,
  22. public AbstractAudioPlayer
  23. {
  24. public:
  25. AudioFilePlugin(const NativeHostDescriptor* const host)
  26. : NativePluginClass(host),
  27. AbstractAudioPlayer(),
  28. fLoopMode(false),
  29. fDoProcess(false),
  30. fLastFrame(0),
  31. fMaxFrame(0),
  32. fThread(this, getSampleRate())
  33. {
  34. fPool.create(getSampleRate());
  35. }
  36. ~AudioFilePlugin() override
  37. {
  38. fPool.destroy();
  39. fThread.stopNow();
  40. }
  41. uint32_t getLastFrame() const override
  42. {
  43. return fLastFrame;
  44. }
  45. protected:
  46. // -------------------------------------------------------------------
  47. // Plugin parameter calls
  48. uint32_t getParameterCount() const override
  49. {
  50. return 0; // TODO - loopMode
  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>(PARAMETER_IS_ENABLED|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. bool b = (value > 0.5f);
  83. if (b == fLoopMode)
  84. return;
  85. fLoopMode = b;
  86. fThread.setNeedsRead();
  87. }
  88. void setCustomData(const char* const key, const char* const value) override
  89. {
  90. if (std::strcmp(key, "file") != 0)
  91. return;
  92. loadFilename(value);
  93. }
  94. // -------------------------------------------------------------------
  95. // Plugin process calls
  96. void process(float**, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  97. {
  98. const NativeTimeInfo* const timePos(getTimeInfo());
  99. float* out1 = outBuffer[0];
  100. float* out2 = outBuffer[1];
  101. if (! fDoProcess)
  102. {
  103. //carla_stderr("P: no process");
  104. fLastFrame = timePos->frame;
  105. #ifdef USE_JUCE
  106. FloatVectorOperations::clear(out1, frames);
  107. FloatVectorOperations::clear(out2, frames);
  108. #else
  109. carla_zeroFloat(out1, frames);
  110. carla_zeroFloat(out2, frames);
  111. #endif
  112. return;
  113. }
  114. // not playing
  115. if (! timePos->playing)
  116. {
  117. //carla_stderr("P: not playing");
  118. fLastFrame = timePos->frame;
  119. if (timePos->frame == 0 && fLastFrame > 0)
  120. fThread.setNeedsRead();
  121. #ifdef USE_JUCE
  122. FloatVectorOperations::clear(out1, frames);
  123. FloatVectorOperations::clear(out2, frames);
  124. #else
  125. carla_zeroFloat(out1, frames);
  126. carla_zeroFloat(out2, frames);
  127. #endif
  128. return;
  129. }
  130. fThread.tryPutData(fPool);
  131. // out of reach
  132. if (timePos->frame + frames < fPool.startFrame || timePos->frame >= fMaxFrame) /*&& ! loopMode)*/
  133. {
  134. //carla_stderr("P: out of reach");
  135. fLastFrame = timePos->frame;
  136. if (timePos->frame + frames < fPool.startFrame)
  137. fThread.setNeedsRead();
  138. #ifdef USE_JUCE
  139. FloatVectorOperations::clear(out1, frames);
  140. FloatVectorOperations::clear(out2, frames);
  141. #else
  142. carla_zeroFloat(out1, frames);
  143. carla_zeroFloat(out2, frames);
  144. #endif
  145. return;
  146. }
  147. int64_t poolFrame = (int64_t)timePos->frame - fPool.startFrame;
  148. int64_t poolSize = fPool.size;
  149. for (uint32_t i=0; i < frames; ++i, ++poolFrame)
  150. {
  151. if (poolFrame >= 0 && poolFrame < poolSize)
  152. {
  153. out1[i] = fPool.buffer[0][poolFrame];
  154. out2[i] = fPool.buffer[1][poolFrame];
  155. // reset
  156. fPool.buffer[0][poolFrame] = 0.0f;
  157. fPool.buffer[1][poolFrame] = 0.0f;
  158. }
  159. else
  160. {
  161. out1[i] = 0.0f;
  162. out2[i] = 0.0f;
  163. }
  164. }
  165. fLastFrame = timePos->frame;
  166. }
  167. // -------------------------------------------------------------------
  168. // Plugin UI calls
  169. void uiShow(const bool show) override
  170. {
  171. if (! show)
  172. return;
  173. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  174. uiCustomDataChanged("file", filename);
  175. uiClosed();
  176. }
  177. private:
  178. bool fLoopMode;
  179. bool fDoProcess;
  180. uint32_t fLastFrame;
  181. uint32_t fMaxFrame;
  182. AudioFilePool fPool;
  183. AudioFileThread fThread;
  184. void loadFilename(const char* const filename)
  185. {
  186. CARLA_ASSERT(filename != nullptr);
  187. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  188. fThread.stopNow();
  189. if (filename == nullptr || *filename == '\0')
  190. {
  191. fDoProcess = false;
  192. fMaxFrame = 0;
  193. return;
  194. }
  195. if (fThread.loadFilename(filename))
  196. {
  197. fThread.startNow();
  198. fMaxFrame = fThread.getMaxFrame();
  199. fDoProcess = true;
  200. }
  201. else
  202. {
  203. fDoProcess = false;
  204. fMaxFrame = 0;
  205. }
  206. }
  207. PluginClassEND(AudioFilePlugin)
  208. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  209. };
  210. // -----------------------------------------------------------------------
  211. static const NativePluginDescriptor audiofileDesc = {
  212. /* category */ PLUGIN_CATEGORY_UTILITY,
  213. /* hints */ static_cast<NativePluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_UI|PLUGIN_NEEDS_UI_OPEN_SAVE),
  214. /* supports */ static_cast<NativePluginSupports>(0x0),
  215. /* audioIns */ 0,
  216. /* audioOuts */ 2,
  217. /* midiIns */ 0,
  218. /* midiOuts */ 0,
  219. /* paramIns */ 0, // TODO - loopMode
  220. /* paramOuts */ 0,
  221. /* name */ "Audio File",
  222. /* label */ "audiofile",
  223. /* maker */ "falkTX",
  224. /* copyright */ "GNU GPL v2+",
  225. PluginDescriptorFILL(AudioFilePlugin)
  226. };
  227. // -----------------------------------------------------------------------
  228. CARLA_EXPORT
  229. void carla_register_native_plugin_audiofile()
  230. {
  231. carla_register_native_plugin(&audiofileDesc);
  232. }
  233. // -----------------------------------------------------------------------