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