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-base.hpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #ifndef AUDIO_BASE_HPP_INCLUDED
  18. #define AUDIO_BASE_HPP_INCLUDED
  19. #include "CarlaThread.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. extern "C" {
  22. #include "audio_decoder/ad.h"
  23. }
  24. typedef struct adinfo ADInfo;
  25. struct AudioFilePool {
  26. float* buffer[2];
  27. uint32_t numFrames;
  28. volatile uint64_t startFrame;
  29. #ifdef CARLA_PROPER_CPP11_SUPPORT
  30. AudioFilePool() noexcept
  31. : buffer{nullptr},
  32. numFrames(0),
  33. startFrame(0) {}
  34. #else
  35. AudioFilePool() noexcept
  36. : numFrames(0),
  37. startFrame(0)
  38. {
  39. buffer[0] = buffer[1] = nullptr;
  40. }
  41. #endif
  42. ~AudioFilePool()
  43. {
  44. destroy();
  45. }
  46. void create(const uint32_t desiredNumFrames)
  47. {
  48. CARLA_ASSERT(buffer[0] == nullptr);
  49. CARLA_ASSERT(buffer[1] == nullptr);
  50. CARLA_ASSERT(startFrame == 0);
  51. CARLA_ASSERT(numFrames == 0);
  52. numFrames = desiredNumFrames;
  53. buffer[0] = new float[numFrames];
  54. buffer[1] = new float[numFrames];
  55. reset();
  56. }
  57. void destroy() noexcept
  58. {
  59. if (buffer[0] != nullptr)
  60. {
  61. delete[] buffer[0];
  62. buffer[0] = nullptr;
  63. }
  64. if (buffer[1] != nullptr)
  65. {
  66. delete[] buffer[1];
  67. buffer[1] = nullptr;
  68. }
  69. startFrame = 0;
  70. numFrames = 0;
  71. }
  72. void reset() noexcept
  73. {
  74. startFrame = 0;
  75. if (numFrames != 0)
  76. {
  77. carla_zeroFloats(buffer[0], numFrames);
  78. carla_zeroFloats(buffer[1], numFrames);
  79. }
  80. }
  81. CARLA_DECLARE_NON_COPY_STRUCT(AudioFilePool)
  82. };
  83. class AudioFileThread : public CarlaThread
  84. {
  85. public:
  86. AudioFileThread()
  87. : CarlaThread("AudioFileThread"),
  88. fEntireFileLoaded(false),
  89. fLoopingMode(true),
  90. fNeedsFrame(0),
  91. fNeedsRead(false),
  92. fQuitNow(true),
  93. fFilePtr(nullptr),
  94. fFileNfo(),
  95. fNumFileFrames(0),
  96. fPollTempData(nullptr),
  97. fPollTempSize(0),
  98. fPool(),
  99. fMutex(),
  100. fSignal()
  101. {
  102. static bool adInitiated = false;
  103. if (! adInitiated)
  104. {
  105. ad_init();
  106. adInitiated = true;
  107. }
  108. ad_clear_nfo(&fFileNfo);
  109. }
  110. ~AudioFileThread() override
  111. {
  112. CARLA_ASSERT(fQuitNow);
  113. CARLA_ASSERT(! isThreadRunning());
  114. cleanup();
  115. }
  116. void cleanup()
  117. {
  118. fEntireFileLoaded = false;
  119. if (fFilePtr != nullptr)
  120. {
  121. ad_close(fFilePtr);
  122. fFilePtr = nullptr;
  123. }
  124. if (fPollTempData != nullptr)
  125. {
  126. delete[] fPollTempData;
  127. fPollTempData = nullptr;
  128. fPollTempSize = 0;
  129. }
  130. fPool.destroy();
  131. }
  132. void startNow()
  133. {
  134. if (fPollTempData == nullptr)
  135. return;
  136. fNeedsFrame = 0;
  137. fNeedsRead = false;
  138. fQuitNow = false;
  139. startThread();
  140. }
  141. void stopNow()
  142. {
  143. fNeedsFrame = 0;
  144. fNeedsRead = false;
  145. fQuitNow = true;
  146. fSignal.signal();
  147. stopThread(1000);
  148. const CarlaMutexLocker cml(fMutex);
  149. fPool.reset();
  150. }
  151. bool isEntireFileLoaded() const noexcept
  152. {
  153. return fEntireFileLoaded;
  154. }
  155. uint32_t getMaxFrame() const noexcept
  156. {
  157. return fNumFileFrames;
  158. }
  159. uint64_t getPoolStartFrame() const noexcept
  160. {
  161. return fPool.startFrame;
  162. }
  163. uint32_t getPoolNumFrames() const noexcept
  164. {
  165. return fPool.numFrames;
  166. }
  167. void setLoopingMode(const bool on) noexcept
  168. {
  169. fLoopingMode = on;
  170. }
  171. void setNeedsRead(const uint64_t frame) noexcept
  172. {
  173. if (fEntireFileLoaded)
  174. return;
  175. fNeedsFrame = frame;
  176. fNeedsRead = true;
  177. fSignal.signal();
  178. }
  179. bool loadFilename(const char* const filename, const uint32_t sampleRate)
  180. {
  181. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), false);
  182. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && *filename != '\0', false);
  183. cleanup();
  184. ad_clear_nfo(&fFileNfo);
  185. // open new
  186. fFilePtr = ad_open(filename, &fFileNfo);
  187. if (fFilePtr == nullptr)
  188. return false;
  189. ad_dump_nfo(99, &fFileNfo);
  190. // Fix for misinformation using libsndfile
  191. if (fFileNfo.frames % fFileNfo.channels)
  192. --fFileNfo.frames;
  193. if (fFileNfo.frames <= 0)
  194. carla_stderr("L: filename \"%s\" has 0 frames", filename);
  195. if ((fFileNfo.channels == 1 || fFileNfo.channels == 2) && fFileNfo.frames > 0)
  196. {
  197. // valid
  198. const uint32_t fileNumFrames = static_cast<uint32_t>(fFileNfo.frames);
  199. const uint32_t poolNumFrames = sampleRate * 5;
  200. if (fileNumFrames <= poolNumFrames)
  201. {
  202. // entire file fits in a small pool, lets read it now
  203. fPool.create(fileNumFrames);
  204. readEntireFileIntoPool();
  205. ad_close(fFilePtr);
  206. fFilePtr = nullptr;
  207. }
  208. else
  209. {
  210. // file is too big for our audio pool, we need an extra buffer
  211. fPool.create(poolNumFrames);
  212. const size_t pollTempSize = poolNumFrames * fFileNfo.channels;
  213. try {
  214. fPollTempData = new float[pollTempSize];
  215. } catch (...) {
  216. ad_close(fFilePtr);
  217. fFilePtr = nullptr;
  218. return false;
  219. }
  220. fPollTempSize = pollTempSize;
  221. }
  222. fNumFileFrames = fileNumFrames;
  223. readPoll();
  224. return true;
  225. }
  226. else
  227. {
  228. // invalid
  229. ad_clear_nfo(&fFileNfo);
  230. ad_close(fFilePtr);
  231. fFilePtr = nullptr;
  232. return false;
  233. }
  234. }
  235. void putAllData(AudioFilePool& pool)
  236. {
  237. CARLA_SAFE_ASSERT_RETURN(pool.numFrames == fPool.numFrames,);
  238. const CarlaMutexLocker cml(fMutex);
  239. pool.startFrame = fPool.startFrame;
  240. carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.numFrames);
  241. carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.numFrames);
  242. }
  243. bool tryPutData(float* const out1, float* const out2, uint64_t framePos, const uint32_t frames)
  244. {
  245. CARLA_SAFE_ASSERT_RETURN(fPool.numFrames != 0, false);
  246. if (framePos >= fNumFileFrames)
  247. {
  248. if (fLoopingMode)
  249. framePos %= fNumFileFrames;
  250. else
  251. return false;
  252. }
  253. uint64_t frameDiff;
  254. const uint64_t numFramesNearEnd = fPool.numFrames*3/4;
  255. #if 1
  256. const CarlaMutexLocker cml(fMutex);
  257. #else
  258. const CarlaMutexTryLocker cmtl(fMutex);
  259. if (! cmtl.wasLocked())
  260. {
  261. for (int i=0; i<5; ++i)
  262. {
  263. pthread_yield();
  264. if (cmtl.tryAgain())
  265. break;
  266. if (i == 4)
  267. return false;
  268. }
  269. }
  270. #endif
  271. if (framePos < fPool.startFrame)
  272. {
  273. if (fPool.startFrame + fPool.numFrames <= fNumFileFrames)
  274. {
  275. setNeedsRead(framePos);
  276. return false;
  277. }
  278. frameDiff = framePos + (fNumFileFrames - fPool.startFrame);
  279. if (frameDiff + frames >= fPool.numFrames)
  280. {
  281. setNeedsRead(framePos);
  282. return false;
  283. }
  284. carla_copyFloats(out1, fPool.buffer[0] + frameDiff, frames);
  285. carla_copyFloats(out2, fPool.buffer[1] + frameDiff, frames);
  286. }
  287. else
  288. {
  289. frameDiff = framePos - fPool.startFrame;
  290. if (frameDiff + frames >= fPool.numFrames)
  291. {
  292. setNeedsRead(framePos);
  293. return false;
  294. }
  295. carla_copyFloats(out1, fPool.buffer[0] + frameDiff, frames);
  296. carla_copyFloats(out2, fPool.buffer[1] + frameDiff, frames);
  297. }
  298. if (frameDiff > numFramesNearEnd)
  299. setNeedsRead(framePos + frames);
  300. return true;
  301. }
  302. void readEntireFileIntoPool()
  303. {
  304. CARLA_SAFE_ASSERT_RETURN(fPool.numFrames > 0,);
  305. const uint numChannels = fFileNfo.channels;
  306. const size_t bufferSize = fPool.numFrames * numChannels;
  307. float* const buffer = (float*)std::malloc(bufferSize*sizeof(float));
  308. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  309. carla_zeroFloats(buffer, bufferSize);
  310. ad_seek(fFilePtr, 0);
  311. ssize_t rv = ad_read(fFilePtr, buffer, bufferSize);
  312. CARLA_SAFE_ASSERT_INT2_RETURN(rv == static_cast<ssize_t>(bufferSize),
  313. static_cast<int>(rv),
  314. static_cast<int>(bufferSize),
  315. std::free(buffer));
  316. {
  317. // lock, and put data asap
  318. const CarlaMutexLocker cml(fMutex);
  319. for (ssize_t i=0, j=0; j < rv; ++j)
  320. {
  321. if (numChannels == 1)
  322. {
  323. fPool.buffer[0][i] = buffer[j];
  324. fPool.buffer[1][i] = buffer[j];
  325. ++i;
  326. }
  327. else
  328. {
  329. if (j % 2 == 0)
  330. {
  331. fPool.buffer[0][i] = buffer[j];
  332. }
  333. else
  334. {
  335. fPool.buffer[1][i] = buffer[j];
  336. ++i;
  337. }
  338. }
  339. }
  340. }
  341. std::free(buffer);
  342. fEntireFileLoaded = true;
  343. }
  344. void readPoll()
  345. {
  346. if (fNumFileFrames == 0 || fFileNfo.channels == 0 || fFilePtr == nullptr)
  347. {
  348. carla_debug("R: no song loaded");
  349. fNeedsFrame = 0;
  350. fNeedsRead = false;
  351. return;
  352. }
  353. if (fPollTempData == nullptr)
  354. {
  355. carla_debug("R: nothing to poll");
  356. fNeedsFrame = 0;
  357. fNeedsRead = false;
  358. return;
  359. }
  360. uint64_t lastFrame = fNeedsFrame;
  361. int64_t readFrameCheck;
  362. if (lastFrame >= fNumFileFrames)
  363. {
  364. if (fLoopingMode)
  365. {
  366. const uint64_t readFrameCheckLoop = lastFrame % fNumFileFrames;
  367. CARLA_SAFE_ASSERT_RETURN(readFrameCheckLoop < INT32_MAX,);
  368. carla_debug("R: transport out of bounds for loop");
  369. readFrameCheck = static_cast<int64_t>(readFrameCheckLoop);
  370. }
  371. else
  372. {
  373. carla_debug("R: transport out of bounds");
  374. fNeedsFrame = 0;
  375. fNeedsRead = false;
  376. return;
  377. }
  378. }
  379. else
  380. {
  381. CARLA_SAFE_ASSERT_RETURN(lastFrame < INT32_MAX,);
  382. readFrameCheck = static_cast<int64_t>(lastFrame);
  383. }
  384. const int64_t readFrame = readFrameCheck;
  385. // temp data buffer
  386. carla_zeroFloats(fPollTempData, fPollTempSize);
  387. {
  388. #if 0
  389. const int32_t sampleRate = 44100;
  390. carla_debug("R: poll data - reading at frame %li, time %li:%02li, lastFrame %li",
  391. readFrame, readFrame/sampleRate/60, (readFrame/sampleRate) % 60, lastFrame);
  392. #endif
  393. ad_seek(fFilePtr, readFrame);
  394. size_t i = 0;
  395. ssize_t j = 0;
  396. ssize_t rv = ad_read(fFilePtr, fPollTempData, fPollTempSize);
  397. if (rv < 0)
  398. {
  399. carla_stderr("R: ad_read failed");
  400. fNeedsFrame = 0;
  401. fNeedsRead = false;
  402. return;
  403. }
  404. const size_t urv = static_cast<size_t>(rv);
  405. // see if we can read more
  406. if (readFrame + rv >= static_cast<ssize_t>(fFileNfo.frames) && urv < fPollTempSize)
  407. {
  408. carla_debug("R: from start");
  409. ad_seek(fFilePtr, 0);
  410. rv += ad_read(fFilePtr, fPollTempData+urv, fPollTempSize-urv);
  411. }
  412. carla_debug("R: reading %li frames at frame %lu", rv, readFrameCheck);
  413. // local copy
  414. const uint32_t poolNumFrame = fPool.numFrames;
  415. const int64_t fileFrames = fFileNfo.frames;
  416. const bool isMonoFile = fFileNfo.channels == 1;
  417. float* const pbuffer0 = fPool.buffer[0];
  418. float* const pbuffer1 = fPool.buffer[1];
  419. const float* const tmpbuf = fPollTempData;
  420. // lock, and put data asap
  421. const CarlaMutexLocker cml(fMutex);
  422. do {
  423. for (; i < poolNumFrame && j < rv; ++j)
  424. {
  425. if (isMonoFile)
  426. {
  427. pbuffer0[i] = pbuffer1[i] = tmpbuf[j];
  428. i++;
  429. }
  430. else
  431. {
  432. if (j % 2 == 0)
  433. {
  434. pbuffer0[i] = tmpbuf[j];
  435. }
  436. else
  437. {
  438. pbuffer1[i] = tmpbuf[j];
  439. ++i;
  440. }
  441. }
  442. }
  443. if (i >= poolNumFrame) {
  444. break;
  445. }
  446. if (rv == fileFrames)
  447. {
  448. // full file read
  449. j = 0;
  450. carla_debug("R: full file was read, filling buffers again");
  451. }
  452. else
  453. {
  454. carla_debug("read break, not enough space");
  455. carla_zeroFloats(pbuffer0, poolNumFrame - i);
  456. carla_zeroFloats(pbuffer1, poolNumFrame - i);
  457. break;
  458. }
  459. } while (i < poolNumFrame);
  460. fPool.startFrame = static_cast<uint64_t>(readFrame);
  461. }
  462. fNeedsRead = false;
  463. }
  464. protected:
  465. void run() override
  466. {
  467. while (! fQuitNow)
  468. {
  469. if (fNeedsRead)
  470. readPoll();
  471. if (fQuitNow)
  472. break;
  473. fSignal.wait();
  474. }
  475. }
  476. private:
  477. bool fEntireFileLoaded;
  478. bool fLoopingMode;
  479. volatile uint64_t fNeedsFrame;
  480. volatile bool fNeedsRead;
  481. volatile bool fQuitNow;
  482. void* fFilePtr;
  483. ADInfo fFileNfo;
  484. uint32_t fNumFileFrames;
  485. float* fPollTempData;
  486. size_t fPollTempSize;
  487. AudioFilePool fPool;
  488. CarlaMutex fMutex;
  489. CarlaSignal fSignal;
  490. CARLA_DECLARE_NON_COPY_STRUCT(AudioFileThread)
  491. };
  492. #endif // AUDIO_BASE_HPP_INCLUDED