2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../core/juce_StandardHeader.h"
30 #include "juce_AudioThumbnailCache.h"
31 #include "../../io/streams/juce_MemoryInputStream.h"
32 #include "../../io/streams/juce_MemoryOutputStream.h"
35 //==============================================================================
36 struct ThumbnailCacheEntry
42 JUCE_LEAK_DETECTOR (ThumbnailCacheEntry
);
45 //==============================================================================
46 AudioThumbnailCache::AudioThumbnailCache (const int maxNumThumbsToStore_
)
47 : TimeSliceThread ("thumb cache"),
48 maxNumThumbsToStore (maxNumThumbsToStore_
)
53 AudioThumbnailCache::~AudioThumbnailCache()
57 ThumbnailCacheEntry
* AudioThumbnailCache::findThumbFor (const int64 hash
) const
59 for (int i
= thumbs
.size(); --i
>= 0;)
60 if (thumbs
.getUnchecked(i
)->hash
== hash
)
61 return thumbs
.getUnchecked(i
);
66 bool AudioThumbnailCache::loadThumb (AudioThumbnail
& thumb
, const int64 hashCode
)
68 ThumbnailCacheEntry
* te
= findThumbFor (hashCode
);
72 te
->lastUsed
= Time::getMillisecondCounter();
74 MemoryInputStream
in (te
->data
, false);
82 void AudioThumbnailCache::storeThumb (const AudioThumbnail
& thumb
,
85 ThumbnailCacheEntry
* te
= findThumbFor (hashCode
);
89 te
= new ThumbnailCacheEntry();
92 if (thumbs
.size() < maxNumThumbsToStore
)
99 uint32 oldestTime
= Time::getMillisecondCounter() + 1;
101 for (int i
= thumbs
.size(); --i
>= 0;)
103 if (thumbs
.getUnchecked(i
)->lastUsed
< oldestTime
)
106 oldestTime
= thumbs
.getUnchecked(i
)->lastUsed
;
110 thumbs
.set (oldest
, te
);
114 te
->lastUsed
= Time::getMillisecondCounter();
116 MemoryOutputStream
out (te
->data
, false);
120 void AudioThumbnailCache::clear()