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.

456 lines
12KB

  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",
  281. readFrame/static_cast<int32_t>(fPool.sampleRate)/60,
  282. (readFrame/static_cast<int32_t>(fPool.sampleRate)) % 60);
  283. ad_seek(fFilePtr, readFrame);
  284. size_t i = 0;
  285. ssize_t j = 0;
  286. ssize_t rv = ad_read(fFilePtr, fPollTempData, fPollTempSize);
  287. if (rv < 0)
  288. {
  289. carla_stderr("R: ad_read failed");
  290. fNeedsRead = false;
  291. return;
  292. }
  293. const size_t urv = static_cast<size_t>(rv);
  294. // see if we can read more
  295. if (readFrame + rv >= static_cast<ssize_t>(fFileNfo.frames) && urv < fPollTempSize)
  296. {
  297. carla_debug("R: from start");
  298. ad_seek(fFilePtr, 0);
  299. rv += ad_read(fFilePtr, fPollTempData+urv, fPollTempSize-urv);
  300. }
  301. // lock, and put data asap
  302. const CarlaMutexLocker cml(fMutex);
  303. do {
  304. for (; i < fPool.size && j < rv; ++j)
  305. {
  306. if (fFileNfo.channels == 1)
  307. {
  308. fPool.buffer[0][i] = fPollTempData[j];
  309. fPool.buffer[1][i] = fPollTempData[j];
  310. i++;
  311. }
  312. else
  313. {
  314. if (j % 2 == 0)
  315. {
  316. fPool.buffer[0][i] = fPollTempData[j];
  317. }
  318. else
  319. {
  320. fPool.buffer[1][i] = fPollTempData[j];
  321. i++;
  322. }
  323. }
  324. }
  325. if (i >= fPool.size)
  326. break;
  327. if (rv == fFileNfo.frames)
  328. {
  329. // full file read
  330. j = 0;
  331. carla_debug("R: full file was read, filling buffers again");
  332. }
  333. else
  334. {
  335. carla_debug("read break, not enough space");
  336. carla_zeroFloats(fPool.buffer[0] + i, fPool.size - i);
  337. carla_zeroFloats(fPool.buffer[1] + i, fPool.size - i);
  338. break;
  339. }
  340. } while (i < fPool.size);
  341. fPool.startFrame = lastFrame;
  342. }
  343. fNeedsRead = false;
  344. }
  345. protected:
  346. void run() override
  347. {
  348. while (! fQuitNow)
  349. {
  350. const uint64_t lastFrame = kPlayer->getLastFrame();
  351. const uint64_t loopedFrame = fLoopingMode ? lastFrame % fMaxPlayerFrame : lastFrame;
  352. if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame))
  353. readPoll();
  354. carla_msleep(50);
  355. }
  356. }
  357. private:
  358. AbstractAudioPlayer* const kPlayer;
  359. bool fLoopingMode;
  360. bool fNeedsRead;
  361. bool fQuitNow;
  362. void* fFilePtr;
  363. ADInfo fFileNfo;
  364. uint32_t fMaxPlayerFrame;
  365. float* fPollTempData;
  366. size_t fPollTempSize;
  367. AudioFilePool fPool;
  368. CarlaMutex fMutex;
  369. CARLA_DECLARE_NON_COPY_STRUCT(AudioFileThread)
  370. };
  371. #endif // AUDIO_BASE_HPP_INCLUDED