summaryrefslogtreecommitdiffstats
path: root/src/sound/AudioCache.h
blob: de7c6e0b82bff0d69bd9f55b2cfb851f55363fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// -*- c-basic-offset: 4 -*-

/*
    Rosegarden
    A sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _AUDIO_CACHE_H_
#define _AUDIO_CACHE_H_

#include <cstddef>
#include <map>

namespace Rosegarden
{

/**
 * A simple cache for smallish bits of audio data, indexed by some
 * opaque pointer type.  (The PlayableAudioFile uses this with an
 * AudioFile* index type, for example.)  With reference counting.
 */

class AudioCache
{
public:
    AudioCache() { }
    virtual ~AudioCache();

    /**
     * Look some audio data up in the cache and report whether it
     * exists.
     */
    bool has(void *index);

    /**
     * Look some audio data up in the cache and return it if it
     * exists, returning the number of channels in channels, the frame
     * count in frames, and one pointer per channel to samples in the
     * return value.  Return 0 if the data is not in cache.  Does not
     * affect the reference count.  Ownership of the returned data
     * remains with the cache object.  You should not call this unless
     * you have already called incrementReference (or addData) to
     * register your interest.
     */
    float **getData(void *index, size_t &channels, size_t &frames);

    /**
     * Add a piece of data to the cache, and increment the reference
     * count for that data (to 1).  Ownership of the data is passed
     * to the cache, which will delete it with delete[] when done.
     */
    void addData(void *index, size_t channels, size_t nframes, float **data);

    /**
     * Increment the reference count for a given piece of data.
     */
    void incrementReference(void *index);

    /**
     * Decrement the reference count for a given piece of data,
     * and delete the data from the cache if the reference count has
     * reached zero.
     */
    void decrementReference(void *index);

protected:
    void clear();

    struct CacheRec {
        CacheRec() : data(0), channels(0), nframes(0), refCount(0) { }
        CacheRec(float **d, size_t c, size_t n) :
            data(d), channels(c), nframes(n), refCount(1) { }
        ~CacheRec();
        float **data;
        size_t channels;
        size_t nframes;
        int refCount;
    };

    std::map<void *, CacheRec *> m_cache;
};

}

#endif