summaryrefslogtreecommitdiff
path: root/libs/fluidsynth/src/fluid_samplecache.c
blob: 036964f5843594d8d498231e8bad944bf6e1c648 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* FluidSynth - A Software Synthesizer
 *
 * Copyright (C) 2003  Peter Hanappe and others.
 *
 * SoundFont file loading code borrowed from Smurf SoundFont Editor
 * Copyright (C) 1999-2001 Josh Green
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

/* CACHED SAMPLE DATA LOADER
 *
 * This is a wrapper around fluid_sffile_read_sample_data that attempts to cache the read
 * data across all FluidSynth instances in a global (process-wide) list.
 */

#include "fluid_samplecache.h"
#include "fluid_sys.h"
#include "fluidsynth.h"
#include "fluid_list.h"


typedef struct _fluid_samplecache_entry_t fluid_samplecache_entry_t;

struct _fluid_samplecache_entry_t
{
    /* The follwing members all form the cache key */
    char *filename;
    time_t modification_time;
    unsigned int sf_samplepos;
    unsigned int sf_samplesize;
    unsigned int sf_sample24pos;
    unsigned int sf_sample24size;
    unsigned int sample_start;
    unsigned int sample_end;
    int sample_type;
    /*  End of cache key members */

    short *sample_data;
    char *sample_data24;
    int sample_count;

    int num_references;
    int mlocked;
};

static fluid_list_t *samplecache_list = NULL;
static fluid_mutex_t samplecache_mutex = FLUID_MUTEX_INIT;

static fluid_samplecache_entry_t *new_samplecache_entry(SFData *sf, unsigned int sample_start,
        unsigned int sample_end, int sample_type, time_t mtime);
static fluid_samplecache_entry_t *get_samplecache_entry(SFData *sf, unsigned int sample_start,
        unsigned int sample_end, int sample_type, time_t mtime);
static void delete_samplecache_entry(fluid_samplecache_entry_t *entry);

static int fluid_get_file_modification_time(char *filename, time_t *modification_time);


/* PUBLIC INTERFACE */

int fluid_samplecache_load(SFData *sf,
                           unsigned int sample_start, unsigned int sample_end, int sample_type,
                           int try_mlock, short **sample_data, char **sample_data24)
{
    fluid_samplecache_entry_t *entry;
    int ret;
    time_t mtime;

    fluid_mutex_lock(samplecache_mutex);

    if(fluid_get_file_modification_time(sf->fname, &mtime) == FLUID_FAILED)
    {
        mtime = 0;
    }

    entry = get_samplecache_entry(sf, sample_start, sample_end, sample_type, mtime);

    if(entry == NULL)
    {
        entry = new_samplecache_entry(sf, sample_start, sample_end, sample_type, mtime);

        if(entry == NULL)
        {
            ret = -1;
            goto unlock_exit;
        }

        samplecache_list = fluid_list_prepend(samplecache_list, entry);
    }

    if(try_mlock && !entry->mlocked)
    {
        /* Lock the memory to disable paging. It's okay if this fails. It
         * probably means that the user doesn't have the required permission. */
        if(fluid_mlock(entry->sample_data, entry->sample_count * sizeof(short)) == 0)
        {
            if(entry->sample_data24 != NULL)
            {
                entry->mlocked = (fluid_mlock(entry->sample_data24, entry->sample_count) == 0);
            }
            else
            {
                entry->mlocked = TRUE;
            }

            if(!entry->mlocked)
            {
                fluid_munlock(entry->sample_data, entry->sample_count * sizeof(short));
                FLUID_LOG(FLUID_WARN, "Failed to pin the sample data to RAM; swapping is possible.");
            }
        }
    }

    entry->num_references++;
    *sample_data = entry->sample_data;
    *sample_data24 = entry->sample_data24;
    ret = entry->sample_count;

unlock_exit:
    fluid_mutex_unlock(samplecache_mutex);
    return ret;
}

