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.

191 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class AudioThumbnailCache::ThumbnailCacheEntry
  16. {
  17. public:
  18. ThumbnailCacheEntry (const int64 hashCode)
  19. : hash (hashCode),
  20. lastUsed (Time::getMillisecondCounter())
  21. {
  22. }
  23. ThumbnailCacheEntry (InputStream& in)
  24. : hash (in.readInt64()),
  25. lastUsed (0)
  26. {
  27. const int64 len = in.readInt64();
  28. in.readIntoMemoryBlock (data, (ssize_t) len);
  29. }
  30. void write (OutputStream& out)
  31. {
  32. out.writeInt64 (hash);
  33. out.writeInt64 ((int64) data.getSize());
  34. out << data;
  35. }
  36. int64 hash;
  37. uint32 lastUsed;
  38. MemoryBlock data;
  39. private:
  40. JUCE_LEAK_DETECTOR (ThumbnailCacheEntry)
  41. };
  42. //==============================================================================
  43. AudioThumbnailCache::AudioThumbnailCache (const int maxNumThumbs)
  44. : thread ("thumb cache"),
  45. maxNumThumbsToStore (maxNumThumbs)
  46. {
  47. jassert (maxNumThumbsToStore > 0);
  48. thread.startThread (2);
  49. }
  50. AudioThumbnailCache::~AudioThumbnailCache()
  51. {
  52. }
  53. AudioThumbnailCache::ThumbnailCacheEntry* AudioThumbnailCache::findThumbFor (const int64 hash) const
  54. {
  55. for (int i = thumbs.size(); --i >= 0;)
  56. if (thumbs.getUnchecked(i)->hash == hash)
  57. return thumbs.getUnchecked(i);
  58. return nullptr;
  59. }
  60. int AudioThumbnailCache::findOldestThumb() const
  61. {
  62. int oldest = 0;
  63. uint32 oldestTime = Time::getMillisecondCounter() + 1;
  64. for (int i = thumbs.size(); --i >= 0;)
  65. {
  66. const ThumbnailCacheEntry* const te = thumbs.getUnchecked(i);
  67. if (te->lastUsed < oldestTime)
  68. {
  69. oldest = i;
  70. oldestTime = te->lastUsed;
  71. }
  72. }
  73. return oldest;
  74. }
  75. bool AudioThumbnailCache::loadThumb (AudioThumbnailBase& thumb, const int64 hashCode)
  76. {
  77. const ScopedLock sl (lock);
  78. if (ThumbnailCacheEntry* te = findThumbFor (hashCode))
  79. {
  80. te->lastUsed = Time::getMillisecondCounter();
  81. MemoryInputStream in (te->data, false);
  82. thumb.loadFrom (in);
  83. return true;
  84. }
  85. return loadNewThumb (thumb, hashCode);
  86. }
  87. void AudioThumbnailCache::storeThumb (const AudioThumbnailBase& thumb,
  88. const int64 hashCode)
  89. {
  90. const ScopedLock sl (lock);
  91. ThumbnailCacheEntry* te = findThumbFor (hashCode);
  92. if (te == nullptr)
  93. {
  94. te = new ThumbnailCacheEntry (hashCode);
  95. if (thumbs.size() < maxNumThumbsToStore)
  96. thumbs.add (te);
  97. else
  98. thumbs.set (findOldestThumb(), te);
  99. }
  100. {
  101. MemoryOutputStream out (te->data, false);
  102. thumb.saveTo (out);
  103. }
  104. saveNewlyFinishedThumbnail (thumb, hashCode);
  105. }
  106. void AudioThumbnailCache::clear()
  107. {
  108. const ScopedLock sl (lock);
  109. thumbs.clear();
  110. }
  111. void AudioThumbnailCache::removeThumb (const int64 hashCode)
  112. {
  113. const ScopedLock sl (lock);
  114. for (int i = thumbs.size(); --i >= 0;)
  115. if (thumbs.getUnchecked(i)->hash == hashCode)
  116. thumbs.remove (i);
  117. }
  118. static int getThumbnailCacheFileMagicHeader() noexcept
  119. {
  120. return (int) ByteOrder::littleEndianInt ("ThmC");
  121. }
  122. bool AudioThumbnailCache::readFromStream (InputStream& source)
  123. {
  124. if (source.readInt() != getThumbnailCacheFileMagicHeader())
  125. return false;
  126. const ScopedLock sl (lock);
  127. clear();
  128. int numThumbnails = jmin (maxNumThumbsToStore, source.readInt());
  129. while (--numThumbnails >= 0 && ! source.isExhausted())
  130. thumbs.add (new ThumbnailCacheEntry (source));
  131. return true;
  132. }
  133. void AudioThumbnailCache::writeToStream (OutputStream& out)
  134. {
  135. const ScopedLock sl (lock);
  136. out.writeInt (getThumbnailCacheFileMagicHeader());
  137. out.writeInt (thumbs.size());
  138. for (int i = 0; i < thumbs.size(); ++i)
  139. thumbs.getUnchecked(i)->write (out);
  140. }
  141. void AudioThumbnailCache::saveNewlyFinishedThumbnail (const AudioThumbnailBase&, int64)
  142. {
  143. }
  144. bool AudioThumbnailCache::loadNewThumb (AudioThumbnailBase&, int64)
  145. {
  146. return false;
  147. }
  148. } // namespace juce