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 Library General Public License
7  * as published by the Free Software Foundation; either version 2 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library 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 
new_fluid_tuning(const char * name,int bank,int prog)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   if (tuning == NULL) {
34     FLUID_LOG(FLUID_PANIC, "Out of memory");
35     return NULL;
36   }
37 
38   tuning->name = NULL;
39 
40   if (name != NULL) {
41     tuning->name = FLUID_STRDUP(name);
42   }
43 
44   tuning->bank = bank;
45   tuning->prog = prog;
46 
47   for (i = 0; i < 128; i++) {
48     tuning->pitch[i] = i * 100.0;
49   }
50 
51   tuning->refcount = 1;         /* Start with a refcount of 1 */
52 
53   return tuning;
54 }
55 
56 /* Duplicate a tuning */
57 fluid_tuning_t *
fluid_tuning_duplicate(fluid_tuning_t * tuning)58 fluid_tuning_duplicate (fluid_tuning_t *tuning)
59 {
60   fluid_tuning_t *new_tuning;
61   int i;
62 
63   new_tuning = FLUID_NEW (fluid_tuning_t);
64 
65   if (!new_tuning) {
66     FLUID_LOG (FLUID_PANIC, "Out of memory");
67     return NULL;
68   }
69 
70   if (tuning->name)
71   {
72     new_tuning->name = FLUID_STRDUP (tuning->name);
73 
74     if (!new_tuning->name)
75     {
76       FLUID_FREE (new_tuning);
77       FLUID_LOG (FLUID_PANIC, "Out of memory");
78       return NULL;
79     }
80   }
81   else new_tuning->name = NULL;
82 
83   new_tuning->bank = tuning->bank;
84   new_tuning->prog = tuning->prog;
85 
86   for (i = 0; i < 128; i++)
87     new_tuning->pitch[i] = tuning->pitch[i];
88 
89   new_tuning->refcount = 1;     /* Start with a refcount of 1 */
90 
91   return new_tuning;
92 }
93 
94 void
delete_fluid_tuning(fluid_tuning_t * tuning)95 delete_fluid_tuning (fluid_tuning_t *tuning)
96 {
97   if (tuning->name) FLUID_FREE (tuning->name);
98   FLUID_FREE (tuning);
99 }
100 
101 /* Add a reference to a tuning object */
102 void
fluid_tuning_ref(fluid_tuning_t * tuning)103 fluid_tuning_ref (fluid_tuning_t *tuning)
104 {
105   fluid_return_if_fail (tuning != NULL);
106 
107   fluid_atomic_int_inc (&tuning->refcount);
108 }
109 
110 /* Unref a tuning object, when it reaches 0 it is deleted, returns TRUE if deleted */
111 int
fluid_tuning_unref(fluid_tuning_t * tuning,int count)112 fluid_tuning_unref (fluid_tuning_t *tuning, int count)
113 {
114   fluid_return_val_if_fail (tuning != NULL, FALSE);
115 
116   /* Add and compare are separate, but that is OK, since refcount will only
117    * reach 0 when there are no references and therefore no possibility of
118    * another thread adding a reference in between */
119   fluid_atomic_int_add (&tuning->refcount, -count);
120 
121   /* Delete when refcount reaches 0 */
122   if (!fluid_atomic_int_get (&tuning->refcount))
123   {
124     delete_fluid_tuning (tuning);
125     return TRUE;
126   }
127   else return FALSE;
128 }
129 
fluid_tuning_set_name(fluid_tuning_t * tuning,char * name)130 void fluid_tuning_set_name(fluid_tuning_t* tuning, char* name)
131 {
132   if (tuning->name != NULL) {
133     FLUID_FREE(tuning->name);
134     tuning->name = NULL;
135   }
136   if (name != NULL) {
137     tuning->name = FLUID_STRDUP(name);
138   }
139 }
140 
fluid_tuning_get_name(fluid_tuning_t * tuning)141 char* fluid_tuning_get_name(fluid_tuning_t* tuning)
142 {
143   return tuning->name;
144 }
145 
fluid_tuning_set_key(fluid_tuning_t * tuning,int key,double pitch)146 void fluid_tuning_set_key(fluid_tuning_t* tuning, int key, double pitch)
147 {
148   tuning->pitch[key] = pitch;
149 }
150 
fluid_tuning_set_octave(fluid_tuning_t * tuning,const double * pitch_deriv)151 void fluid_tuning_set_octave(fluid_tuning_t* tuning, const double* pitch_deriv)
152 {
153   int i;
154 
155   for (i = 0; i < 128; i++) {
156     tuning->pitch[i] = i * 100.0 + pitch_deriv[i % 12];
157   }
158 }
159 
fluid_tuning_set_all(fluid_tuning_t * tuning,const double * pitch)160 void fluid_tuning_set_all(fluid_tuning_t* tuning, const double* pitch)
161 {
162   int i;
163 
164   for (i = 0; i < 128; i++) {
165     tuning->pitch[i] = pitch[i];
166   }
167 }
168 
fluid_tuning_set_pitch(fluid_tuning_t * tuning,int key,double pitch)169 void fluid_tuning_set_pitch(fluid_tuning_t* tuning, int key, double pitch)
170 {
171   if ((key >= 0) && (key < 128)) {
172     tuning->pitch[key] = pitch;
173   }
174 }
175