1 /*
2  * libInstPatch
3  * Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; version 2.1
8  * of the License only.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA or on the web at http://www.gnu.org.
19  */
20 #ifndef __IPATCH_SAMPLE_STORE_CACHE_H__
21 #define __IPATCH_SAMPLE_STORE_CACHE_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 #include <libinstpatch/IpatchSampleStore.h>
26 
27 /* forward type declarations */
28 
29 typedef struct _IpatchSampleStoreCache IpatchSampleStoreCache;
30 typedef struct _IpatchSampleStoreCacheClass IpatchSampleStoreCacheClass;
31 
32 #define IPATCH_TYPE_SAMPLE_STORE_CACHE (ipatch_sample_store_cache_get_type ())
33 #define IPATCH_SAMPLE_STORE_CACHE(obj) \
34   (G_TYPE_CHECK_INSTANCE_CAST ((obj), IPATCH_TYPE_SAMPLE_STORE_CACHE, \
35    IpatchSampleStoreCache))
36 #define IPATCH_SAMPLE_STORE_CACHE_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST ((klass), IPATCH_TYPE_SAMPLE_STORE_CACHE, \
38    IpatchSampleStoreCacheClass))
39 #define IPATCH_IS_SAMPLE_STORE_CACHE(obj) \
40   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IPATCH_TYPE_SAMPLE_STORE_CACHE))
41 #define IPATCH_IS_SAMPLE_STORE_CACHE_CLASS(klass) \
42   (G_TYPE_CHECK_CLASS_TYPE ((klass), IPATCH_TYPE_SAMPLE_STORE_CACHE))
43 
44 /* RAM sample store instance */
45 struct _IpatchSampleStoreCache
46 {
47     IpatchSampleStore parent_instance;
48     gpointer location;    /* Pointer to the sample data in memory */
49     guint32 channel_map;  /* Channel map of cached sample in reference to native sample */
50     glong last_open;      /* Unix time of last open or 0 if currently open */
51     int open_count;       /* Current number of opens (atomic int) */
52 };
53 
54 /* RAM sample store class */
55 struct _IpatchSampleStoreCacheClass
56 {
57     IpatchSampleStoreClass parent_class;
58 };
59 
60 /**
61  * ipatch_sample_store_cache_get_location:
62  * @store: Sample store to get sample data location from
63  *
64  * Macro to quickly fetch a cache sample store's data location pointer.
65  *
66  * Returns: Sample data pointer.
67  */
68 #define ipatch_sample_store_cache_get_location(store) \
69   ((store)->location)   /* No lock needed, set only once during creation */
70 
71 /**
72  * ipatch_sample_store_cache_get_channel_map:
73  * @store: Sample store to get channel map from
74  *
75  * Macro to quickly fetch a cache sample store's channel map value.  Cached
76  * samples store a channel map in reference to the native sample of their
77  * parent #IpatchSampleData.
78  *
79  * Returns: Channel map value.
80  */
81 #define ipatch_sample_store_cache_get_channel_map(store) \
82   ((store)->channel_map)   /* No lock needed, set only once during creation */
83 
84 /* Used by #IpatchSampleData */
85 #define ipatch_sample_store_cache_get_open_count(store) \
86   g_atomic_int_get (&((store)->open_count))
87 
88 /**
89  * IPATCH_SAMPLE_STORE_CACHE_UNUSED_FLAG_SHIFT: (skip)
90  */
91 /* we reserve 4 bits for future expansion */
92 #define IPATCH_SAMPLE_STORE_CACHE_UNUSED_FLAG_SHIFT \
93   (IPATCH_SAMPLE_STORE_UNUSED_FLAG_SHIFT + 4)
94 
95 GType ipatch_sample_store_cache_get_type(void);
96 IpatchSample *ipatch_sample_store_cache_new(gpointer location);
97 void ipatch_sample_store_cache_open(IpatchSampleStoreCache *store);
98 void ipatch_sample_store_cache_close(IpatchSampleStoreCache *store);
99 
100 #endif
101