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.

447 lines
11KB

  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 * 8;
  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. void tryPutData(AudioFilePool& pool)
  223. {
  224. CARLA_SAFE_ASSERT_RETURN(pool.size == fPool.size,);
  225. if (! fMutex.tryLock())
  226. return;
  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. carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.size);
  231. carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.size);
  232. }
  233. fMutex.unlock();
  234. }
  235. void readPoll()
  236. {
  237. if (fFileNfo.frames <= 0 || fFilePtr == nullptr)
  238. {
  239. carla_stderr("R: no song loaded");
  240. fNeedsRead = false;
  241. return;
  242. }
  243. const uint64_t lastFrame = kPlayer->getLastFrame();
  244. int32_t readFrameCheck;
  245. if (lastFrame >= static_cast<uint64_t>(fFileNfo.frames))
  246. {
  247. if (fLoopingMode)
  248. {
  249. const uint64_t readFrameCheckLoop = lastFrame % fMaxPlayerFrame;
  250. CARLA_SAFE_ASSERT_RETURN(readFrameCheckLoop < INT32_MAX,);
  251. carla_debug("R: transport out of bounds for loop");
  252. readFrameCheck = static_cast<int32_t>(readFrameCheckLoop);
  253. }
  254. else
  255. {
  256. carla_stderr("R: transport out of bounds");
  257. fNeedsRead = false;
  258. return;
  259. }
  260. }
  261. else
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(lastFrame < INT32_MAX,);
  264. readFrameCheck = static_cast<int32_t>(lastFrame);
  265. }
  266. const int32_t readFrame = readFrameCheck;
  267. // temp data buffer
  268. carla_zeroFloats(fPollTempData, fPollTempSize);
  269. {
  270. carla_debug("R: poll data - reading at %li:%02li", readFrame/fPool.sampleRate/60, (readFrame/fPool.sampleRate) % 60);
  271. ad_seek(fFilePtr, readFrame);
  272. size_t i = 0;
  273. ssize_t j = 0;
  274. ssize_t rv = ad_read(fFilePtr, fPollTempData, fPollTempSize);
  275. if (rv < 0)
  276. {
  277. carla_stderr("R: ad_read failed");
  278. fNeedsRead = false;
  279. return;
  280. }
  281. const size_t urv = static_cast<size_t>(rv);
  282. // see if we can read more
  283. if (readFrame + rv >= static_cast<ssize_t>(fFileNfo.frames) && urv < fPollTempSize)
  284. {
  285. carla_debug("R: from start");
  286. ad_seek(fFilePtr, 0);
  287. rv += ad_read(fFilePtr, fPollTempData+urv, fPollTempSize-urv);
  288. }
  289. // lock, and put data asap
  290. const CarlaMutexLocker cml(fMutex);
  291. do {
  292. for (; i < fPool.size && j < rv; ++j)
  293. {
  294. if (fFileNfo.channels == 1)
  295. {
  296. fPool.buffer[0][i] = fPollTempData[j];
  297. fPool.buffer[1][i] = fPollTempData[j];
  298. i++;
  299. }
  300. else
  301. {
  302. if (j % 2 == 0)
  303. {
  304. fPool.buffer[0][i] = fPollTempData[j];
  305. }
  306. else
  307. {
  308. fPool.buffer[1][i] = fPollTempData[j];
  309. i++;
  310. }
  311. }
  312. }
  313. if (i >= fPool.size)
  314. break;
  315. if (rv == fFileNfo.frames)
  316. {
  317. // full file read
  318. j = 0;
  319. carla_debug("R: full file was read, filling buffers again");
  320. }
  321. else
  322. {
  323. carla_debug("read break, not enough space");
  324. // FIXME use carla_zeroFloats
  325. for (; i < fPool.size; ++i)
  326. {
  327. fPool.buffer[0][i] = 0.0f;
  328. fPool.buffer[1][i] = 0.0f;
  329. }
  330. break;
  331. }
  332. } while (i < fPool.size);
  333. fPool.startFrame = lastFrame;
  334. }
  335. fNeedsRead = false;
  336. }
  337. protected:
  338. void run() override
  339. {
  340. while (! fQuitNow)
  341. {
  342. const uint64_t lastFrame = kPlayer->getLastFrame();
  343. const uint64_t loopedFrame = fLoopingMode ? lastFrame % fMaxPlayerFrame : lastFrame;
  344. if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame))
  345. readPoll();
  346. else
  347. carla_msleep(50);
  348. }
  349. }
  350. private:
  351. AbstractAudioPlayer* const kPlayer;
  352. bool fLoopingMode;
  353. bool fNeedsRead;
  354. bool fQuitNow;
  355. void* fFilePtr;
  356. ADInfo fFileNfo;
  357. uint32_t fMaxPlayerFrame;
  358. float* fPollTempData;
  359. size_t fPollTempSize;
  360. AudioFilePool fPool;
  361. CarlaMutex fMutex;
  362. CARLA_DECLARE_NON_COPY_STRUCT(AudioFileThread)
  363. };
  364. #endif // AUDIO_BASE_HPP_INCLUDED