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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. bool needsIdleRequest = false;
  407. if (fReader.tickFrames(outBuffer, 0, frames, framePos, fLoopMode, isOffline()) && ! fPendingFileRead)
  408. {
  409. fPendingFileRead = true;
  410. needsIdleRequest = true;
  411. }
  412. fLastPosition = fReader.getLastPlayPosition() * 100.f;
  413. fReadableBufferFill = fReader.getReadableBufferFill() * 100.f;
  414. fVolumeFilter.processStereo(fVolume, outBuffer, frames);
  415. #ifndef __MOD_DEVICES__
  416. if (fInlineDisplay.writtenValues < 32)
  417. {
  418. fInlineDisplay.lastValuesL[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out1, frames);
  419. fInlineDisplay.lastValuesR[fInlineDisplay.writtenValues] = carla_findMaxNormalizedFloat(out2, frames);
  420. ++fInlineDisplay.writtenValues;
  421. }
  422. if (fInlineDisplay.pending == InlineDisplayNotPending)
  423. {
  424. needsIdleRequest = true;
  425. fInlineDisplay.pending = InlineDisplayNeedRequest;
  426. }
  427. #endif
  428. if (needsIdleRequest)
  429. hostRequestIdle();
  430. }
  431. // ----------------------------------------------------------------------------------------------------------------
  432. // Plugin UI calls
  433. void uiShow(const bool show) override
  434. {
  435. if (! show)
  436. return;
  437. if (const char* const filename = uiOpenFile(false, "Open Audio File", ""))
  438. uiCustomDataChanged("file", filename);
  439. uiClosed();
  440. }
  441. // ----------------------------------------------------------------------------------------------------------------
  442. // Plugin dispatcher calls
  443. void idle() override
  444. {
  445. #ifndef __MOD_DEVICES__
  446. NativePluginWithMidiPrograms<FileAudio>::idle();
  447. if (fInlineDisplay.pending == InlineDisplayNeedRequest)
  448. {
  449. fInlineDisplay.pending = InlineDisplayRequesting;
  450. hostQueueDrawInlineDisplay();
  451. }
  452. #endif
  453. if (fPendingFileReload)
  454. {
  455. fPendingFileReload = fPendingFileRead = false;
  456. if (char* const filename = fFilename.releaseBufferPointer())
  457. {
  458. loadFilename(filename);
  459. std::free(filename);
  460. }
  461. }
  462. else if (fPendingFileRead)
  463. {
  464. fPendingFileRead = false;
  465. fReader.readPoll();
  466. }
  467. }
  468. void sampleRateChanged(const double sampleRate) override
  469. {
  470. fVolumeFilter.setSampleRate(sampleRate);
  471. if (char* const filename = fFilename.releaseBufferPointer())
  472. {
  473. loadFilename(filename);
  474. std::free(filename);
  475. }
  476. }
  477. #ifndef __MOD_DEVICES__
  478. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t rwidth, const uint32_t height) override
  479. {
  480. CARLA_SAFE_ASSERT_RETURN(height > 4, nullptr);
  481. const uint32_t width = rwidth == height ? height * 4 : rwidth;
  482. /* NOTE the code is this function is not optimized, still learning my way through pixels...
  483. */
  484. const size_t stride = width * 4;
  485. const size_t dataSize = stride * height;
  486. const uint pxToMove = fDoProcess ? fInlineDisplay.writtenValues : 0;
  487. uchar* data = fInlineDisplay.data;
  488. if (fInlineDisplay.dataSize != dataSize || data == nullptr)
  489. {
  490. delete[] data;
  491. data = new uchar[dataSize];
  492. std::memset(data, 0, dataSize);
  493. fInlineDisplay.data = data;
  494. fInlineDisplay.dataSize = dataSize;
  495. }
  496. else if (pxToMove != 0)
  497. {
  498. // shift all previous values to the left
  499. for (uint w=0; w < width - pxToMove; ++w)
  500. for (uint h=0; h < height; ++h)
  501. std::memmove(&data[h * stride + w * 4], &data[h * stride + (w+pxToMove) * 4], 4);
  502. }
  503. fInlineDisplay.width = static_cast<int>(width);
  504. fInlineDisplay.height = static_cast<int>(height);
  505. fInlineDisplay.stride = static_cast<int>(stride);
  506. if (pxToMove != 0)
  507. {
  508. const uint h2 = height / 2;
  509. // clear current line
  510. for (uint w=width-pxToMove; w < width; ++w)
  511. for (uint h=0; h < height; ++h)
  512. memset(&data[h * stride + w * 4], 0, 4);
  513. // draw upper/left
  514. for (uint i=0; i < pxToMove && i < 32; ++i)
  515. {
  516. const float valueL = fInlineDisplay.lastValuesL[i];
  517. const float valueR = fInlineDisplay.lastValuesR[i];
  518. const uint h2L = static_cast<uint>(valueL * (float)h2);
  519. const uint h2R = static_cast<uint>(valueR * (float)h2);
  520. const uint w = width - pxToMove + i;
  521. for (uint h=0; h < h2L; ++h)
  522. {
  523. // -30dB
  524. //if (valueL < 0.032f)
  525. // continue;
  526. data[(h2 - h) * stride + w * 4 + 3] = 160;
  527. // -12dB
  528. if (valueL < 0.25f)
  529. {
  530. data[(h2 - h) * stride + w * 4 + 1] = 255;
  531. }
  532. // -3dB
  533. else if (valueL < 0.70f)
  534. {
  535. data[(h2 - h) * stride + w * 4 + 2] = 255;
  536. data[(h2 - h) * stride + w * 4 + 1] = 255;
  537. }
  538. else
  539. {
  540. data[(h2 - h) * stride + w * 4 + 2] = 255;
  541. }
  542. }
  543. for (uint h=0; h < h2R; ++h)
  544. {
  545. // -30dB
  546. //if (valueR < 0.032f)
  547. // continue;
  548. data[(h2 + h) * stride + w * 4 + 3] = 160;
  549. // -12dB
  550. if (valueR < 0.25f)
  551. {
  552. data[(h2 + h) * stride + w * 4 + 1] = 255;
  553. }
  554. // -3dB
  555. else if (valueR < 0.70f)
  556. {
  557. data[(h2 + h) * stride + w * 4 + 2] = 255;
  558. data[(h2 + h) * stride + w * 4 + 1] = 255;
  559. }
  560. else
  561. {
  562. data[(h2 + h) * stride + w * 4 + 2] = 255;
  563. }
  564. }
  565. }
  566. }
  567. fInlineDisplay.writtenValues = 0;
  568. fInlineDisplay.pending = InlineDisplayNotPending;
  569. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  570. }
  571. #endif
  572. // ----------------------------------------------------------------------------------------------------------------
  573. private:
  574. bool fLoopMode = true;
  575. #ifdef __MOD_DEVICES__
  576. bool fHostSync = false;
  577. #else
  578. bool fHostSync = true;
  579. #endif
  580. bool fEnabled = true;
  581. bool fDoProcess = false;
  582. bool fPendingFileRead = false;
  583. bool fPendingFileReload = false;
  584. AudioFileReader::QuadMode fQuadMode = AudioFileReader::kQuad1and2;
  585. uint32_t fInternalTransportFrame = 0;
  586. float fLastPosition = 0.f;
  587. float fReadableBufferFill = 0.f;
  588. float fVolume = 1.f;
  589. AudioFileReader fReader;
  590. CarlaString fFilename;
  591. float fPreviewData[108] = {};
  592. #ifndef __MOD_DEVICES__
  593. NativeMidiPrograms fPrograms;
  594. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  595. float lastValuesL[32] = {};
  596. float lastValuesR[32] = {};
  597. volatile PendingInlineDisplay pending = InlineDisplayNotPending;
  598. volatile uint8_t writtenValues = 0;
  599. InlineDisplay()
  600. : NativeInlineDisplayImageSurfaceCompat() {}
  601. ~InlineDisplay()
  602. {
  603. if (data != nullptr)
  604. {
  605. delete[] data;
  606. data = nullptr;
  607. }
  608. }
  609. CARLA_DECLARE_NON_COPYABLE(InlineDisplay)
  610. CARLA_PREVENT_HEAP_ALLOCATION
  611. } fInlineDisplay;
  612. #endif
  613. VolumeFilter fVolumeFilter;
  614. void loadFilename(const char* const filename)
  615. {
  616. CARLA_ASSERT(filename != nullptr);
  617. carla_stdout("AudioFilePlugin::loadFilename(\"%s\")", filename);
  618. fDoProcess = false;
  619. fReader.destroy();
  620. fFilename.clear();
  621. if (filename == nullptr || *filename == '\0')
  622. return;
  623. constexpr uint32_t kPreviewDataLen = sizeof(fPreviewData)/sizeof(float);
  624. if (fReader.loadFilename(filename, static_cast<uint32_t>(getSampleRate()), fQuadMode,
  625. kPreviewDataLen, fPreviewData))
  626. {
  627. fInternalTransportFrame = 0;
  628. fDoProcess = true;
  629. fFilename = filename;
  630. hostSendPreviewBufferData('f', kPreviewDataLen, fPreviewData);
  631. }
  632. }
  633. PluginClassEND(AudioFilePlugin)
  634. static const char* _get_buffer_port_name(NativePluginHandle, const uint32_t index, const bool isOutput)
  635. {
  636. if (!isOutput)
  637. return nullptr;
  638. switch (index)
  639. {
  640. case 0:
  641. return "output_1";
  642. case 1:
  643. return "output_2";
  644. case 2:
  645. return "Play status";
  646. }
  647. return nullptr;
  648. }
  649. static const NativePortRange* _get_buffer_port_range(NativePluginHandle, const uint32_t index, const bool isOutput)
  650. {
  651. if (!isOutput || index != 2)
  652. return nullptr;
  653. static NativePortRange npr = { 0.f, 10.f };
  654. return &npr;
  655. }
  656. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioFilePlugin)
  657. };
  658. // --------------------------------------------------------------------------------------------------------------------
  659. CARLA_API_EXPORT
  660. void carla_register_native_plugin_audiofile();
  661. CARLA_API_EXPORT
  662. void carla_register_native_plugin_audiofile()
  663. {
  664. static const NativePluginDescriptor audiofileDesc = {
  665. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  666. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  667. #ifndef __MOD_DEVICES__
  668. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  669. #endif
  670. |NATIVE_PLUGIN_HAS_UI
  671. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  672. |NATIVE_PLUGIN_REQUESTS_IDLE
  673. |NATIVE_PLUGIN_USES_CONTROL_VOLTAGE
  674. |NATIVE_PLUGIN_USES_TIME),
  675. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  676. /* audioIns */ 0,
  677. /* audioOuts */ 2,
  678. /* midiIns */ 0,
  679. /* midiOuts */ 0,
  680. /* paramIns */ 1,
  681. /* paramOuts */ 0,
  682. /* name */ "Audio File",
  683. /* label */ "audiofile",
  684. /* maker */ "falkTX",
  685. /* copyright */ "GNU GPL v2+",
  686. AudioFilePlugin::_instantiate,
  687. AudioFilePlugin::_cleanup,
  688. AudioFilePlugin::_get_parameter_count,
  689. AudioFilePlugin::_get_parameter_info,
  690. AudioFilePlugin::_get_parameter_value,
  691. AudioFilePlugin::_get_midi_program_count,
  692. AudioFilePlugin::_get_midi_program_info,
  693. AudioFilePlugin::_set_parameter_value,
  694. AudioFilePlugin::_set_midi_program,
  695. AudioFilePlugin::_set_custom_data,
  696. AudioFilePlugin::_ui_show,
  697. AudioFilePlugin::_ui_idle,
  698. AudioFilePlugin::_ui_set_parameter_value,
  699. AudioFilePlugin::_ui_set_midi_program,
  700. AudioFilePlugin::_ui_set_custom_data,
  701. AudioFilePlugin::_activate,
  702. AudioFilePlugin::_deactivate,
  703. AudioFilePlugin::_process,
  704. AudioFilePlugin::_get_state,
  705. AudioFilePlugin::_set_state,
  706. AudioFilePlugin::_dispatcher,
  707. AudioFilePlugin::_render_inline_display,
  708. /* cvIns */ 0,
  709. /* cvOuts */ 1,
  710. AudioFilePlugin::_get_buffer_port_name,
  711. AudioFilePlugin::_get_buffer_port_range,
  712. /* ui_width */ 0,
  713. /* ui_height */ 0
  714. };
  715. carla_register_native_plugin(&audiofileDesc);
  716. }
  717. // --------------------------------------------------------------------------------------------------------------------