Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / src / audio / audio_file_formats / juce_AudioThumbnailCache.cpp
blob34cf38be1fdccb4be3d38a5f9bb580e97314a3dd
1 /*
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"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_AudioThumbnailCache.h"
31 #include "../../io/streams/juce_MemoryInputStream.h"
32 #include "../../io/streams/juce_MemoryOutputStream.h"
35 //==============================================================================
36 struct ThumbnailCacheEntry
38 int64 hash;
39 uint32 lastUsed;
40 MemoryBlock data;
42 JUCE_LEAK_DETECTOR (ThumbnailCacheEntry);
45 //==============================================================================
46 AudioThumbnailCache::AudioThumbnailCache (const int maxNumThumbsToStore_)
47 : TimeSliceThread ("thumb cache"),
48 maxNumThumbsToStore (maxNumThumbsToStore_)
50 startThread (2);
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);
63 return nullptr;
66 bool AudioThumbnailCache::loadThumb (AudioThumbnail& thumb, const int64 hashCode)
68 ThumbnailCacheEntry* te = findThumbFor (hashCode);
70 if (te != nullptr)
72 te->lastUsed = Time::getMillisecondCounter();
74 MemoryInputStream in (te->data, false);
75 thumb.loadFrom (in);
76 return true;
79 return false;
82 void AudioThumbnailCache::storeThumb (const AudioThumbnail& thumb,
83 const int64 hashCode)
85 ThumbnailCacheEntry* te = findThumbFor (hashCode);
87 if (te == nullptr)
89 te = new ThumbnailCacheEntry();
90 te->hash = hashCode;
92 if (thumbs.size() < maxNumThumbsToStore)
94 thumbs.add (te);
96 else
98 int oldest = 0;
99 uint32 oldestTime = Time::getMillisecondCounter() + 1;
101 for (int i = thumbs.size(); --i >= 0;)
103 if (thumbs.getUnchecked(i)->lastUsed < oldestTime)
105 oldest = i;
106 oldestTime = thumbs.getUnchecked(i)->lastUsed;
110 thumbs.set (oldest, te);
114 te->lastUsed = Time::getMillisecondCounter();
116 MemoryOutputStream out (te->data, false);
117 thumb.saveTo (out);
120 void AudioThumbnailCache::clear()
122 thumbs.clear();
126 END_JUCE_NAMESPACE