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.

288 lines
8.0KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2019 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 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(true),
  29. fDoProcess(false),
  30. fLastFrame(0),
  31. fMaxFrame(0),
  32. fPool(),
  33. fThread(this, static_cast<uint32_t>(getSampleRate()))
  34. {
  35. fPool.create(static_cast<uint32_t>(getSampleRate()));
  36. }
  37. ~AudioFilePlugin() override
  38. {
  39. fPool.destroy();
  40. fThread.stopNow();
  41. }
  42. uint64_t getLastFrame() const override
  43. {
  44. return fLastFrame;
  45. }
  46. protected:
  47. // -------------------------------------------------------------------
  48. // Plugin parameter calls
  49. uint32_t getParameterCount() const override
  50. {
  51. return 1;
  52. }
  53. const NativeParameter* getParameterInfo(const uint32_t index) const override
  54. {
  55. if (index != 0)
  56. return nullptr;
  57. static NativeParameter param;
  58. param.name = "Loop Mode";
  59. param.unit = nullptr;
  60. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMABLE|NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_BOOLEAN);
  61. param.ranges.def = 1.0f;
  62. param.ranges.min = 0.0f;
  63. param.ranges.max = 1.0f;
  64. param.ranges.step = 1.0f;
  65. param.ranges.stepSmall = 1.0f;
  66. param.ranges.stepLarge = 1.0f;
  67. param.scalePointCount = 0;
  68. param.scalePoints = nullptr;
  69. return &param;
  70. }
  71. float getParameterValue(const uint32_t index) const override
  72. {
  73. if (index != 0)
  74. return 0.0f;
  75. return fLoopMode ? 1.0f : 0.0f;
  76. }
  77. // -------------------------------------------------------------------
  78. // Plugin state calls
  79. void setParameterValue(const uint32_t index, const float value) override
  80. {
  81. if (index != 0)
  82. return;
  83. bool b = (value > 0.5f);
  84. if (b == fLoopMode)
  85. return;
  86. fLoopMode = b;
  87. fThread.setLoopingMode(b);
  88. fThread.setNeedsRead();
  89. }
  90. void setCustomData(const char* const key, const char* const value) override
  91. {
  92. if (std::strcmp(key, "file") != 0)
  93. return;
  94. loadFilename(value);
  95. }
  96. // -------------------------------------------------------------------
  97. // Plugin process calls
  98. void process(const float**, float** const outBuffer, const uint32_t frames,
  99. const NativeMidiEvent*, uint32_t) override
  100. {
  101. const NativeTimeInfo* const timePos(getTimeInfo());
  102. float* out1 = outBuffer[0];
  103. float* out2 = outBuffer[1];
  104. if (! fDoProcess)
  105. {
  106. //carla_stderr("P: no process");
  107. fLastFrame = timePos->frame;
  108. carla_zeroFloats(out1, frames);
  109. carla_zeroFloats(out2, frames);
  110. return;
  111. }
  112. // not playing
  113. if (! timePos->playing)
  114. {
  115. //carla_stderr("P: not playing");
  116. fLastFrame = timePos->frame;
  117. if (timePos->frame == 0 && fLastFrame > 0)
  118. fThread.setNeedsRead();
  119. carla_zeroFloats(out1, frames);
  120. carla_zeroFloats(out2, frames);
  121. return;
  122. }
  123. // out of reach
  124. if (timePos->frame + frames < fPool.startFrame || (timePos->frame >= fMaxFrame && !fLoopMode))
  125. {
  126. if (fLoopMode) {
  127. carla_stderr("P: out of reach");
  128. }
  129. fLastFrame = timePos->frame;
  130. if (timePos->frame + frames < fPool.startFrame)
  131. fThread.setNeedsRead();
  132. carla_zeroFloats(out1, frames);
  133. carla_zeroFloats(out2, frames);
  134. return;
  135. }
  136. const uint32_t poolSize = fPool.size;
  137. float* const bufferL = fPool.buffer[0];
  138. float* const bufferR = fPool.buffer[1];
  139. int64_t poolFrame = static_cast<int64_t>(timePos->frame - fPool.startFrame);
  140. if (poolFrame >= 0 && poolFrame < poolSize && fThread.tryPutData(fPool, static_cast<uint32_t>(poolFrame), frames))
  141. {
  142. const uint32_t framesToCopy = std::min(frames, static_cast<uint32_t>(poolSize - poolFrame));
  143. carla_copyFloats(out1, bufferL + poolFrame, framesToCopy);
  144. carla_copyFloats(out2, bufferR + poolFrame, framesToCopy);
  145. if (const uint32_t remainingFrames = frames - framesToCopy)
  146. {
  147. carla_zeroFloats(out1 + framesToCopy, remainingFrames);
  148. carla_zeroFloats(out2 + framesToCopy, remainingFrames);
  149. }
  150. carla_zeroFloats(bufferL + poolFrame, framesToCopy);
  151. carla_zeroFloats(bufferR + poolFrame, framesToCopy);
  152. }
  153. else
  154. {
  155. carla_zeroFloats(out1, frames);
  156. carla_zeroFloats(out2, frames);
  157. }
  158. fLastFrame = timePos->frame;
  159. }
  160. // -------------------------------------------------------------------
  161. // Plugin UI calls
  162. void uiShow(const bool show) override
  163. {
  164. if (! show)
  165. return;
  166. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  167. uiCustomDataChanged("file", filename);
  168. uiClosed();
  169. }
  170. private:
  171. bool fLoopMode;
  172. bool fDoProcess;
  173. uint64_t fLastFrame;
  174. uint32_t fMaxFrame;
  175. AudioFilePool fPool;
  176. AudioFileThread fThread;
  177. void loadFilename(const char* const filename)
  178. {
  179. CARLA_ASSERT(filename != nullptr);
  180. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  181. fThread.stopNow();
  182. if (filename == nullptr || *filename == '\0')
  183. {
  184. fDoProcess = false;
  185. fMaxFrame = 0;
  186. return;
  187. }
  188. if (fThread.loadFilename(filename))
  189. {
  190. fThread.startNow();
  191. fMaxFrame = fThread.getMaxFrame();
  192. fDoProcess = true;
  193. }
  194. else
  195. {
  196. fDoProcess = false;
  197. fMaxFrame = 0;
  198. }
  199. }
  200. PluginClassEND(AudioFilePlugin)
  201. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  202. };
  203. // -----------------------------------------------------------------------
  204. static const NativePluginDescriptor audiofileDesc = {
  205. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  206. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  207. |NATIVE_PLUGIN_HAS_UI
  208. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  209. |NATIVE_PLUGIN_USES_TIME),
  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. CARLA_EXPORT
  225. void carla_register_native_plugin_audiofile();
  226. CARLA_EXPORT
  227. void carla_register_native_plugin_audiofile()
  228. {
  229. carla_register_native_plugin(&audiofileDesc);
  230. }
  231. // -----------------------------------------------------------------------