Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

audio-base.hpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #ifndef AUDIO_BASE_HPP_INCLUDED
  18. #define AUDIO_BASE_HPP_INCLUDED
  19. #include "CarlaThread.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. extern "C" {
  22. #include "audio_decoder/ad.h"
  23. }
  24. typedef struct adinfo ADInfo;
  25. struct AudioFilePool {
  26. float* buffer[2];
  27. uint64_t startFrame;
  28. uint32_t sampleRate;
  29. uint32_t size;
  30. #ifdef CARLA_PROPER_CPP11_SUPPORT
  31. AudioFilePool()
  32. : buffer{nullptr},
  33. startFrame(0),
  34. sampleRate(0),
  35. size(0) {}
  36. #else
  37. AudioFilePool()
  38. : startFrame(0),
  39. sampleRate(0),
  40. size(0)
  41. {
  42. buffer[0] = buffer[1] = nullptr;
  43. }
  44. #endif
  45. ~AudioFilePool()
  46. {
  47. CARLA_ASSERT(buffer[0] == nullptr);
  48. CARLA_ASSERT(buffer[1] == nullptr);
  49. CARLA_ASSERT(startFrame == 0);
  50. CARLA_ASSERT(size == 0);
  51. }
  52. void create(const uint32_t srate)
  53. {
  54. CARLA_ASSERT(buffer[0] == nullptr);
  55. CARLA_ASSERT(buffer[1] == nullptr);
  56. CARLA_ASSERT(startFrame == 0);
  57. CARLA_ASSERT(size == 0);
  58. size = srate * 60; // buffer of 60 secs
  59. sampleRate = srate;
  60. buffer[0] = new float[size];
  61. buffer[1] = new float[size];
  62. reset();
  63. }
  64. void destroy()
  65. {
  66. CARLA_ASSERT(buffer[0] != nullptr);
  67. CARLA_ASSERT(buffer[1] != nullptr);
  68. CARLA_ASSERT(size != 0);
  69. if (buffer[0] != nullptr)
  70. {
  71. delete[] buffer[0];
  72. buffer[0] = nullptr;
  73. }
  74. if (buffer[1] != nullptr)
  75. {
  76. delete[] buffer[1];
  77. buffer[1] = nullptr;
  78. }
  79. startFrame = 0;
  80. size = 0;
  81. }
  82. void reset()
  83. {
  84. startFrame = 0;
  85. CARLA_SAFE_ASSERT_RETURN(size != 0,);
  86. carla_zeroFloats(buffer[0], size);
  87. carla_zeroFloats(buffer[1], size);
  88. }
  89. CARLA_DECLARE_NON_COPY_STRUCT(AudioFilePool)
  90. };
  91. class AbstractAudioPlayer
  92. {
  93. public:
  94. virtual ~AbstractAudioPlayer() {}
  95. virtual uint64_t getLastFrame() const = 0;
  96. };
  97. class AudioFileThread : public CarlaThread
  98. {
  99. public:
  100. AudioFileThread(AbstractAudioPlayer* const player, const uint32_t sampleRate)
  101. : CarlaThread("AudioFileThread"),
  102. kPlayer(player),
  103. fLoopingMode(true),
  104. fNeedsRead(false),
  105. fQuitNow(true),
  106. fFilePtr(nullptr),
  107. fFileNfo(),
  108. fMaxPlayerFrame(0),
  109. fPollTempData(nullptr),
  110. fPollTempSize(0),
  111. fPool(),
  112. fMutex()
  113. {
  114. CARLA_ASSERT(kPlayer != nullptr);
  115. static bool adInitiated = false;
  116. if (! adInitiated)
  117. {
  118. ad_init();
  119. adInitiated = true;
  120. }
  121. ad_clear_nfo(&fFileNfo);
  122. fPool.create(sampleRate);
  123. }
  124. ~AudioFileThread() override
  125. {
  126. CARLA_ASSERT(fQuitNow);
  127. CARLA_ASSERT(! isThreadRunning());
  128. if (fFilePtr != nullptr)
  129. {
  130. ad_close(fFilePtr);
  131. fFilePtr = nullptr;
  132. }
  133. if (fPollTempData != nullptr)
  134. {
  135. delete[] fPollTempData;
  136. fPollTempData = nullptr;
  137. fPollTempSize = 0;
  138. }
  139. fPool.destroy();
  140. }
  141. void startNow()
  142. {
  143. fNeedsRead = true;
  144. fQuitNow = false;
  145. startThread();
  146. }
  147. void stopNow()
  148. {
  149. fNeedsRead = false;
  150. fQuitNow = true;
  151. stopThread(1000);
  152. const CarlaMutexLocker cml(fMutex);
  153. fPool.reset();
  154. }
  155. uint32_t getMaxFrame() const noexcept
  156. {
  157. return fMaxPlayerFrame;
  158. }
  159. void setLoopingMode(const bool on) noexcept
  160. {
  161. fLoopingMode = on;
  162. }
  163. void setNeedsRead() noexcept
  164. {
  165. fNeedsRead = true;
  166. }
  167. bool loadFilename(const char* const filename)
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), false);
  170. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && *filename != '\0', false);
  171. fPool.startFrame = 0;
  172. // clear old data
  173. if (fFilePtr != nullptr)
  174. {
  175. ad_close(fFilePtr);
  176. fFilePtr = nullptr;
  177. }
  178. if (fPollTempData != nullptr)
  179. {
  180. delete[] fPollTempData;
  181. fPollTempData = nullptr;
  182. fPollTempSize = 0;
  183. fMaxPlayerFrame = 0;
  184. }
  185. ad_clear_nfo(&fFileNfo);
  186. // open new
  187. fFilePtr = ad_open(filename, &fFileNfo);
  188. if (fFilePtr == nullptr)
  189. return false;
  190. ad_dump_nfo(99, &fFileNfo);
  191. // Fix for misinformation using libsndfile
  192. if (fFileNfo.frames % fFileNfo.channels)
  193. --fFileNfo.frames;
  194. if (fFileNfo.frames <= 0)
  195. carla_stderr("L: filename \"%s\" has 0 frames", filename);
  196. if ((fFileNfo.channels == 1 || fFileNfo.channels == 2) && fFileNfo.frames > 0)
  197. {
  198. // valid
  199. const size_t pollTempSize = std::min(static_cast<uint>(fFileNfo.frames),
  200. fPool.size * fFileNfo.channels);
  201. try {
  202. fPollTempData = new float[pollTempSize];
  203. } catch (...) {
  204. ad_close(fFilePtr);
  205. fFilePtr = nullptr;
  206. return false;
  207. }
  208. fMaxPlayerFrame = static_cast<uint32_t>(fFileNfo.frames/fFileNfo.channels);
  209. fPollTempSize = pollTempSize;
  210. readPoll();
  211. return true;
  212. }
  213. else
  214. {
  215. // invalid
  216. ad_clear_nfo(&fFileNfo);
  217. ad_close(fFilePtr);
  218. fFilePtr = nullptr;
  219. return false;
  220. }
  221. }
  222. bool tryPutData(AudioFilePool& pool, const uint32_t framePos, const uint32_t frames)
  223. {
  224. CARLA_SAFE_ASSERT_RETURN(pool.size == fPool.size, false);
  225. if (! fMutex.tryLock())
  226. return false;
  227. //if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1])
  228. {
  229. pool.startFrame = fPool.startFrame;
  230. if (frames == 0)
  231. {
  232. carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.size);
  233. carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.size);
  234. }
  235. else
  236. {
  237. CARLA_SAFE_ASSERT_UINT2_RETURN(framePos + frames < fPool.size, framePos, fPool.size, false);
  238. carla_copyFloats(pool.buffer[0] + framePos, fPool.buffer[0] + framePos, frames);
  239. carla_copyFloats(pool.buffer[1] + framePos, fPool.buffer[1] + framePos, frames);
  240. }
  241. }
  242. fMutex.unlock();
  243. return true;
  244. }
  245. void readPoll()
  246. {
  247. if (fFileNfo.frames <= 0 || fFilePtr == nullptr)
  248. {
  249. carla_stderr("R: no song loaded");
  250. fNeedsRead = false;
  251. return;
  252. }
  253. const uint64_t lastFrame = kPlayer->getLastFrame();
  254. int32_t readFrameCheck;
  255. if (lastFrame >= static_cast<uint64_t>(fFileNfo.frames))
  256. {
  257. if (fLoopingMode)
  258. {
  259. const uint64_t readFrameCheckLoop = lastFrame % fMaxPlayerFrame;
  260. CARLA_SAFE_ASSERT_RETURN(readFrameCheckLoop < INT32_MAX,);
  261. carla_debug("R: transport out of bounds for loop");
  262. readFrameCheck = static_cast<int32_t>(readFrameCheckLoop);
  263. }
  264. else
  265. {
  266. carla_stderr("R: transport out of bounds");
  267. fNeedsRead = false;
  268. return;
  269. }
  270. }
  271. else
  272. {
  273. CARLA_SAFE_ASSERT_RETURN(lastFrame < INT32_MAX,);
  274. readFrameCheck = static_cast<int32_t>(lastFrame);
  275. }
  276. const int32_t readFrame = readFrameCheck;
  277. // temp data buffer
  278. carla_zeroFloats(fPollTempData, fPollTempSize);
  279. {
  280. carla_debug("R: poll data - reading at %li:%02li", readFrame/fPool.sampleRate/60, (readFrame/fPool.sampleRate) % 60);
  281. ad_seek(fFilePtr, readFrame);
  282. size_t i = 0;
  283. ssize_t j = 0;
  284. ssize_t rv = ad_read(fFilePtr, fPollTempData, fPollTempSize);
  285. if (rv < 0)
  286. {
  287. carla_stderr("R: ad_read failed");
  288. fNeedsRead = false;
  289. return;
  290. }
  291. const size_t urv = static_cast<size_t>(rv);
  292. // see if we can read more
  293. if (readFrame + rv >= static_cast<ssize_t>(fFileNfo.frames) && urv < fPollTempSize)
  294. {
  295. carla_debug("R: from start");
  296. ad_seek(fFilePtr, 0);
  297. rv += ad_read(fFilePtr, fPollTempData+urv, fPollTempSize-urv);
  298. }
  299. // lock, and put data asap
  300. const CarlaMutexLocker cml(fMutex);
  301. do {
  302. for (; i < fPool.size && j < rv; ++j)
  303. {
  304. if (fFileNfo.channels == 1)
  305. {
  306. fPool.buffer[0][i] = fPollTempData[j];
  307. fPool.buffer[1][i] = fPollTempData[j];
  308. i++;
  309. }
  310. else
  311. {
  312. if (j % 2 == 0)
  313. {
  314. fPool.buffer[0][i] = fPollTempData[j];
  315. }
  316. else
  317. {
  318. fPool.buffer[1][i] = fPollTempData[j];
  319. i++;
  320. }
  321. }
  322. }
  323. if (i >= fPool.size)
  324. break;
  325. if (rv == fFileNfo.frames)
  326. {
  327. // full file read
  328. j = 0;
  329. carla_debug("R: full file was read, filling buffers again");
  330. }
  331. else
  332. {
  333. carla_debug("read break, not enough space");
  334. carla_zeroFloats(fPool.buffer[0] + i, fPool.size - i);
  335. carla_zeroFloats(fPool.buffer[1] + i, fPool.size - i);
  336. break;
  337. }
  338. } while (i < fPool.size);
  339. fPool.startFrame = lastFrame;
  340. }
  341. fNeedsRead = false;
  342. }
  343. protected:
  344. void run() override
  345. {
  346. while (! fQuitNow)
  347. {
  348. const uint64_t lastFrame = kPlayer->getLastFrame();
  349. const uint64_t loopedFrame = fLoopingMode ? lastFrame % fMaxPlayerFrame : lastFrame;
  350. if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame))
  351. readPoll();
  352. carla_msleep(50);
  353. }
  354. }
  355. private:
  356. AbstractAudioPlayer* const kPlayer;
  357. bool fLoopingMode;
  358. bool fNeedsRead;
  359. bool fQuitNow;
  360. void* fFilePtr;
  361. ADInfo fFileNfo;
  362. uint32_t fMaxPlayerFrame;
  363. float* fPollTempData;
  364. size_t fPollTempSize;
  365. AudioFilePool fPool;
  366. CarlaMutex fMutex;
  367. CARLA_DECLARE_NON_COPY_STRUCT(AudioFileThread)
  368. };
  369. #endif // AUDIO_BASE_HPP_INCLUDED