Path: blob/master/libs/fluidsynth/src/synth/fluid_tuning.c
4396 views
/* FluidSynth - A Software Synthesizer1*2* Copyright (C) 2003 Peter Hanappe and others.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public License6* as published by the Free Software Foundation; either version 2.1 of7* the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public15* License along with this library; if not, write to the Free16* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*/192021#include "fluid_tuning.h"22#include "fluid_sys.h"232425fluid_tuning_t *new_fluid_tuning(const char *name, int bank, int prog)26{27fluid_tuning_t *tuning;28int i;2930tuning = FLUID_NEW(fluid_tuning_t);3132if(tuning == NULL)33{34FLUID_LOG(FLUID_PANIC, "Out of memory");35return NULL;36}3738FLUID_MEMSET(tuning, 0, sizeof(fluid_tuning_t));3940if(fluid_tuning_set_name(tuning, name) != FLUID_OK)41{42delete_fluid_tuning(tuning);43return NULL;44}4546tuning->bank = bank;47tuning->prog = prog;4849for(i = 0; i < 128; i++)50{51tuning->pitch[i] = i * 100.0;52}5354fluid_atomic_int_set(&tuning->refcount, 1); /* Start with a refcount of 1 */5556return tuning;57}5859/* Duplicate a tuning */60fluid_tuning_t *61fluid_tuning_duplicate(fluid_tuning_t *tuning)62{63fluid_tuning_t *new_tuning;64int i;6566new_tuning = FLUID_NEW(fluid_tuning_t);6768if(!new_tuning)69{70FLUID_LOG(FLUID_PANIC, "Out of memory");71return NULL;72}7374FLUID_MEMSET(new_tuning, 0, sizeof(fluid_tuning_t));7576if(fluid_tuning_set_name(new_tuning, tuning->name) != FLUID_OK)77{78delete_fluid_tuning(new_tuning);79return NULL;80}8182new_tuning->bank = tuning->bank;83new_tuning->prog = tuning->prog;8485for(i = 0; i < 128; i++)86{87new_tuning->pitch[i] = tuning->pitch[i];88}8990fluid_atomic_int_set(&new_tuning->refcount, 1); /* Start with a refcount of 1 */9192return new_tuning;93}9495void96delete_fluid_tuning(fluid_tuning_t *tuning)97{98fluid_return_if_fail(tuning != NULL);99100FLUID_FREE(tuning->name);101FLUID_FREE(tuning);102}103104/* Add a reference to a tuning object */105void106fluid_tuning_ref(fluid_tuning_t *tuning)107{108fluid_return_if_fail(tuning != NULL);109110fluid_atomic_int_inc(&tuning->refcount);111}112113/* Unref a tuning object, when it reaches 0 it is deleted, returns TRUE if deleted */114int115fluid_tuning_unref(fluid_tuning_t *tuning, int count)116{117fluid_return_val_if_fail(tuning != NULL, FALSE);118119/* Add and compare are separate, but that is OK, since refcount will only120* reach 0 when there are no references and therefore no possibility of121* another thread adding a reference in between */122fluid_atomic_int_add(&tuning->refcount, -count);123124/* Delete when refcount reaches 0 */125if(!fluid_atomic_int_get(&tuning->refcount))126{127delete_fluid_tuning(tuning);128return TRUE;129}130else131{132return FALSE;133}134}135136int fluid_tuning_set_name(fluid_tuning_t *tuning, const char *name)137{138if(tuning->name != NULL)139{140FLUID_FREE(tuning->name);141tuning->name = NULL;142}143144if(name != NULL)145{146tuning->name = FLUID_STRDUP(name);147148if(tuning->name == NULL)149{150FLUID_LOG(FLUID_ERR, "Out of memory");151return FLUID_FAILED;152}153}154155return FLUID_OK;156}157158char *fluid_tuning_get_name(fluid_tuning_t *tuning)159{160return tuning->name;161}162163void fluid_tuning_set_octave(fluid_tuning_t *tuning, const double *pitch_deriv)164{165int i;166167for(i = 0; i < 128; i++)168{169tuning->pitch[i] = i * 100.0 + pitch_deriv[i % 12];170}171}172173void fluid_tuning_set_all(fluid_tuning_t *tuning, const double *pitch)174{175int i;176177for(i = 0; i < 128; i++)178{179tuning->pitch[i] = pitch[i];180}181}182183void fluid_tuning_set_pitch(fluid_tuning_t *tuning, int key, double pitch)184{185if((key >= 0) && (key < 128))186{187tuning->pitch[key] = pitch;188}189}190191192