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.

923 lines
29KB

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