audioutils/fmsynth: Add create instance for static
To use this with static instance, add create function with arguments.
This commit is contained in:
parent
5365fc9ffd
commit
4a4c550ced
@ -107,6 +107,24 @@ int fmsynth_initialize(int fs)
|
|||||||
return fmsynthop_set_samplerate(fs);
|
return fmsynthop_set_samplerate(fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* name: create_fmsynthsnd
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR fmsynth_sound_t *create_fmsynthsnd(FAR fmsynth_sound_t *snd)
|
||||||
|
{
|
||||||
|
if (snd)
|
||||||
|
{
|
||||||
|
snd->own_allocate = 0;
|
||||||
|
snd->phase_time = 0;
|
||||||
|
snd->volume = FMSYNTH_MAX_VOLUME;
|
||||||
|
snd->operators = NULL;
|
||||||
|
snd->next_sound = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return snd;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* name: fmsynthsnd_create
|
* name: fmsynthsnd_create
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -115,12 +133,11 @@ FAR fmsynth_sound_t *fmsynthsnd_create(void)
|
|||||||
{
|
{
|
||||||
FAR fmsynth_sound_t *ret;
|
FAR fmsynth_sound_t *ret;
|
||||||
ret = (FAR fmsynth_sound_t *)malloc(sizeof(fmsynth_sound_t));
|
ret = (FAR fmsynth_sound_t *)malloc(sizeof(fmsynth_sound_t));
|
||||||
|
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
ret->phase_time = 0;
|
create_fmsynthsnd(ret);
|
||||||
ret->volume = FMSYNTH_MAX_VOLUME;
|
ret->own_allocate = 1;
|
||||||
ret->operators = NULL;
|
|
||||||
ret->next_sound = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -132,7 +149,7 @@ FAR fmsynth_sound_t *fmsynthsnd_create(void)
|
|||||||
|
|
||||||
void fmsynthsnd_delete(FAR fmsynth_sound_t *snd)
|
void fmsynthsnd_delete(FAR fmsynth_sound_t *snd)
|
||||||
{
|
{
|
||||||
if (snd != NULL)
|
if (snd != NULL && snd->own_allocate == 1)
|
||||||
{
|
{
|
||||||
free(snd);
|
free(snd);
|
||||||
}
|
}
|
||||||
@ -164,6 +181,20 @@ void fmsynthsnd_set_soundfreq(FAR fmsynth_sound_t *snd, float freq)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* name: fmsynthsnd_stop
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void fmsynthsnd_stop(FAR fmsynth_sound_t *snd)
|
||||||
|
{
|
||||||
|
FAR fmsynth_op_t *op;
|
||||||
|
|
||||||
|
for (op = snd->operators; op != NULL; op = op->parallelop)
|
||||||
|
{
|
||||||
|
fmsynthop_stop(op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* name: fmsynthsnd_set_volume
|
* name: fmsynthsnd_set_volume
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@ -64,29 +64,39 @@ static int set_egparams(int fs,
|
|||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* name: create_fmsyntheg
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR fmsynth_eg_t *create_fmsyntheg(FAR fmsynth_eg_t *eg)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (eg)
|
||||||
|
{
|
||||||
|
eg->state = EGSTATE_RELEASED;
|
||||||
|
eg->state_counter = 0;
|
||||||
|
for (i = 0; i < EGSTATE_MAX; i++)
|
||||||
|
{
|
||||||
|
eg->state_params[i].initval = 0;
|
||||||
|
eg->state_params[i].period = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
eg->state_params[EGSTATE_RELEASED].initval = FMSYNTH_MAX_EGLEVEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return eg;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* name: fmsyntheg_create
|
* name: fmsyntheg_create
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
FAR fmsynth_eg_t *fmsyntheg_create(void)
|
FAR fmsynth_eg_t *fmsyntheg_create(void)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
FAR fmsynth_eg_t *ret = (FAR fmsynth_eg_t *)malloc(sizeof(fmsynth_eg_t));
|
FAR fmsynth_eg_t *ret = (FAR fmsynth_eg_t *)malloc(sizeof(fmsynth_eg_t));
|
||||||
|
|
||||||
if (ret)
|
return create_fmsyntheg(ret);
|
||||||
{
|
|
||||||
ret->state = EGSTATE_RELEASED;
|
|
||||||
ret->state_counter = 0;
|
|
||||||
for (i = 0; i < EGSTATE_MAX; i++)
|
|
||||||
{
|
|
||||||
ret->state_params[i].initval = 0;
|
|
||||||
ret->state_params[i].period = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret->state_params[EGSTATE_RELEASED].initval = FMSYNTH_MAX_EGLEVEL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
@ -248,6 +248,34 @@ int fmsynthop_set_samplerate(int fs)
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* name: create_fmsynthop
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR fmsynth_op_t *create_fmsynthop(FAR fmsynth_op_t *op,
|
||||||
|
FAR fmsynth_eg_t *eg)
|
||||||
|
{
|
||||||
|
if (op)
|
||||||
|
{
|
||||||
|
op->eg = eg;
|
||||||
|
|
||||||
|
op->own_allocate = 0;
|
||||||
|
op->wavegen = NULL;
|
||||||
|
op->cascadeop = NULL;
|
||||||
|
op->parallelop = NULL;
|
||||||
|
op->feedback_ref = NULL;
|
||||||
|
op->feedback_val = 0;
|
||||||
|
op->feedbackrate = 0;
|
||||||
|
op->last_sigval = 0;
|
||||||
|
op->freq_rate = 1.f;
|
||||||
|
op->sound_freq = 0.f;
|
||||||
|
op->delta_phase = 0.f;
|
||||||
|
op->current_phase = 0.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* name: fmsynthop_create
|
* name: fmsynthop_create
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -267,17 +295,8 @@ FAR fmsynth_op_t *fmsynthop_create(void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret->wavegen = NULL;
|
create_fmsynthop(ret, ret->eg);
|
||||||
ret->cascadeop = NULL;
|
ret->own_allocate = 1;
|
||||||
ret->parallelop = NULL;
|
|
||||||
ret->feedback_ref = NULL;
|
|
||||||
ret->feedback_val = 0;
|
|
||||||
ret->feedbackrate = 0;
|
|
||||||
ret->last_sigval = 0;
|
|
||||||
ret->freq_rate = 1.f;
|
|
||||||
ret->sound_freq = 0.f;
|
|
||||||
ret->delta_phase = 0.f;
|
|
||||||
ret->current_phase = 0.f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -289,11 +308,11 @@ FAR fmsynth_op_t *fmsynthop_create(void)
|
|||||||
|
|
||||||
void fmsynthop_delete(FAR fmsynth_op_t *op)
|
void fmsynthop_delete(FAR fmsynth_op_t *op)
|
||||||
{
|
{
|
||||||
if (op != NULL)
|
if (op != NULL && op->own_allocate == 1)
|
||||||
{
|
{
|
||||||
if (op->eg)
|
if (op->eg)
|
||||||
{
|
{
|
||||||
free(op->eg);
|
fmsyntheg_delete(op->eg);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(op);
|
free(op);
|
||||||
@ -499,12 +518,18 @@ void fmsynthop_stop(FAR fmsynth_op_t *op)
|
|||||||
|
|
||||||
int fmsynthop_operate(FAR fmsynth_op_t *op, int phase_time)
|
int fmsynthop_operate(FAR fmsynth_op_t *op, int phase_time)
|
||||||
{
|
{
|
||||||
|
int val;
|
||||||
|
int val2;
|
||||||
int phase;
|
int phase;
|
||||||
FAR fmsynth_op_t *subop;
|
FAR fmsynth_op_t *subop;
|
||||||
|
|
||||||
op->current_phase = phase_time ? op->current_phase + op->delta_phase : 0.f;
|
op->current_phase = phase_time ? op->current_phase + op->delta_phase : 0.f;
|
||||||
|
|
||||||
phase = (int)op->current_phase + op->feedback_val;
|
val = (int)op->current_phase;
|
||||||
|
val2 = (val / (2 * FMSYNTH_PI));
|
||||||
|
|
||||||
|
phase = (int)val + op->feedback_val;
|
||||||
|
op->current_phase = op->current_phase - (float)(val2 * (2 * FMSYNTH_PI));
|
||||||
|
|
||||||
subop = op->cascadeop;
|
subop = op->cascadeop;
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
|
|
||||||
typedef struct fmsynth_sound_s
|
typedef struct fmsynth_sound_s
|
||||||
{
|
{
|
||||||
|
int own_allocate;
|
||||||
int phase_time;
|
int phase_time;
|
||||||
int max_phase_time;
|
int max_phase_time;
|
||||||
int volume;
|
int volume;
|
||||||
@ -64,6 +65,8 @@ extern "C"
|
|||||||
|
|
||||||
int fmsynth_initialize(int fs);
|
int fmsynth_initialize(int fs);
|
||||||
FAR fmsynth_sound_t *fmsynthsnd_create(void);
|
FAR fmsynth_sound_t *fmsynthsnd_create(void);
|
||||||
|
FAR fmsynth_sound_t *create_fmsynthsnd(FAR fmsynth_sound_t *);
|
||||||
|
void fmsynthsnd_stop(FAR fmsynth_sound_t *snd);
|
||||||
void fmsynthsnd_delete(FAR fmsynth_sound_t *snd);
|
void fmsynthsnd_delete(FAR fmsynth_sound_t *snd);
|
||||||
int fmsynthsnd_set_operator(FAR fmsynth_sound_t *snd, FAR fmsynth_op_t *op);
|
int fmsynthsnd_set_operator(FAR fmsynth_sound_t *snd, FAR fmsynth_op_t *op);
|
||||||
void fmsynthsnd_set_soundfreq(FAR fmsynth_sound_t *snd, float freq);
|
void fmsynthsnd_set_soundfreq(FAR fmsynth_sound_t *snd, float freq);
|
||||||
|
@ -85,6 +85,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
FAR fmsynth_eg_t *create_fmsyntheg(fmsynth_eg_t *eg);
|
||||||
FAR fmsynth_eg_t *fmsyntheg_create(void);
|
FAR fmsynth_eg_t *fmsyntheg_create(void);
|
||||||
void fmsyntheg_delete(FAR fmsynth_eg_t *eg);
|
void fmsyntheg_delete(FAR fmsynth_eg_t *eg);
|
||||||
int fmsyntheg_set_param(FAR fmsynth_eg_t *eg,
|
int fmsyntheg_set_param(FAR fmsynth_eg_t *eg,
|
||||||
|
@ -52,6 +52,7 @@ typedef struct fmsynth_op_s
|
|||||||
struct fmsynth_op_s *cascadeop;
|
struct fmsynth_op_s *cascadeop;
|
||||||
struct fmsynth_op_s *parallelop;
|
struct fmsynth_op_s *parallelop;
|
||||||
|
|
||||||
|
int own_allocate;
|
||||||
FAR int *feedback_ref;
|
FAR int *feedback_ref;
|
||||||
int feedback_val;
|
int feedback_val;
|
||||||
int feedbackrate;
|
int feedbackrate;
|
||||||
@ -75,6 +76,8 @@ extern "C"
|
|||||||
int fmsynthop_set_samplerate(int fs);
|
int fmsynthop_set_samplerate(int fs);
|
||||||
|
|
||||||
FAR fmsynth_op_t *fmsynthop_create(void);
|
FAR fmsynth_op_t *fmsynthop_create(void);
|
||||||
|
FAR fmsynth_op_t *create_fmsynthop(FAR fmsynth_op_t *op,
|
||||||
|
FAR fmsynth_eg_t *eg);
|
||||||
void fmsynthop_delete(FAR fmsynth_op_t *op);
|
void fmsynthop_delete(FAR fmsynth_op_t *op);
|
||||||
int fmsynthop_select_opfunc(FAR fmsynth_op_t *op, int type);
|
int fmsynthop_select_opfunc(FAR fmsynth_op_t *op, int type);
|
||||||
int fmsynthop_set_envelope(FAR fmsynth_op_t *op,
|
int fmsynthop_set_envelope(FAR fmsynth_op_t *op,
|
||||||
|
Loading…
Reference in New Issue
Block a user