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.

878 lines
26KB

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