audioutils/fmsynth: Add create instance for static

To use this with static instance, add create function with
arguments.
This commit is contained in:
SPRESENSE 2023-07-12 00:26:05 +09:00 committed by Xiang Xiao
parent 5365fc9ffd
commit 4a4c550ced
6 changed files with 107 additions and 34 deletions

View File

@ -107,6 +107,24 @@ int fmsynth_initialize(int 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
****************************************************************************/
@ -115,12 +133,11 @@ FAR fmsynth_sound_t *fmsynthsnd_create(void)
{
FAR fmsynth_sound_t *ret;
ret = (FAR fmsynth_sound_t *)malloc(sizeof(fmsynth_sound_t));
if (ret)
{
ret->phase_time = 0;
ret->volume = FMSYNTH_MAX_VOLUME;
ret->operators = NULL;
ret->next_sound = NULL;
create_fmsynthsnd(ret);
ret->own_allocate = 1;
}
return ret;
@ -132,7 +149,7 @@ FAR fmsynth_sound_t *fmsynthsnd_create(void)
void fmsynthsnd_delete(FAR fmsynth_sound_t *snd)
{
if (snd != NULL)
if (snd != NULL && snd->own_allocate == 1)
{
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
****************************************************************************/

View File

@ -64,29 +64,39 @@ static int set_egparams(int fs,
* 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
****************************************************************************/
FAR fmsynth_eg_t *fmsyntheg_create(void)
{
int i;
FAR fmsynth_eg_t *ret = (FAR fmsynth_eg_t *)malloc(sizeof(fmsynth_eg_t));
if (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;
return create_fmsyntheg(ret);
}
/****************************************************************************

View File

@ -248,6 +248,34 @@ int fmsynthop_set_samplerate(int fs)
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
****************************************************************************/
@ -267,17 +295,8 @@ FAR fmsynth_op_t *fmsynthop_create(void)
return NULL;
}
ret->wavegen = NULL;
ret->cascadeop = NULL;
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;
create_fmsynthop(ret, ret->eg);
ret->own_allocate = 1;
}
return ret;
@ -289,11 +308,11 @@ FAR fmsynth_op_t *fmsynthop_create(void)
void fmsynthop_delete(FAR fmsynth_op_t *op)
{
if (op != NULL)
if (op != NULL && op->own_allocate == 1)
{
if (op->eg)
{
free(op->eg);
fmsyntheg_delete(op->eg);
}
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 val;
int val2;
int phase;
FAR fmsynth_op_t *subop;
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;

View File

@ -43,6 +43,7 @@
typedef struct fmsynth_sound_s
{
int own_allocate;
int phase_time;
int max_phase_time;
int volume;
@ -64,6 +65,8 @@ extern "C"
int fmsynth_initialize(int fs);
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);
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);

View File

@ -85,6 +85,7 @@ extern "C"
{
#endif
FAR fmsynth_eg_t *create_fmsyntheg(fmsynth_eg_t *eg);
FAR fmsynth_eg_t *fmsyntheg_create(void);
void fmsyntheg_delete(FAR fmsynth_eg_t *eg);
int fmsyntheg_set_param(FAR fmsynth_eg_t *eg,

View File

@ -52,6 +52,7 @@ typedef struct fmsynth_op_s
struct fmsynth_op_s *cascadeop;
struct fmsynth_op_s *parallelop;
int own_allocate;
FAR int *feedback_ref;
int feedback_val;
int feedbackrate;
@ -75,6 +76,8 @@ extern "C"
int fmsynthop_set_samplerate(int fs);
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);
int fmsynthop_select_opfunc(FAR fmsynth_op_t *op, int type);
int fmsynthop_set_envelope(FAR fmsynth_op_t *op,