int fluid_samplecache_unload(const short *sample_data)
{
    fluid_list_t *entry_list;
    fluid_samplecache_entry_t *entry;
    int ret;

    fluid_mutex_lock(samplecache_mutex);

    entry_list = samplecache_list;

    while(entry_list)
    {
        entry = (fluid_samplecache_entry_t *)fluid_list_get(entry_list);

        if(sample_data == entry->sample_data)
        {
            entry->num_references--;

            if(entry->num_references == 0)
            {
                if(entry->mlocked)
                {
                    fluid_munlock(entry->sample_data, entry->sample_count * sizeof(short));

                    if(entry->sample_data24 != NULL)
                    {
                        fluid_munlock(entry->sample_data24, entry->sample_count);
                    }
                }

                samplecache_list = fluid_list_remove(samplecache_list, entry);
                delete_samplecache_entry(entry);
            }

            ret = FLUID_OK;
            goto unlock_exit;
        }

        entry_list = fluid_list_next(entry_list);
    }

    FLUID_LOG(FLUID_ERR, "Trying to free sample data not found in cache.");
    ret = FLUID_FAILED;

unlock_exit:
    fluid_mutex_unlock(samplecache_mutex);
    return ret;
}


/* Private functions */
static fluid_samplecache_entry_t *new_samplecache_entry(SFData *sf,
        unsigned int sample_start,
        unsigned int sample_end,
        int sample_type,
        time_t mtime)
{
    fluid_samplecache_entry_t *entry;

    entry = FLUID_NEW(fluid_samplecache_entry_t);

    if(entry == NULL)
    {
        FLUID_LOG(FLUID_ERR, "Out of memory");
        return NULL;
    }

    FLUID_MEMSET(entry, 0, sizeof(*entry));

    entry->filename = FLUID_STRDUP(sf->fname);

    if(entry->filename == NULL)
    {
        FLUID_LOG(FLUID_ERR, "Out of memory");
        goto error_exit;
    }

    entry->sf_samplepos = sf->samplepos;
    entry->sf_samplesize = sf->samplesize;
    entry->sf_sample24pos = sf->sample24pos;
    entry->sf_sample24size = sf->sample24size;
    entry->sample_start = sample_start;
    entry->sample_end = sample_end;
    entry->sample_type = sample_type;
    entry->modification_time = mtime;

    entry->sample_count = fluid_sffile_read_sample_data(sf, sample_start, sample_end, sample_type,
                          &entry->sample_data, &entry->sample_data24);

    if(entry->sample_count < 0)
    {
        goto error_exit;
    }

    return entry;

error_exit:
    delete_samplecache_entry(entry);
    return NULL;
}

static void delete_samplecache_entry(fluid_samplecache_entry_t *entry)
{
    fluid_return_if_fail(entry != NULL);

    FLUID_FREE(entry->filename);
    FLUID_FREE(entry->sample_data);
    FLUID_FREE(entry->sample_data24);
    FLUID_FREE(entry);
}

static fluid_samplecache_entry_t *get_samplecache_entry(SFData *sf,
        unsigned int sample_start,
        unsigned int sample_end,
        int sample_type,
        time_t mtime)
{
    fluid_list_t *entry_list;
    fluid_samplecache_entry_t *entry;

    entry_list = samplecache_list;

    while(entry_list)
    {
        entry = (fluid_samplecache_entry_t *)fluid_list_get(entry_list);

        if((FLUID_STRCMP(sf->fname, entry->filename) == 0) &&
                (mtime == entry->modification_time) &&
                (sf->samplepos == entry->sf_samplepos) &&
                (sf->samplesize == entry->sf_samplesize) &&
                (sf->sample24pos == entry->sf_sample24pos) &&
                (sf->sample24size == entry->sf_sample24size) &&
                (sample_start == entry->sample_start) &&
                (sample_end == entry->sample_end) &&
                (sample_type == entry->sample_type))
        {
            return entry;
        }

        entry_list = fluid_list_next(entry_list);
    }

    return NULL;
}

static int fluid_get_file_modification_time(char *filename, time_t *modification_time)
{
    fluid_stat_buf_t buf;

    if(fluid_stat(filename, &buf))
    {
        return FLUID_FAILED;
    }

    *modification_time = buf.st_mtime;
    return FLUID_OK;
}