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.

797 lines
25KB

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