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.

390 lines
9.2KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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__
  18. #define __AUDIO_BASE_HPP__
  19. #include "CarlaMutex.hpp"
  20. #include <QtCore/QThread>
  21. extern "C" {
  22. #include "audio_decoder/ad.h"
  23. }
  24. typedef struct adinfo ADInfo;
  25. struct AudioFilePool {
  26. float* buffer[2];
  27. uint32_t startFrame;
  28. uint32_t size;
  29. #ifdef CARLA_PROPER_CPP11_SUPPORT
  30. AudioFilePool()
  31. : buffer{nullptr},
  32. startFrame(0),
  33. size(0) {}
  34. #else
  35. AudioFilePool()
  36. : startFrame(0),
  37. size(0)
  38. {
  39. buffer[0] = buffer[1] = nullptr;
  40. }
  41. #endif
  42. ~AudioFilePool()
  43. {
  44. CARLA_ASSERT(buffer[0] == nullptr);
  45. CARLA_ASSERT(buffer[1] == nullptr);
  46. CARLA_ASSERT(startFrame == 0);
  47. CARLA_ASSERT(size == 0);
  48. }
  49. void create(const uint32_t sampleRate)
  50. {
  51. CARLA_ASSERT(buffer[0] == nullptr);
  52. CARLA_ASSERT(buffer[1] == nullptr);
  53. CARLA_ASSERT(startFrame == 0);
  54. CARLA_ASSERT(size == 0);
  55. size = sampleRate * 2;
  56. buffer[0] = new float[size];
  57. buffer[1] = new float[size];
  58. reset();
  59. }
  60. void destroy()
  61. {
  62. CARLA_ASSERT(buffer[0] != nullptr);
  63. CARLA_ASSERT(buffer[1] != nullptr);
  64. CARLA_ASSERT(size != 0);
  65. if (buffer[0] != nullptr)
  66. {
  67. delete[] buffer[0];
  68. buffer[0] = nullptr;
  69. }
  70. if (buffer[1] != nullptr)
  71. {
  72. delete[] buffer[1];
  73. buffer[1] = nullptr;
  74. }
  75. startFrame = 0;
  76. size = 0;
  77. }
  78. void reset()
  79. {
  80. CARLA_ASSERT(size != 0);
  81. startFrame = 0;
  82. carla_zeroFloat(buffer[0], size);
  83. carla_zeroFloat(buffer[1], size);
  84. }
  85. };
  86. class AbstractAudioPlayer
  87. {
  88. public:
  89. virtual ~AbstractAudioPlayer() {}
  90. virtual uint32_t getLastFrame() const = 0;
  91. };
  92. class AudioFileThread : public QThread
  93. {
  94. public:
  95. AudioFileThread(AbstractAudioPlayer* const player, const double sampleRate)
  96. : QThread(nullptr),
  97. kPlayer(player),
  98. fNeedsRead(false),
  99. fQuitNow(true),
  100. fFilePtr(nullptr)
  101. {
  102. CARLA_ASSERT(kPlayer != nullptr);
  103. static bool adInitiated = false;
  104. if (! adInitiated)
  105. {
  106. ad_init();
  107. adInitiated = true;
  108. }
  109. ad_clear_nfo(&fFileNfo);
  110. fPool.create(sampleRate);
  111. }
  112. ~AudioFileThread() override
  113. {
  114. CARLA_ASSERT(fQuitNow);
  115. CARLA_ASSERT(! isRunning());
  116. if (fFilePtr != nullptr)
  117. ad_close(fFilePtr);
  118. fPool.destroy();
  119. }
  120. void startNow()
  121. {
  122. fNeedsRead = true;
  123. fQuitNow = false;
  124. start(IdlePriority);
  125. }
  126. void stopNow()
  127. {
  128. fNeedsRead = false;
  129. fQuitNow = true;
  130. if (isRunning() && ! wait(1000))
  131. terminate();
  132. const CarlaMutex::ScopedLocker sl(fMutex);
  133. fPool.reset();
  134. }
  135. uint32_t getMaxFrame() const
  136. {
  137. return fFileNfo.frames > 0 ? fFileNfo.frames : 0;
  138. }
  139. void setNeedsRead()
  140. {
  141. fNeedsRead = true;
  142. }
  143. bool loadFilename(const char* const filename)
  144. {
  145. CARLA_ASSERT(! isRunning());
  146. CARLA_ASSERT(filename != nullptr);
  147. fPool.startFrame = 0;
  148. // clear old data
  149. if (fFilePtr != nullptr)
  150. {
  151. ad_close(fFilePtr);
  152. fFilePtr = nullptr;
  153. }
  154. ad_clear_nfo(&fFileNfo);
  155. // open new
  156. fFilePtr = ad_open(filename, &fFileNfo);
  157. if (fFilePtr == nullptr)
  158. return false;
  159. ad_dump_nfo(99, &fFileNfo);
  160. if (fFileNfo.frames == 0)
  161. carla_stderr("L: filename \"%s\" has 0 frames", filename);
  162. if ((fFileNfo.channels == 1 || fFileNfo.channels == 2) && fFileNfo.frames > 0)
  163. {
  164. // valid
  165. readPoll();
  166. return true;
  167. }
  168. else
  169. {
  170. // invalid
  171. ad_clear_nfo(&fFileNfo);
  172. ad_close(fFilePtr);
  173. fFilePtr = nullptr;
  174. return false;
  175. }
  176. }
  177. void tryPutData(AudioFilePool& pool)
  178. {
  179. CARLA_ASSERT(pool.size == fPool.size);
  180. if (pool.size != fPool.size)
  181. return;
  182. if (! fMutex.tryLock())
  183. return;
  184. //if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1])
  185. {
  186. pool.startFrame = fPool.startFrame;
  187. carla_copyFloat(pool.buffer[0], fPool.buffer[0], fPool.size);
  188. carla_copyFloat(pool.buffer[1], fPool.buffer[1], fPool.size);
  189. }
  190. fMutex.unlock();
  191. }
  192. void readPoll()
  193. {
  194. if (fFileNfo.frames <= 0 || fFilePtr == nullptr)
  195. {
  196. carla_stderr("R: no song loaded");
  197. fNeedsRead = false;
  198. return;
  199. }
  200. int64_t lastFrame = kPlayer->getLastFrame();
  201. int64_t readFrame = lastFrame;
  202. int64_t maxFrame = fFileNfo.frames;
  203. if (lastFrame >= maxFrame)
  204. {
  205. #if 0
  206. if (false)
  207. //if (handlePtr->loopMode)
  208. {
  209. carla_stderr("R: DEBUG read loop, lastFrame:%i, maxFrame:%i", lastFrame, maxFrame);
  210. if (maxFrame >= static_cast<int64_t>(fPool.size))
  211. {
  212. readFrame %= maxFrame;
  213. }
  214. else
  215. {
  216. readFrame = 0;
  217. lastFrame -= lastFrame % maxFrame;
  218. }
  219. }
  220. else
  221. #endif
  222. {
  223. carla_stderr("R: transport out of bounds");
  224. fNeedsRead = false;
  225. return;
  226. }
  227. }
  228. // temp data buffer
  229. const size_t tmpSize = fPool.size * fFileNfo.channels;
  230. float tmpData[tmpSize];
  231. carla_zeroFloat(tmpData, tmpSize);
  232. {
  233. carla_stderr("R: poll data - reading at %li:%02li", readFrame/44100/60, (readFrame/44100) % 60);
  234. ad_seek(fFilePtr, readFrame);
  235. ssize_t i, j, rv = ad_read(fFilePtr, tmpData, tmpSize);
  236. i = j = 0;
  237. // lock, and put data asap
  238. const CarlaMutex::ScopedLocker sl(fMutex);
  239. for (; i < fPool.size && j < rv; ++j)
  240. {
  241. if (fFileNfo.channels == 1)
  242. {
  243. fPool.buffer[0][i] = tmpData[j];
  244. fPool.buffer[1][i] = tmpData[j];
  245. i++;
  246. }
  247. else
  248. {
  249. if (j % 2 == 0)
  250. {
  251. fPool.buffer[0][i] = tmpData[j];
  252. }
  253. else
  254. {
  255. fPool.buffer[1][i] = tmpData[j];
  256. i++;
  257. }
  258. }
  259. }
  260. #if 0
  261. if (false)
  262. //if (handlePtr->loopMode && i < fPool.size)
  263. {
  264. while (i < fPool.size)
  265. {
  266. for (j=0; i < fPool.size && j < rv; ++j)
  267. {
  268. if (fFileNfo.channels == 1)
  269. {
  270. fPool.buffer[0][i] = tmpData[j];
  271. fPool.buffer[1][i] = tmpData[j];
  272. i++;
  273. }
  274. else
  275. {
  276. if (j % 2 == 0)
  277. {
  278. fPool.buffer[0][i] = tmpData[j];
  279. }
  280. else
  281. {
  282. fPool.buffer[1][i] = tmpData[j];
  283. i++;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. else
  290. #endif
  291. {
  292. for (; i < fPool.size; ++i)
  293. {
  294. fPool.buffer[0][i] = 0.0f;
  295. fPool.buffer[1][i] = 0.0f;
  296. }
  297. }
  298. fPool.startFrame = lastFrame;
  299. }
  300. fNeedsRead = false;
  301. }
  302. protected:
  303. void run() override
  304. {
  305. while (! fQuitNow)
  306. {
  307. const uint32_t lastFrame(kPlayer->getLastFrame());
  308. if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && lastFrame < fFileNfo.frames))
  309. readPoll();
  310. else
  311. carla_msleep(50);
  312. }
  313. }
  314. private:
  315. AbstractAudioPlayer* const kPlayer;
  316. bool fNeedsRead;
  317. bool fQuitNow;
  318. void* fFilePtr;
  319. ADInfo fFileNfo;
  320. AudioFilePool fPool;
  321. CarlaMutex fMutex;
  322. };
  323. #endif // __AUDIO_BASE_HPP__