Update Fluidsynth to 2.0.1
[ardour.git] / libs / fluidsynth / src / fluid_tuning.c
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * This library 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; either version 2.1 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  */
20
21
22 #include "fluid_tuning.h"
23 #include "fluidsynth_priv.h"
24 #include "fluid_sys.h"
25
26
27 fluid_tuning_t *new_fluid_tuning(const char *name, int bank, int prog)
28 {
29     fluid_tuning_t *tuning;
30     int i;
31
32     tuning = FLUID_NEW(fluid_tuning_t);
33
34     if(tuning == NULL)
35     {
36         FLUID_LOG(FLUID_PANIC, "Out of memory");
37         return NULL;
38     }
39
40     FLUID_MEMSET(tuning, 0, sizeof(fluid_tuning_t));
41
42     if(fluid_tuning_set_name(tuning, name) != FLUID_OK)
43     {
44         delete_fluid_tuning(tuning);
45         return NULL;
46     }
47
48     tuning->bank = bank;
49     tuning->prog = prog;
50
51     for(i = 0; i < 128; i++)
52     {
53         tuning->pitch[i] = i * 100.0;
54     }
55
56     fluid_atomic_int_set(&tuning->refcount, 1);         /* Start with a refcount of 1 */
57
58     return tuning;
59 }
60
61 /* Duplicate a tuning */
62 fluid_tuning_t *
63 fluid_tuning_duplicate(fluid_tuning_t *tuning)
64 {
65     fluid_tuning_t *new_tuning;
66     int i;
67
68     new_tuning = FLUID_NEW(fluid_tuning_t);
69
70     if(!new_tuning)
71     {
72         FLUID_LOG(FLUID_PANIC, "Out of memory");
73         return NULL;
74     }
75
76     FLUID_MEMSET(new_tuning, 0, sizeof(fluid_tuning_t));
77
78     if(fluid_tuning_set_name(new_tuning, tuning->name) != FLUID_OK)
79     {
80         delete_fluid_tuning(new_tuning);
81         return NULL;
82     }
83
84     new_tuning->bank = tuning->bank;
85     new_tuning->prog = tuning->prog;
86
87     for(i = 0; i < 128; i++)
88     {
89         new_tuning->pitch[i] = tuning->pitch[i];
90     }
91
92     fluid_atomic_int_set(&new_tuning->refcount, 1);     /* Start with a refcount of 1 */
93
94     return new_tuning;
95 }
96
97 void
98 delete_fluid_tuning(fluid_tuning_t *tuning)
99 {
100     fluid_return_if_fail(tuning != NULL);
101
102     FLUID_FREE(tuning->name);
103     FLUID_FREE(tuning);
104 }
105
106 /* Add a reference to a tuning object */
107 void
108 fluid_tuning_ref(fluid_tuning_t *tuning)
109 {
110     fluid_return_if_fail(tuning != NULL);
111
112     fluid_atomic_int_inc(&tuning->refcount);
113 }
114
115 /* Unref a tuning object, when it reaches 0 it is deleted, returns TRUE if deleted */
116 int
117 fluid_tuning_unref(fluid_tuning_t *tuning, int count)
118 {
119     fluid_return_val_if_fail(tuning != NULL, FALSE);
120
121     /* Add and compare are separate, but that is OK, since refcount will only
122      * reach 0 when there are no references and therefore no possibility of
123      * another thread adding a reference in between */
124     fluid_atomic_int_add(&tuning->refcount, -count);
125
126     /* Delete when refcount reaches 0 */
127     if(!fluid_atomic_int_get(&tuning->refcount))
128     {
129         delete_fluid_tuning(tuning);
130         return TRUE;
131     }
132     else
133     {
134         return FALSE;
135     }
136 }
137
138 int fluid_tuning_set_name(fluid_tuning_t *tuning, const char *name)
139 {
140     if(tuning->name != NULL)
141     {
142         FLUID_FREE(tuning->name);
143         tuning->name = NULL;
144     }
145
146     if(name != NULL)
147     {
148         tuning->name = FLUID_STRDUP(name);
149
150         if(tuning->name == NULL)
151         {
152             FLUID_LOG(FLUID_ERR, "Out of memory");
153             return FLUID_FAILED;
154         }
155     }
156
157     return FLUID_OK;
158 }
159
160 char *fluid_tuning_get_name(fluid_tuning_t *tuning)
161 {
162     return tuning->name;
163 }
164
165 void fluid_tuning_set_octave(fluid_tuning_t *tuning, const double *pitch_deriv)
166 {
167     int i;
168
169     for(i = 0; i < 128; i++)
170     {
171         tuning->pitch[i] = i * 100.0 + pitch_deriv[i % 12];
172     }
173 }
174
175 void fluid_tuning_set_all(fluid_tuning_t *tuning, const double *pitch)
176 {
177     int i;
178
179     for(i = 0; i < 128; i++)
180     {
181         tuning->pitch[i] = pitch[i];
182     }
183 }
184
185 void fluid_tuning_set_pitch(fluid_tuning_t *tuning, int key, double pitch)
186 {
187     if((key >= 0) && (key < 128))
188     {
189         tuning->pitch[key] = pitch;
190     }
191 }