JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_AudioThumbnailCache.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31public:
32 ThumbnailCacheEntry (const int64 hashCode)
33 : hash (hashCode),
35 {
36 }
37
39 : hash (in.readInt64()),
40 lastUsed (0)
41 {
42 const int64 len = in.readInt64();
43 in.readIntoMemoryBlock (data, (ssize_t) len);
44 }
45
46 void write (OutputStream& out)
47 {
48 out.writeInt64 (hash);
49 out.writeInt64 ((int64) data.getSize());
50 out << data;
51 }
52
53 int64 hash;
54 uint32 lastUsed;
55 MemoryBlock data;
56
57private:
59};
60
61//==============================================================================
63 : thread ("thumb cache"),
64 maxNumThumbsToStore (maxNumThumbs)
65{
66 jassert (maxNumThumbsToStore > 0);
68}
69
73
74AudioThumbnailCache::ThumbnailCacheEntry* AudioThumbnailCache::findThumbFor (const int64 hash) const
75{
76 for (int i = thumbs.size(); --i >= 0;)
77 if (thumbs.getUnchecked (i)->hash == hash)
78 return thumbs.getUnchecked (i);
79
80 return nullptr;
81}
82
83int AudioThumbnailCache::findOldestThumb() const
84{
85 int oldest = 0;
87
88 for (int i = thumbs.size(); --i >= 0;)
89 {
90 const ThumbnailCacheEntry* const te = thumbs.getUnchecked (i);
91
92 if (te->lastUsed < oldestTime)
93 {
94 oldest = i;
95 oldestTime = te->lastUsed;
96 }
97 }
98
99 return oldest;
100}
101
103{
104 const ScopedLock sl (lock);
105
106 if (ThumbnailCacheEntry* te = findThumbFor (hashCode))
107 {
108 te->lastUsed = Time::getMillisecondCounter();
109
110 MemoryInputStream in (te->data, false);
111 thumb.loadFrom (in);
112 return true;
113 }
114
115 return loadNewThumb (thumb, hashCode);
116}
117
119 const int64 hashCode)
120{
121 const ScopedLock sl (lock);
122 ThumbnailCacheEntry* te = findThumbFor (hashCode);
123
124 if (te == nullptr)
125 {
126 te = new ThumbnailCacheEntry (hashCode);
127
128 if (thumbs.size() < maxNumThumbsToStore)
129 thumbs.add (te);
130 else
131 thumbs.set (findOldestThumb(), te);
132 }
133
134 {
135 MemoryOutputStream out (te->data, false);
136 thumb.saveTo (out);
137 }
138
140}
141
143{
144 const ScopedLock sl (lock);
145 thumbs.clear();
146}
147
149{
150 const ScopedLock sl (lock);
151
152 for (int i = thumbs.size(); --i >= 0;)
153 if (thumbs.getUnchecked (i)->hash == hashCode)
154 thumbs.remove (i);
155}
156
157static int getThumbnailCacheFileMagicHeader() noexcept
158{
159 return (int) ByteOrder::littleEndianInt ("ThmC");
160}
161
163{
164 if (source.readInt() != getThumbnailCacheFileMagicHeader())
165 return false;
166
167 const ScopedLock sl (lock);
168 clear();
169 int numThumbnails = jmin (maxNumThumbsToStore, source.readInt());
170
171 while (--numThumbnails >= 0 && ! source.isExhausted())
172 thumbs.add (new ThumbnailCacheEntry (source));
173
174 return true;
175}
176
178{
179 const ScopedLock sl (lock);
180
181 out.writeInt (getThumbnailCacheFileMagicHeader());
182 out.writeInt (thumbs.size());
183
184 for (int i = 0; i < thumbs.size(); ++i)
185 thumbs.getUnchecked (i)->write (out);
186}
187
191
193{
194 return false;
195}
196
197} // namespace juce
Provides a base for classes that can store and draw scaled views of an audio waveform.
virtual ~AudioThumbnailCache()
Destructor.
bool readFromStream(InputStream &source)
Attempts to re-load a saved cache of thumbnails from a stream.
virtual void saveNewlyFinishedThumbnail(const AudioThumbnailBase &, int64 hashCode)
This can be overridden to provide a custom callback for saving thumbnails once they have finished bei...
AudioThumbnailCache(int maxNumThumbsToStore)
Creates a cache object.
void clear()
Clears out any stored thumbnails.
void storeThumb(const AudioThumbnailBase &thumb, int64 hashCode)
Stores the cacheable data from the specified thumb in this cache.
void removeThumb(int64 hashCode)
Tells the cache to forget about the thumb with the given hashcode.
bool loadThumb(AudioThumbnailBase &thumb, int64 hashCode)
Reloads the specified thumb if this cache contains the appropriate stored data.
virtual bool loadNewThumb(AudioThumbnailBase &, int64 hashCode)
This can be overridden to provide a custom callback for loading thumbnails from pre-saved files to sa...
void writeToStream(OutputStream &stream)
Writes all currently-loaded cache data to a stream.
static constexpr uint32 littleEndianInt(const void *bytes) noexcept
Turns 4 bytes into a little-endian integer.
Automatically locks and unlocks a mutex object.
The base class for streams that read data.
virtual int64 readInt64()
Reads eight bytes from the stream as a little-endian 64-bit value.
virtual bool isExhausted()=0
Returns true if the stream has no more data to read.
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Reads from the stream and appends the data to a MemoryBlock.
virtual int readInt()
Reads four bytes from the stream as a little-endian 32-bit value.
A class to hold a resizable block of raw data.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
Allows a block of data to be accessed as a stream.
Writes data to an internal memory buffer, which grows as required.
The base class for streams that write data to some kind of destination.
virtual bool writeInt64(int64 value)
Writes a 64-bit integer to the stream in a little-endian byte order.
virtual bool writeInt(int value)
Writes a 32-bit integer to the stream in a little-endian byte order.
bool startThread()
Attempts to start a new thread with default ('Priority::normal') priority.
@ low
Uses efficiency cores when possible.
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define jassert(expression)
Platform-independent assertion macro.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.
typedef ssize_t