This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 4a4c550ce audioutils/fmsynth: Add create instance for static
4a4c550ce is described below
commit 4a4c550cedb1411b34d8d88345dfe7bdda8cfb97
Author: SPRESENSE <[email protected]>
AuthorDate: Wed Jul 12 00:26:05 2023 +0900
audioutils/fmsynth: Add create instance for static
To use this with static instance, add create function with
arguments.
---
audioutils/fmsynth/fmsynth.c | 41 +++++++++++++++++++++++++++----
audioutils/fmsynth/fmsynth_eg.c | 30 +++++++++++++++--------
audioutils/fmsynth/fmsynth_op.c | 53 ++++++++++++++++++++++++++++++-----------
include/audioutils/fmsynth.h | 3 +++
include/audioutils/fmsynth_eg.h | 1 +
include/audioutils/fmsynth_op.h | 3 +++
6 files changed, 102 insertions(+), 29 deletions(-)
diff --git a/audioutils/fmsynth/fmsynth.c b/audioutils/fmsynth/fmsynth.c
index 86a791fab..21b43bcf0 100644
--- a/audioutils/fmsynth/fmsynth.c
+++ b/audioutils/fmsynth/fmsynth.c
@@ -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
****************************************************************************/
diff --git a/audioutils/fmsynth/fmsynth_eg.c b/audioutils/fmsynth/fmsynth_eg.c
index 6f7ca4852..a2b97aded 100644
--- a/audioutils/fmsynth/fmsynth_eg.c
+++ b/audioutils/fmsynth/fmsynth_eg.c
@@ -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);
}
/****************************************************************************
diff --git a/audioutils/fmsynth/fmsynth_op.c b/audioutils/fmsynth/fmsynth_op.c
index 6bb200cd9..bb07f12f7 100644
--- a/audioutils/fmsynth/fmsynth_op.c
+++ b/audioutils/fmsynth/fmsynth_op.c
@@ -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;
diff --git a/include/audioutils/fmsynth.h b/include/audioutils/fmsynth.h
index 827be95df..b5676a358 100644
--- a/include/audioutils/fmsynth.h
+++ b/include/audioutils/fmsynth.h
@@ -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);
diff --git a/include/audioutils/fmsynth_eg.h b/include/audioutils/fmsynth_eg.h
index 040cc7b98..f69b42e49 100644
--- a/include/audioutils/fmsynth_eg.h
+++ b/include/audioutils/fmsynth_eg.h
@@ -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,
diff --git a/include/audioutils/fmsynth_op.h b/include/audioutils/fmsynth_op.h
index bbba43216..ec54ccf1e 100644
--- a/include/audioutils/fmsynth_op.h
+++ b/include/audioutils/fmsynth_op.h
@@ -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,