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 - "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. struct AudioThumbnail::MinMaxValue
  19. {
  20. MinMaxValue() noexcept
  21. {
  22. values[0] = 0;
  23. values[1] = 0;
  24. }
  25. inline void set (const char newMin, const char newMax) noexcept
  26. {
  27. values[0] = newMin;
  28. values[1] = newMax;
  29. }
  30. inline char getMinValue() const noexcept { return values[0]; }
  31. inline char getMaxValue() const noexcept { return values[1]; }
  32. inline void setFloat (const float newMin, const float newMax) noexcept
  33. {
  34. values[0] = (char) jlimit (-128, 127, roundFloatToInt (newMin * 127.0f));
  35. values[1] = (char) jlimit (-128, 127, roundFloatToInt (newMax * 127.0f));
  36. if (values[0] == values[1])
  37. {
  38. if (values[1] == 127)
  39. values[0]--;
  40. else
  41. values[1]++;
  42. }
  43. }
  44. inline bool isNonZero() const noexcept
  45. {
  46. return values[1] > values[0];
  47. }
  48. inline int getPeak() const noexcept
  49. {
  50. return jmax (std::abs ((int) values[0]),
  51. std::abs ((int) values[1]));
  52. }
  53. inline void read (InputStream& input) { input.read (values, 2); }
  54. inline void write (OutputStream& output) { output.write (values, 2); }
  55. private:
  56. char values[2];
  57. };
  58. //==============================================================================
  59. class AudioThumbnail::LevelDataSource : public TimeSliceClient
  60. {
  61. public:
  62. LevelDataSource (AudioThumbnail& thumb, AudioFormatReader* newReader, int64 hash)
  63. : lengthInSamples (0), numSamplesFinished (0), sampleRate (0), numChannels (0),
  64. hashCode (hash), owner (thumb), reader (newReader)
  65. {
  66. }
  67. LevelDataSource (AudioThumbnail& thumb, InputSource* src)
  68. : lengthInSamples (0), numSamplesFinished (0), sampleRate (0), numChannels (0),
  69. hashCode (src->hashCode()), owner (thumb), source (src)
  70. {
  71. }
  72. ~LevelDataSource()
  73. {
  74. owner.cache.getTimeSliceThread().removeTimeSliceClient (this);
  75. }
  76. enum { timeBeforeDeletingReader = 3000 };
  77. void initialise (int64 samplesFinished)
  78. {
  79. const ScopedLock sl (readerLock);
  80. numSamplesFinished = samplesFinished;
  81. createReader();
  82. if (reader != nullptr)
  83. {
  84. lengthInSamples = reader->lengthInSamples;
  85. numChannels = reader->numChannels;
  86. sampleRate = reader->sampleRate;
  87. if (lengthInSamples <= 0 || isFullyLoaded())
  88. reader = nullptr;
  89. else
  90. owner.cache.getTimeSliceThread().addTimeSliceClient (this);
  91. }
  92. }
  93. void getLevels (int64 startSample, int numSamples, Array<float>& levels)
  94. {
  95. const ScopedLock sl (readerLock);
  96. if (reader == nullptr)
  97. {
  98. createReader();
  99. if (reader != nullptr)
  100. {
  101. lastReaderUseTime = Time::getMillisecondCounter();
  102. owner.cache.getTimeSliceThread().addTimeSliceClient (this);
  103. }
  104. }
  105. if (reader != nullptr)
  106. {
  107. float l[4] = { 0 };
  108. reader->readMaxLevels (startSample, numSamples, l[0], l[1], l[2], l[3]);
  109. levels.clearQuick();
  110. levels.addArray ((const float*) l, 4);
  111. lastReaderUseTime = Time::getMillisecondCounter();
  112. }
  113. }
  114. void releaseResources()
  115. {
  116. const ScopedLock sl (readerLock);
  117. reader = nullptr;
  118. }
  119. int useTimeSlice()
  120. {
  121. if (isFullyLoaded())
  122. {
  123. if (reader != nullptr && source != nullptr)
  124. {
  125. if (Time::getMillisecondCounter() > lastReaderUseTime + timeBeforeDeletingReader)
  126. releaseResources();
  127. else
  128. return 200;
  129. }
  130. return -1;
  131. }
  132. bool justFinished = false;
  133. {
  134. const ScopedLock sl (readerLock);
  135. createReader();
  136. if (reader != nullptr)
  137. {
  138. if (! readNextBlock())
  139. return 0;
  140. justFinished = true;
  141. }
  142. }
  143. if (justFinished)
  144. owner.cache.storeThumb (owner, hashCode);
  145. return 200;
  146. }
  147. bool isFullyLoaded() const noexcept
  148. {
  149. return numSamplesFinished >= lengthInSamples;
  150. }
  151. inline int sampleToThumbSample (const int64 originalSample) const noexcept
  152. {
  153. return (int) (originalSample / owner.samplesPerThumbSample);
  154. }
  155. int64 lengthInSamples, numSamplesFinished;
  156. double sampleRate;
  157. unsigned int numChannels;
  158. int64 hashCode;
  159. private:
  160. AudioThumbnail& owner;
  161. ScopedPointer <InputSource> source;
  162. ScopedPointer <AudioFormatReader> reader;
  163. CriticalSection readerLock;
  164. uint32 lastReaderUseTime;
  165. void createReader()
  166. {
  167. if (reader == nullptr && source != nullptr)
  168. if (InputStream* audioFileStream = source->createInputStream())
  169. reader = owner.formatManagerToUse.createReaderFor (audioFileStream);
  170. }
  171. bool readNextBlock()
  172. {
  173. jassert (reader != nullptr);
  174. if (! isFullyLoaded())
  175. {
  176. const int numToDo = (int) jmin (256 * (int64) owner.samplesPerThumbSample, lengthInSamples - numSamplesFinished);
  177. if (numToDo > 0)
  178. {
  179. int64 startSample = numSamplesFinished;
  180. const int firstThumbIndex = sampleToThumbSample (startSample);
  181. const int lastThumbIndex = sampleToThumbSample (startSample + numToDo);
  182. const int numThumbSamps = lastThumbIndex - firstThumbIndex;
  183. HeapBlock<MinMaxValue> levelData ((size_t) numThumbSamps * 2);
  184. MinMaxValue* levels[2] = { levelData, levelData + numThumbSamps };
  185. for (int i = 0; i < numThumbSamps; ++i)
  186. {
  187. float lowestLeft, highestLeft, lowestRight, highestRight;
  188. reader->readMaxLevels ((firstThumbIndex + i) * owner.samplesPerThumbSample, owner.samplesPerThumbSample,
  189. lowestLeft, highestLeft, lowestRight, highestRight);
  190. levels[0][i].setFloat (lowestLeft, highestLeft);
  191. levels[1][i].setFloat (lowestRight, highestRight);
  192. }
  193. {
  194. const ScopedUnlock su (readerLock);
  195. owner.setLevels (levels, firstThumbIndex, 2, numThumbSamps);
  196. }
  197. numSamplesFinished += numToDo;
  198. lastReaderUseTime = Time::getMillisecondCounter();
  199. }
  200. }
  201. return isFullyLoaded();
  202. }
  203. };
  204. //==============================================================================
  205. class AudioThumbnail::ThumbData
  206. {
  207. public:
  208. ThumbData (const int numThumbSamples)
  209. : peakLevel (-1)
  210. {
  211. ensureSize (numThumbSamples);
  212. }
  213. inline MinMaxValue* getData (const int thumbSampleIndex) noexcept
  214. {
  215. jassert (thumbSampleIndex < data.size());
  216. return data.getRawDataPointer() + thumbSampleIndex;
  217. }
  218. int getSize() const noexcept
  219. {
  220. return data.size();
  221. }
  222. void getMinMax (int startSample, int endSample, MinMaxValue& result) const noexcept
  223. {
  224. if (startSample >= 0)
  225. {
  226. endSample = jmin (endSample, data.size() - 1);
  227. char mx = -128;
  228. char mn = 127;
  229. while (startSample <= endSample)
  230. {
  231. const MinMaxValue& v = data.getReference (startSample);
  232. if (v.getMinValue() < mn) mn = v.getMinValue();
  233. if (v.getMaxValue() > mx) mx = v.getMaxValue();
  234. ++startSample;
  235. }
  236. if (mn <= mx)
  237. {
  238. result.set (mn, mx);
  239. return;
  240. }
  241. }
  242. result.set (1, 0);
  243. }
  244. void write (const MinMaxValue* const values, const int startIndex, const int numValues)
  245. {
  246. resetPeak();
  247. if (startIndex + numValues > data.size())
  248. ensureSize (startIndex + numValues);
  249. MinMaxValue* const dest = getData (startIndex);
  250. for (int i = 0; i < numValues; ++i)
  251. dest[i] = values[i];
  252. }
  253. void resetPeak() noexcept
  254. {
  255. peakLevel = -1;
  256. }
  257. int getPeak() noexcept
  258. {
  259. if (peakLevel < 0)
  260. {
  261. for (int i = 0; i < data.size(); ++i)
  262. {
  263. const int peak = data[i].getPeak();
  264. if (peak > peakLevel)
  265. peakLevel = peak;
  266. }
  267. }
  268. return peakLevel;
  269. }
  270. private:
  271. Array <MinMaxValue> data;
  272. int peakLevel;
  273. void ensureSize (const int thumbSamples)
  274. {
  275. const int extraNeeded = thumbSamples - data.size();
  276. if (extraNeeded > 0)
  277. data.insertMultiple (-1, MinMaxValue(), extraNeeded);
  278. }
  279. };
  280. //==============================================================================
  281. class AudioThumbnail::CachedWindow
  282. {
  283. public:
  284. CachedWindow()
  285. : cachedStart (0), cachedTimePerPixel (0),
  286. numChannelsCached (0), numSamplesCached (0),
  287. cacheNeedsRefilling (true)
  288. {
  289. }
  290. void invalidate()
  291. {
  292. cacheNeedsRefilling = true;
  293. }
  294. void drawChannel (Graphics& g, const Rectangle<int>& area,
  295. const double startTime, const double endTime,
  296. const int channelNum, const float verticalZoomFactor,
  297. const double rate, const int numChans, const int sampsPerThumbSample,
  298. LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
  299. {
  300. if (refillCache (area.getWidth(), startTime, endTime, rate,
  301. numChans, sampsPerThumbSample, levelData, chans)
  302. && isPositiveAndBelow (channelNum, numChannelsCached))
  303. {
  304. const Rectangle<int> clip (g.getClipBounds().getIntersection (area.withWidth (jmin (numSamplesCached, area.getWidth()))));
  305. if (! clip.isEmpty())
  306. {
  307. const float topY = (float) area.getY();
  308. const float bottomY = (float) area.getBottom();
  309. const float midY = (topY + bottomY) * 0.5f;
  310. const float vscale = verticalZoomFactor * (bottomY - topY) / 256.0f;
  311. const MinMaxValue* cacheData = getData (channelNum, clip.getX() - area.getX());
  312. int x = clip.getX();
  313. for (int w = clip.getWidth(); --w >= 0;)
  314. {
  315. if (cacheData->isNonZero())
  316. g.drawVerticalLine (x, jmax (midY - cacheData->getMaxValue() * vscale - 0.3f, topY),
  317. jmin (midY - cacheData->getMinValue() * vscale + 0.3f, bottomY));
  318. ++x;
  319. ++cacheData;
  320. }
  321. }
  322. }
  323. }
  324. private:
  325. Array <MinMaxValue> data;
  326. double cachedStart, cachedTimePerPixel;
  327. int numChannelsCached, numSamplesCached;
  328. bool cacheNeedsRefilling;
  329. bool refillCache (const int numSamples, double startTime, const double endTime,
  330. const double rate, const int numChans, const int sampsPerThumbSample,
  331. LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
  332. {
  333. const double timePerPixel = (endTime - startTime) / numSamples;
  334. if (numSamples <= 0 || timePerPixel <= 0.0 || rate <= 0)
  335. {
  336. invalidate();
  337. return false;
  338. }
  339. if (numSamples == numSamplesCached
  340. && numChannelsCached == numChans
  341. && startTime == cachedStart
  342. && timePerPixel == cachedTimePerPixel
  343. && ! cacheNeedsRefilling)
  344. {
  345. return ! cacheNeedsRefilling;
  346. }
  347. numSamplesCached = numSamples;
  348. numChannelsCached = numChans;
  349. cachedStart = startTime;
  350. cachedTimePerPixel = timePerPixel;
  351. cacheNeedsRefilling = false;
  352. ensureSize (numSamples);
  353. if (timePerPixel * rate <= sampsPerThumbSample && levelData != nullptr)
  354. {
  355. int sample = roundToInt (startTime * rate);
  356. Array<float> levels;
  357. int i;
  358. for (i = 0; i < numSamples; ++i)
  359. {
  360. const int nextSample = roundToInt ((startTime + timePerPixel) * rate);
  361. if (sample >= 0)
  362. {
  363. if (sample >= levelData->lengthInSamples)
  364. break;
  365. levelData->getLevels (sample, jmax (1, nextSample - sample), levels);
  366. const int totalChans = jmin (levels.size() / 2, numChannelsCached);
  367. for (int chan = 0; chan < totalChans; ++chan)
  368. getData (chan, i)->setFloat (levels.getUnchecked (chan * 2),
  369. levels.getUnchecked (chan * 2 + 1));
  370. }
  371. startTime += timePerPixel;
  372. sample = nextSample;
  373. }
  374. numSamplesCached = i;
  375. }
  376. else
  377. {
  378. jassert (chans.size() == numChannelsCached);
  379. for (int channelNum = 0; channelNum < numChannelsCached; ++channelNum)
  380. {
  381. ThumbData* channelData = chans.getUnchecked (channelNum);
  382. MinMaxValue* cacheData = getData (channelNum, 0);
  383. const double timeToThumbSampleFactor = rate / (double) sampsPerThumbSample;
  384. startTime = cachedStart;
  385. int sample = roundToInt (startTime * timeToThumbSampleFactor);
  386. for (int i = numSamples; --i >= 0;)
  387. {
  388. const int nextSample = roundToInt ((startTime + timePerPixel) * timeToThumbSampleFactor);
  389. channelData->getMinMax (sample, nextSample, *cacheData);
  390. ++cacheData;
  391. startTime += timePerPixel;
  392. sample = nextSample;
  393. }
  394. }
  395. }
  396. return true;
  397. }
  398. MinMaxValue* getData (const int channelNum, const int cacheIndex) noexcept
  399. {
  400. jassert (isPositiveAndBelow (channelNum, numChannelsCached) && isPositiveAndBelow (cacheIndex, data.size()));
  401. return data.getRawDataPointer() + channelNum * numSamplesCached
  402. + cacheIndex;
  403. }
  404. void ensureSize (const int numSamples)
  405. {
  406. const int itemsRequired = numSamples * numChannelsCached;
  407. if (data.size() < itemsRequired)
  408. data.insertMultiple (-1, MinMaxValue(), itemsRequired - data.size());
  409. }
  410. };
  411. //==============================================================================
  412. AudioThumbnail::AudioThumbnail (const int originalSamplesPerThumbnailSample,
  413. AudioFormatManager& formatManager,
  414. AudioThumbnailCache& cacheToUse)
  415. : formatManagerToUse (formatManager),
  416. cache (cacheToUse),
  417. window (new CachedWindow()),
  418. samplesPerThumbSample (originalSamplesPerThumbnailSample),
  419. totalSamples (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. }