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.

548 lines
16KB

  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 doc/GPL.txt file.
  16. */
  17. #include "CarlaNativePrograms.hpp"
  18. #include "CarlaString.hpp"
  19. #include "audio-base.hpp"
  20. #ifdef HAVE_PYQT
  21. static const char* const audiofilesWildcard =
  22. # ifdef HAVE_SNDFILE
  23. "*.aif;*.aifc;*.aiff;*.au;*.bwf;*.flac;*.htk;*.iff;*.mat4;*.mat5;*.oga;*.ogg;"
  24. "*.paf;*.pvf;*.pvf5;*.sd2;*.sf;*.snd;*.svx;*.vcc;*.w64;*.wav;*.xi;"
  25. # endif
  26. # ifdef HAVE_FFMPEG
  27. "*.3g2;*.3gp;*.aac;*.ac3;*.amr;*.ape;*.mp2;*.mp3;*.mpc;*.wma;"
  28. # ifndef HAVE_SNDFILE
  29. "*.flac;*.oga;*.ogg;*.w64;*.wav;"
  30. # endif
  31. # endif
  32. # if !defined(HAVE_SNDFILE) && !defined(HAVE_FFMPEG)
  33. ""
  34. # ifndef BUILDING_FOR_CI
  35. # warning sndfile and ffmpeg libraries missing, no audio file support will be available
  36. # endif
  37. # endif
  38. ;
  39. #else
  40. # define process2 process
  41. #endif
  42. // -----------------------------------------------------------------------
  43. #ifdef HAVE_PYQT
  44. class AudioFilePlugin : public NativePluginWithMidiPrograms<FileAudio>,
  45. public AbstractAudioPlayer
  46. #else
  47. class AudioFilePlugin : public NativePluginClass,
  48. public AbstractAudioPlayer
  49. #endif
  50. {
  51. public:
  52. AudioFilePlugin(const NativeHostDescriptor* const host)
  53. #ifdef HAVE_PYQT
  54. : NativePluginWithMidiPrograms<FileAudio>(host, fPrograms, 2),
  55. #else
  56. : NativePluginClass(host),
  57. #endif
  58. AbstractAudioPlayer(),
  59. fLoopMode(true),
  60. fDoProcess(false),
  61. fLastFrame(0),
  62. fMaxFrame(0),
  63. fPool(),
  64. fThread(this)
  65. #ifdef HAVE_PYQT
  66. , fPrograms(hostGetFilePath("audio"), audiofilesWildcard),
  67. fInlineDisplay()
  68. #endif
  69. {
  70. }
  71. ~AudioFilePlugin() override
  72. {
  73. fThread.stopNow();
  74. fPool.destroy();
  75. }
  76. uint64_t getLastFrame() const override
  77. {
  78. return fLastFrame;
  79. }
  80. protected:
  81. // -------------------------------------------------------------------
  82. // Plugin parameter calls
  83. uint32_t getParameterCount() const override
  84. {
  85. return 1;
  86. }
  87. const NativeParameter* getParameterInfo(const uint32_t index) const override
  88. {
  89. if (index != 0)
  90. return nullptr;
  91. static NativeParameter param;
  92. param.name = "Loop Mode";
  93. param.unit = nullptr;
  94. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMABLE|NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_BOOLEAN);
  95. param.ranges.def = 1.0f;
  96. param.ranges.min = 0.0f;
  97. param.ranges.max = 1.0f;
  98. param.ranges.step = 1.0f;
  99. param.ranges.stepSmall = 1.0f;
  100. param.ranges.stepLarge = 1.0f;
  101. param.scalePointCount = 0;
  102. param.scalePoints = nullptr;
  103. return &param;
  104. }
  105. float getParameterValue(const uint32_t index) const override
  106. {
  107. if (index != 0)
  108. return 0.0f;
  109. return fLoopMode ? 1.0f : 0.0f;
  110. }
  111. // -------------------------------------------------------------------
  112. // Plugin state calls
  113. void setParameterValue(const uint32_t index, const float value) override
  114. {
  115. if (index != 0)
  116. return;
  117. bool b = (value > 0.5f);
  118. if (b == fLoopMode)
  119. return;
  120. fLoopMode = b;
  121. fThread.setLoopingMode(b);
  122. fThread.setNeedsRead();
  123. }
  124. void setCustomData(const char* const key, const char* const value) override
  125. {
  126. if (std::strcmp(key, "file") != 0)
  127. return;
  128. loadFilename(value);
  129. }
  130. // -------------------------------------------------------------------
  131. // Plugin process calls
  132. void process2(const float**, float** const outBuffer, const uint32_t frames,
  133. const NativeMidiEvent*, uint32_t) override
  134. {
  135. const NativeTimeInfo* const timePos(getTimeInfo());
  136. float* out1 = outBuffer[0];
  137. float* out2 = outBuffer[1];
  138. if (! fDoProcess)
  139. {
  140. //carla_stderr("P: no process");
  141. fLastFrame = timePos->frame;
  142. carla_zeroFloats(out1, frames);
  143. carla_zeroFloats(out2, frames);
  144. return;
  145. }
  146. // not playing
  147. if (! timePos->playing)
  148. {
  149. //carla_stderr("P: not playing");
  150. if (timePos->frame == 0 && fLastFrame > 0)
  151. fThread.setNeedsRead();
  152. fLastFrame = timePos->frame;
  153. carla_zeroFloats(out1, frames);
  154. carla_zeroFloats(out2, frames);
  155. return;
  156. }
  157. // out of reach
  158. if ((timePos->frame < fPool.startFrame || timePos->frame >= fMaxFrame) && !fLoopMode)
  159. {
  160. if (timePos->frame < fPool.startFrame)
  161. fThread.setNeedsRead();
  162. fLastFrame = timePos->frame;
  163. carla_zeroFloats(out1, frames);
  164. carla_zeroFloats(out2, frames);
  165. #ifdef HAVE_PYQT
  166. if (fInlineDisplay.writtenValues < 32)
  167. {
  168. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = 0.0f;
  169. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = 0.0f;
  170. ++fInlineDisplay.writtenValues;
  171. }
  172. if (! fInlineDisplay.pending)
  173. {
  174. fInlineDisplay.pending = true;
  175. hostQueueDrawInlineDisplay();
  176. }
  177. #endif
  178. return;
  179. }
  180. if (fThread.isEntireFileLoaded())
  181. {
  182. // NOTE: timePos->frame is always < fMaxFrame (or looping)
  183. uint32_t targetStartFrame = static_cast<uint32_t>(fLoopMode ? timePos->frame % fMaxFrame : timePos->frame);
  184. for (uint32_t framesDone=0, framesToDo=frames, remainingFrames; framesDone < frames;)
  185. {
  186. if (targetStartFrame + framesToDo <= fMaxFrame)
  187. {
  188. // everything fits together
  189. carla_copyFloats(out1+framesDone, fPool.buffer[0]+targetStartFrame, framesToDo);
  190. carla_copyFloats(out2+framesDone, fPool.buffer[1]+targetStartFrame, framesToDo);
  191. break;
  192. }
  193. remainingFrames = std::min(fMaxFrame - targetStartFrame, framesToDo);
  194. carla_copyFloats(out1+framesDone, fPool.buffer[0]+targetStartFrame, remainingFrames);
  195. carla_copyFloats(out2+framesDone, fPool.buffer[1]+targetStartFrame, remainingFrames);
  196. framesDone += remainingFrames;
  197. framesToDo -= remainingFrames;
  198. if (! fLoopMode)
  199. {
  200. // not looping, stop here
  201. if (framesToDo != 0)
  202. {
  203. carla_zeroFloats(out1+framesDone, framesToDo);
  204. carla_zeroFloats(out2+framesDone, framesToDo);
  205. }
  206. break;
  207. }
  208. // reset for next loop
  209. targetStartFrame = 0;
  210. }
  211. }
  212. else
  213. {
  214. // NOTE: timePos->frame is always >= fPool.startFrame
  215. const uint64_t poolStartFrame = timePos->frame - fThread.getPoolStartFrame();
  216. if (fThread.tryPutData(fPool, poolStartFrame, frames))
  217. {
  218. carla_copyFloats(out1, fPool.buffer[0]+poolStartFrame, frames);
  219. carla_copyFloats(out2, fPool.buffer[1]+poolStartFrame, frames);
  220. }
  221. else
  222. {
  223. carla_zeroFloats(out1, frames);
  224. carla_zeroFloats(out2, frames);
  225. }
  226. }
  227. #ifdef HAVE_PYQT
  228. if (fInlineDisplay.writtenValues < 32)
  229. {
  230. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out1, frames);
  231. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out2, frames);
  232. ++fInlineDisplay.writtenValues;
  233. }
  234. if (! fInlineDisplay.pending)
  235. {
  236. fInlineDisplay.pending = true;
  237. hostQueueDrawInlineDisplay();
  238. }
  239. #endif
  240. fLastFrame = timePos->frame;
  241. }
  242. // -------------------------------------------------------------------
  243. // Plugin UI calls
  244. void uiShow(const bool show) override
  245. {
  246. if (! show)
  247. return;
  248. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  249. uiCustomDataChanged("file", filename);
  250. uiClosed();
  251. }
  252. // -------------------------------------------------------------------
  253. // Plugin state calls
  254. #ifdef HAVE_PYQT
  255. void setStateFromFile(const char* const filename) override
  256. {
  257. loadFilename(filename);
  258. }
  259. #endif
  260. // -------------------------------------------------------------------
  261. // Plugin dispatcher calls
  262. #ifdef HAVE_PYQT
  263. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height) override
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(width > 0 && height > 0, nullptr);
  266. /* NOTE the code is this function is not optimized, still learning my way through pixels...
  267. */
  268. const size_t stride = width * 4;
  269. const size_t dataSize = stride * height;
  270. const uint pxToMove = fInlineDisplay.writtenValues;
  271. uchar* data = fInlineDisplay.data;
  272. if (fInlineDisplay.dataSize != dataSize || data == nullptr)
  273. {
  274. delete[] data;
  275. data = new uchar[dataSize];
  276. std::memset(data, 0, dataSize);
  277. fInlineDisplay.data = data;
  278. fInlineDisplay.dataSize = dataSize;
  279. }
  280. else if (pxToMove != 0)
  281. {
  282. // shift all previous values to the left
  283. for (uint w=0; w < width - pxToMove; ++w)
  284. for (uint h=0; h < height; ++h)
  285. std::memmove(&data[h * stride + w * 4], &data[h * stride + (w+pxToMove) * 4], 4);
  286. }
  287. fInlineDisplay.width = static_cast<int>(width);
  288. fInlineDisplay.height = static_cast<int>(height);
  289. fInlineDisplay.stride = static_cast<int>(stride);
  290. const uint h2 = height / 2;
  291. // clear current line
  292. for (uint w=width-pxToMove; w < width; ++w)
  293. for (uint h=0; h < height; ++h)
  294. memset(&data[h * stride + w * 4], 0, 4);
  295. // draw upper/left
  296. for (uint i=0; i < pxToMove; ++i)
  297. {
  298. const float valueL = fInlineDisplay.lastValuesL[i];
  299. const float valueR = fInlineDisplay.lastValuesR[i];
  300. const uint h2L = static_cast<uint>(valueL * (float)h2);
  301. const uint h2R = static_cast<uint>(valueR * (float)h2);
  302. const uint w = width - pxToMove + i;
  303. for (uint h=0; h < h2L; ++h)
  304. {
  305. // -30dB
  306. //if (valueL < 0.032f)
  307. // continue;
  308. data[(h2 - h) * stride + w * 4 + 3] = 160;
  309. // -12dB
  310. if (valueL < 0.25f)
  311. {
  312. data[(h2 - h) * stride + w * 4 + 1] = 255;
  313. }
  314. // -3dB
  315. else if (valueL < 0.70f)
  316. {
  317. data[(h2 - h) * stride + w * 4 + 2] = 255;
  318. data[(h2 - h) * stride + w * 4 + 1] = 255;
  319. }
  320. else
  321. {
  322. data[(h2 - h) * stride + w * 4 + 2] = 255;
  323. }
  324. }
  325. for (uint h=0; h < h2R; ++h)
  326. {
  327. // -30dB
  328. //if (valueR < 0.032f)
  329. // continue;
  330. data[(h2 + h) * stride + w * 4 + 3] = 160;
  331. // -12dB
  332. if (valueR < 0.25f)
  333. {
  334. data[(h2 + h) * stride + w * 4 + 1] = 255;
  335. }
  336. // -3dB
  337. else if (valueR < 0.70f)
  338. {
  339. data[(h2 + h) * stride + w * 4 + 2] = 255;
  340. data[(h2 + h) * stride + w * 4 + 1] = 255;
  341. }
  342. else
  343. {
  344. data[(h2 + h) * stride + w * 4 + 2] = 255;
  345. }
  346. }
  347. }
  348. fInlineDisplay.writtenValues = 0;
  349. fInlineDisplay.pending = false;
  350. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  351. }
  352. #endif
  353. // -------------------------------------------------------------------
  354. private:
  355. bool fLoopMode;
  356. bool fDoProcess;
  357. volatile uint64_t fLastFrame;
  358. uint32_t fMaxFrame;
  359. AudioFilePool fPool;
  360. AudioFileThread fThread;
  361. #ifdef HAVE_PYQT
  362. NativeMidiPrograms fPrograms;
  363. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  364. float lastValuesL[32];
  365. float lastValuesR[32];
  366. volatile uint8_t writtenValues;
  367. volatile bool pending;
  368. InlineDisplay()
  369. : NativeInlineDisplayImageSurfaceCompat(),
  370. # ifdef CARLA_PROPER_CPP11_SUPPORT
  371. lastValuesL{0.0f},
  372. lastValuesR{0.0f},
  373. # endif
  374. writtenValues(0),
  375. pending(false)
  376. {
  377. # ifndef CARLA_PROPER_CPP11_SUPPORT
  378. carla_zeroFloats(lastValuesL, 32);
  379. carla_zeroFloats(lastValuesR, 32);
  380. # endif
  381. }
  382. ~InlineDisplay()
  383. {
  384. if (data != nullptr)
  385. {
  386. delete[] data;
  387. data = nullptr;
  388. }
  389. }
  390. CARLA_DECLARE_NON_COPY_STRUCT(InlineDisplay)
  391. CARLA_PREVENT_HEAP_ALLOCATION
  392. } fInlineDisplay;
  393. #endif
  394. void loadFilename(const char* const filename)
  395. {
  396. CARLA_ASSERT(filename != nullptr);
  397. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  398. fThread.stopNow();
  399. fPool.destroy();
  400. if (filename == nullptr || *filename == '\0')
  401. {
  402. fDoProcess = false;
  403. fMaxFrame = 0;
  404. return;
  405. }
  406. if (fThread.loadFilename(filename, static_cast<uint32_t>(getSampleRate())))
  407. {
  408. fPool.create(fThread.getPoolNumFrames());
  409. fMaxFrame = fThread.getMaxFrame();
  410. if (fThread.isEntireFileLoaded())
  411. fThread.putAllData(fPool);
  412. else
  413. fThread.startNow();
  414. fDoProcess = true;
  415. }
  416. else
  417. {
  418. fDoProcess = false;
  419. fMaxFrame = 0;
  420. }
  421. }
  422. PluginClassEND(AudioFilePlugin)
  423. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  424. };
  425. // -----------------------------------------------------------------------
  426. static const NativePluginDescriptor audiofileDesc = {
  427. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  428. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  429. |NATIVE_PLUGIN_HAS_UI
  430. #ifdef HAVE_PYQT
  431. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  432. |NATIVE_PLUGIN_REQUESTS_IDLE
  433. #endif
  434. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  435. |NATIVE_PLUGIN_USES_TIME),
  436. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  437. /* audioIns */ 0,
  438. /* audioOuts */ 2,
  439. /* midiIns */ 0,
  440. /* midiOuts */ 0,
  441. /* paramIns */ 1,
  442. /* paramOuts */ 0,
  443. /* name */ "Audio File",
  444. /* label */ "audiofile",
  445. /* maker */ "falkTX",
  446. /* copyright */ "GNU GPL v2+",
  447. PluginDescriptorFILL(AudioFilePlugin)
  448. };
  449. // -----------------------------------------------------------------------
  450. CARLA_EXPORT
  451. void carla_register_native_plugin_audiofile();
  452. CARLA_EXPORT
  453. void carla_register_native_plugin_audiofile()
  454. {
  455. carla_register_native_plugin(&audiofileDesc);
  456. }
  457. // -----------------------------------------------------------------------
  458. #ifndef HAVE_PYQT
  459. # undef process2
  460. #endif