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.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_AUDIOTHUMBNAILCACHE_H_INCLUDED
  18. #define JUCE_AUDIOTHUMBNAILCACHE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. An instance of this class is used to manage multiple AudioThumbnail objects.
  22. The cache runs a single background thread that is shared by all the thumbnails
  23. that need it, and it maintains a set of low-res previews in memory, to avoid
  24. having to re-scan audio files too often.
  25. @see AudioThumbnail
  26. */
  27. class JUCE_API AudioThumbnailCache
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a cache object.
  32. The maxNumThumbsToStore parameter lets you specify how many previews should
  33. be kept in memory at once.
  34. */
  35. explicit AudioThumbnailCache (int maxNumThumbsToStore);
  36. /** Destructor. */
  37. virtual ~AudioThumbnailCache();
  38. //==============================================================================
  39. /** Clears out any stored thumbnails. */
  40. void clear();
  41. /** Reloads the specified thumb if this cache contains the appropriate stored
  42. data.
  43. This is called automatically by the AudioThumbnail class, so you shouldn't
  44. normally need to call it directly.
  45. */
  46. bool loadThumb (AudioThumbnailBase& thumb, int64 hashCode);
  47. /** Stores the cachable data from the specified thumb in this cache.
  48. This is called automatically by the AudioThumbnail class, so you shouldn't
  49. normally need to call it directly.
  50. */
  51. void storeThumb (const AudioThumbnailBase& thumb, int64 hashCode);
  52. /** Tells the cache to forget about the thumb with the given hashcode. */
  53. void removeThumb (int64 hashCode);
  54. //==============================================================================
  55. /** Attempts to re-load a saved cache of thumbnails from a stream.
  56. The cache data must have been written by the writeToStream() method.
  57. This will replace all currently-loaded thumbnails with the new data.
  58. */
  59. bool readFromStream (InputStream& source);
  60. /** Writes all currently-loaded cache data to a stream.
  61. The resulting data can be re-loaded with readFromStream().
  62. */
  63. void writeToStream (OutputStream& stream);
  64. /** Returns the thread that client thumbnails can use. */
  65. TimeSliceThread& getTimeSliceThread() noexcept { return thread; }
  66. protected:
  67. /** This can be overridden to provide a custom callback for saving thumbnails
  68. once they have finished being loaded.
  69. */
  70. virtual void saveNewlyFinishedThumbnail (const AudioThumbnailBase&, int64 hashCode);
  71. /** This can be overridden to provide a custom callback for loading thumbnails
  72. from pre-saved files to save the cache the trouble of having to create them.
  73. */
  74. virtual bool loadNewThumb (AudioThumbnailBase&, int64 hashCode);
  75. private:
  76. //==============================================================================
  77. TimeSliceThread thread;
  78. class ThumbnailCacheEntry;
  79. friend struct ContainerDeletePolicy<ThumbnailCacheEntry>;
  80. OwnedArray<ThumbnailCacheEntry> thumbs;
  81. CriticalSection lock;
  82. int maxNumThumbsToStore;
  83. ThumbnailCacheEntry* findThumbFor (int64 hash) const;
  84. int findOldestThumb() const;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioThumbnailCache)
  86. };
  87. #endif // JUCE_AUDIOTHUMBNAILCACHE_H_INCLUDED