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.

378 lines
9.0KB

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