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.

389 lines
9.2KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2017 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. 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. startFrame = 0;
  81. CARLA_SAFE_ASSERT_RETURN(size != 0,);
  82. carla_zeroFloats(buffer[0], size);
  83. carla_zeroFloats(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 CarlaThread
  93. {
  94. public:
  95. AudioFileThread(AbstractAudioPlayer* const player, const double sampleRate)
  96. : CarlaThread("AudioFileThread"),
  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(! isThreadRunning());
  116. if (fFilePtr != nullptr)
  117. ad_close(fFilePtr);
  118. fPool.destroy();
  119. }
  120. void startNow()
  121. {
  122. fNeedsRead = true;
  123. fQuitNow = false;
  124. startThread();
  125. }
  126. void stopNow()
  127. {
  128. fNeedsRead = false;
  129. fQuitNow = true;
  130. stopThread(1000);
  131. const CarlaMutexLocker cml(fMutex);
  132. fPool.reset();
  133. }
  134. uint32_t getMaxFrame() const
  135. {
  136. return fFileNfo.frames > 0 ? fFileNfo.frames : 0;
  137. }
  138. void setNeedsRead()
  139. {
  140. fNeedsRead = true;
  141. }
  142. bool loadFilename(const char* const filename)
  143. {
  144. CARLA_ASSERT(! isThreadRunning());
  145. CARLA_ASSERT(filename != nullptr);
  146. fPool.startFrame = 0;
  147. // clear old data
  148. if (fFilePtr != nullptr)
  149. {
  150. ad_close(fFilePtr);
  151. fFilePtr = nullptr;
  152. }
  153. ad_clear_nfo(&fFileNfo);
  154. // open new
  155. fFilePtr = ad_open(filename, &fFileNfo);
  156. if (fFilePtr == nullptr)
  157. return false;
  158. ad_dump_nfo(99, &fFileNfo);
  159. if (fFileNfo.frames == 0)
  160. carla_stderr("L: filename \"%s\" has 0 frames", filename);
  161. if ((fFileNfo.channels == 1 || fFileNfo.channels == 2) && fFileNfo.frames > 0)
  162. {
  163. // valid
  164. readPoll();
  165. return true;
  166. }
  167. else
  168. {
  169. // invalid
  170. ad_clear_nfo(&fFileNfo);
  171. ad_close(fFilePtr);
  172. fFilePtr = nullptr;
  173. return false;
  174. }
  175. }
  176. void tryPutData(AudioFilePool& pool)
  177. {
  178. CARLA_ASSERT(pool.size == fPool.size);
  179. if (pool.size != fPool.size)
  180. return;
  181. if (! fMutex.tryLock())
  182. return;
  183. //if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1])
  184. {
  185. pool.startFrame = fPool.startFrame;
  186. carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.size);
  187. carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.size);
  188. }
  189. fMutex.unlock();
  190. }
  191. void readPoll()
  192. {
  193. if (fFileNfo.frames <= 0 || fFilePtr == nullptr)
  194. {
  195. carla_stderr("R: no song loaded");
  196. fNeedsRead = false;
  197. return;
  198. }
  199. int64_t lastFrame = kPlayer->getLastFrame();
  200. int64_t readFrame = lastFrame;
  201. int64_t maxFrame = fFileNfo.frames;
  202. if (lastFrame >= maxFrame)
  203. {
  204. #if 0
  205. if (false)
  206. //if (handlePtr->loopMode)
  207. {
  208. carla_stderr("R: DEBUG read loop, lastFrame:%i, maxFrame:%i", lastFrame, maxFrame);
  209. if (maxFrame >= static_cast<int64_t>(fPool.size))
  210. {
  211. readFrame %= maxFrame;
  212. }
  213. else
  214. {
  215. readFrame = 0;
  216. lastFrame -= lastFrame % maxFrame;
  217. }
  218. }
  219. else
  220. #endif
  221. {
  222. carla_stderr("R: transport out of bounds");
  223. fNeedsRead = false;
  224. return;
  225. }
  226. }
  227. // temp data buffer
  228. const size_t tmpSize = fPool.size * fFileNfo.channels;
  229. float tmpData[tmpSize];
  230. carla_zeroFloats(tmpData, tmpSize);
  231. {
  232. carla_stderr("R: poll data - reading at %li:%02li", readFrame/44100/60, (readFrame/44100) % 60);
  233. ad_seek(fFilePtr, readFrame);
  234. size_t i = 0;
  235. ssize_t j = 0;
  236. ssize_t rv = ad_read(fFilePtr, tmpData, tmpSize);
  237. // lock, and put data asap
  238. const CarlaMutexLocker cml(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_INCLUDED