The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

828 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. struct AudioThumbnail::MinMaxValue
  22. {
  23. MinMaxValue() noexcept
  24. {
  25. values[0] = 0;
  26. values[1] = 0;
  27. }
  28. inline void set (const int8 newMin, const int8 newMax) noexcept
  29. {
  30. values[0] = newMin;
  31. values[1] = newMax;
  32. }
  33. inline int8 getMinValue() const noexcept { return values[0]; }
  34. inline int8 getMaxValue() const noexcept { return values[1]; }
  35. inline void setFloat (Range<float> newRange) noexcept
  36. {
  37. // Workaround for an ndk armeabi compiler bug which crashes on signed saturation
  38. #if JUCE_ANDROID
  39. Range<float> limitedRange (jlimit (-1.0f, 1.0f, newRange.getStart()),
  40. jlimit (-1.0f, 1.0f, newRange.getEnd()));
  41. values[0] = (int8) (limitedRange.getStart() * 127.0f);
  42. values[1] = (int8) (limitedRange.getEnd() * 127.0f);
  43. #else
  44. values[0] = (int8) jlimit (-128, 127, roundFloatToInt (newRange.getStart() * 127.0f));
  45. values[1] = (int8) jlimit (-128, 127, roundFloatToInt (newRange.getEnd() * 127.0f));
  46. #endif
  47. if (values[0] == values[1])
  48. {
  49. if (values[1] == 127)
  50. values[0]--;
  51. else
  52. values[1]++;
  53. }
  54. }
  55. inline bool isNonZero() const noexcept
  56. {
  57. return values[1] > values[0];
  58. }
  59. inline int getPeak() const noexcept
  60. {
  61. return jmax (std::abs ((int) values[0]),
  62. std::abs ((int) values[1]));
  63. }
  64. inline void read (InputStream& input) { input.read (values, 2); }
  65. inline void write (OutputStream& output) { output.write (values, 2); }
  66. private:
  67. int8 values[2];
  68. };
  69. //==============================================================================
  70. class AudioThumbnail::LevelDataSource : public TimeSliceClient
  71. {
  72. public:
  73. LevelDataSource (AudioThumbnail& thumb, AudioFormatReader* newReader, int64 hash)
  74. : hashCode (hash), owner (thumb), reader (newReader)
  75. {
  76. }
  77. LevelDataSource (AudioThumbnail& thumb, InputSource* src)
  78. : hashCode (src->hashCode()), owner (thumb), source (src)
  79. {
  80. }
  81. ~LevelDataSource()
  82. {
  83. owner.cache.getTimeSliceThread().removeTimeSliceClient (this);
  84. }
  85. enum { timeBeforeDeletingReader = 3000 };
  86. void initialise (int64 samplesFinished)
  87. {
  88. const ScopedLock sl (readerLock);
  89. numSamplesFinished = samplesFinished;
  90. createReader();
  91. if (reader != nullptr)
  92. {
  93. lengthInSamples = reader->lengthInSamples;
  94. numChannels = reader->numChannels;
  95. sampleRate = reader->sampleRate;
  96. if (lengthInSamples <= 0 || isFullyLoaded())
  97. reader.reset();
  98. else
  99. owner.cache.getTimeSliceThread().addTimeSliceClient (this);
  100. }
  101. }
  102. void getLevels (int64 startSample, int numSamples, Array<Range<float>>& levels)
  103. {
  104. const ScopedLock sl (readerLock);
  105. if (reader == nullptr)
  106. {
  107. createReader();
  108. if (reader != nullptr)
  109. {
  110. lastReaderUseTime = Time::getMillisecondCounter();
  111. owner.cache.getTimeSliceThread().addTimeSliceClient (this);
  112. }
  113. }
  114. if (reader != nullptr)
  115. {
  116. if (levels.size() < (int) reader->numChannels)
  117. levels.insertMultiple (0, {}, (int) reader->numChannels - levels.size());
  118. reader->readMaxLevels (startSample, numSamples, levels.getRawDataPointer(), (int) reader->numChannels);
  119. lastReaderUseTime = Time::getMillisecondCounter();
  120. }
  121. }
  122. void releaseResources()
  123. {
  124. const ScopedLock sl (readerLock);
  125. reader.reset();
  126. }
  127. int useTimeSlice() override
  128. {
  129. if (isFullyLoaded())
  130. {
  131. if (reader != nullptr && source != nullptr)
  132. {
  133. if (Time::getMillisecondCounter() > lastReaderUseTime + timeBeforeDeletingReader)
  134. releaseResources();
  135. else
  136. return 200;
  137. }
  138. return -1;
  139. }
  140. bool justFinished = false;
  141. {
  142. const ScopedLock sl (readerLock);
  143. createReader();
  144. if (reader != nullptr)
  145. {
  146. if (! readNextBlock())
  147. return 0;
  148. justFinished = true;
  149. }
  150. }
  151. if (justFinished)
  152. owner.cache.storeThumb (owner, hashCode);
  153. return 200;
  154. }
  155. bool isFullyLoaded() const noexcept
  156. {
  157. return numSamplesFinished >= lengthInSamples;
  158. }
  159. inline int sampleToThumbSample (const int64 originalSample) const noexcept
  160. {
  161. return (int) (originalSample / owner.samplesPerThumbSample);
  162. }
  163. int64 lengthInSamples = 0, numSamplesFinished = 0;
  164. double sampleRate = 0;
  165. unsigned int numChannels = 0;
  166. int64 hashCode = 0;
  167. private:
  168. AudioThumbnail& owner;
  169. ScopedPointer<InputSource> source;
  170. ScopedPointer<AudioFormatReader> reader;
  171. CriticalSection readerLock;
  172. uint32 lastReaderUseTime = 0;
  173. void createReader()
  174. {
  175. if (reader == nullptr && source != nullptr)
  176. if (InputStream* audioFileStream = source->createInputStream())
  177. reader = owner.formatManagerToUse.createReaderFor (audioFileStream);
  178. }
  179. bool readNextBlock()
  180. {
  181. jassert (reader != nullptr);
  182. if (! isFullyLoaded())
  183. {
  184. auto numToDo = (int) jmin (256 * (int64) owner.samplesPerThumbSample, lengthInSamples - numSamplesFinished);
  185. if (numToDo > 0)
  186. {
  187. auto startSample = numSamplesFinished;
  188. auto firstThumbIndex = sampleToThumbSample (startSample);
  189. auto lastThumbIndex = sampleToThumbSample (startSample + numToDo);
  190. auto numThumbSamps = lastThumbIndex - firstThumbIndex;
  191. HeapBlock<MinMaxValue> levelData ((unsigned int) numThumbSamps * numChannels);
  192. HeapBlock<MinMaxValue*> levels (numChannels);
  193. for (int i = 0; i < (int) numChannels; ++i)
  194. levels[i] = levelData + i * numThumbSamps;
  195. HeapBlock<Range<float>> levelsRead (numChannels);
  196. for (int i = 0; i < numThumbSamps; ++i)
  197. {
  198. reader->readMaxLevels ((firstThumbIndex + i) * owner.samplesPerThumbSample,
  199. owner.samplesPerThumbSample, levelsRead, (int) numChannels);
  200. for (int j = 0; j < (int) numChannels; ++j)
  201. levels[j][i].setFloat (levelsRead[j]);
  202. }
  203. {
  204. const ScopedUnlock su (readerLock);
  205. owner.setLevels (levels, firstThumbIndex, (int) numChannels, numThumbSamps);
  206. }
  207. numSamplesFinished += numToDo;
  208. lastReaderUseTime = Time::getMillisecondCounter();
  209. }
  210. }
  211. return isFullyLoaded();
  212. }
  213. };
  214. //==============================================================================
  215. class AudioThumbnail::ThumbData
  216. {
  217. public:
  218. ThumbData (const int numThumbSamples)
  219. : peakLevel (-1)
  220. {
  221. ensureSize (numThumbSamples);
  222. }
  223. inline MinMaxValue* getData (int thumbSampleIndex) noexcept
  224. {
  225. jassert (thumbSampleIndex < data.size());
  226. return data.getRawDataPointer() + thumbSampleIndex;
  227. }
  228. int getSize() const noexcept
  229. {
  230. return data.size();
  231. }
  232. void getMinMax (int startSample, int endSample, MinMaxValue& result) const noexcept
  233. {
  234. if (startSample >= 0)
  235. {
  236. endSample = jmin (endSample, data.size() - 1);
  237. int8 mx = -128;
  238. int8 mn = 127;
  239. while (startSample <= endSample)
  240. {
  241. auto& v = data.getReference (startSample);
  242. if (v.getMinValue() < mn) mn = v.getMinValue();
  243. if (v.getMaxValue() > mx) mx = v.getMaxValue();
  244. ++startSample;
  245. }
  246. if (mn <= mx)
  247. {
  248. result.set (mn, mx);
  249. return;
  250. }
  251. }
  252. result.set (1, 0);
  253. }
  254. void write (const MinMaxValue* values, int startIndex, int numValues)
  255. {
  256. resetPeak();
  257. if (startIndex + numValues > data.size())
  258. ensureSize (startIndex + numValues);
  259. auto* dest = getData (startIndex);
  260. for (int i = 0; i < numValues; ++i)
  261. dest[i] = values[i];
  262. }
  263. void resetPeak() noexcept
  264. {
  265. peakLevel = -1;
  266. }
  267. int getPeak() noexcept
  268. {
  269. if (peakLevel < 0)
  270. {
  271. for (auto& s : data)
  272. {
  273. auto peak = s.getPeak();
  274. if (peak > peakLevel)
  275. peakLevel = peak;
  276. }
  277. }
  278. return peakLevel;
  279. }
  280. private:
  281. Array<MinMaxValue> data;
  282. int peakLevel;
  283. void ensureSize (int thumbSamples)
  284. {
  285. auto extraNeeded = thumbSamples - data.size();
  286. if (extraNeeded > 0)
  287. data.insertMultiple (-1, MinMaxValue(), extraNeeded);
  288. }
  289. };
  290. //==============================================================================
  291. class AudioThumbnail::CachedWindow
  292. {
  293. public:
  294. CachedWindow() {}
  295. void invalidate()
  296. {
  297. cacheNeedsRefilling = true;
  298. }
  299. void drawChannel (Graphics& g, const Rectangle<int>& area,
  300. const double startTime, const double endTime,
  301. const int channelNum, const float verticalZoomFactor,
  302. const double rate, const int numChans, const int sampsPerThumbSample,
  303. LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
  304. {
  305. if (refillCache (area.getWidth(), startTime, endTime, rate,
  306. numChans, sampsPerThumbSample, levelData, chans)
  307. && isPositiveAndBelow (channelNum, numChannelsCached))
  308. {
  309. auto clip = g.getClipBounds().getIntersection (area.withWidth (jmin (numSamplesCached, area.getWidth())));
  310. if (! clip.isEmpty())
  311. {
  312. auto topY = (float) area.getY();
  313. auto bottomY = (float) area.getBottom();
  314. auto midY = (topY + bottomY) * 0.5f;
  315. auto vscale = verticalZoomFactor * (bottomY - topY) / 256.0f;
  316. auto* cacheData = getData (channelNum, clip.getX() - area.getX());
  317. RectangleList<float> waveform;
  318. waveform.ensureStorageAllocated (clip.getWidth());
  319. auto x = (float) clip.getX();
  320. for (int w = clip.getWidth(); --w >= 0;)
  321. {
  322. if (cacheData->isNonZero())
  323. {
  324. auto top = jmax (midY - cacheData->getMaxValue() * vscale - 0.3f, topY);
  325. auto bottom = jmin (midY - cacheData->getMinValue() * vscale + 0.3f, bottomY);
  326. waveform.addWithoutMerging (Rectangle<float> (x, top, 1.0f, bottom - top));
  327. }
  328. x += 1.0f;
  329. ++cacheData;
  330. }
  331. g.fillRectList (waveform);
  332. }
  333. }
  334. }
  335. private:
  336. Array<MinMaxValue> data;
  337. double cachedStart = 0, cachedTimePerPixel = 0;
  338. int numChannelsCached = 0, numSamplesCached = 0;
  339. bool cacheNeedsRefilling = true;
  340. bool refillCache (int numSamples, double startTime, double endTime,
  341. double rate, int numChans, int sampsPerThumbSample,
  342. LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
  343. {
  344. auto timePerPixel = (endTime - startTime) / numSamples;
  345. if (numSamples <= 0 || timePerPixel <= 0.0 || rate <= 0)
  346. {
  347. invalidate();
  348. return false;
  349. }
  350. if (numSamples == numSamplesCached
  351. && numChannelsCached == numChans
  352. && startTime == cachedStart
  353. && timePerPixel == cachedTimePerPixel
  354. && ! cacheNeedsRefilling)
  355. {
  356. return ! cacheNeedsRefilling;
  357. }
  358. numSamplesCached = numSamples;
  359. numChannelsCached = numChans;
  360. cachedStart = startTime;
  361. cachedTimePerPixel = timePerPixel;
  362. cacheNeedsRefilling = false;
  363. ensureSize (numSamples);
  364. if (timePerPixel * rate <= sampsPerThumbSample && levelData != nullptr)
  365. {
  366. auto sample = roundToInt (startTime * rate);
  367. Array<Range<float>> levels;
  368. int i;
  369. for (i = 0; i < numSamples; ++i)
  370. {
  371. auto nextSample = roundToInt ((startTime + timePerPixel) * rate);
  372. if (sample >= 0)
  373. {
  374. if (sample >= levelData->lengthInSamples)
  375. {
  376. for (int chan = 0; chan < numChannelsCached; ++chan)
  377. *getData (chan, i) = MinMaxValue();
  378. }
  379. else
  380. {
  381. levelData->getLevels (sample, jmax (1, nextSample - sample), levels);
  382. auto totalChans = jmin (levels.size(), numChannelsCached);
  383. for (int chan = 0; chan < totalChans; ++chan)
  384. getData (chan, i)->setFloat (levels.getReference (chan));
  385. }
  386. }
  387. startTime += timePerPixel;
  388. sample = nextSample;
  389. }
  390. numSamplesCached = i;
  391. }
  392. else
  393. {
  394. jassert (chans.size() == numChannelsCached);
  395. for (int channelNum = 0; channelNum < numChannelsCached; ++channelNum)
  396. {
  397. ThumbData* channelData = chans.getUnchecked (channelNum);
  398. MinMaxValue* cacheData = getData (channelNum, 0);
  399. auto timeToThumbSampleFactor = rate / (double) sampsPerThumbSample;
  400. startTime = cachedStart;
  401. auto sample = roundToInt (startTime * timeToThumbSampleFactor);
  402. for (int i = numSamples; --i >= 0;)
  403. {
  404. auto nextSample = roundToInt ((startTime + timePerPixel) * timeToThumbSampleFactor);
  405. channelData->getMinMax (sample, nextSample, *cacheData);
  406. ++cacheData;
  407. startTime += timePerPixel;
  408. sample = nextSample;
  409. }
  410. }
  411. }
  412. return true;
  413. }
  414. MinMaxValue* getData (const int channelNum, const int cacheIndex) noexcept
  415. {
  416. jassert (isPositiveAndBelow (channelNum, numChannelsCached) && isPositiveAndBelow (cacheIndex, data.size()));
  417. return data.getRawDataPointer() + channelNum * numSamplesCached
  418. + cacheIndex;
  419. }
  420. void ensureSize (const int numSamples)
  421. {
  422. auto itemsRequired = numSamples * numChannelsCached;
  423. if (data.size() < itemsRequired)
  424. data.insertMultiple (-1, MinMaxValue(), itemsRequired - data.size());
  425. }
  426. };
  427. //==============================================================================
  428. AudioThumbnail::AudioThumbnail (const int originalSamplesPerThumbnailSample,
  429. AudioFormatManager& formatManager,
  430. AudioThumbnailCache& cacheToUse)
  431. : formatManagerToUse (formatManager),
  432. cache (cacheToUse),
  433. window (new CachedWindow()),
  434. samplesPerThumbSample (originalSamplesPerThumbnailSample)
  435. {
  436. }
  437. AudioThumbnail::~AudioThumbnail()
  438. {
  439. clear();
  440. }
  441. void AudioThumbnail::clear()
  442. {
  443. source.reset();
  444. const ScopedLock sl (lock);
  445. clearChannelData();
  446. }
  447. void AudioThumbnail::clearChannelData()
  448. {
  449. window->invalidate();
  450. channels.clear();
  451. totalSamples = numSamplesFinished = 0;
  452. numChannels = 0;
  453. sampleRate = 0;
  454. sendChangeMessage();
  455. }
  456. void AudioThumbnail::reset (int newNumChannels, double newSampleRate, int64 totalSamplesInSource)
  457. {
  458. clear();
  459. const ScopedLock sl (lock);
  460. numChannels = newNumChannels;
  461. sampleRate = newSampleRate;
  462. totalSamples = totalSamplesInSource;
  463. createChannels (1 + (int) (totalSamplesInSource / samplesPerThumbSample));
  464. }
  465. void AudioThumbnail::createChannels (const int length)
  466. {
  467. while (channels.size() < numChannels)
  468. channels.add (new ThumbData (length));
  469. }
  470. //==============================================================================
  471. bool AudioThumbnail::loadFrom (InputStream& rawInput)
  472. {
  473. BufferedInputStream input (rawInput, 4096);
  474. if (input.readByte() != 'j' || input.readByte() != 'a' || input.readByte() != 't' || input.readByte() != 'm')
  475. return false;
  476. const ScopedLock sl (lock);
  477. clearChannelData();
  478. samplesPerThumbSample = input.readInt();
  479. totalSamples = input.readInt64(); // Total number of source samples.
  480. numSamplesFinished = input.readInt64(); // Number of valid source samples that have been read into the thumbnail.
  481. int32 numThumbnailSamples = input.readInt(); // Number of samples in the thumbnail data.
  482. numChannels = input.readInt(); // Number of audio channels.
  483. sampleRate = input.readInt(); // Source sample rate.
  484. input.skipNextBytes (16); // (reserved)
  485. createChannels (numThumbnailSamples);
  486. for (int i = 0; i < numThumbnailSamples; ++i)
  487. for (int chan = 0; chan < numChannels; ++chan)
  488. channels.getUnchecked(chan)->getData(i)->read (input);
  489. return true;
  490. }
  491. void AudioThumbnail::saveTo (OutputStream& output) const
  492. {
  493. const ScopedLock sl (lock);
  494. const int numThumbnailSamples = channels.size() == 0 ? 0 : channels.getUnchecked(0)->getSize();
  495. output.write ("jatm", 4);
  496. output.writeInt (samplesPerThumbSample);
  497. output.writeInt64 (totalSamples);
  498. output.writeInt64 (numSamplesFinished);
  499. output.writeInt (numThumbnailSamples);
  500. output.writeInt (numChannels);
  501. output.writeInt ((int) sampleRate);
  502. output.writeInt64 (0);
  503. output.writeInt64 (0);
  504. for (int i = 0; i < numThumbnailSamples; ++i)
  505. for (int chan = 0; chan < numChannels; ++chan)
  506. channels.getUnchecked(chan)->getData(i)->write (output);
  507. }
  508. //==============================================================================
  509. bool AudioThumbnail::setDataSource (LevelDataSource* newSource)
  510. {
  511. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  512. numSamplesFinished = 0;
  513. if (cache.loadThumb (*this, newSource->hashCode) && isFullyLoaded())
  514. {
  515. source = newSource; // (make sure this isn't done before loadThumb is called)
  516. source->lengthInSamples = totalSamples;
  517. source->sampleRate = sampleRate;
  518. source->numChannels = (unsigned int) numChannels;
  519. source->numSamplesFinished = numSamplesFinished;
  520. }
  521. else
  522. {
  523. source = newSource; // (make sure this isn't done before loadThumb is called)
  524. const ScopedLock sl (lock);
  525. source->initialise (numSamplesFinished);
  526. totalSamples = source->lengthInSamples;
  527. sampleRate = source->sampleRate;
  528. numChannels = (int32) source->numChannels;
  529. createChannels (1 + (int) (totalSamples / samplesPerThumbSample));
  530. }
  531. return sampleRate > 0 && totalSamples > 0;
  532. }
  533. bool AudioThumbnail::setSource (InputSource* const newSource)
  534. {
  535. clear();
  536. return newSource != nullptr && setDataSource (new LevelDataSource (*this, newSource));
  537. }
  538. void AudioThumbnail::setReader (AudioFormatReader* newReader, int64 hash)
  539. {
  540. clear();
  541. if (newReader != nullptr)
  542. setDataSource (new LevelDataSource (*this, newReader, hash));
  543. }
  544. int64 AudioThumbnail::getHashCode() const
  545. {
  546. return source == nullptr ? 0 : source->hashCode;
  547. }
  548. void AudioThumbnail::addBlock (int64 startSample, const AudioBuffer<float>& incoming,
  549. int startOffsetInBuffer, int numSamples)
  550. {
  551. jassert (startSample >= 0
  552. && startOffsetInBuffer >= 0
  553. && startOffsetInBuffer + numSamples <= incoming.getNumSamples());
  554. auto firstThumbIndex = (int) (startSample / samplesPerThumbSample);
  555. auto lastThumbIndex = (int) ((startSample + numSamples + (samplesPerThumbSample - 1)) / samplesPerThumbSample);
  556. auto numToDo = lastThumbIndex - firstThumbIndex;
  557. if (numToDo > 0)
  558. {
  559. auto numChans = jmin (channels.size(), incoming.getNumChannels());
  560. const HeapBlock<MinMaxValue> thumbData (numToDo * numChans);
  561. const HeapBlock<MinMaxValue*> thumbChannels (numChans);
  562. for (int chan = 0; chan < numChans; ++chan)
  563. {
  564. auto* sourceData = incoming.getReadPointer (chan, startOffsetInBuffer);
  565. auto* dest = thumbData + numToDo * chan;
  566. thumbChannels [chan] = dest;
  567. for (int i = 0; i < numToDo; ++i)
  568. {
  569. auto start = i * samplesPerThumbSample;
  570. dest[i].setFloat (FloatVectorOperations::findMinAndMax (sourceData + start, jmin (samplesPerThumbSample, numSamples - start)));
  571. }
  572. }
  573. setLevels (thumbChannels, firstThumbIndex, numChans, numToDo);
  574. }
  575. }
  576. void AudioThumbnail::setLevels (const MinMaxValue* const* values, int thumbIndex, int numChans, int numValues)
  577. {
  578. const ScopedLock sl (lock);
  579. for (int i = jmin (numChans, channels.size()); --i >= 0;)
  580. channels.getUnchecked(i)->write (values[i], thumbIndex, numValues);
  581. auto start = thumbIndex * (int64) samplesPerThumbSample;
  582. auto end = (thumbIndex + numValues) * (int64) samplesPerThumbSample;
  583. if (numSamplesFinished >= start && end > numSamplesFinished)
  584. numSamplesFinished = end;
  585. totalSamples = jmax (numSamplesFinished, totalSamples);
  586. window->invalidate();
  587. sendChangeMessage();
  588. }
  589. //==============================================================================
  590. int AudioThumbnail::getNumChannels() const noexcept
  591. {
  592. return numChannels;
  593. }
  594. double AudioThumbnail::getTotalLength() const noexcept
  595. {
  596. return sampleRate > 0 ? (totalSamples / sampleRate) : 0;
  597. }
  598. bool AudioThumbnail::isFullyLoaded() const noexcept
  599. {
  600. return numSamplesFinished >= totalSamples - samplesPerThumbSample;
  601. }
  602. double AudioThumbnail::getProportionComplete() const noexcept
  603. {
  604. return jlimit (0.0, 1.0, numSamplesFinished / (double) jmax ((int64) 1, totalSamples));
  605. }
  606. int64 AudioThumbnail::getNumSamplesFinished() const noexcept
  607. {
  608. return numSamplesFinished;
  609. }
  610. float AudioThumbnail::getApproximatePeak() const
  611. {
  612. const ScopedLock sl (lock);
  613. int peak = 0;
  614. for (auto* c : channels)
  615. peak = jmax (peak, c->getPeak());
  616. return jlimit (0, 127, peak) / 127.0f;
  617. }
  618. void AudioThumbnail::getApproximateMinMax (double startTime, double endTime, int channelIndex,
  619. float& minValue, float& maxValue) const noexcept
  620. {
  621. const ScopedLock sl (lock);
  622. MinMaxValue result;
  623. auto* data = channels [channelIndex];
  624. if (data != nullptr && sampleRate > 0)
  625. {
  626. auto firstThumbIndex = (int) ((startTime * sampleRate) / samplesPerThumbSample);
  627. auto lastThumbIndex = (int) (((endTime * sampleRate) + samplesPerThumbSample - 1) / samplesPerThumbSample);
  628. data->getMinMax (jmax (0, firstThumbIndex), lastThumbIndex, result);
  629. }
  630. minValue = result.getMinValue() / 128.0f;
  631. maxValue = result.getMaxValue() / 128.0f;
  632. }
  633. void AudioThumbnail::drawChannel (Graphics& g, const Rectangle<int>& area, double startTime,
  634. double endTime, int channelNum, float verticalZoomFactor)
  635. {
  636. const ScopedLock sl (lock);
  637. window->drawChannel (g, area, startTime, endTime, channelNum, verticalZoomFactor,
  638. sampleRate, numChannels, samplesPerThumbSample, source, channels);
  639. }
  640. void AudioThumbnail::drawChannels (Graphics& g, const Rectangle<int>& area, double startTimeSeconds,
  641. double endTimeSeconds, float verticalZoomFactor)
  642. {
  643. for (int i = 0; i < numChannels; ++i)
  644. {
  645. auto y1 = roundToInt ((i * area.getHeight()) / numChannels);
  646. auto y2 = roundToInt (((i + 1) * area.getHeight()) / numChannels);
  647. drawChannel (g, { area.getX(), area.getY() + y1, area.getWidth(), y2 - y1 },
  648. startTimeSeconds, endTimeSeconds, i, verticalZoomFactor);
  649. }
  650. }
  651. } // namespace juce