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.

120 lines
3.4KB

  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 ThumbnailCacheEntry
  21. {
  22. int64 hash;
  23. uint32 lastUsed;
  24. MemoryBlock data;
  25. JUCE_LEAK_DETECTOR (ThumbnailCacheEntry);
  26. };
  27. //==============================================================================
  28. AudioThumbnailCache::AudioThumbnailCache (const int maxNumThumbsToStore_)
  29. : TimeSliceThread ("thumb cache"),
  30. maxNumThumbsToStore (maxNumThumbsToStore_)
  31. {
  32. startThread (2);
  33. }
  34. AudioThumbnailCache::~AudioThumbnailCache()
  35. {
  36. }
  37. ThumbnailCacheEntry* AudioThumbnailCache::findThumbFor (const int64 hash) const
  38. {
  39. for (int i = thumbs.size(); --i >= 0;)
  40. if (thumbs.getUnchecked(i)->hash == hash)
  41. return thumbs.getUnchecked(i);
  42. return nullptr;
  43. }
  44. bool AudioThumbnailCache::loadThumb (AudioThumbnail& thumb, const int64 hashCode)
  45. {
  46. ThumbnailCacheEntry* te = findThumbFor (hashCode);
  47. if (te != nullptr)
  48. {
  49. te->lastUsed = Time::getMillisecondCounter();
  50. MemoryInputStream in (te->data, false);
  51. thumb.loadFrom (in);
  52. return true;
  53. }
  54. return false;
  55. }
  56. void AudioThumbnailCache::storeThumb (const AudioThumbnail& thumb,
  57. const int64 hashCode)
  58. {
  59. ThumbnailCacheEntry* te = findThumbFor (hashCode);
  60. if (te == nullptr)
  61. {
  62. te = new ThumbnailCacheEntry();
  63. te->hash = hashCode;
  64. if (thumbs.size() < maxNumThumbsToStore)
  65. {
  66. thumbs.add (te);
  67. }
  68. else
  69. {
  70. int oldest = 0;
  71. uint32 oldestTime = Time::getMillisecondCounter() + 1;
  72. for (int i = thumbs.size(); --i >= 0;)
  73. {
  74. if (thumbs.getUnchecked(i)->lastUsed < oldestTime)
  75. {
  76. oldest = i;
  77. oldestTime = thumbs.getUnchecked(i)->lastUsed;
  78. }
  79. }
  80. thumbs.set (oldest, te);
  81. }
  82. }
  83. te->lastUsed = Time::getMillisecondCounter();
  84. MemoryOutputStream out (te->data, false);
  85. thumb.saveTo (out);
  86. }
  87. void AudioThumbnailCache::clear()
  88. {
  89. thumbs.clear();
  90. }
  91. END_JUCE_NAMESPACE