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.

119 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. An instance of this class is used to manage multiple AudioThumbnail objects.
  23. The cache runs a single background thread that is shared by all the thumbnails
  24. that need it, and it maintains a set of low-res previews in memory, to avoid
  25. having to re-scan audio files too often.
  26. @see AudioThumbnail
  27. @tags{Audio}
  28. */
  29. class JUCE_API AudioThumbnailCache
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a cache object.
  34. The maxNumThumbsToStore parameter lets you specify how many previews should
  35. be kept in memory at once.
  36. */
  37. explicit AudioThumbnailCache (int maxNumThumbsToStore);
  38. /** Destructor. */
  39. virtual ~AudioThumbnailCache();
  40. //==============================================================================
  41. /** Clears out any stored thumbnails. */
  42. void clear();
  43. /** Reloads the specified thumb if this cache contains the appropriate stored
  44. data.
  45. This is called automatically by the AudioThumbnail class, so you shouldn't
  46. normally need to call it directly.
  47. */
  48. bool loadThumb (AudioThumbnailBase& thumb, int64 hashCode);
  49. /** Stores the cachable data from the specified thumb in this cache.
  50. This is called automatically by the AudioThumbnail class, so you shouldn't
  51. normally need to call it directly.
  52. */
  53. void storeThumb (const AudioThumbnailBase& thumb, int64 hashCode);
  54. /** Tells the cache to forget about the thumb with the given hashcode. */
  55. void removeThumb (int64 hashCode);
  56. //==============================================================================
  57. /** Attempts to re-load a saved cache of thumbnails from a stream.
  58. The cache data must have been written by the writeToStream() method.
  59. This will replace all currently-loaded thumbnails with the new data.
  60. */
  61. bool readFromStream (InputStream& source);
  62. /** Writes all currently-loaded cache data to a stream.
  63. The resulting data can be re-loaded with readFromStream().
  64. */
  65. void writeToStream (OutputStream& stream);
  66. /** Returns the thread that client thumbnails can use. */
  67. TimeSliceThread& getTimeSliceThread() noexcept { return thread; }
  68. protected:
  69. /** This can be overridden to provide a custom callback for saving thumbnails
  70. once they have finished being loaded.
  71. */
  72. virtual void saveNewlyFinishedThumbnail (const AudioThumbnailBase&, int64 hashCode);
  73. /** This can be overridden to provide a custom callback for loading thumbnails
  74. from pre-saved files to save the cache the trouble of having to create them.
  75. */
  76. virtual bool loadNewThumb (AudioThumbnailBase&, int64 hashCode);
  77. private:
  78. //==============================================================================
  79. TimeSliceThread thread;
  80. class ThumbnailCacheEntry;
  81. OwnedArray<ThumbnailCacheEntry> thumbs;
  82. CriticalSection lock;
  83. int maxNumThumbsToStore;
  84. ThumbnailCacheEntry* findThumbFor (int64 hash) const;
  85. int findOldestThumb() const;
  86. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioThumbnailCache)
  87. };
  88. } // namespace juce