[Asterisk-code-review] func_volume: Accept decimal number as argument (asterisk[master])
Jean Aunis - Prescom
asteriskteam at digium.com
Tue Apr 7 10:48:08 CDT 2020
Jean Aunis - Prescom has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14089 )
Change subject: func_volume: Accept decimal number as argument
......................................................................
func_volume: Accept decimal number as argument
Allow voice volume to be multiplied or divided by a floating point number.
ASTERISK-28813
Change-Id: I5b42b890ec4e1f6b0b3400cb44ff16522b021c8c
---
M funcs/func_volume.c
M include/asterisk/frame.h
M include/asterisk/utils.h
M main/frame.c
4 files changed, 66 insertions(+), 6 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/89/14089/1
diff --git a/funcs/func_volume.c b/funcs/func_volume.c
index 533eb55..8587479 100644
--- a/funcs/func_volume.c
+++ b/funcs/func_volume.c
@@ -70,8 +70,8 @@
struct volume_information {
struct ast_audiohook audiohook;
- int tx_gain;
- int rx_gain;
+ float tx_gain;
+ float rx_gain;
unsigned int flags;
};
@@ -107,7 +107,7 @@
{
struct ast_datastore *datastore = NULL;
struct volume_information *vi = NULL;
- int *gain = NULL;
+ float *gain = NULL;
/* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
@@ -141,7 +141,7 @@
if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
return 0;
/* Apply gain to frame... easy as pi */
- ast_frame_adjust_volume(frame, *gain);
+ ast_frame_adjust_volume_float(frame, *gain);
}
return 0;
@@ -193,9 +193,9 @@
}
if (!strcasecmp(args.direction, "tx")) {
- vi->tx_gain = atoi(value);
+ vi->tx_gain = atof(value);
} else if (!strcasecmp(args.direction, "rx")) {
- vi->rx_gain = atoi(value);
+ vi->rx_gain = atof(value);
} else {
ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
}
diff --git a/include/asterisk/frame.h b/include/asterisk/frame.h
index 780cba3..0ead3b8 100644
--- a/include/asterisk/frame.h
+++ b/include/asterisk/frame.h
@@ -597,6 +597,14 @@
int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);
/*!
+ \brief Adjusts the volume of the audio samples contained in a frame.
+ \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)
+ \param adjustment The number of dB to adjust up or down.
+ \return 0 for success, non-zero for an error
+ */
+int ast_frame_adjust_volume_float(struct ast_frame *f, float adjustment);
+
+/*!
\brief Sums two frames of audio samples.
\param f1 The first frame (which will contain the result)
\param f2 The second frame
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 2b332a5..10dfe83 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -375,11 +375,35 @@
*input = (short) res;
}
+static force_inline void ast_slinear_saturated_multiply_float(short *input, float *value)
+{
+ float res;
+
+ res = (float) *input * *value;
+ if (res > 32767)
+ *input = 32767;
+ else if (res < -32768)
+ *input = -32768;
+ else
+ *input = (short) res;
+}
+
static force_inline void ast_slinear_saturated_divide(short *input, short *value)
{
*input /= *value;
}
+static force_inline void ast_slinear_saturated_divide_float(short *input, float *value)
+{
+ float res = (float) *input / *value;
+ if (res > 32767)
+ *input = 32767;
+ else if (res < -32768)
+ *input = -32768;
+ else
+ *input = (short) res;
+}
+
#ifdef localtime_r
#undef localtime_r
#endif
diff --git a/main/frame.c b/main/frame.c
index f7a5222..e618896 100644
--- a/main/frame.c
+++ b/main/frame.c
@@ -43,6 +43,8 @@
#include "asterisk/dsp.h"
#include "asterisk/file.h"
+#include <math.h>
+
#if (defined(LOW_MEMORY) || defined(MALLOC_DEBUG)) && !defined(NO_FRAME_CACHE)
#define NO_FRAME_CACHE
#endif
@@ -561,6 +563,7 @@
break;
case AST_FRAME_RTCP:
ast_copy_string(subclass, "RTCP", slen);
+ break;
default:
ast_copy_string(subclass, "Unknown Subclass", slen);
break;
@@ -706,6 +709,31 @@
return 0;
}
+int ast_frame_adjust_volume_float(struct ast_frame *f, float adjustment)
+{
+ int count;
+ short *fdata = f->data.ptr;
+ float adjust_value = fabs(adjustment);
+
+ if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_cache_is_slinear(f->subclass.format))) {
+ return -1;
+ }
+
+ if (!adjustment) {
+ return 0;
+ }
+
+ for (count = 0; count < f->samples; count++) {
+ if (adjustment > 0) {
+ ast_slinear_saturated_multiply_float(&fdata[count], &adjust_value);
+ } else if (adjustment < 0) {
+ ast_slinear_saturated_divide_float(&fdata[count], &adjust_value);
+ }
+ }
+
+ return 0;
+}
+
int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
{
int count;
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14089
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I5b42b890ec4e1f6b0b3400cb44ff16522b021c8c
Gerrit-Change-Number: 14089
Gerrit-PatchSet: 1
Gerrit-Owner: Jean Aunis - Prescom <jean.aunis at prescom.fr>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200407/bd34698b/attachment-0001.html>
More information about the asterisk-code-review
mailing list