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.

807 lines
26KB

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