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.

275 lines
7.1KB

  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 PluginClass,
  22. public AbstractAudioPlayer
  23. {
  24. public:
  25. AudioFilePlugin(const PluginHostDescriptor* const host)
  26. : PluginClass(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 Parameter* getParameterInfo(const uint32_t index) const override
  53. {
  54. if (index != 0)
  55. return nullptr;
  56. static Parameter param;
  57. param.hints = PARAMETER_IS_ENABLED ":" PARAMETER_IS_BOOLEAN;
  58. param.name = "Loop Mode";
  59. param.unit = nullptr;
  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. void setParameterValue(const uint32_t index, const float value) override
  77. {
  78. if (index != 0)
  79. return;
  80. bool b = (value > 0.5f);
  81. if (b == fLoopMode)
  82. return;
  83. fLoopMode = b;
  84. fThread.setNeedsRead();
  85. }
  86. // -------------------------------------------------------------------
  87. // Plugin state calls
  88. // void setCustomData(const char* const key, const char* const value) override
  89. // {
  90. // if (std::strcmp(key, "file") != 0)
  91. // return;
  92. //
  93. // loadFilename(value);
  94. // }
  95. // -------------------------------------------------------------------
  96. // Plugin process calls
  97. void process(float**, float** const outBuffer, const uint32_t frames, const Event* const, const uint32_t) override
  98. {
  99. const TimeInfo* const timePos(getTimeInfo());
  100. float* out1 = outBuffer[0];
  101. float* out2 = outBuffer[1];
  102. if (! fDoProcess)
  103. {
  104. //carla_stderr("P: no process");
  105. fLastFrame = timePos->frame;
  106. carla_zeroFloat(out1, frames);
  107. carla_zeroFloat(out2, frames);
  108. return;
  109. }
  110. // not playing
  111. if (! timePos->playing)
  112. {
  113. //carla_stderr("P: not playing");
  114. fLastFrame = timePos->frame;
  115. if (timePos->frame == 0 && fLastFrame > 0)
  116. fThread.setNeedsRead();
  117. carla_zeroFloat(out1, frames);
  118. carla_zeroFloat(out2, frames);
  119. return;
  120. }
  121. fThread.tryPutData(fPool);
  122. // out of reach
  123. if (timePos->frame + frames < fPool.startFrame || timePos->frame >= fMaxFrame) /*&& ! loopMode)*/
  124. {
  125. //carla_stderr("P: out of reach");
  126. fLastFrame = timePos->frame;
  127. if (timePos->frame + frames < fPool.startFrame)
  128. fThread.setNeedsRead();
  129. carla_zeroFloat(out1, frames);
  130. carla_zeroFloat(out2, frames);
  131. return;
  132. }
  133. int64_t poolFrame = (int64_t)timePos->frame - fPool.startFrame;
  134. int64_t poolSize = fPool.size;
  135. for (uint32_t i=0; i < frames; ++i, ++poolFrame)
  136. {
  137. if (poolFrame >= 0 && poolFrame < poolSize)
  138. {
  139. out1[i] = fPool.buffer[0][poolFrame];
  140. out2[i] = fPool.buffer[1][poolFrame];
  141. // reset
  142. fPool.buffer[0][poolFrame] = 0.0f;
  143. fPool.buffer[1][poolFrame] = 0.0f;
  144. }
  145. else
  146. {
  147. out1[i] = 0.0f;
  148. out2[i] = 0.0f;
  149. }
  150. }
  151. fLastFrame = timePos->frame;
  152. }
  153. // -------------------------------------------------------------------
  154. // Plugin UI calls
  155. // void uiShow(const bool show) override
  156. // {
  157. // if (! show)
  158. // return;
  159. //
  160. // if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  161. // uiCustomDataChanged("file", filename);
  162. //
  163. // uiClosed();
  164. // }
  165. private:
  166. bool fLoopMode;
  167. bool fDoProcess;
  168. uint32_t fLastFrame;
  169. uint32_t fMaxFrame;
  170. AudioFilePool fPool;
  171. AudioFileThread fThread;
  172. void loadFilename(const char* const filename)
  173. {
  174. CARLA_ASSERT(filename != nullptr);
  175. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  176. fThread.stopNow();
  177. if (filename == nullptr || *filename == '\0')
  178. {
  179. fDoProcess = false;
  180. fMaxFrame = 0;
  181. return;
  182. }
  183. if (fThread.loadFilename(filename))
  184. {
  185. fThread.startNow();
  186. fMaxFrame = fThread.getMaxFrame();
  187. fDoProcess = true;
  188. }
  189. else
  190. {
  191. fDoProcess = false;
  192. fMaxFrame = 0;
  193. }
  194. }
  195. PluginClassEND(AudioFilePlugin)
  196. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  197. };
  198. // -----------------------------------------------------------------------
  199. static const PluginDescriptor audiofileDesc = {
  200. /* api */ CARLA_NATIVE_API_VERSION,
  201. /* categories */ PLUGIN_CATEGORY_UTILITY,
  202. /* features */ PLUGIN_FEATURE_RTSAFE,
  203. /* supports */ nullptr,
  204. /* metadata */ nullptr,
  205. /* audioIns */ 0,
  206. /* audioOuts */ 2,
  207. /* midiIns */ 0,
  208. /* midiOuts */ 0,
  209. /* paramIns */ 0, // TODO - loopMode
  210. /* paramOuts */ 0,
  211. /* author */ "falkTX",
  212. /* name */ "Audio File",
  213. /* label */ "audiofile",
  214. /* copyright */ "GNU GPL v2+",
  215. /* version */ 0x1000,
  216. PluginDescriptorFILL(AudioFilePlugin)
  217. };
  218. // -----------------------------------------------------------------------
  219. CARLA_EXPORT
  220. void carla_register_native_plugin_audiofile()
  221. {
  222. carla_register_native_plugin(&audiofileDesc);
  223. }
  224. // -----------------------------------------------------------------------