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.

740 lines
21KB

  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. #include "water/threads/ScopedLock.h"
  25. #include "water/threads/SpinLock.h"
  26. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  27. # pragma GCC diagnostic push
  28. # pragma GCC diagnostic ignored "-Weffc++"
  29. #endif
  30. #include "zita-resampler/resampler.h"
  31. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  32. # pragma GCC diagnostic pop
  33. #endif
  34. #ifdef CARLA_OS_WIN
  35. # include <windows.h>
  36. # define CARLA_MLOCK(ptr, size) VirtualLock((ptr), (size))
  37. #else
  38. # include <sys/mman.h>
  39. # define CARLA_MLOCK(ptr, size) mlock((ptr), (size))
  40. #endif
  41. typedef struct adinfo ADInfo;
  42. struct AudioFilePool {
  43. float* buffer[2];
  44. float* tmpbuf[2];
  45. uint32_t numFrames;
  46. volatile uint64_t startFrame;
  47. #ifdef CARLA_PROPER_CPP11_SUPPORT
  48. AudioFilePool() noexcept
  49. : buffer{nullptr},
  50. tmpbuf{nullptr},
  51. numFrames(0),
  52. startFrame(0) {}
  53. #else
  54. AudioFilePool() noexcept
  55. : numFrames(0),
  56. startFrame(0)
  57. {
  58. buffer[0] = buffer[1] = nullptr;
  59. tmpbuf[0] = tmpbuf[1] = nullptr;
  60. }
  61. #endif
  62. ~AudioFilePool()
  63. {
  64. destroy();
  65. }
  66. void create(const uint32_t desiredNumFrames, const bool withTempBuffers)
  67. {
  68. CARLA_ASSERT(buffer[0] == nullptr);
  69. CARLA_ASSERT(buffer[1] == nullptr);
  70. CARLA_ASSERT(tmpbuf[0] == nullptr);
  71. CARLA_ASSERT(tmpbuf[1] == nullptr);
  72. CARLA_ASSERT(startFrame == 0);
  73. CARLA_ASSERT(numFrames == 0);
  74. numFrames = desiredNumFrames;
  75. buffer[0] = new float[numFrames];
  76. buffer[1] = new float[numFrames];
  77. CARLA_MLOCK(buffer[0], sizeof(float)*numFrames);
  78. CARLA_MLOCK(buffer[1], sizeof(float)*numFrames);
  79. if (withTempBuffers)
  80. {
  81. tmpbuf[0] = new float[numFrames];
  82. tmpbuf[1] = new float[numFrames];
  83. CARLA_MLOCK(tmpbuf[0], sizeof(float)*numFrames);
  84. CARLA_MLOCK(tmpbuf[1], sizeof(float)*numFrames);
  85. }
  86. reset();
  87. }
  88. void destroy() noexcept
  89. {
  90. if (buffer[0] != nullptr)
  91. {
  92. delete[] buffer[0];
  93. buffer[0] = nullptr;
  94. }
  95. if (buffer[1] != nullptr)
  96. {
  97. delete[] buffer[1];
  98. buffer[1] = nullptr;
  99. }
  100. if (tmpbuf[0] != nullptr)
  101. {
  102. delete[] tmpbuf[0];
  103. tmpbuf[0] = nullptr;
  104. }
  105. if (tmpbuf[1] != nullptr)
  106. {
  107. delete[] tmpbuf[1];
  108. tmpbuf[1] = nullptr;
  109. }
  110. startFrame = 0;
  111. numFrames = 0;
  112. }
  113. void reset() noexcept
  114. {
  115. startFrame = 0;
  116. if (numFrames != 0)
  117. {
  118. carla_zeroFloats(buffer[0], numFrames);
  119. carla_zeroFloats(buffer[1], numFrames);
  120. if (tmpbuf[0] != nullptr)
  121. carla_zeroFloats(tmpbuf[0], numFrames);
  122. if (tmpbuf[1] != nullptr)
  123. carla_zeroFloats(tmpbuf[1], numFrames);
  124. }
  125. }
  126. CARLA_DECLARE_NON_COPY_STRUCT(AudioFilePool)
  127. };
  128. class AudioFileReader
  129. {
  130. public:
  131. AudioFileReader()
  132. : fEntireFileLoaded(false),
  133. fLoopingMode(true),
  134. fNeedsFrame(0),
  135. fNeedsRead(false),
  136. fFilePtr(nullptr),
  137. fFileNfo(),
  138. fMaxFrame(0),
  139. fResampleRatio(0.0),
  140. fPollTempData(nullptr),
  141. fPollTempSize(0),
  142. fResampleTempData(nullptr),
  143. fResampleTempSize(0),
  144. fPool(),
  145. fResampler(),
  146. fPoolMutex(),
  147. fReaderMutex()
  148. {
  149. static bool adInitiated = false;
  150. if (! adInitiated)
  151. {
  152. ad_init();
  153. adInitiated = true;
  154. }
  155. ad_clear_nfo(&fFileNfo);
  156. }
  157. ~AudioFileReader()
  158. {
  159. cleanup();
  160. }
  161. void cleanup()
  162. {
  163. fEntireFileLoaded = false;
  164. if (fFilePtr != nullptr)
  165. {
  166. ad_close(fFilePtr);
  167. fFilePtr = nullptr;
  168. }
  169. if (fPollTempData != nullptr)
  170. {
  171. delete[] fPollTempData;
  172. fPollTempData = nullptr;
  173. fPollTempSize = 0;
  174. }
  175. if (fResampleTempData != nullptr)
  176. {
  177. delete[] fResampleTempData;
  178. fResampleTempData = nullptr;
  179. fResampleTempSize = 0;
  180. }
  181. fPool.destroy();
  182. }
  183. void reset()
  184. {
  185. const water::GenericScopedLock<water::SpinLock> gsl(fPoolMutex);
  186. const CarlaMutexLocker cml(fReaderMutex);
  187. fNeedsFrame = 0;
  188. fNeedsRead = false;
  189. fPool.reset();
  190. }
  191. bool isEntireFileLoaded() const noexcept
  192. {
  193. return fEntireFileLoaded;
  194. }
  195. uint32_t getMaxFrame() const noexcept
  196. {
  197. return fMaxFrame;
  198. }
  199. uint64_t getPoolStartFrame() const noexcept
  200. {
  201. return fPool.startFrame;
  202. }
  203. uint32_t getPoolNumFrames() const noexcept
  204. {
  205. return fPool.numFrames;
  206. }
  207. ADInfo getFileInfo() const noexcept
  208. {
  209. return fFileNfo;
  210. }
  211. void setLoopingMode(const bool on) noexcept
  212. {
  213. fLoopingMode = on;
  214. }
  215. void setNeedsRead(const uint64_t frame) noexcept
  216. {
  217. if (fEntireFileLoaded)
  218. return;
  219. fNeedsFrame = frame;
  220. fNeedsRead = true;
  221. }
  222. bool loadFilename(const char* const filename, const uint32_t sampleRate)
  223. {
  224. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && *filename != '\0', false);
  225. const CarlaMutexLocker cml(fReaderMutex);
  226. cleanup();
  227. ad_clear_nfo(&fFileNfo);
  228. // open new
  229. fFilePtr = ad_open(filename, &fFileNfo);
  230. if (fFilePtr == nullptr)
  231. return false;
  232. ad_dump_nfo(99, &fFileNfo);
  233. // Fix for misinformation using libsndfile
  234. if (fFileNfo.frames % fFileNfo.channels)
  235. --fFileNfo.frames;
  236. if (fFileNfo.frames <= 0)
  237. carla_stderr("L: filename \"%s\" has 0 frames", filename);
  238. if ((fFileNfo.channels == 1 || fFileNfo.channels == 2) && fFileNfo.frames > 0)
  239. {
  240. // valid
  241. const uint32_t fileNumFrames = static_cast<uint32_t>(fFileNfo.frames);
  242. const uint32_t maxPoolNumFrames = sampleRate * 10;
  243. const bool needsResample = fFileNfo.sample_rate != sampleRate;
  244. if (needsResample)
  245. {
  246. if (! fResampler.setup(fFileNfo.sample_rate, sampleRate, fFileNfo.channels, 32))
  247. {
  248. ad_clear_nfo(&fFileNfo);
  249. ad_close(fFilePtr);
  250. fFilePtr = nullptr;
  251. carla_stderr2("loadFilename error, resampler setup failed");
  252. return false;
  253. }
  254. fResampleRatio = static_cast<double>(sampleRate) / static_cast<double>(fFileNfo.sample_rate);
  255. }
  256. else
  257. {
  258. fResampler.clear();
  259. fResampleRatio = 0.0;
  260. }
  261. if (fileNumFrames <= maxPoolNumFrames)
  262. {
  263. // entire file fits in a small pool, lets read it now
  264. fPool.create(needsResample ? static_cast<uint32_t>(static_cast<double>(fileNumFrames) * fResampleRatio + 0.5)
  265. : fileNumFrames, false);
  266. readEntireFileIntoPool(needsResample);
  267. ad_close(fFilePtr);
  268. fFilePtr = nullptr;
  269. }
  270. else
  271. {
  272. // file is too big for our audio pool, we need an extra buffer
  273. const uint32_t poolNumFrames = sampleRate * 1;
  274. const uint pollTempSize = poolNumFrames * fFileNfo.channels;
  275. uint resampleTempSize = 0;
  276. fPool.create(poolNumFrames, true);
  277. try {
  278. fPollTempData = new float[pollTempSize];
  279. } catch (...) {
  280. ad_clear_nfo(&fFileNfo);
  281. ad_close(fFilePtr);
  282. fFilePtr = nullptr;
  283. carla_stderr2("loadFilename error, out of memory");
  284. return false;
  285. }
  286. CARLA_MLOCK(fPollTempData, sizeof(float)*pollTempSize);
  287. if (needsResample)
  288. {
  289. resampleTempSize = static_cast<uint32_t>(static_cast<double>(poolNumFrames) * fResampleRatio + 0.5);
  290. resampleTempSize *= fFileNfo.channels;
  291. try {
  292. fResampleTempData = new float[resampleTempSize];
  293. } catch (...) {
  294. delete[] fPollTempData;
  295. fPollTempData = nullptr;
  296. ad_clear_nfo(&fFileNfo);
  297. ad_close(fFilePtr);
  298. fFilePtr = nullptr;
  299. carla_stderr2("loadFilename error, out of memory");
  300. return false;
  301. }
  302. CARLA_MLOCK(fResampleTempData, sizeof(float)*resampleTempSize);
  303. }
  304. fPollTempSize = pollTempSize;
  305. fResampleTempSize = resampleTempSize;
  306. }
  307. fMaxFrame = carla_isNotZero(fResampleRatio)
  308. ? static_cast<uint32_t>(static_cast<double>(fileNumFrames) * fResampleRatio + 0.5)
  309. : fileNumFrames;
  310. fNeedsRead = true;
  311. return true;
  312. }
  313. else
  314. {
  315. // invalid
  316. ad_clear_nfo(&fFileNfo);
  317. ad_close(fFilePtr);
  318. fFilePtr = nullptr;
  319. return false;
  320. }
  321. }
  322. void putAllData(AudioFilePool& pool)
  323. {
  324. CARLA_SAFE_ASSERT_RETURN(pool.numFrames == fPool.numFrames,);
  325. const water::GenericScopedLock<water::SpinLock> gsl(fPoolMutex);
  326. pool.startFrame = fPool.startFrame;
  327. carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.numFrames);
  328. carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.numFrames);
  329. }
  330. bool tryPutData(float* const out1,
  331. float* const out2,
  332. uint64_t framePos,
  333. const uint32_t frames,
  334. const bool isOffline,
  335. bool& needsRead)
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(fPool.numFrames != 0, false);
  338. if (framePos >= fMaxFrame)
  339. {
  340. if (fLoopingMode)
  341. framePos %= fMaxFrame;
  342. else
  343. return false;
  344. }
  345. uint64_t frameDiff;
  346. const uint64_t numFramesNearEnd = fPool.numFrames*3/5;
  347. const water::GenericScopedLock<water::SpinLock> gsl(fPoolMutex);
  348. if (framePos < fPool.startFrame)
  349. {
  350. if (fPool.startFrame + fPool.numFrames <= fMaxFrame)
  351. {
  352. needsRead = true;
  353. setNeedsRead(framePos);
  354. return false;
  355. }
  356. frameDiff = framePos + (fMaxFrame - fPool.startFrame);
  357. if (frameDiff + frames >= fPool.numFrames)
  358. {
  359. needsRead = true;
  360. setNeedsRead(framePos);
  361. return false;
  362. }
  363. carla_copyFloats(out1, fPool.buffer[0] + frameDiff, frames);
  364. carla_copyFloats(out2, fPool.buffer[1] + frameDiff, frames);
  365. }
  366. else
  367. {
  368. frameDiff = framePos - fPool.startFrame;
  369. if (frameDiff + frames >= fPool.numFrames)
  370. {
  371. needsRead = true;
  372. setNeedsRead(framePos);
  373. return false;
  374. }
  375. carla_copyFloats(out1, fPool.buffer[0] + frameDiff, frames);
  376. carla_copyFloats(out2, fPool.buffer[1] + frameDiff, frames);
  377. }
  378. if (frameDiff > numFramesNearEnd)
  379. {
  380. needsRead = true;
  381. setNeedsRead(framePos + (isOffline ? 0 : frames));
  382. }
  383. return true;
  384. }
  385. void readEntireFileIntoPool(const bool needsResample)
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fPool.numFrames > 0,);
  388. const uint numChannels = fFileNfo.channels;
  389. const uint numFrames = static_cast<uint>(fFileNfo.frames);
  390. uint bufferSize = numFrames * numChannels;
  391. float* const buffer = (float*)std::malloc(bufferSize * sizeof(float));
  392. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  393. carla_zeroFloats(buffer, bufferSize);
  394. ad_seek(fFilePtr, 0);
  395. ssize_t rv = ad_read(fFilePtr, buffer, bufferSize);
  396. CARLA_SAFE_ASSERT_INT2_RETURN(rv == static_cast<ssize_t>(bufferSize),
  397. static_cast<int>(rv),
  398. static_cast<int>(bufferSize),
  399. std::free(buffer));
  400. float* rbuffer;
  401. if (needsResample)
  402. {
  403. bufferSize = fPool.numFrames * numChannels;
  404. rbuffer = (float*)std::malloc(bufferSize * sizeof(float));
  405. CARLA_SAFE_ASSERT_RETURN(rbuffer != nullptr, std::free(buffer););
  406. carla_zeroFloats(rbuffer, bufferSize);
  407. rv = static_cast<ssize_t>(bufferSize);
  408. fResampler.inp_count = bufferSize / numChannels;
  409. fResampler.out_count = fPool.numFrames;
  410. fResampler.inp_data = buffer;
  411. fResampler.out_data = rbuffer;
  412. fResampler.process();
  413. CARLA_SAFE_ASSERT_INT(fResampler.inp_count <= 1, fResampler.inp_count);
  414. }
  415. else
  416. {
  417. rbuffer = buffer;
  418. }
  419. {
  420. // lock, and put data asap
  421. const water::GenericScopedLock<water::SpinLock> gsl(fPoolMutex);
  422. if (numChannels == 1)
  423. {
  424. for (ssize_t i=0, j=0; j < rv; ++i, ++j)
  425. fPool.buffer[0][i] = fPool.buffer[1][i] = rbuffer[j];
  426. }
  427. else
  428. {
  429. for (ssize_t i=0, j=0; j < rv; ++j)
  430. {
  431. if (j % 2 == 0)
  432. {
  433. fPool.buffer[0][i] = rbuffer[j];
  434. }
  435. else
  436. {
  437. fPool.buffer[1][i] = rbuffer[j];
  438. ++i;
  439. }
  440. }
  441. }
  442. }
  443. if (rbuffer != buffer)
  444. std::free(rbuffer);
  445. std::free(buffer);
  446. fEntireFileLoaded = true;
  447. }
  448. void readPoll()
  449. {
  450. const CarlaMutexLocker cml(fReaderMutex);
  451. if (fMaxFrame == 0 || fFileNfo.channels == 0 || fFilePtr == nullptr)
  452. {
  453. carla_debug("R: no song loaded");
  454. fNeedsFrame = 0;
  455. fNeedsRead = false;
  456. return;
  457. }
  458. if (fPollTempData == nullptr)
  459. {
  460. carla_debug("R: nothing to poll");
  461. fNeedsFrame = 0;
  462. fNeedsRead = false;
  463. return;
  464. }
  465. uint64_t lastFrame = fNeedsFrame;
  466. int64_t readFrameCheck;
  467. if (lastFrame >= fMaxFrame)
  468. {
  469. if (fLoopingMode)
  470. {
  471. const uint64_t readFrameCheckLoop = lastFrame % fMaxFrame;
  472. CARLA_SAFE_ASSERT_RETURN(readFrameCheckLoop < INT32_MAX,);
  473. carla_debug("R: transport out of bounds for loop");
  474. readFrameCheck = static_cast<int64_t>(readFrameCheckLoop);
  475. }
  476. else
  477. {
  478. carla_debug("R: transport out of bounds");
  479. fNeedsFrame = 0;
  480. fNeedsRead = false;
  481. return;
  482. }
  483. }
  484. else
  485. {
  486. CARLA_SAFE_ASSERT_RETURN(lastFrame < INT32_MAX,);
  487. readFrameCheck = static_cast<int64_t>(lastFrame);
  488. }
  489. const int64_t readFrame = readFrameCheck;
  490. // temp data buffer
  491. carla_zeroFloats(fPollTempData, fPollTempSize);
  492. {
  493. #if 0
  494. const int32_t sampleRate = 44100;
  495. carla_debug("R: poll data - reading at frame %li, time %li:%02li, lastFrame %li",
  496. readFrame, readFrame/sampleRate/60, (readFrame/sampleRate) % 60, lastFrame);
  497. #endif
  498. const int64_t readFrameReal = carla_isNotZero(fResampleRatio)
  499. ? static_cast<int64_t>(static_cast<double>(readFrame) / fResampleRatio + 0.5)
  500. : readFrame;
  501. ad_seek(fFilePtr, readFrameReal);
  502. size_t i = 0;
  503. ssize_t j = 0;
  504. ssize_t rv = ad_read(fFilePtr, fPollTempData, fPollTempSize);
  505. if (rv < 0)
  506. {
  507. carla_stderr("R: ad_read1 failed");
  508. fNeedsFrame = 0;
  509. fNeedsRead = false;
  510. return;
  511. }
  512. const size_t urv = static_cast<size_t>(rv);
  513. // see if we can read more
  514. if (readFrameReal + rv >= static_cast<ssize_t>(fFileNfo.frames) && urv < fPollTempSize)
  515. {
  516. carla_debug("R: from start");
  517. ad_seek(fFilePtr, 0);
  518. j = ad_read(fFilePtr, fPollTempData+urv, fPollTempSize-urv);
  519. if (j < 0)
  520. {
  521. carla_stderr("R: ad_read2 failed");
  522. fNeedsFrame = 0;
  523. fNeedsRead = false;
  524. return;
  525. }
  526. rv += j;
  527. }
  528. carla_debug("R: reading %li frames at frame %lu", rv, readFrameCheck);
  529. // local copy
  530. const uint32_t poolNumFrames = fPool.numFrames;
  531. float* const pbuffer0 = fPool.tmpbuf[0];
  532. float* const pbuffer1 = fPool.tmpbuf[1];
  533. const float* tmpbuf = fPollTempData;
  534. // resample as needed
  535. if (fResampleTempSize != 0)
  536. {
  537. tmpbuf = fResampleTempData;
  538. fResampler.inp_count = static_cast<uint>(rv / fFileNfo.channels);
  539. fResampler.out_count = fResampleTempSize / fFileNfo.channels;
  540. fResampler.inp_data = fPollTempData;
  541. fResampler.out_data = fResampleTempData;
  542. fResampler.process();
  543. CARLA_ASSERT_INT(fResampler.inp_count <= 1, fResampler.inp_count);
  544. }
  545. j = 0;
  546. do {
  547. if (fFileNfo.channels == 1)
  548. {
  549. for (; i < poolNumFrames && j < rv; ++i, ++j)
  550. pbuffer0[i] = pbuffer1[i] = tmpbuf[j];
  551. }
  552. else
  553. {
  554. for (; i < poolNumFrames && j < rv; ++j)
  555. {
  556. if (j % 2 == 0)
  557. {
  558. pbuffer0[i] = tmpbuf[j];
  559. }
  560. else
  561. {
  562. pbuffer1[i] = tmpbuf[j];
  563. ++i;
  564. }
  565. }
  566. }
  567. if (i >= poolNumFrames)
  568. break;
  569. if (rv == fFileNfo.frames)
  570. {
  571. // full file read
  572. j = 0;
  573. carla_debug("R: full file was read, filling buffers again");
  574. }
  575. else
  576. {
  577. carla_debug("read break, not enough space");
  578. carla_zeroFloats(pbuffer0, poolNumFrames - i);
  579. carla_zeroFloats(pbuffer1, poolNumFrames - i);
  580. break;
  581. }
  582. } while (i < poolNumFrames);
  583. // lock, and put data asap
  584. const water::GenericScopedLock<water::SpinLock> gsl(fPoolMutex);
  585. std::memcpy(fPool.buffer[0], pbuffer0, sizeof(float)*poolNumFrames);
  586. std::memcpy(fPool.buffer[1], pbuffer1, sizeof(float)*poolNumFrames);
  587. fPool.startFrame = static_cast<uint64_t>(readFrame);
  588. }
  589. fNeedsRead = false;
  590. }
  591. private:
  592. bool fEntireFileLoaded;
  593. bool fLoopingMode;
  594. volatile uint64_t fNeedsFrame;
  595. volatile bool fNeedsRead;
  596. void* fFilePtr;
  597. ADInfo fFileNfo;
  598. uint32_t fMaxFrame;
  599. double fResampleRatio;
  600. float* fPollTempData;
  601. uint fPollTempSize;
  602. float* fResampleTempData;
  603. uint fResampleTempSize;
  604. AudioFilePool fPool;
  605. Resampler fResampler;
  606. water::SpinLock fPoolMutex;
  607. CarlaMutex fReaderMutex;
  608. CARLA_DECLARE_NON_COPY_STRUCT(AudioFileReader)
  609. };
  610. #endif // AUDIO_BASE_HPP_INCLUDED