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.

856 lines
25KB

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