File: test_preset_pinning.c

package info (click to toggle)
fluidsynth 2.4.4%2Bdfsg-1%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,328 kB
  • sloc: ansic: 43,529; cpp: 1,434; xml: 1,020; makefile: 71; sh: 46
file content (194 lines) | stat: -rw-r--r-- 7,305 bytes parent folder | download | duplicates (3)
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
#include "test.h"
#include "fluidsynth.h"
#include "sfloader/fluid_sfont.h"
#include "sfloader/fluid_defsfont.h"
#include "sfloader/fluid_samplecache.h"
#include "utils/fluid_sys.h"
#include "utils/fluid_list.h"

static int count_loaded_samples(fluid_synth_t *synth, int sfont_id);


/* Test the preset pinning and unpinning feature */
int main(void)
{
    int id;
    fluid_synth_t *synth;
    fluid_sfont_t *sfont;
    fluid_defsfont_t *defsfont;

    /* Setup */
    fluid_settings_t *settings = new_fluid_settings();


    /* Test that pinning and unpinning has no effect and throws no errors
     * when dynamic-sample-loading is disabled */
    fluid_settings_setint(settings, "synth.dynamic-sample-loading", 0);
    synth = new_fluid_synth(settings);
    id = fluid_synth_sfload(synth, TEST_SOUNDFONT, 0);
    TEST_ASSERT(count_loaded_samples(synth, id) == 123);
    TEST_ASSERT(fluid_samplecache_count_entries() == 1);

    /* Attempt to pin and unpin an existing preset should succeed (but have no effect) */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 123);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 123);

    /* Attempt to pin and unpin a non-existent preset should fail */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 42, 42) == FLUID_FAILED);
    TEST_ASSERT(count_loaded_samples(synth, id) == 123);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 42, 42) == FLUID_FAILED);
    TEST_ASSERT(count_loaded_samples(synth, id) == 123);

    delete_fluid_synth(synth);


    /* Test pinning and unpinning with dyamic-sample loading enabled */
    fluid_settings_setint(settings, "synth.dynamic-sample-loading", 1);
    synth = new_fluid_synth(settings);
    id = fluid_synth_sfload(synth, TEST_SOUNDFONT, 0);

    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* For the following tests, preset 42 (Lead Synth 2) consists of 4 samples,
     * preset 40 (Aluminum Plate) consists of 1 sample */

    /* Simple pin and unpin */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Pinning and unpinning twice should have exactly the same effect */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Pin a single preset, select both presets, unselect both, ensure the pinned
     * is still in memory and gone after unpinning */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_program_select(synth, 0, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_program_select(synth, 1, id, 0, 40) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 5);
    TEST_ASSERT(fluid_samplecache_count_entries() == 5);

    TEST_ASSERT(fluid_synth_unset_program(synth, 0) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 5);
    TEST_ASSERT(fluid_samplecache_count_entries() == 5);

    TEST_ASSERT(fluid_synth_unset_program(synth, 1) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Unpinning an unpinned and selected preset should not remove it from memory */
    TEST_ASSERT(fluid_synth_program_select(synth, 0, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_unpin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_unset_program(synth, 0) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 0);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Test unload of soundfont with pinned sample automatically unpins
     * the sample and removes all samples from cache */
    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    TEST_ASSERT(fluid_synth_sfunload(synth, id, 0) == FLUID_OK);
    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Ensure that failures during load of preset results in an
     * error returned by pinning function */
    id = fluid_synth_sfload(synth, TEST_SOUNDFONT, 0);
    // hack the soundfont filename so the next load won't find the file anymore
    sfont = fluid_synth_get_sfont_by_id(synth, id);
    defsfont = fluid_sfont_get_data(sfont);
    defsfont->filename[0]++;

    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_FAILED);

    defsfont->filename[0]--;
    TEST_ASSERT(fluid_synth_sfunload(synth, id, 0) == FLUID_OK);


    /* Test that deleting the synth with a pinned preset also leaves no
     * samples in cache */
    id = fluid_synth_sfload(synth, TEST_SOUNDFONT, 0);

    TEST_ASSERT(fluid_synth_pin_preset(synth, id, 0, 42) == FLUID_OK);
    TEST_ASSERT(count_loaded_samples(synth, id) == 4);
    TEST_ASSERT(fluid_samplecache_count_entries() == 4);

    delete_fluid_synth(synth);

    TEST_ASSERT(fluid_samplecache_count_entries() == 0);


    /* Tear down */
    delete_fluid_settings(settings);

    return EXIT_SUCCESS;
}


static int count_loaded_samples(fluid_synth_t *synth, int sfont_id)
{
    fluid_list_t *list;
    int count = 0;

    fluid_sfont_t *sfont = fluid_synth_get_sfont_by_id(synth, sfont_id);
    fluid_defsfont_t *defsfont = fluid_sfont_get_data(sfont);

    for(list = defsfont->sample; list; list = fluid_list_next(list))
    {
        fluid_sample_t *sample = fluid_list_get(list);

        if(sample->data != NULL)
        {
            count++;
        }
    }

    FLUID_LOG(FLUID_INFO, "Loaded samples on sfont %d: %d\n", sfont_id, count);
    return count;
}