1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 2 3 /* 4 Rosegarden 5 A sequencer and musical notation editor. 6 Copyright 2000-2021 the Rosegarden development team. 7 See the AUTHORS file for more details. 8 9 This program is free software; you can redistribute it and/or 10 modify it under the terms of the GNU General Public License as 11 published by the Free Software Foundation; either version 2 of the 12 License, or (at your option) any later version. See the file 13 COPYING included with this distribution for more information. 14 */ 15 16 #ifndef RG_AUDIO_CACHE_H 17 #define RG_AUDIO_CACHE_H 18 19 #include <map> 20 #include <stddef.h> 21 22 namespace Rosegarden 23 { 24 25 /** 26 * A simple cache for smallish bits of audio data, indexed by some 27 * opaque pointer type. (The PlayableAudioFile uses this with an 28 * AudioFile* index type, for example.) With reference counting. 29 */ 30 31 class AudioCache 32 { 33 public: 34 AudioCache() { } 35 virtual ~AudioCache(); 36 37 /** 38 * Look some audio data up in the cache and report whether it 39 * exists. 40 */ 41 bool has(void *index); 42 43 /** 44 * Look some audio data up in the cache and return it if it 45 * exists, returning the number of channels in channels, the frame 46 * count in frames, and one pointer per channel to samples in the 47 * return value. Return 0 if the data is not in cache. Does not 48 * affect the reference count. Ownership of the returned data 49 * remains with the cache object. You should not call this unless 50 * you have already called incrementReference (or addData) to 51 * register your interest. 52 */ 53 float **getData(void *index, size_t &channels, size_t &frames); 54 55 /** 56 * Add a piece of data to the cache, and increment the reference 57 * count for that data (to 1). Ownership of the data is passed 58 * to the cache, which will delete it with delete[] when done. 59 */ 60 void addData(void *index, size_t channels, size_t nframes, float **data); 61 62 /** 63 * Increment the reference count for a given piece of data. 64 */ 65 void incrementReference(void *index); 66 67 /** 68 * Decrement the reference count for a given piece of data, 69 * and delete the data from the cache if the reference count has 70 * reached zero. 71 */ 72 void decrementReference(void *index); 73 74 protected: 75 void clear(); 76 77 struct CacheRec { 78 CacheRec() : data(nullptr), channels(0), nframes(0), refCount(0) { } 79 CacheRec(float **d, size_t c, size_t n) : 80 data(d), channels(c), nframes(n), refCount(1) { } 81 ~CacheRec(); 82 float **data; 83 size_t channels; 84 size_t nframes; 85 int refCount; 86 }; 87 88 std::map<void *, CacheRec *> m_cache; 89 }; 90 91 } 92 93 #endif 94