Skip to content

Commit

Permalink
audioutils/fmsynth: Add create instance for static
Browse files Browse the repository at this point in the history
To use this with static instance, add create function with
arguments.
  • Loading branch information
SPRESENSE authored and xiaoxiang781216 committed Aug 1, 2023
1 parent 5365fc9 commit 4a4c550
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 29 deletions.
41 changes: 36 additions & 5 deletions audioutils/fmsynth/fmsynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
****************************************************************************/
Expand Down
30 changes: 20 additions & 10 deletions audioutils/fmsynth/fmsynth_eg.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,38 @@ static int set_egparams(int fs,
****************************************************************************/

/****************************************************************************
* name: fmsyntheg_create
* name: create_fmsyntheg
****************************************************************************/

FAR fmsynth_eg_t *fmsyntheg_create(void)
FAR fmsynth_eg_t *create_fmsyntheg(FAR fmsynth_eg_t *eg)
{
int i;
FAR fmsynth_eg_t *ret = (FAR fmsynth_eg_t *)malloc(sizeof(fmsynth_eg_t));

if (ret)
if (eg)
{
ret->state = EGSTATE_RELEASED;
ret->state_counter = 0;
eg->state = EGSTATE_RELEASED;
eg->state_counter = 0;
for (i = 0; i < EGSTATE_MAX; i++)
{
ret->state_params[i].initval = 0;
ret->state_params[i].period = 0;
eg->state_params[i].initval = 0;
eg->state_params[i].period = 0;
}

ret->state_params[EGSTATE_RELEASED].initval = FMSYNTH_MAX_EGLEVEL;
eg->state_params[EGSTATE_RELEASED].initval = FMSYNTH_MAX_EGLEVEL;
}

return ret;
return eg;
}

/****************************************************************************
* name: fmsyntheg_create
****************************************************************************/

FAR fmsynth_eg_t *fmsyntheg_create(void)
{
FAR fmsynth_eg_t *ret = (FAR fmsynth_eg_t *)malloc(sizeof(fmsynth_eg_t));

return create_fmsyntheg(ret);
}

/****************************************************************************
Expand Down
53 changes: 39 additions & 14 deletions audioutils/fmsynth/fmsynth_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions include/audioutils/fmsynth.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

typedef struct fmsynth_sound_s
{
int own_allocate;
int phase_time;
int max_phase_time;
int volume;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions include/audioutils/fmsynth_eg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions include/audioutils/fmsynth_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 4a4c550

Please sign in to comment.