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.

403 lines
9.5KB

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