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.

audio-file.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 = 2.0f;
  181. {
  182. static const NativeParameterScalePoint scalePoints[3] = {
  183. { "Channels 1 + 2", 0 },
  184. { "Channels 3 + 4", 1 },
  185. { "Channels 1&2 + 3&4", 2 }
  186. };
  187. param.scalePointCount = 3;
  188. param.scalePoints = scalePoints;
  189. }
  190. break;
  191. case kParameterInfoChannels:
  192. param.name = "Num Channels";
  193. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  194. NATIVE_PARAMETER_IS_ENABLED|
  195. NATIVE_PARAMETER_IS_INTEGER|
  196. NATIVE_PARAMETER_IS_OUTPUT);
  197. param.ranges.def = 0.0f;
  198. param.ranges.min = 0.0f;
  199. param.ranges.max = 2.0f;
  200. break;
  201. case kParameterInfoBitRate:
  202. param.name = "Bit Rate";
  203. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  204. NATIVE_PARAMETER_IS_ENABLED|
  205. NATIVE_PARAMETER_IS_INTEGER|
  206. NATIVE_PARAMETER_IS_OUTPUT);
  207. param.ranges.def = 0.0f;
  208. param.ranges.min = -1.0f;
  209. param.ranges.max = 384000.0f * 64.0f * 2.0f;
  210. break;
  211. case kParameterInfoBitDepth:
  212. param.name = "Bit Depth";
  213. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  214. NATIVE_PARAMETER_IS_ENABLED|
  215. NATIVE_PARAMETER_IS_INTEGER|
  216. NATIVE_PARAMETER_IS_OUTPUT);
  217. param.ranges.def = 0.0f;
  218. param.ranges.min = 0.0f;
  219. param.ranges.max = 64.0f;
  220. break;
  221. case kParameterInfoSampleRate:
  222. param.name = "Sample Rate";
  223. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  224. NATIVE_PARAMETER_IS_ENABLED|
  225. NATIVE_PARAMETER_IS_INTEGER|
  226. NATIVE_PARAMETER_IS_OUTPUT);
  227. param.ranges.def = 0.0f;
  228. param.ranges.min = 0.0f;
  229. param.ranges.max = 384000.0f;
  230. break;
  231. case kParameterInfoLength:
  232. param.name = "Length";
  233. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  234. NATIVE_PARAMETER_IS_ENABLED|
  235. NATIVE_PARAMETER_IS_OUTPUT);
  236. param.ranges.def = 0.0f;
  237. param.ranges.min = 0.0f;
  238. param.ranges.max = (float)INT64_MAX;
  239. param.unit = "s";
  240. break;
  241. case kParameterInfoPosition:
  242. param.name = "Position";
  243. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  244. NATIVE_PARAMETER_IS_ENABLED|
  245. NATIVE_PARAMETER_IS_OUTPUT);
  246. param.ranges.def = 0.0f;
  247. param.ranges.min = 0.0f;
  248. param.ranges.max = 100.0f;
  249. param.unit = "%";
  250. break;
  251. case kParameterInfoPoolFill:
  252. param.name = "Pool Fill";
  253. param.hints = static_cast<NativeParameterHints>(NATIVE_PARAMETER_IS_AUTOMATABLE|
  254. NATIVE_PARAMETER_IS_ENABLED|
  255. NATIVE_PARAMETER_IS_OUTPUT);
  256. param.ranges.def = 0.0f;
  257. param.ranges.min = 0.0f;
  258. param.ranges.max = 100.0f;
  259. param.unit = "%";
  260. break;
  261. default:
  262. return nullptr;
  263. }
  264. return &param;
  265. }
  266. float getParameterValue(const uint32_t index) const override
  267. {
  268. switch (index)
  269. {
  270. case kParameterLooping:
  271. return fLoopMode ? 1.0f : 0.0f;
  272. case kParameterHostSync:
  273. return fHostSync ? 1.f : 0.f;
  274. case kParameterEnabled:
  275. return fEnabled ? 1.f : 0.f;
  276. case kParameterQuadChannels:
  277. return fQuadMode;
  278. case kParameterVolume:
  279. return fVolume * 100.f;
  280. case kParameterInfoPosition:
  281. return fLastPosition;
  282. case kParameterInfoPoolFill:
  283. return fReadableBufferFill;
  284. case kParameterInfoBitRate:
  285. return static_cast<float>(fReader.getCurrentBitRate());
  286. }
  287. const ADInfo nfo = fReader.getFileInfo();
  288. switch (index)
  289. {
  290. case kParameterInfoChannels:
  291. return static_cast<float>(nfo.channels);
  292. case kParameterInfoBitDepth:
  293. return static_cast<float>(nfo.bit_depth);
  294. case kParameterInfoSampleRate:
  295. return static_cast<float>(nfo.sample_rate);
  296. case kParameterInfoLength:
  297. return static_cast<float>(nfo.length)/1000.f;
  298. }
  299. return 0.f;
  300. }
  301. void setParameterValue(const uint32_t index, const float value) override
  302. {
  303. if (index == kParameterVolume)
  304. {
  305. fVolume = value / 100.f;
  306. return;
  307. }
  308. if (index == kParameterQuadChannels)
  309. {
  310. const int ivalue = static_cast<int>(value + 0.5f);
  311. CARLA_SAFE_ASSERT_INT_RETURN(value >= AudioFileReader::kQuad1and2 && value <= AudioFileReader::kQuadAll,
  312. value,);
  313. fQuadMode = static_cast<AudioFileReader::QuadMode>(ivalue);
  314. fPendingFileReload = true;
  315. hostRequestIdle();
  316. return;
  317. }
  318. const bool b = value > 0.5f;
  319. switch (index)
  320. {
  321. case kParameterLooping:
  322. if (fLoopMode != b)
  323. fLoopMode = b;
  324. break;
  325. case kParameterHostSync:
  326. if (fHostSync != b)
  327. {
  328. fInternalTransportFrame = 0;
  329. fHostSync = b;
  330. }
  331. break;
  332. case kParameterEnabled:
  333. if (fEnabled != b)
  334. {
  335. fInternalTransportFrame = 0;
  336. fEnabled = b;
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. // ----------------------------------------------------------------------------------------------------------------
  344. // Plugin state calls
  345. void setCustomData(const char* const key, const char* const value) override
  346. {
  347. if (std::strcmp(key, "file") != 0)
  348. return;
  349. #ifndef __MOD_DEVICES__
  350. invalidateNextFilename();
  351. #endif
  352. loadFilename(value);
  353. }
  354. #ifndef __MOD_DEVICES__
  355. void setStateFromFile(const char* const filename) override
  356. {
  357. loadFilename(filename);
  358. }
  359. #endif
  360. // ----------------------------------------------------------------------------------------------------------------
  361. // Plugin process calls
  362. #ifndef __MOD_DEVICES__
  363. void process2(const float* const*, float** const outBuffer, const uint32_t frames,
  364. const NativeMidiEvent*, uint32_t) override
  365. #else
  366. void process(const float* const*, float** const outBuffer, const uint32_t frames,
  367. const NativeMidiEvent*, uint32_t) override
  368. #endif
  369. {
  370. float* const out1 = outBuffer[0];
  371. float* const out2 = outBuffer[1];
  372. float* const playCV = outBuffer[2];
  373. if (! fDoProcess)
  374. {
  375. // carla_stderr("P: no process");
  376. carla_zeroFloats(out1, frames);
  377. carla_zeroFloats(out2, frames);
  378. carla_zeroFloats(playCV, frames);
  379. fLastPosition = 0.f;
  380. fReadableBufferFill = 0.f;
  381. return;
  382. }
  383. bool playing;
  384. uint64_t framePos;
  385. if (fHostSync)
  386. {
  387. const NativeTimeInfo* const timePos = getTimeInfo();
  388. playing = fEnabled && timePos->playing;
  389. framePos = timePos->frame;
  390. }
  391. else
  392. {
  393. playing = fEnabled;
  394. framePos = fInternalTransportFrame;
  395. if (playing)
  396. fInternalTransportFrame += frames;
  397. }
  398. // not playing
  399. if (! playing)
  400. {
  401. carla_zeroFloats(out1, frames);
  402. carla_zeroFloats(out2, frames);
  403. carla_zeroFloats(playCV, frames);
  404. return;
  405. }
  406. const bool offline = isOffline();
  407. bool needsIdleRequest = false;
  408. if (fReader.tickFrames(outBuffer, 0, frames, framePos, fLoopMode, offline) && ! fPendingFileRead)
  409. {
  410. if (offline)
  411. {
  412. fPendingFileRead = false;
  413. fReader.readPoll();
  414. }
  415. else
  416. {
  417. fPendingFileRead = true;
  418. needsIdleRequest = true;
  419. }
  420. }
  421. fLastPosition = fReader.getLastPlayPosition() * 100.f;
  422. fReadableBufferFill = fReader.getReadableBufferFill() * 100.f;
  423. fVolumeFilter.processStereo(fVolume, outBuffer, frames);
  424. #ifndef __MOD_DEVICES__
  425. if (fInlineDisplay.writtenValues < 32)
  426. {
  427. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out1, frames);
  428. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out2, frames);
  429. ++fInlineDisplay.writtenValues;
  430. }
  431. if (fInlineDisplay.pending == InlineDisplayNotPending)
  432. {
  433. needsIdleRequest = true;
  434. fInlineDisplay.pending = InlineDisplayNeedRequest;
  435. }
  436. #endif
  437. if (needsIdleRequest)
  438. hostRequestIdle();
  439. }
  440. // ----------------------------------------------------------------------------------------------------------------
  441. // Plugin UI calls
  442. void uiShow(const bool show) override
  443. {
  444. if (! show)
  445. return;
  446. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  447. uiCustomDataChanged("file", filename);
  448. uiClosed();
  449. }
  450. // ----------------------------------------------------------------------------------------------------------------
  451. // Plugin dispatcher calls
  452. void idle() override
  453. {
  454. #ifndef __MOD_DEVICES__
  455. NativePluginWithMidiPrograms<FileAudio>::idle();
  456. if (fInlineDisplay.pending == InlineDisplayNeedRequest)
  457. {
  458. fInlineDisplay.pending = InlineDisplayRequesting;
  459. hostQueueDrawInlineDisplay();
  460. }
  461. #endif
  462. if (fPendingFileReload)
  463. {
  464. fPendingFileReload = fPendingFileRead = false;
  465. if (char* const filename = fFilename.releaseBufferPointer())
  466. {
  467. loadFilename(filename);
  468. std::free(filename);
  469. }
  470. }
  471. else if (fPendingFileRead)
  472. {
  473. fPendingFileRead = false;
  474. fReader.readPoll();
  475. }
  476. }
  477. void sampleRateChanged(const double sampleRate) override
  478. {
  479. fVolumeFilter.setSampleRate(sampleRate);
  480. if (char* const filename = fFilename.releaseBufferPointer())
  481. {
  482. loadFilename(filename);
  483. std::free(filename);
  484. }
  485. }
  486. #ifndef __MOD_DEVICES__
  487. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t rwidth, const uint32_t height) override
  488. {
  489. CARLA_SAFE_ASSERT_RETURN(height > 4, nullptr);
  490. const uint32_t width = rwidth == height ? height * 4 : rwidth;
  491. /* NOTE the code is this function is not optimized, still learning my way through pixels...
  492. */
  493. const size_t stride = width * 4;
  494. const size_t dataSize = stride * height;
  495. const uint pxToMove = fDoProcess ? fInlineDisplay.writtenValues : 0;
  496. uchar* data = fInlineDisplay.data;
  497. if (fInlineDisplay.dataSize != dataSize || data == nullptr)
  498. {
  499. delete[] data;
  500. data = new uchar[dataSize];
  501. std::memset(data, 0, dataSize);
  502. fInlineDisplay.data = data;
  503. fInlineDisplay.dataSize = dataSize;
  504. }
  505. else if (pxToMove != 0)
  506. {
  507. // shift all previous values to the left
  508. for (uint w=0; w < width - pxToMove; ++w)
  509. for (uint h=0; h < height; ++h)
  510. std::memmove(&data[h * stride + w * 4], &data[h * stride + (w+pxToMove) * 4], 4);
  511. }
  512. fInlineDisplay.width = static_cast<int>(width);
  513. fInlineDisplay.height = static_cast<int>(height);
  514. fInlineDisplay.stride = static_cast<int>(stride);
  515. if (pxToMove != 0)
  516. {
  517. const uint h2 = height / 2;
  518. // clear current line
  519. for (uint w=width-pxToMove; w < width; ++w)
  520. for (uint h=0; h < height; ++h)
  521. memset(&data[h * stride + w * 4], 0, 4);
  522. // draw upper/left
  523. for (uint i=0; i < pxToMove && i < 32; ++i)
  524. {
  525. const float valueL = fInlineDisplay.lastValuesL[i];
  526. const float valueR = fInlineDisplay.lastValuesR[i];
  527. const uint h2L = static_cast<uint>(valueL * (float)h2);
  528. const uint h2R = static_cast<uint>(valueR * (float)h2);
  529. const uint w = width - pxToMove + i;
  530. for (uint h=0; h < h2L; ++h)
  531. {
  532. // -30dB
  533. //if (valueL < 0.032f)
  534. // continue;
  535. data[(h2 - h) * stride + w * 4 + 3] = 160;
  536. // -12dB
  537. if (valueL < 0.25f)
  538. {
  539. data[(h2 - h) * stride + w * 4 + 1] = 255;
  540. }
  541. // -3dB
  542. else if (valueL < 0.70f)
  543. {
  544. data[(h2 - h) * stride + w * 4 + 2] = 255;
  545. data[(h2 - h) * stride + w * 4 + 1] = 255;
  546. }
  547. else
  548. {
  549. data[(h2 - h) * stride + w * 4 + 2] = 255;
  550. }
  551. }
  552. for (uint h=0; h < h2R; ++h)
  553. {
  554. // -30dB
  555. //if (valueR < 0.032f)
  556. // continue;
  557. data[(h2 + h) * stride + w * 4 + 3] = 160;
  558. // -12dB
  559. if (valueR < 0.25f)
  560. {
  561. data[(h2 + h) * stride + w * 4 + 1] = 255;
  562. }
  563. // -3dB
  564. else if (valueR < 0.70f)
  565. {
  566. data[(h2 + h) * stride + w * 4 + 2] = 255;
  567. data[(h2 + h) * stride + w * 4 + 1] = 255;
  568. }
  569. else
  570. {
  571. data[(h2 + h) * stride + w * 4 + 2] = 255;
  572. }
  573. }
  574. }
  575. }
  576. fInlineDisplay.writtenValues = 0;
  577. fInlineDisplay.pending = InlineDisplayNotPending;
  578. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  579. }
  580. #endif
  581. // ----------------------------------------------------------------------------------------------------------------
  582. private:
  583. bool fLoopMode = true;
  584. #ifdef __MOD_DEVICES__
  585. bool fHostSync = false;
  586. #else
  587. bool fHostSync = true;
  588. #endif
  589. bool fEnabled = true;
  590. bool fDoProcess = false;
  591. bool fPendingFileRead = false;
  592. bool fPendingFileReload = false;
  593. AudioFileReader::QuadMode fQuadMode = AudioFileReader::kQuad1and2;
  594. uint32_t fInternalTransportFrame = 0;
  595. float fLastPosition = 0.f;
  596. float fReadableBufferFill = 0.f;
  597. float fVolume = 1.f;
  598. AudioFileReader fReader;
  599. CarlaString fFilename;
  600. float fPreviewData[108] = {};
  601. #ifndef __MOD_DEVICES__
  602. NativeMidiPrograms fPrograms;
  603. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  604. float lastValuesL[32] = {};
  605. float lastValuesR[32] = {};
  606. volatile PendingInlineDisplay pending = InlineDisplayNotPending;
  607. volatile uint8_t writtenValues = 0;
  608. InlineDisplay()
  609. : NativeInlineDisplayImageSurfaceCompat() {}
  610. ~InlineDisplay()
  611. {
  612. if (data != nullptr)
  613. {
  614. delete[] data;
  615. data = nullptr;
  616. }
  617. }
  618. CARLA_DECLARE_NON_COPYABLE(InlineDisplay)
  619. CARLA_PREVENT_HEAP_ALLOCATION
  620. } fInlineDisplay;
  621. #endif
  622. VolumeFilter fVolumeFilter;
  623. void loadFilename(const char* const filename)
  624. {
  625. CARLA_ASSERT(filename != nullptr);
  626. carla_stdout("AudioFilePlugin::loadFilename(\"%s\")", filename);
  627. fDoProcess = false;
  628. fReader.destroy();
  629. fFilename.clear();
  630. if (filename == nullptr || *filename == '\0')
  631. return;
  632. constexpr uint32_t kPreviewDataLen = sizeof(fPreviewData)/sizeof(float);
  633. if (fReader.loadFilename(filename, static_cast<uint32_t>(getSampleRate()), fQuadMode,
  634. kPreviewDataLen, fPreviewData))
  635. {
  636. fInternalTransportFrame = 0;
  637. fDoProcess = true;
  638. fFilename = filename;
  639. hostSendPreviewBufferData('f', kPreviewDataLen, fPreviewData);
  640. }
  641. }
  642. PluginClassEND(AudioFilePlugin)
  643. static const char* _get_buffer_port_name(NativePluginHandle, const uint32_t index, const bool isOutput)
  644. {
  645. if (!isOutput)
  646. return nullptr;
  647. switch (index)
  648. {
  649. case 0:
  650. return "output_1";
  651. case 1:
  652. return "output_2";
  653. case 2:
  654. return "Play status";
  655. }
  656. return nullptr;
  657. }
  658. static const NativePortRange* _get_buffer_port_range(NativePluginHandle, const uint32_t index, const bool isOutput)
  659. {
  660. if (!isOutput || index != 2)
  661. return nullptr;
  662. static NativePortRange npr = { 0.f, 10.f };
  663. return &npr;
  664. }
  665. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  666. };
  667. // --------------------------------------------------------------------------------------------------------------------
  668. CARLA_API_EXPORT
  669. void carla_register_native_plugin_audiofile();
  670. CARLA_API_EXPORT
  671. void carla_register_native_plugin_audiofile()
  672. {
  673. static const NativePluginDescriptor audiofileDesc = {
  674. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  675. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  676. #ifndef __MOD_DEVICES__
  677. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  678. #endif
  679. |NATIVE_PLUGIN_HAS_UI
  680. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  681. |NATIVE_PLUGIN_REQUESTS_IDLE
  682. |NATIVE_PLUGIN_USES_CONTROL_VOLTAGE
  683. |NATIVE_PLUGIN_USES_TIME),
  684. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  685. /* audioIns */ 0,
  686. /* audioOuts */ 2,
  687. /* midiIns */ 0,
  688. /* midiOuts */ 0,
  689. /* paramIns */ 1,
  690. /* paramOuts */ 0,
  691. /* name */ "Audio File",
  692. /* label */ "audiofile",
  693. /* maker */ "falkTX",
  694. /* copyright */ "GNU GPL v2+",
  695. AudioFilePlugin::_instantiate,
  696. AudioFilePlugin::_cleanup,
  697. AudioFilePlugin::_get_parameter_count,
  698. AudioFilePlugin::_get_parameter_info,
  699. AudioFilePlugin::_get_parameter_value,
  700. AudioFilePlugin::_get_midi_program_count,
  701. AudioFilePlugin::_get_midi_program_info,
  702. AudioFilePlugin::_set_parameter_value,
  703. AudioFilePlugin::_set_midi_program,
  704. AudioFilePlugin::_set_custom_data,
  705. AudioFilePlugin::_ui_show,
  706. AudioFilePlugin::_ui_idle,
  707. AudioFilePlugin::_ui_set_parameter_value,
  708. AudioFilePlugin::_ui_set_midi_program,
  709. AudioFilePlugin::_ui_set_custom_data,
  710. AudioFilePlugin::_activate,
  711. AudioFilePlugin::_deactivate,
  712. AudioFilePlugin::_process,
  713. AudioFilePlugin::_get_state,
  714. AudioFilePlugin::_set_state,
  715. AudioFilePlugin::_dispatcher,
  716. AudioFilePlugin::_render_inline_display,
  717. /* cvIns */ 0,
  718. /* cvOuts */ 1,
  719. AudioFilePlugin::_get_buffer_port_name,
  720. AudioFilePlugin::_get_buffer_port_range,
  721. /* ui_width */ 0,
  722. /* ui_height */ 0
  723. };
  724. carla_register_native_plugin(&audiofileDesc);
  725. }
  726. // --------------------------------------------------------------------------------------------------------------------