1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
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.
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.
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
22 #include "fluid_tuning.h"
23 #include "fluidsynth_priv.h"
24 #include "fluid_sys.h"
27 fluid_tuning_t *new_fluid_tuning(const char *name, int bank, int prog)
29 fluid_tuning_t *tuning;
32 tuning = FLUID_NEW(fluid_tuning_t);
36 FLUID_LOG(FLUID_PANIC, "Out of memory");
40 FLUID_MEMSET(tuning, 0, sizeof(fluid_tuning_t));
42 if(fluid_tuning_set_name(tuning, name) != FLUID_OK)
44 delete_fluid_tuning(tuning);
51 for(i = 0; i < 128; i++)
53 tuning->pitch[i] = i * 100.0;
56 fluid_atomic_int_set(&tuning->refcount, 1); /* Start with a refcount of 1 */
61 /* Duplicate a tuning */
63 fluid_tuning_duplicate(fluid_tuning_t *tuning)
65 fluid_tuning_t *new_tuning;
68 new_tuning = FLUID_NEW(fluid_tuning_t);
72 FLUID_LOG(FLUID_PANIC, "Out of memory");
76 FLUID_MEMSET(new_tuning, 0, sizeof(fluid_tuning_t));
78 if(fluid_tuning_set_name(new_tuning, tuning->name) != FLUID_OK)
80 delete_fluid_tuning(new_tuning);
84 new_tuning->bank = tuning->bank;
85 new_tuning->prog = tuning->prog;
87 for(i = 0; i < 128; i++)
89 new_tuning->pitch[i] = tuning->pitch[i];
92 fluid_atomic_int_set(&new_tuning->refcount, 1); /* Start with a refcount of 1 */
98 delete_fluid_tuning(fluid_tuning_t *tuning)
100 fluid_return_if_fail(tuning != NULL);
102 FLUID_FREE(tuning->name);
106 /* Add a reference to a tuning object */
108 fluid_tuning_ref(fluid_tuning_t *tuning)
110 fluid_return_if_fail(tuning != NULL);
112 fluid_atomic_int_inc(&tuning->refcount);
115 /* Unref a tuning object, when it reaches 0 it is deleted, returns TRUE if deleted */
117 fluid_tuning_unref(fluid_tuning_t *tuning, int count)
119 fluid_return_val_if_fail(tuning != NULL, FALSE);
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);
126 /* Delete when refcount reaches 0 */
127 if(!fluid_atomic_int_get(&tuning->refcount))
129 delete_fluid_tuning(tuning);
138 int fluid_tuning_set_name(fluid_tuning_t *tuning, const char *name)
140 if(tuning->name != NULL)
142 FLUID_FREE(tuning->name);
148 tuning->name = FLUID_STRDUP(name);
150 if(tuning->name == NULL)
152 FLUID_LOG(FLUID_ERR, "Out of memory");
160 char *fluid_tuning_get_name(fluid_tuning_t *tuning)
165 void fluid_tuning_set_octave(fluid_tuning_t *tuning, const double *pitch_deriv)
169 for(i = 0; i < 128; i++)
171 tuning->pitch[i] = i * 100.0 + pitch_deriv[i % 12];
175 void fluid_tuning_set_all(fluid_tuning_t *tuning, const double *pitch)
179 for(i = 0; i < 128; i++)
181 tuning->pitch[i] = pitch[i];
185 void fluid_tuning_set_pitch(fluid_tuning_t *tuning, int key, double pitch)
187 if((key >= 0) && (key < 128))
189 tuning->pitch[key] = pitch;