1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce
{
struct AudioThumbnail::MinMaxValue
{
MinMaxValue() noexcept
{
values[0] = 0;
values[1] = 0;
}
inline void set (const int8 newMin, const int8 newMax) noexcept
{
values[0] = newMin;
values[1] = newMax;
}
inline int8 getMinValue() const noexcept { return values[0]; }
inline int8 getMaxValue() const noexcept { return values[1]; }
inline void setFloat (Range<float> newRange) noexcept
{
// Workaround for an ndk armeabi compiler bug which crashes on signed saturation
#if JUCE_ANDROID
Range<float> limitedRange (jlimit (-1.0f, 1.0f, newRange.getStart()),
jlimit (-1.0f, 1.0f, newRange.getEnd()));
values[0] = (int8) (limitedRange.getStart() * 127.0f);
values[1] = (int8) (limitedRange.getEnd() * 127.0f);
#else
values[0] = (int8) jlimit (-128, 127, roundToInt (newRange.getStart() * 127.0f));
values[1] = (int8) jlimit (-128, 127, roundToInt (newRange.getEnd() * 127.0f));
#endif
if (values[0] == values[1])
{
if (values[1] == 127)
values[0]--;
else
values[1]++;
}
}
inline bool isNonZero() const noexcept
{
return values[1] > values[0];
}
inline int getPeak() const noexcept
{
return jmax (std::abs ((int) values[0]),
std::abs ((int) values[1]));
}
inline void read (InputStream& input) { input.read (values, 2); }
inline void write (OutputStream& output) { output.write (values, 2); }
private:
int8 values[2];
};
//==============================================================================
template <typename T>
class AudioBufferReader final : public AudioFormatReader
{
public:
AudioBufferReader (const AudioBuffer<T>* bufferIn, double rate)
: AudioFormatReader (nullptr, "AudioBuffer"), buffer (bufferIn)
{
sampleRate = rate;
bitsPerSample = 32;
lengthInSamples = buffer->getNumSamples();
numChannels = (unsigned int) buffer->getNumChannels();
usesFloatingPointData = std::is_floating_point_v<T>;
}
bool readSamples (int* const* destChannels,
int numDestChannels,
int startOffsetInDestBuffer,
int64 startSampleInFile,
int numSamples) override
{
clearSamplesBeyondAvailableLength (destChannels, numDestChannels, startOffsetInDestBuffer,
startSampleInFile, numSamples, lengthInSamples);
const auto numAvailableSamples = (int) ((int64) buffer->getNumSamples() - startSampleInFile);
const auto numSamplesToCopy = std::clamp (numAvailableSamples, 0, numSamples);
if (numSamplesToCopy == 0)
return true;
for (int i = 0; i < numDestChannels; ++i)
{
if (void* targetChannel = destChannels[i])
{
const auto dest = DestType (targetChannel) + startOffsetInDestBuffer;
if (i < buffer->getNumChannels())
dest.convertSamples (SourceType (buffer->getReadPointer (i) + startSampleInFile), numSamplesToCopy);
else
dest.clearSamples (numSamples);
}
}
return true;
}
private:
using SourceNumericalType =
std::conditional_t<std::is_same_v<T, int>, AudioData::Int32,
std::conditional_t<std::is_same_v<T, float>, AudioData::Float32, void>>;
using DestinationNumericalType = std::conditional_t<std::is_floating_point_v<T>, AudioData::Float32, AudioData::Int32>;
using DestType = AudioData::Pointer<DestinationNumericalType, AudioData::LittleEndian, AudioData::NonInterleaved, AudioData::NonConst>;
using SourceType = AudioData::Pointer<SourceNumericalType, AudioData::LittleEndian, AudioData::NonInterleaved, AudioData::Const>;
const AudioBuffer<T>* buffer;
};
//==============================================================================
class AudioThumbnail::LevelDataSource final : public TimeSliceClient
{
public:
LevelDataSource (AudioThumbnail& thumb, AudioFormatReader* newReader, int64 hash)
: hashCode (hash), owner (thumb), reader (newReader)
{
}
LevelDataSource (AudioThumbnail& thumb, InputSource* src)
: hashCode (src->hashCode()), owner (thumb), source (src)
{
}
~LevelDataSource() override
{
owner.cache.getTimeSliceThread().removeTimeSliceClient (this);
}
enum { timeBeforeDeletingReader = 3000 };
void initialise (int64 samplesFinished)
{
const ScopedLock sl (readerLock);
numSamplesFinished = samplesFinished;
createReader();
if (reader != nullptr)
{
lengthInSamples = reader->lengthInSamples;
numChannels = reader->numChannels;
sampleRate = reader->sampleRate;
if (lengthInSamples <= 0 || isFullyLoaded())
reader.reset();
else
owner.cache.getTimeSliceThread().addTimeSliceClient (this);
}
}
void getLevels (int64 startSample, int numSamples, Array<Range<float>>& levels)
{
const ScopedLock sl (readerLock);
if (reader == nullptr)
{
createReader();
if (reader != nullptr)
{
lastReaderUseTime = Time::getMillisecondCounter();
owner.cache.getTimeSliceThread().addTimeSliceClient (this);
}
}
if (reader != nullptr)
{
if (levels.size() < (int) reader->numChannels)
levels.insertMultiple (0, {}, (int) reader->numChannels - levels.size());
reader->readMaxLevels (startSample, numSamples, levels.getRawDataPointer(), (int) reader->numChannels);
lastReaderUseTime = Time::getMillisecondCounter();
}
}
void releaseResources()
{
const ScopedLock sl (readerLock);
reader.reset();
}
int useTimeSlice() override
{
if (isFullyLoaded())
{
if (reader != nullptr && source != nullptr)
{
if (Time::getMillisecondCounter() > lastReaderUseTime + timeBeforeDeletingReader)
releaseResources();
else
return 200;
}
return -1;
}
bool justFinished = false;
{
const ScopedLock sl (readerLock);
createReader();
if (reader != nullptr)
{
if (! readNextBlock())
return 0;
justFinished = true;
}
}
if (justFinished)
owner.cache.storeThumb (owner, hashCode);
return 200;
}
bool isFullyLoaded() const noexcept
{
return numSamplesFinished >= lengthInSamples;
}
inline int sampleToThumbSample (const int64 originalSample) const noexcept
{
return (int) (originalSample / owner.samplesPerThumbSample);
}
int64 lengthInSamples = 0, numSamplesFinished = 0;
double sampleRate = 0;
unsigned int numChannels = 0;
int64 hashCode = 0;
private:
AudioThumbnail& owner;
std::unique_ptr<InputSource> source;
std::unique_ptr<AudioFormatReader> reader;
CriticalSection readerLock;
std::atomic<uint32> lastReaderUseTime { 0 };
void createReader()
{
if (reader == nullptr && source != nullptr)
if (auto* audioFileStream = source->createInputStream())
reader.reset (owner.formatManagerToUse.createReaderFor (std::unique_ptr<InputStream> (audioFileStream)));
}
bool readNextBlock()
{
jassert (reader != nullptr);
if (! isFullyLoaded())
{
auto numToDo = (int) jmin (256 * (int64) owner.samplesPerThumbSample, lengthInSamples - numSamplesFinished);
if (numToDo > 0)
{
auto startSample = numSamplesFinished;
auto firstThumbIndex = sampleToThumbSample (startSample);
auto lastThumbIndex = sampleToThumbSample (startSample + numToDo);
auto numThumbSamps = lastThumbIndex - firstThumbIndex;
HeapBlock<MinMaxValue> levelData ((unsigned int) numThumbSamps * numChannels);
HeapBlock<MinMaxValue*> levels (numChannels);
for (int i = 0; i < (int) numChannels; ++i)
levels[i] = levelData + i * numThumbSamps;
HeapBlock<Range<float>> levelsRead (numChannels);
for (int i = 0; i < numThumbSamps; ++i)
{
reader->readMaxLevels ((firstThumbIndex + i) * owner.samplesPerThumbSample,
owner.samplesPerThumbSample, levelsRead, (int) numChannels);
for (int j = 0; j < (int) numChannels; ++j)
levels[j][i].setFloat (levelsRead[j]);
}
{
const ScopedUnlock su (readerLock);
owner.setLevels (levels, firstThumbIndex, (int) numChannels, numThumbSamps);
}
numSamplesFinished += numToDo;
lastReaderUseTime = Time::getMillisecondCounter();
}
}
return isFullyLoaded();
}
};
//==============================================================================
class AudioThumbnail::ThumbData
{
public:
ThumbData (int numThumbSamples)
{
ensureSize (numThumbSamples);
}
inline MinMaxValue* getData (int thumbSampleIndex) noexcept
{
jassert (thumbSampleIndex < data.size());
return data.getRawDataPointer() + thumbSampleIndex;
}
int getSize() const noexcept
{
return data.size();
}
void getMinMax (int startSample, int endSample, MinMaxValue& result) const noexcept
{
if (startSample >= 0)
{
endSample = jmin (endSample, data.size() - 1);
int8 mx = -128;
int8 mn = 127;
while (startSample <= endSample)
{
auto& v = data.getReference (startSample);
if (v.getMinValue() < mn) mn = v.getMinValue();
if (v.getMaxValue() > mx) mx = v.getMaxValue();
++startSample;
}
if (mn <= mx)
{
result.set (mn, mx);
return;
}
}
result.set (1, 0);
}
void write (const MinMaxValue* values, int startIndex, int numValues)
{
resetPeak();
if (startIndex + numValues > data.size())
ensureSize (startIndex + numValues);
auto* dest = getData (startIndex);
for (int i = 0; i < numValues; ++i)
dest[i] = values[i];
}
void resetPeak() noexcept
{
peakLevel = -1;
}
int getPeak() noexcept
{
if (peakLevel < 0)
{
for (auto& s : data)
{
auto peak = s.getPeak();
if (peak > peakLevel)
peakLevel = peak;
}
}
return peakLevel;
}
private:
Array<MinMaxValue> data;
int peakLevel = -1;
void ensureSize (int thumbSamples)
{
auto extraNeeded = thumbSamples - data.size();
if (extraNeeded > 0)
data.insertMultiple (-1, MinMaxValue(), extraNeeded);
}
};
//==============================================================================
class AudioThumbnail::CachedWindow
{
public:
CachedWindow() {}
void invalidate()
{
cacheNeedsRefilling = true;
}
void drawChannel (Graphics& g, const Rectangle<int>& area,
const double startTime, const double endTime,
const int channelNum, const float verticalZoomFactor,
const double rate, const int numChans, const int sampsPerThumbSample,
LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
{
if (refillCache (area.getWidth(), startTime, endTime, rate,
numChans, sampsPerThumbSample, levelData, chans)
&& isPositiveAndBelow (channelNum, numChannelsCached))
{
auto clip = g.getClipBounds().getIntersection (area.withWidth (jmin (numSamplesCached, area.getWidth())));
if (! clip.isEmpty())
{
auto topY = (float) area.getY();
auto bottomY = (float) area.getBottom();
auto midY = (topY + bottomY) * 0.5f;
auto vscale = verticalZoomFactor * (bottomY - topY) / 256.0f;
auto* cacheData = getData (channelNum, clip.getX() - area.getX());
RectangleList<float> waveform;
waveform.ensureStorageAllocated (clip.getWidth());
auto x = (float) clip.getX();
for (int w = clip.getWidth(); --w >= 0;)
{
if (cacheData->isNonZero())
{
auto top = jmax (midY - cacheData->getMaxValue() * vscale - 0.3f, topY);
auto bottom = jmin (midY - cacheData->getMinValue() * vscale + 0.3f, bottomY);
waveform.addWithoutMerging (Rectangle<float> (x, top, 1.0f, bottom - top));
}
x += 1.0f;
++cacheData;
}
g.fillRectList (waveform);
}
}
}
private:
Array<MinMaxValue> data;
double cachedStart = 0, cachedTimePerPixel = 0;
int numChannelsCached = 0, numSamplesCached = 0;
bool cacheNeedsRefilling = true;
bool refillCache (int numSamples, double startTime, double endTime,
double rate, int numChans, int sampsPerThumbSample,
LevelDataSource* levelData, const OwnedArray<ThumbData>& chans)
{
auto timePerPixel = (endTime - startTime) / numSamples;
if (numSamples <= 0 || timePerPixel <= 0.0 || rate <= 0)
{
invalidate();
return false;
}
if (numSamples == numSamplesCached
&& numChannelsCached == numChans
&& approximatelyEqual (startTime, cachedStart)
&& approximatelyEqual (timePerPixel, cachedTimePerPixel)
&& ! cacheNeedsRefilling)
{
return ! cacheNeedsRefilling;
}
numSamplesCached = numSamples;
numChannelsCached = numChans;
cachedStart = startTime;
cachedTimePerPixel = timePerPixel;
cacheNeedsRefilling = false;
ensureSize (numSamples);
if (timePerPixel * rate <= sampsPerThumbSample && levelData != nullptr)
{
auto sample = roundToInt (startTime * rate);
Array<Range<float>> levels;
int i;
for (i = 0; i < numSamples; ++i)
{
auto nextSample = roundToInt ((startTime + timePerPixel) * rate);
if (sample >= 0)
{
if (sample >= levelData->lengthInSamples)
{
for (int chan = 0; chan < numChannelsCached; ++chan)
*getData (chan, i) = MinMaxValue();
}
else
{
levelData->getLevels (sample, jmax (1, nextSample - sample), levels);
auto totalChans = jmin (levels.size(), numChannelsCached);
for (int chan = 0; chan < totalChans; ++chan)
getData (chan, i)->setFloat (levels.getReference (chan));
}
}
startTime += timePerPixel;
sample = nextSample;
}
numSamplesCached = i;
}
else
{
jassert (chans.size() == numChannelsCached);
for (int channelNum = 0; channelNum < numChannelsCached; ++channelNum)
{
ThumbData* channelData = chans.getUnchecked (channelNum);
MinMaxValue* cacheData = getData (channelNum, 0);
auto timeToThumbSampleFactor = rate / (double) sampsPerThumbSample;
startTime = cachedStart;
auto sample = roundToInt (startTime * timeToThumbSampleFactor);
for (int i = numSamples; --i >= 0;)
{
auto nextSample = roundToInt ((startTime + timePerPixel) * timeToThumbSampleFactor);
channelData->getMinMax (sample, nextSample, *cacheData);
++cacheData;
startTime += timePerPixel;
sample = nextSample;
}
}
}
return true;
}
MinMaxValue* getData (const int channelNum, const int cacheIndex) noexcept
{
jassert (isPositiveAndBelow (channelNum, numChannelsCached) && isPositiveAndBelow (cacheIndex, data.size()));
return data.getRawDataPointer() + channelNum * numSamplesCached
+ cacheIndex;
}
void ensureSize (const int numSamples)
{
auto itemsRequired = numSamples * numChannelsCached;
if (data.size() < itemsRequired)
data.insertMultiple (-1, MinMaxValue(), itemsRequired - data.size());
}
};
//==============================================================================
AudioThumbnail::AudioThumbnail (const int originalSamplesPerThumbnailSample,
AudioFormatManager& formatManager,
AudioThumbnailCache& cacheToUse)
: formatManagerToUse (formatManager),
cache (cacheToUse),
window (new CachedWindow()),
samplesPerThumbSample (originalSamplesPerThumbnailSample)
{
}
AudioThumbnail::~AudioThumbnail()
{
clear();
}
void AudioThumbnail::clear()
{
source.reset();
const ScopedLock sl (lock);
clearChannelData();
}
void AudioThumbnail::clearChannelData()
{
window->invalidate();
channels.clear();
totalSamples = numSamplesFinished = 0;
numChannels = 0;
sampleRate = 0;
sendChangeMessage();
}
void AudioThumbnail::reset (int newNumChannels, double newSampleRate, int64 totalSamplesInSource)
{
clear();
const ScopedLock sl (lock);
numChannels = newNumChannels;
sampleRate = newSampleRate;
totalSamples = totalSamplesInSource;
createChannels (1 + (int) (totalSamplesInSource / samplesPerThumbSample));
}
void AudioThumbnail::createChannels (const int length)
{
while (channels.size() < numChannels)
channels.add (new ThumbData (length));
}
//==============================================================================
bool AudioThumbnail::loadFrom (InputStream& rawInput)
{
BufferedInputStream input (rawInput, 4096);
if (input.readByte() != 'j' || input.readByte() != 'a' || input.readByte() != 't' || input.readByte() != 'm')
return false;
const ScopedLock sl (lock);
clearChannelData();
samplesPerThumbSample = input.readInt();
totalSamples = input.readInt64(); // Total number of source samples.
numSamplesFinished = input.readInt64(); // Number of valid source samples that have been read into the thumbnail.
int32 numThumbnailSamples = input.readInt(); // Number of samples in the thumbnail data.
numChannels = input.readInt(); // Number of audio channels.
sampleRate = input.readInt(); // Source sample rate.
input.skipNextBytes (16); // (reserved)
createChannels (numThumbnailSamples);
for (int i = 0; i < numThumbnailSamples; ++i)
for (int chan = 0; chan < numChannels; ++chan)
channels.getUnchecked (chan)->getData (i)->read (input);
return true;
}
void AudioThumbnail::saveTo (OutputStream& output) const
{
const ScopedLock sl (lock);
const int numThumbnailSamples = channels.size() == 0 ? 0 : channels.getUnchecked (0)->getSize();
output.write ("jatm", 4);
output.writeInt (samplesPerThumbSample);
output.writeInt64 (totalSamples);
output.writeInt64 (numSamplesFinished);
output.writeInt (numThumbnailSamples);
output.writeInt (numChannels);
output.writeInt ((int) sampleRate);
output.writeInt64 (0);
output.writeInt64 (0);
for (int i = 0; i < numThumbnailSamples; ++i)
for (int chan = 0; chan < numChannels; ++chan)
channels.getUnchecked (chan)->getData (i)->write (output);
}
//==============================================================================
bool AudioThumbnail::setDataSource (LevelDataSource* newSource)
{
JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
numSamplesFinished = 0;
auto wasSuccessful = [&] { return sampleRate > 0 && totalSamples > 0; };
if (cache.loadThumb (*this, newSource->hashCode) && isFullyLoaded())
{
source.reset (newSource); // (make sure this isn't done before loadThumb is called)
source->lengthInSamples = totalSamples;
source->sampleRate = sampleRate;
source->numChannels = (unsigned int) numChannels;
source->numSamplesFinished = numSamplesFinished;
return wasSuccessful();
}
source.reset (newSource);
const ScopedLock sl (lock);
source->initialise (numSamplesFinished);
totalSamples = source->lengthInSamples;
sampleRate = source->sampleRate;
numChannels = (int32) source->numChannels;
createChannels (1 + (int) (totalSamples / samplesPerThumbSample));
return wasSuccessful();
}
bool AudioThumbnail::setSource (InputSource* const newSource)
{
clear();
return newSource != nullptr && setDataSource (new LevelDataSource (*this, newSource));
}
void AudioThumbnail::setReader (AudioFormatReader* newReader, int64 hash)
{
clear();
if (newReader != nullptr)
setDataSource (new LevelDataSource (*this, newReader, hash));
}
void AudioThumbnail::setSource (const AudioBuffer<float>* newSource, double rate, int64 hash)
{
setReader (new AudioBufferReader<float> (newSource, rate), hash);
}
void AudioThumbnail::setSource (const AudioBuffer<int>* newSource, double rate, int64 hash)
{
setReader (new AudioBufferReader<int> (newSource, rate), hash);
}
int64 AudioThumbnail::getHashCode() const
{
return source == nullptr ? 0 : source->hashCode;
}
void AudioThumbnail::addBlock (int64 startSample, const AudioBuffer<float>& incoming,
int startOffsetInBuffer, int numSamples)
{
jassert (startSample >= 0
&& startOffsetInBuffer >= 0
&& startOffsetInBuffer + numSamples <= incoming.getNumSamples());
auto firstThumbIndex = (int) (startSample / samplesPerThumbSample);
auto lastThumbIndex = (int) ((startSample + numSamples + (samplesPerThumbSample - 1)) / samplesPerThumbSample);
auto numToDo = lastThumbIndex - firstThumbIndex;
if (numToDo > 0)
{
auto numChans = jmin (channels.size(), incoming.getNumChannels());
const HeapBlock<MinMaxValue> thumbData (numToDo * numChans);
const HeapBlock<MinMaxValue*> thumbChannels (numChans);
for (int chan = 0; chan < numChans; ++chan)
{
auto* sourceData = incoming.getReadPointer (chan, startOffsetInBuffer);
auto* dest = thumbData + numToDo * chan;
thumbChannels [chan] = dest;
for (int i = 0; i < numToDo; ++i)
{
auto start = i * samplesPerThumbSample;
dest[i].setFloat (FloatVectorOperations::findMinAndMax (sourceData + start, jmin (samplesPerThumbSample, numSamples - start)));
}
}
setLevels (thumbChannels, firstThumbIndex, numChans, numToDo);
}
}
void AudioThumbnail::setLevels (const MinMaxValue* const* values, int thumbIndex, int numChans, int numValues)
{
const ScopedLock sl (lock);
for (int i = jmin (numChans, channels.size()); --i >= 0;)
channels.getUnchecked (i)->write (values[i], thumbIndex, numValues);
auto start = thumbIndex * (int64) samplesPerThumbSample;
auto end = (thumbIndex + numValues) * (int64) samplesPerThumbSample;
if (numSamplesFinished >= start && end > numSamplesFinished)
numSamplesFinished = end;
totalSamples = jmax (numSamplesFinished, totalSamples);
window->invalidate();
sendChangeMessage();
}
//==============================================================================
int AudioThumbnail::getNumChannels() const noexcept
{
const ScopedLock sl (lock);
return numChannels;
}
double AudioThumbnail::getTotalLength() const noexcept
{
const ScopedLock sl (lock);
return sampleRate > 0 ? ((double) totalSamples / sampleRate) : 0.0;
}
bool AudioThumbnail::isFullyLoaded() const noexcept
{
const ScopedLock sl (lock);
return numSamplesFinished >= totalSamples - samplesPerThumbSample;
}
double AudioThumbnail::getProportionComplete() const noexcept
{
const ScopedLock sl (lock);
return jlimit (0.0, 1.0, (double) numSamplesFinished / (double) jmax ((int64) 1, totalSamples));
}
int64 AudioThumbnail::getNumSamplesFinished() const noexcept
{
const ScopedLock sl (lock);
return numSamplesFinished;
}
float AudioThumbnail::getApproximatePeak() const
{
const ScopedLock sl (lock);
int peak = 0;
for (auto* c : channels)
peak = jmax (peak, c->getPeak());
return (float) jlimit (0, 127, peak) / 127.0f;
}
void AudioThumbnail::getApproximateMinMax (double startTime, double endTime, int channelIndex,
float& minValue, float& maxValue) const noexcept
{
const ScopedLock sl (lock);
MinMaxValue result;
auto* data = channels [channelIndex];
if (data != nullptr && sampleRate > 0)
{
auto firstThumbIndex = (int) ((startTime * sampleRate) / samplesPerThumbSample);
auto lastThumbIndex = (int) (((endTime * sampleRate) + samplesPerThumbSample - 1) / samplesPerThumbSample);
data->getMinMax (jmax (0, firstThumbIndex), lastThumbIndex, result);
}
minValue = result.getMinValue() / 128.0f;
maxValue = result.getMaxValue() / 128.0f;
}
void AudioThumbnail::drawChannel (Graphics& g, const Rectangle<int>& area, double startTime,
double endTime, int channelNum, float verticalZoomFactor)
{
const ScopedLock sl (lock);
window->drawChannel (g, area, startTime, endTime, channelNum, verticalZoomFactor,
sampleRate, numChannels, samplesPerThumbSample, source.get(), channels);
}
void AudioThumbnail::drawChannels (Graphics& g, const Rectangle<int>& area, double startTimeSeconds,
double endTimeSeconds, float verticalZoomFactor)
{
for (int i = 0; i < numChannels; ++i)
{
auto y1 = roundToInt ((i * area.getHeight()) / numChannels);
auto y2 = roundToInt (((i + 1) * area.getHeight()) / numChannels);
drawChannel (g, { area.getX(), area.getY() + y1, area.getWidth(), y2 - y1 },
startTimeSeconds, endTimeSeconds, i, verticalZoomFactor);
}
}
} // namespace juce
|