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.

556 lines
17KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2020 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. #ifdef HAVE_PYQT
  129. invalidateNextFilename();
  130. #endif
  131. loadFilename(value);
  132. }
  133. // -------------------------------------------------------------------
  134. // Plugin process calls
  135. void process2(const float* const*, float** const outBuffer, const uint32_t frames,
  136. const NativeMidiEvent*, uint32_t) override
  137. {
  138. const NativeTimeInfo* const timePos(getTimeInfo());
  139. float* out1 = outBuffer[0];
  140. float* out2 = outBuffer[1];
  141. if (! fDoProcess)
  142. {
  143. //carla_stderr("P: no process");
  144. fLastFrame = timePos->frame;
  145. carla_zeroFloats(out1, frames);
  146. carla_zeroFloats(out2, frames);
  147. return;
  148. }
  149. // not playing
  150. if (! timePos->playing)
  151. {
  152. //carla_stderr("P: not playing");
  153. if (timePos->frame == 0 && fLastFrame > 0)
  154. fThread.setNeedsRead();
  155. fLastFrame = timePos->frame;
  156. carla_zeroFloats(out1, frames);
  157. carla_zeroFloats(out2, frames);
  158. return;
  159. }
  160. // out of reach
  161. if ((timePos->frame < fPool.startFrame || timePos->frame >= fMaxFrame) && !fLoopMode)
  162. {
  163. if (timePos->frame < fPool.startFrame)
  164. fThread.setNeedsRead();
  165. fLastFrame = timePos->frame;
  166. carla_zeroFloats(out1, frames);
  167. carla_zeroFloats(out2, frames);
  168. #ifdef HAVE_PYQT
  169. if (fInlineDisplay.writtenValues < 32)
  170. {
  171. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = 0.0f;
  172. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = 0.0f;
  173. ++fInlineDisplay.writtenValues;
  174. }
  175. if (! fInlineDisplay.pending)
  176. {
  177. fInlineDisplay.pending = true;
  178. hostQueueDrawInlineDisplay();
  179. }
  180. #endif
  181. return;
  182. }
  183. if (fThread.isEntireFileLoaded())
  184. {
  185. // NOTE: timePos->frame is always < fMaxFrame (or looping)
  186. uint32_t targetStartFrame = static_cast<uint32_t>(fLoopMode ? timePos->frame % fMaxFrame : timePos->frame);
  187. for (uint32_t framesDone=0, framesToDo=frames, remainingFrames; framesDone < frames;)
  188. {
  189. if (targetStartFrame + framesToDo <= fMaxFrame)
  190. {
  191. // everything fits together
  192. carla_copyFloats(out1+framesDone, fPool.buffer[0]+targetStartFrame, framesToDo);
  193. carla_copyFloats(out2+framesDone, fPool.buffer[1]+targetStartFrame, framesToDo);
  194. break;
  195. }
  196. remainingFrames = std::min(fMaxFrame - targetStartFrame, framesToDo);
  197. carla_copyFloats(out1+framesDone, fPool.buffer[0]+targetStartFrame, remainingFrames);
  198. carla_copyFloats(out2+framesDone, fPool.buffer[1]+targetStartFrame, remainingFrames);
  199. framesDone += remainingFrames;
  200. framesToDo -= remainingFrames;
  201. if (! fLoopMode)
  202. {
  203. // not looping, stop here
  204. if (framesToDo != 0)
  205. {
  206. carla_zeroFloats(out1+framesDone, framesToDo);
  207. carla_zeroFloats(out2+framesDone, framesToDo);
  208. }
  209. break;
  210. }
  211. // reset for next loop
  212. targetStartFrame = 0;
  213. }
  214. }
  215. else
  216. {
  217. // NOTE: timePos->frame is always >= fPool.startFrame
  218. const uint64_t poolStartFrame = timePos->frame - fThread.getPoolStartFrame();
  219. if (fThread.tryPutData(fPool, poolStartFrame, frames))
  220. {
  221. carla_copyFloats(out1, fPool.buffer[0]+poolStartFrame, frames);
  222. carla_copyFloats(out2, fPool.buffer[1]+poolStartFrame, frames);
  223. }
  224. else
  225. {
  226. carla_zeroFloats(out1, frames);
  227. carla_zeroFloats(out2, frames);
  228. }
  229. }
  230. #ifdef HAVE_PYQT
  231. if (fInlineDisplay.writtenValues < 32)
  232. {
  233. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out1, frames);
  234. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out2, frames);
  235. ++fInlineDisplay.writtenValues;
  236. }
  237. if (! fInlineDisplay.pending)
  238. {
  239. fInlineDisplay.pending = true;
  240. hostQueueDrawInlineDisplay();
  241. }
  242. #endif
  243. fLastFrame = timePos->frame;
  244. }
  245. // -------------------------------------------------------------------
  246. // Plugin UI calls
  247. void uiShow(const bool show) override
  248. {
  249. if (! show)
  250. return;
  251. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  252. uiCustomDataChanged("file", filename);
  253. uiClosed();
  254. }
  255. // -------------------------------------------------------------------
  256. // Plugin state calls
  257. #ifdef HAVE_PYQT
  258. void setStateFromFile(const char* const filename) override
  259. {
  260. loadFilename(filename);
  261. }
  262. #endif
  263. // -------------------------------------------------------------------
  264. // Plugin dispatcher calls
  265. #ifdef HAVE_PYQT
  266. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t rwidth, const uint32_t height) override
  267. {
  268. CARLA_SAFE_ASSERT_RETURN(height > 4, nullptr);
  269. const uint32_t width = rwidth == height ? height * 4 : rwidth;
  270. /* NOTE the code is this function is not optimized, still learning my way through pixels...
  271. */
  272. const size_t stride = width * 4;
  273. const size_t dataSize = stride * height;
  274. const uint pxToMove = fDoProcess ? fInlineDisplay.writtenValues : 0;
  275. uchar* data = fInlineDisplay.data;
  276. if (fInlineDisplay.dataSize != dataSize || data == nullptr)
  277. {
  278. delete[] data;
  279. data = new uchar[dataSize];
  280. std::memset(data, 0, dataSize);
  281. fInlineDisplay.data = data;
  282. fInlineDisplay.dataSize = dataSize;
  283. }
  284. else if (pxToMove != 0)
  285. {
  286. // shift all previous values to the left
  287. for (uint w=0; w < width - pxToMove; ++w)
  288. for (uint h=0; h < height; ++h)
  289. std::memmove(&data[h * stride + w * 4], &data[h * stride + (w+pxToMove) * 4], 4);
  290. }
  291. fInlineDisplay.width = static_cast<int>(width);
  292. fInlineDisplay.height = static_cast<int>(height);
  293. fInlineDisplay.stride = static_cast<int>(stride);
  294. if (pxToMove != 0)
  295. {
  296. const uint h2 = height / 2;
  297. // clear current line
  298. for (uint w=width-pxToMove; w < width; ++w)
  299. for (uint h=0; h < height; ++h)
  300. memset(&data[h * stride + w * 4], 0, 4);
  301. // draw upper/left
  302. for (uint i=0; i < pxToMove && i < 32; ++i)
  303. {
  304. const float valueL = fInlineDisplay.lastValuesL[i];
  305. const float valueR = fInlineDisplay.lastValuesR[i];
  306. const uint h2L = static_cast<uint>(valueL * (float)h2);
  307. const uint h2R = static_cast<uint>(valueR * (float)h2);
  308. const uint w = width - pxToMove + i;
  309. for (uint h=0; h < h2L; ++h)
  310. {
  311. // -30dB
  312. //if (valueL < 0.032f)
  313. // continue;
  314. data[(h2 - h) * stride + w * 4 + 3] = 160;
  315. // -12dB
  316. if (valueL < 0.25f)
  317. {
  318. data[(h2 - h) * stride + w * 4 + 1] = 255;
  319. }
  320. // -3dB
  321. else if (valueL < 0.70f)
  322. {
  323. data[(h2 - h) * stride + w * 4 + 2] = 255;
  324. data[(h2 - h) * stride + w * 4 + 1] = 255;
  325. }
  326. else
  327. {
  328. data[(h2 - h) * stride + w * 4 + 2] = 255;
  329. }
  330. }
  331. for (uint h=0; h < h2R; ++h)
  332. {
  333. // -30dB
  334. //if (valueR < 0.032f)
  335. // continue;
  336. data[(h2 + h) * stride + w * 4 + 3] = 160;
  337. // -12dB
  338. if (valueR < 0.25f)
  339. {
  340. data[(h2 + h) * stride + w * 4 + 1] = 255;
  341. }
  342. // -3dB
  343. else if (valueR < 0.70f)
  344. {
  345. data[(h2 + h) * stride + w * 4 + 2] = 255;
  346. data[(h2 + h) * stride + w * 4 + 1] = 255;
  347. }
  348. else
  349. {
  350. data[(h2 + h) * stride + w * 4 + 2] = 255;
  351. }
  352. }
  353. }
  354. }
  355. fInlineDisplay.writtenValues = 0;
  356. fInlineDisplay.pending = false;
  357. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  358. }
  359. #endif
  360. // -------------------------------------------------------------------
  361. private:
  362. bool fLoopMode;
  363. bool fDoProcess;
  364. volatile uint64_t fLastFrame;
  365. uint32_t fMaxFrame;
  366. AudioFilePool fPool;
  367. AudioFileThread fThread;
  368. #ifdef HAVE_PYQT
  369. NativeMidiPrograms fPrograms;
  370. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  371. float lastValuesL[32];
  372. float lastValuesR[32];
  373. volatile uint8_t writtenValues;
  374. volatile bool pending;
  375. InlineDisplay()
  376. : NativeInlineDisplayImageSurfaceCompat(),
  377. # ifdef CARLA_PROPER_CPP11_SUPPORT
  378. lastValuesL{0.0f},
  379. lastValuesR{0.0f},
  380. # endif
  381. writtenValues(0),
  382. pending(false)
  383. {
  384. # ifndef CARLA_PROPER_CPP11_SUPPORT
  385. carla_zeroFloats(lastValuesL, 32);
  386. carla_zeroFloats(lastValuesR, 32);
  387. # endif
  388. }
  389. ~InlineDisplay()
  390. {
  391. if (data != nullptr)
  392. {
  393. delete[] data;
  394. data = nullptr;
  395. }
  396. }
  397. CARLA_DECLARE_NON_COPY_STRUCT(InlineDisplay)
  398. CARLA_PREVENT_HEAP_ALLOCATION
  399. } fInlineDisplay;
  400. #endif
  401. void loadFilename(const char* const filename)
  402. {
  403. CARLA_ASSERT(filename != nullptr);
  404. carla_debug("AudioFilePlugin::loadFilename(\"%s\")", filename);
  405. fThread.stopNow();
  406. fPool.destroy();
  407. if (filename == nullptr || *filename == '\0')
  408. {
  409. fDoProcess = false;
  410. fMaxFrame = 0;
  411. return;
  412. }
  413. if (fThread.loadFilename(filename, static_cast<uint32_t>(getSampleRate())))
  414. {
  415. fPool.create(fThread.getPoolNumFrames());
  416. fMaxFrame = fThread.getMaxFrame();
  417. if (fThread.isEntireFileLoaded())
  418. fThread.putAllData(fPool);
  419. else
  420. fThread.startNow();
  421. fDoProcess = true;
  422. }
  423. else
  424. {
  425. fDoProcess = false;
  426. fMaxFrame = 0;
  427. }
  428. }
  429. PluginClassEND(AudioFilePlugin)
  430. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  431. };
  432. // -----------------------------------------------------------------------
  433. static const NativePluginDescriptor audiofileDesc = {
  434. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  435. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  436. |NATIVE_PLUGIN_HAS_UI
  437. #ifdef HAVE_PYQT
  438. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  439. |NATIVE_PLUGIN_REQUESTS_IDLE
  440. #endif
  441. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  442. |NATIVE_PLUGIN_USES_TIME),
  443. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  444. /* audioIns */ 0,
  445. /* audioOuts */ 2,
  446. /* midiIns */ 0,
  447. /* midiOuts */ 0,
  448. /* paramIns */ 1,
  449. /* paramOuts */ 0,
  450. /* name */ "Audio File",
  451. /* label */ "audiofile",
  452. /* maker */ "falkTX",
  453. /* copyright */ "GNU GPL v2+",
  454. PluginDescriptorFILL(AudioFilePlugin)
  455. };
  456. // -----------------------------------------------------------------------
  457. CARLA_EXPORT
  458. void carla_register_native_plugin_audiofile();
  459. CARLA_EXPORT
  460. void carla_register_native_plugin_audiofile()
  461. {
  462. carla_register_native_plugin(&audiofileDesc);
  463. }
  464. // -----------------------------------------------------------------------
  465. #ifndef HAVE_PYQT
  466. # undef process2
  467. #endif