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.

819 lines
27KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2023 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. #include <cmath>
  21. // --------------------------------------------------------------------------------------------------------------------
  22. class VolumeFilter
  23. {
  24. static constexpr const float kPIf = static_cast<float>(M_PI);
  25. float a0, b1, z1;
  26. public:
  27. VolumeFilter(const float sampleRate) noexcept
  28. {
  29. setSampleRate(sampleRate);
  30. }
  31. void reset() noexcept
  32. {
  33. a0 = 1.f - b1;
  34. z1 = 0.f;
  35. }
  36. void setSampleRate(const float sampleRate) noexcept
  37. {
  38. const float frequency = 30.0f / sampleRate;
  39. b1 = std::exp(-2.f * kPIf * frequency);
  40. a0 = 1.f - b1;
  41. z1 = 0.f;
  42. }
  43. void processStereo(const float gain, float* buffers[2], const uint32_t frames) noexcept
  44. {
  45. const float _a0 = a0;
  46. const float _b1 = b1;
  47. float _z1 = z1;
  48. for (uint32_t i=0; i < frames; ++i)
  49. {
  50. _z1 = gain * _a0 + _z1 * _b1;
  51. buffers[0][i] *= _z1;
  52. buffers[1][i] *= _z1;
  53. }
  54. z1 = _z1;
  55. }
  56. };
  57. // --------------------------------------------------------------------------------------------------------------------
  58. #ifndef __MOD_DEVICES__
  59. class AudioFilePlugin : public NativePluginWithMidiPrograms<FileAudio>
  60. #else
  61. class AudioFilePlugin : public NativePluginClass
  62. #endif
  63. {
  64. public:
  65. #ifndef __MOD_DEVICES__
  66. static constexpr const char* const audiofilesWildcard =
  67. #ifdef HAVE_SNDFILE
  68. "*.aif;*.aifc;*.aiff;*.au;*.bwf;*.flac;*.htk;*.iff;*.mat4;*.mat5;*.oga;*.ogg;*.opus;"
  69. "*.paf;*.pvf;*.pvf5;*.sd2;*.sf;*.snd;*.svx;*.vcc;*.w64;*.wav;*.xi;"
  70. #endif
  71. #ifdef HAVE_FFMPEG
  72. "*.3g2;*.3gp;*.aac;*.ac3;*.amr;*.ape;*.mp2;*.mp3;*.mpc;*.wma;"
  73. #ifndef HAVE_SNDFILE
  74. "*.flac;*.oga;*.ogg;*.w64;*.wav;"
  75. #endif
  76. #else
  77. "*.mp3;"
  78. #endif
  79. ;
  80. enum PendingInlineDisplay : uint8_t {
  81. InlineDisplayNotPending,
  82. InlineDisplayNeedRequest,
  83. InlineDisplayRequesting
  84. };
  85. #endif
  86. enum Parameters {
  87. kParameterLooping,
  88. kParameterHostSync,
  89. kParameterVolume,
  90. kParameterEnabled,
  91. kParameterQuadChannels,
  92. kParameterInfoChannels,
  93. kParameterInfoBitRate,
  94. kParameterInfoBitDepth,
  95. kParameterInfoSampleRate,
  96. kParameterInfoLength,
  97. kParameterInfoPosition,
  98. kParameterInfoPoolFill,
  99. kParameterCount
  100. };
  101. AudioFilePlugin(const NativeHostDescriptor* const host)
  102. #ifndef __MOD_DEVICES__
  103. : NativePluginWithMidiPrograms<FileAudio>(host, fPrograms, 3),
  104. fPrograms(hostGetFilePath("audio"), audiofilesWildcard),
  105. #else
  106. : NativePluginClass(host),
  107. #endif
  108. fVolumeFilter(getSampleRate()) {}
  109. protected:
  110. // ----------------------------------------------------------------------------------------------------------------
  111. // Plugin parameter calls
  112. uint32_t getParameterCount() const override
  113. {
  114. return kParameterCount;
  115. }
  116. const NativeParameter* getParameterInfo(const uint32_t index) const override
  117. {
  118. static NativeParameter param;
  119. param.scalePointCount = 0;
  120. param.scalePoints = nullptr;
  121. param.unit = nullptr;
  122. param.ranges.step = 1.0f;
  123. param.ranges.stepSmall = 1.0f;
  124. param.ranges.stepLarge = 1.0f;
  125. param.designation = NATIVE_PARAMETER_DESIGNATION_NONE;
  126. switch (index)
  127. {
  128. case kParameterLooping:
  129. param.name = "Loop Mode";
  130. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  131. NATIVE_PARAMETER_IS_ENABLED|
  132. NATIVE_PARAMETER_IS_BOOLEAN);
  133. param.ranges.def = 1.0f;
  134. param.ranges.min = 0.0f;
  135. param.ranges.max = 1.0f;
  136. break;
  137. case kParameterHostSync:
  138. param.name = "Host Sync";
  139. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  140. NATIVE_PARAMETER_IS_ENABLED|
  141. NATIVE_PARAMETER_IS_BOOLEAN);
  142. #ifdef __MOD_DEVICES__
  143. param.ranges.def = 0.0f;
  144. #else
  145. param.ranges.def = 1.0f;
  146. #endif
  147. param.ranges.min = 0.0f;
  148. param.ranges.max = 1.0f;
  149. break;
  150. case kParameterVolume:
  151. param.name = "Volume";
  152. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  153. NATIVE_PARAMETER_IS_ENABLED);
  154. param.ranges.def = 100.0f;
  155. param.ranges.min = 0.0f;
  156. param.ranges.max = 127.0f;
  157. param.ranges.stepSmall = 0.5f;
  158. param.ranges.stepLarge = 10.0f;
  159. param.unit = "%";
  160. break;
  161. case kParameterEnabled:
  162. param.name = "Enabled";
  163. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  164. NATIVE_PARAMETER_IS_ENABLED|
  165. NATIVE_PARAMETER_IS_BOOLEAN|
  166. NATIVE_PARAMETER_USES_DESIGNATION);
  167. param.ranges.def = 1.0f;
  168. param.ranges.min = 0.0f;
  169. param.ranges.max = 1.0f;
  170. param.designation = NATIVE_PARAMETER_DESIGNATION_ENABLED;
  171. break;
  172. case kParameterQuadChannels:
  173. param.name = "Quad Channels";
  174. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  175. NATIVE_PARAMETER_IS_ENABLED|
  176. NATIVE_PARAMETER_IS_INTEGER|
  177. NATIVE_PARAMETER_USES_SCALEPOINTS);
  178. param.ranges.def = 0.0f;
  179. param.ranges.min = 0.0f;
  180. param.ranges.max = 1.0f;
  181. {
  182. static const NativeParameterScalePoint scalePoints[2] = {
  183. { "Channels 1 + 2", 0 },
  184. { "Channels 3 + 4", 1 }
  185. };
  186. param.scalePointCount = 2;
  187. param.scalePoints = scalePoints;
  188. }
  189. break;
  190. case kParameterInfoChannels:
  191. param.name = "Num Channels";
  192. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  193. NATIVE_PARAMETER_IS_ENABLED|
  194. NATIVE_PARAMETER_IS_INTEGER|
  195. NATIVE_PARAMETER_IS_OUTPUT);
  196. param.ranges.def = 0.0f;
  197. param.ranges.min = 0.0f;
  198. param.ranges.max = 2.0f;
  199. break;
  200. case kParameterInfoBitRate:
  201. param.name = "Bit Rate";
  202. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  203. NATIVE_PARAMETER_IS_ENABLED|
  204. NATIVE_PARAMETER_IS_INTEGER|
  205. NATIVE_PARAMETER_IS_OUTPUT);
  206. param.ranges.def = 0.0f;
  207. param.ranges.min = -1.0f;
  208. param.ranges.max = 384000.0f * 64.0f * 2.0f;
  209. break;
  210. case kParameterInfoBitDepth:
  211. param.name = "Bit Depth";
  212. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  213. NATIVE_PARAMETER_IS_ENABLED|
  214. NATIVE_PARAMETER_IS_INTEGER|
  215. NATIVE_PARAMETER_IS_OUTPUT);
  216. param.ranges.def = 0.0f;
  217. param.ranges.min = 0.0f;
  218. param.ranges.max = 64.0f;
  219. break;
  220. case kParameterInfoSampleRate:
  221. param.name = "Sample Rate";
  222. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  223. NATIVE_PARAMETER_IS_ENABLED|
  224. NATIVE_PARAMETER_IS_INTEGER|
  225. NATIVE_PARAMETER_IS_OUTPUT);
  226. param.ranges.def = 0.0f;
  227. param.ranges.min = 0.0f;
  228. param.ranges.max = 384000.0f;
  229. break;
  230. case kParameterInfoLength:
  231. param.name = "Length";
  232. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  233. NATIVE_PARAMETER_IS_ENABLED|
  234. NATIVE_PARAMETER_IS_OUTPUT);
  235. param.ranges.def = 0.0f;
  236. param.ranges.min = 0.0f;
  237. param.ranges.max = (float)INT64_MAX;
  238. param.unit = "s";
  239. break;
  240. case kParameterInfoPosition:
  241. param.name = "Position";
  242. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  243. NATIVE_PARAMETER_IS_ENABLED|
  244. NATIVE_PARAMETER_IS_OUTPUT);
  245. param.ranges.def = 0.0f;
  246. param.ranges.min = 0.0f;
  247. param.ranges.max = 100.0f;
  248. param.unit = "%";
  249. break;
  250. case kParameterInfoPoolFill:
  251. param.name = "Pool Fill";
  252. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  253. NATIVE_PARAMETER_IS_ENABLED|
  254. NATIVE_PARAMETER_IS_OUTPUT);
  255. param.ranges.def = 0.0f;
  256. param.ranges.min = 0.0f;
  257. param.ranges.max = 100.0f;
  258. param.unit = "%";
  259. break;
  260. default:
  261. return nullptr;
  262. }
  263. return &param;
  264. }
  265. float getParameterValue(const uint32_t index) const override
  266. {
  267. switch (index)
  268. {
  269. case kParameterLooping:
  270. return fLoopMode ? 1.0f : 0.0f;
  271. case kParameterHostSync:
  272. return fHostSync ? 1.f : 0.f;
  273. case kParameterEnabled:
  274. return fEnabled ? 1.f : 0.f;
  275. case kParameterQuadChannels:
  276. return fQuad2ndChannels ? 1.f : 0.f;
  277. case kParameterVolume:
  278. return fVolume * 100.f;
  279. case kParameterInfoPosition:
  280. return fLastPosition;
  281. case kParameterInfoPoolFill:
  282. return fReadableBufferFill;
  283. case kParameterInfoBitRate:
  284. return static_cast<float>(fReader.getCurrentBitRate());
  285. }
  286. const ADInfo nfo = fReader.getFileInfo();
  287. switch (index)
  288. {
  289. case kParameterInfoChannels:
  290. return static_cast<float>(nfo.channels);
  291. case kParameterInfoBitDepth:
  292. return static_cast<float>(nfo.bit_depth);
  293. case kParameterInfoSampleRate:
  294. return static_cast<float>(nfo.sample_rate);
  295. case kParameterInfoLength:
  296. return static_cast<float>(nfo.length)/1000.f;
  297. }
  298. return 0.f;
  299. }
  300. void setParameterValue(const uint32_t index, const float value) override
  301. {
  302. if (index == kParameterVolume)
  303. {
  304. fVolume = value / 100.f;
  305. return;
  306. }
  307. const bool b = value > 0.5f;
  308. switch (index)
  309. {
  310. case kParameterLooping:
  311. if (fLoopMode != b)
  312. fLoopMode = b;
  313. break;
  314. case kParameterHostSync:
  315. if (fHostSync != b)
  316. {
  317. fInternalTransportFrame = 0;
  318. fHostSync = b;
  319. }
  320. break;
  321. case kParameterEnabled:
  322. if (fEnabled != b)
  323. {
  324. fInternalTransportFrame = 0;
  325. fEnabled = b;
  326. }
  327. break;
  328. case kParameterQuadChannels:
  329. if (fQuad2ndChannels != b)
  330. {
  331. fQuad2ndChannels = b;
  332. fPendingFileReload = true;
  333. hostRequestIdle();
  334. }
  335. default:
  336. break;
  337. }
  338. }
  339. // ----------------------------------------------------------------------------------------------------------------
  340. // Plugin state calls
  341. void setCustomData(const char* const key, const char* const value) override
  342. {
  343. if (std::strcmp(key, "file") != 0)
  344. return;
  345. #ifndef __MOD_DEVICES__
  346. invalidateNextFilename();
  347. #endif
  348. loadFilename(value);
  349. }
  350. #ifndef __MOD_DEVICES__
  351. void setStateFromFile(const char* const filename) override
  352. {
  353. loadFilename(filename);
  354. }
  355. #endif
  356. // ----------------------------------------------------------------------------------------------------------------
  357. // Plugin process calls
  358. #ifndef __MOD_DEVICES__
  359. void process2(const float* const*, float** const outBuffer, const uint32_t frames,
  360. const NativeMidiEvent*, uint32_t) override
  361. #else
  362. void process(const float* const*, float** const outBuffer, const uint32_t frames,
  363. const NativeMidiEvent*, uint32_t) override
  364. #endif
  365. {
  366. float* const out1 = outBuffer[0];
  367. float* const out2 = outBuffer[1];
  368. float* const playCV = outBuffer[2];
  369. if (! fDoProcess)
  370. {
  371. // carla_stderr("P: no process");
  372. carla_zeroFloats(out1, frames);
  373. carla_zeroFloats(out2, frames);
  374. carla_zeroFloats(playCV, frames);
  375. fLastPosition = 0.f;
  376. fReadableBufferFill = 0.f;
  377. return;
  378. }
  379. bool playing;
  380. uint64_t framePos;
  381. if (fHostSync)
  382. {
  383. const NativeTimeInfo* const timePos = getTimeInfo();
  384. playing = fEnabled && timePos->playing;
  385. framePos = timePos->frame;
  386. }
  387. else
  388. {
  389. playing = fEnabled;
  390. framePos = fInternalTransportFrame;
  391. if (playing)
  392. fInternalTransportFrame += frames;
  393. }
  394. // not playing
  395. if (! playing)
  396. {
  397. carla_zeroFloats(out1, frames);
  398. carla_zeroFloats(out2, frames);
  399. carla_zeroFloats(playCV, frames);
  400. return;
  401. }
  402. bool needsIdleRequest = false;
  403. if (fReader.tickFrames(outBuffer, 0, frames, framePos, fLoopMode, isOffline()) && ! fPendingFileRead)
  404. {
  405. fPendingFileRead = true;
  406. needsIdleRequest = true;
  407. }
  408. fLastPosition = fReader.getLastPlayPosition() * 100.f;
  409. fReadableBufferFill = fReader.getReadableBufferFill() * 100.f;
  410. fVolumeFilter.processStereo(fVolume, outBuffer, frames);
  411. #ifndef __MOD_DEVICES__
  412. if (fInlineDisplay.writtenValues < 32)
  413. {
  414. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out1, frames);
  415. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out2, frames);
  416. ++fInlineDisplay.writtenValues;
  417. }
  418. if (fInlineDisplay.pending == InlineDisplayNotPending)
  419. {
  420. needsIdleRequest = true;
  421. fInlineDisplay.pending = InlineDisplayNeedRequest;
  422. }
  423. #endif
  424. if (needsIdleRequest)
  425. hostRequestIdle();
  426. }
  427. // ----------------------------------------------------------------------------------------------------------------
  428. // Plugin UI calls
  429. void uiShow(const bool show) override
  430. {
  431. if (! show)
  432. return;
  433. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  434. uiCustomDataChanged("file", filename);
  435. uiClosed();
  436. }
  437. // ----------------------------------------------------------------------------------------------------------------
  438. // Plugin dispatcher calls
  439. void idle() override
  440. {
  441. #ifndef __MOD_DEVICES__
  442. NativePluginWithMidiPrograms<FileAudio>::idle();
  443. if (fInlineDisplay.pending == InlineDisplayNeedRequest)
  444. {
  445. fInlineDisplay.pending = InlineDisplayRequesting;
  446. hostQueueDrawInlineDisplay();
  447. }
  448. #endif
  449. if (fPendingFileReload)
  450. {
  451. fPendingFileReload = fPendingFileRead = false;
  452. if (char* const filename = fFilename.releaseBufferPointer())
  453. {
  454. loadFilename(filename);
  455. std::free(filename);
  456. }
  457. }
  458. else if (fPendingFileRead)
  459. {
  460. fPendingFileRead = false;
  461. fReader.readPoll();
  462. }
  463. }
  464. void sampleRateChanged(const double sampleRate) override
  465. {
  466. fVolumeFilter.setSampleRate(sampleRate);
  467. if (char* const filename = fFilename.releaseBufferPointer())
  468. {
  469. loadFilename(filename);
  470. std::free(filename);
  471. }
  472. }
  473. #ifndef __MOD_DEVICES__
  474. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t rwidth, const uint32_t height) override
  475. {
  476. CARLA_SAFE_ASSERT_RETURN(height > 4, nullptr);
  477. const uint32_t width = rwidth == height ? height * 4 : rwidth;
  478. /* NOTE the code is this function is not optimized, still learning my way through pixels...
  479. */
  480. const size_t stride = width * 4;
  481. const size_t dataSize = stride * height;
  482. const uint pxToMove = fDoProcess ? fInlineDisplay.writtenValues : 0;
  483. uchar* data = fInlineDisplay.data;
  484. if (fInlineDisplay.dataSize != dataSize || data == nullptr)
  485. {
  486. delete[] data;
  487. data = new uchar[dataSize];
  488. std::memset(data, 0, dataSize);
  489. fInlineDisplay.data = data;
  490. fInlineDisplay.dataSize = dataSize;
  491. }
  492. else if (pxToMove != 0)
  493. {
  494. // shift all previous values to the left
  495. for (uint w=0; w < width - pxToMove; ++w)
  496. for (uint h=0; h < height; ++h)
  497. std::memmove(&data[h * stride + w * 4], &data[h * stride + (w+pxToMove) * 4], 4);
  498. }
  499. fInlineDisplay.width = static_cast<int>(width);
  500. fInlineDisplay.height = static_cast<int>(height);
  501. fInlineDisplay.stride = static_cast<int>(stride);
  502. if (pxToMove != 0)
  503. {
  504. const uint h2 = height / 2;
  505. // clear current line
  506. for (uint w=width-pxToMove; w < width; ++w)
  507. for (uint h=0; h < height; ++h)
  508. memset(&data[h * stride + w * 4], 0, 4);
  509. // draw upper/left
  510. for (uint i=0; i < pxToMove && i < 32; ++i)
  511. {
  512. const float valueL = fInlineDisplay.lastValuesL[i];
  513. const float valueR = fInlineDisplay.lastValuesR[i];
  514. const uint h2L = static_cast<uint>(valueL * (float)h2);
  515. const uint h2R = static_cast<uint>(valueR * (float)h2);
  516. const uint w = width - pxToMove + i;
  517. for (uint h=0; h < h2L; ++h)
  518. {
  519. // -30dB
  520. //if (valueL < 0.032f)
  521. // continue;
  522. data[(h2 - h) * stride + w * 4 + 3] = 160;
  523. // -12dB
  524. if (valueL < 0.25f)
  525. {
  526. data[(h2 - h) * stride + w * 4 + 1] = 255;
  527. }
  528. // -3dB
  529. else if (valueL < 0.70f)
  530. {
  531. data[(h2 - h) * stride + w * 4 + 2] = 255;
  532. data[(h2 - h) * stride + w * 4 + 1] = 255;
  533. }
  534. else
  535. {
  536. data[(h2 - h) * stride + w * 4 + 2] = 255;
  537. }
  538. }
  539. for (uint h=0; h < h2R; ++h)
  540. {
  541. // -30dB
  542. //if (valueR < 0.032f)
  543. // continue;
  544. data[(h2 + h) * stride + w * 4 + 3] = 160;
  545. // -12dB
  546. if (valueR < 0.25f)
  547. {
  548. data[(h2 + h) * stride + w * 4 + 1] = 255;
  549. }
  550. // -3dB
  551. else if (valueR < 0.70f)
  552. {
  553. data[(h2 + h) * stride + w * 4 + 2] = 255;
  554. data[(h2 + h) * stride + w * 4 + 1] = 255;
  555. }
  556. else
  557. {
  558. data[(h2 + h) * stride + w * 4 + 2] = 255;
  559. }
  560. }
  561. }
  562. }
  563. fInlineDisplay.writtenValues = 0;
  564. fInlineDisplay.pending = InlineDisplayNotPending;
  565. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  566. }
  567. #endif
  568. // ----------------------------------------------------------------------------------------------------------------
  569. private:
  570. bool fLoopMode = true;
  571. #ifdef __MOD_DEVICES__
  572. bool fHostSync = false;
  573. #else
  574. bool fHostSync = true;
  575. #endif
  576. bool fEnabled = true;
  577. bool fDoProcess = false;
  578. bool fPendingFileRead = false;
  579. bool fPendingFileReload = false;
  580. bool fQuad2ndChannels = false;
  581. uint32_t fInternalTransportFrame = 0;
  582. float fLastPosition = 0.f;
  583. float fReadableBufferFill = 0.f;
  584. float fVolume = 1.f;
  585. AudioFileReader fReader;
  586. CarlaString fFilename;
  587. float fPreviewData[108] = {};
  588. #ifndef __MOD_DEVICES__
  589. NativeMidiPrograms fPrograms;
  590. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  591. float lastValuesL[32] = {};
  592. float lastValuesR[32] = {};
  593. volatile PendingInlineDisplay pending = InlineDisplayNotPending;
  594. volatile uint8_t writtenValues = 0;
  595. InlineDisplay()
  596. : NativeInlineDisplayImageSurfaceCompat() {}
  597. ~InlineDisplay()
  598. {
  599. if (data != nullptr)
  600. {
  601. delete[] data;
  602. data = nullptr;
  603. }
  604. }
  605. CARLA_DECLARE_NON_COPYABLE(InlineDisplay)
  606. CARLA_PREVENT_HEAP_ALLOCATION
  607. } fInlineDisplay;
  608. #endif
  609. VolumeFilter fVolumeFilter;
  610. void loadFilename(const char* const filename)
  611. {
  612. CARLA_ASSERT(filename != nullptr);
  613. carla_stdout("AudioFilePlugin::loadFilename(\"%s\")", filename);
  614. fDoProcess = false;
  615. fReader.destroy();
  616. fFilename.clear();
  617. if (filename == nullptr || *filename == '\0')
  618. return;
  619. constexpr uint32_t kPreviewDataLen = sizeof(fPreviewData)/sizeof(float);
  620. if (fReader.loadFilename(filename, static_cast<uint32_t>(getSampleRate()), fQuad2ndChannels,
  621. kPreviewDataLen, fPreviewData))
  622. {
  623. fInternalTransportFrame = 0;
  624. fDoProcess = true;
  625. fFilename = filename;
  626. hostSendPreviewBufferData('f', kPreviewDataLen, fPreviewData);
  627. }
  628. }
  629. PluginClassEND(AudioFilePlugin)
  630. static const char* _get_buffer_port_name(NativePluginHandle, const uint32_t index, const bool isOutput)
  631. {
  632. if (!isOutput)
  633. return nullptr;
  634. switch (index)
  635. {
  636. case 0:
  637. return "output_1";
  638. case 1:
  639. return "output_2";
  640. case 2:
  641. return "Play status";
  642. }
  643. return nullptr;
  644. }
  645. static const NativePortRange* _get_buffer_port_range(NativePluginHandle, const uint32_t index, const bool isOutput)
  646. {
  647. if (!isOutput || index != 2)
  648. return nullptr;
  649. static NativePortRange npr = { 0.f, 10.f };
  650. return &npr;
  651. }
  652. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  653. };
  654. // --------------------------------------------------------------------------------------------------------------------
  655. CARLA_API_EXPORT
  656. void carla_register_native_plugin_audiofile();
  657. CARLA_API_EXPORT
  658. void carla_register_native_plugin_audiofile()
  659. {
  660. static const NativePluginDescriptor audiofileDesc = {
  661. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  662. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  663. #ifndef __MOD_DEVICES__
  664. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  665. #endif
  666. |NATIVE_PLUGIN_HAS_UI
  667. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  668. |NATIVE_PLUGIN_REQUESTS_IDLE
  669. |NATIVE_PLUGIN_USES_CONTROL_VOLTAGE
  670. |NATIVE_PLUGIN_USES_TIME),
  671. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  672. /* audioIns */ 0,
  673. /* audioOuts */ 2,
  674. /* midiIns */ 0,
  675. /* midiOuts */ 0,
  676. /* paramIns */ 1,
  677. /* paramOuts */ 0,
  678. /* name */ "Audio File",
  679. /* label */ "audiofile",
  680. /* maker */ "falkTX",
  681. /* copyright */ "GNU GPL v2+",
  682. AudioFilePlugin::_instantiate,
  683. AudioFilePlugin::_cleanup,
  684. AudioFilePlugin::_get_parameter_count,
  685. AudioFilePlugin::_get_parameter_info,
  686. AudioFilePlugin::_get_parameter_value,
  687. AudioFilePlugin::_get_midi_program_count,
  688. AudioFilePlugin::_get_midi_program_info,
  689. AudioFilePlugin::_set_parameter_value,
  690. AudioFilePlugin::_set_midi_program,
  691. AudioFilePlugin::_set_custom_data,
  692. AudioFilePlugin::_ui_show,
  693. AudioFilePlugin::_ui_idle,
  694. AudioFilePlugin::_ui_set_parameter_value,
  695. AudioFilePlugin::_ui_set_midi_program,
  696. AudioFilePlugin::_ui_set_custom_data,
  697. AudioFilePlugin::_activate,
  698. AudioFilePlugin::_deactivate,
  699. AudioFilePlugin::_process,
  700. AudioFilePlugin::_get_state,
  701. AudioFilePlugin::_set_state,
  702. AudioFilePlugin::_dispatcher,
  703. AudioFilePlugin::_render_inline_display,
  704. /* cvIns */ 0,
  705. /* cvOuts */ 1,
  706. AudioFilePlugin::_get_buffer_port_name,
  707. AudioFilePlugin::_get_buffer_port_range,
  708. /* ui_width */ 0,
  709. /* ui_height */ 0
  710. };
  711. carla_register_native_plugin(&audiofileDesc);
  712. }
  713. // --------------------------------------------------------------------------------------------------------------------