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.

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