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

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2018 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(float**, float** const outBuffer, const uint32_t frames, const NativeMidiEvent*, uint32_t) override
  99. {
  100. const NativeTimeInfo* const timePos(getTimeInfo());
  101. float* out1 = outBuffer[0];
  102. float* out2 = outBuffer[1];
  103. if (! fDoProcess)
  104. {
  105. //carla_stderr("P: no process");
  106. fLastFrame = timePos->frame;
  107. carla_zeroFloats(out1, frames);
  108. carla_zeroFloats(out2, frames);
  109. return;
  110. }
  111. // not playing
  112. if (! timePos->playing)
  113. {
  114. //carla_stderr("P: not playing");
  115. fLastFrame = timePos->frame;
  116. if (timePos->frame == 0 && fLastFrame > 0)
  117. fThread.setNeedsRead();
  118. carla_zeroFloats(out1, frames);
  119. carla_zeroFloats(out2, frames);
  120. return;
  121. }
  122. fThread.tryPutData(fPool);
  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. int64_t poolFrame = static_cast<uint32_t>(timePos->frame - fPool.startFrame);
  138. float* const bufferL = fPool.buffer[0];
  139. float* const bufferR = fPool.buffer[1];
  140. for (uint32_t i=0; i < frames; ++i, ++poolFrame)
  141. {
  142. if (poolFrame >= 0 && poolFrame < poolSize)
  143. {
  144. out1[i] = bufferL[poolFrame];
  145. out2[i] = bufferR[poolFrame];
  146. // reset
  147. bufferL[poolFrame] = 0.0f;
  148. bufferR[poolFrame] = 0.0f;
  149. }
  150. else
  151. {
  152. out1[i] = 0.0f;
  153. out2[i] = 0.0f;
  154. }
  155. }
  156. fLastFrame = timePos->frame;
  157. }
  158. // -------------------------------------------------------------------
  159. // Plugin UI calls
  160. void uiShow(const bool show) override
  161. {
  162. if (! show)
  163. return;
  164. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  165. uiCustomDataChanged("file", filename);
  166. uiClosed();
  167. }
  168. private:
  169. bool fLoopMode;
  170. bool fDoProcess;
  171. uint64_t fLastFrame;
  172. uint32_t fMaxFrame;
  173. AudioFilePool fPool;
  174. AudioFileThread fThread;
  175. void loadFilename(const char* const filename)
  176. {
  177. CARLA_ASSERT(filename != nullptr);
  178. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  179. fThread.stopNow();
  180. if (filename == nullptr || *filename == '\0')
  181. {
  182. fDoProcess = false;
  183. fMaxFrame = 0;
  184. return;
  185. }
  186. if (fThread.loadFilename(filename))
  187. {
  188. fThread.startNow();
  189. fMaxFrame = fThread.getMaxFrame();
  190. fDoProcess = true;
  191. }
  192. else
  193. {
  194. fDoProcess = false;
  195. fMaxFrame = 0;
  196. }
  197. }
  198. PluginClassEND(AudioFilePlugin)
  199. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  200. };
  201. // -----------------------------------------------------------------------
  202. static const NativePluginDescriptor audiofileDesc = {
  203. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  204. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  205. |NATIVE_PLUGIN_HAS_UI
  206. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  207. |NATIVE_PLUGIN_USES_TIME),
  208. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  209. /* audioIns */ 0,
  210. /* audioOuts */ 2,
  211. /* midiIns */ 0,
  212. /* midiOuts */ 0,
  213. /* paramIns */ 1,
  214. /* paramOuts */ 0,
  215. /* name */ "Audio File",
  216. /* label */ "audiofile",
  217. /* maker */ "falkTX",
  218. /* copyright */ "GNU GPL v2+",
  219. PluginDescriptorFILL(AudioFilePlugin)
  220. };
  221. // -----------------------------------------------------------------------
  222. CARLA_EXPORT
  223. void carla_register_native_plugin_audiofile();
  224. CARLA_EXPORT
  225. void carla_register_native_plugin_audiofile()
  226. {
  227. carla_register_native_plugin(&audiofileDesc);
  228. }
  229. // -----------------------------------------------------------------------