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.

112 lines
3.9KB

  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. //==============================================================================
  16. /**
  17. An instance of this class is used to manage multiple AudioThumbnail objects.
  18. The cache runs a single background thread that is shared by all the thumbnails
  19. that need it, and it maintains a set of low-res previews in memory, to avoid
  20. having to re-scan audio files too often.
  21. @see AudioThumbnail
  22. @tags{Audio}
  23. */
  24. class JUCE_API AudioThumbnailCache
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a cache object.
  29. The maxNumThumbsToStore parameter lets you specify how many previews should
  30. be kept in memory at once.
  31. */
  32. explicit AudioThumbnailCache (int maxNumThumbsToStore);
  33. /** Destructor. */
  34. virtual ~AudioThumbnailCache();
  35. //==============================================================================
  36. /** Clears out any stored thumbnails. */
  37. void clear();
  38. /** Reloads the specified thumb if this cache contains the appropriate stored
  39. data.
  40. This is called automatically by the AudioThumbnail class, so you shouldn't
  41. normally need to call it directly.
  42. */
  43. bool loadThumb (AudioThumbnailBase& thumb, int64 hashCode);
  44. /** Stores the cachable data from the specified thumb in this cache.
  45. This is called automatically by the AudioThumbnail class, so you shouldn't
  46. normally need to call it directly.
  47. */
  48. void storeThumb (const AudioThumbnailBase& thumb, int64 hashCode);
  49. /** Tells the cache to forget about the thumb with the given hashcode. */
  50. void removeThumb (int64 hashCode);
  51. //==============================================================================
  52. /** Attempts to re-load a saved cache of thumbnails from a stream.
  53. The cache data must have been written by the writeToStream() method.
  54. This will replace all currently-loaded thumbnails with the new data.
  55. */
  56. bool readFromStream (InputStream& source);
  57. /** Writes all currently-loaded cache data to a stream.
  58. The resulting data can be re-loaded with readFromStream().
  59. */
  60. void writeToStream (OutputStream& stream);
  61. /** Returns the thread that client thumbnails can use. */
  62. TimeSliceThread& getTimeSliceThread() noexcept { return thread; }
  63. protected:
  64. /** This can be overridden to provide a custom callback for saving thumbnails
  65. once they have finished being loaded.
  66. */
  67. virtual void saveNewlyFinishedThumbnail (const AudioThumbnailBase&, int64 hashCode);
  68. /** This can be overridden to provide a custom callback for loading thumbnails
  69. from pre-saved files to save the cache the trouble of having to create them.
  70. */
  71. virtual bool loadNewThumb (AudioThumbnailBase&, int64 hashCode);
  72. private:
  73. //==============================================================================
  74. TimeSliceThread thread;
  75. class ThumbnailCacheEntry;
  76. OwnedArray<ThumbnailCacheEntry> thumbs;
  77. CriticalSection lock;
  78. int maxNumThumbsToStore;
  79. ThumbnailCacheEntry* findThumbFor (int64 hash) const;
  80. int findOldestThumb() const;
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioThumbnailCache)
  82. };
  83. } // namespace juce