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
4.2KB

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