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 "fluid_sys.h"
26 fluid_tuning_t *new_fluid_tuning(const char *name, int bank, int prog)
28 fluid_tuning_t *tuning;
31 tuning = FLUID_NEW(fluid_tuning_t);
35 FLUID_LOG(FLUID_PANIC, "Out of memory");
39 FLUID_MEMSET(tuning, 0, sizeof(fluid_tuning_t));
41 if(fluid_tuning_set_name(tuning, name) != FLUID_OK)
43 delete_fluid_tuning(tuning);
50 for(i = 0; i < 128; i++)
52 tuning->pitch[i] = i * 100.0;
55 fluid_atomic_int_set(&tuning->refcount, 1); /* Start with a refcount of 1 */
60 /* Duplicate a tuning */
62 fluid_tuning_duplicate(fluid_tuning_t *tuning)
64 fluid_tuning_t *new_tuning;
67 new_tuning = FLUID_NEW(fluid_tuning_t);
71 FLUID_LOG(FLUID_PANIC, "Out of memory");
75 FLUID_MEMSET(new_tuning, 0, sizeof(fluid_tuning_t));
77 if(fluid_tuning_set_name(new_tuning, tuning->name) != FLUID_OK)
79 delete_fluid_tuning(new_tuning);
83 new_tuning->bank = tuning->bank;
84 new_tuning->prog = tuning->prog;
86 for(i = 0; i < 128; i++)
88 new_tuning->pitch[i] = tuning->pitch[i];
91 fluid_atomic_int_set(&new_tuning->refcount, 1); /* Start with a refcount of 1 */
97 delete_fluid_tuning(fluid_tuning_t *tuning)
99 fluid_return_if_fail(tuning != NULL);
101 FLUID_FREE(tuning->name);
105 /* Add a reference to a tuning object */
107 fluid_tuning_ref(fluid_tuning_t *tuning)
109 fluid_return_if_fail(tuning != NULL);
111 fluid_atomic_int_inc(&tuning->refcount);
114 /* Unref a tuning object, when it reaches 0 it is deleted, returns TRUE if deleted */
116 fluid_tuning_unref(fluid_tuning_t *tuning, int count)
118 fluid_return_val_if_fail(tuning != NULL, FALSE);
120 /* Add and compare are separate, but that is OK, since refcount will only
121 * reach 0 when there are no references and therefore no possibility of
122 * another thread adding a reference in between */
123 fluid_atomic_int_add(&tuning->refcount, -count);
125 /* Delete when refcount reaches 0 */
126 if(!fluid_atomic_int_get(&tuning->refcount))
128 delete_fluid_tuning(tuning);
137 int fluid_tuning_set_name(fluid_tuning_t *tuning, const char *name)
139 if(tuning->name != NULL)
141 FLUID_FREE(tuning->name);
147 tuning->name = FLUID_STRDUP(name);
149 if(tuning->name == NULL)
151 FLUID_LOG(FLUID_ERR, "Out of memory");
159 char *fluid_tuning_get_name(fluid_tuning_t *tuning)
164 void fluid_tuning_set_octave(fluid_tuning_t *tuning, const double *pitch_deriv)
168 for(i = 0; i < 128; i++)
170 tuning->pitch[i] = i * 100.0 + pitch_deriv[i % 12];
174 void fluid_tuning_set_all(fluid_tuning_t *tuning, const double *pitch)
178 for(i = 0; i < 128; i++)
180 tuning->pitch[i] = pitch[i];
184 void fluid_tuning_set_pitch(fluid_tuning_t *tuning, int key, double pitch)
186 if((key >= 0) && (key < 128))
188 tuning->pitch[key] = pitch;