<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14089">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_volume: Accept decimal number as argument<br><br>Allow voice volume to be multiplied or divided by a floating point number.<br><br>ASTERISK-28813<br><br>Change-Id: I5b42b890ec4e1f6b0b3400cb44ff16522b021c8c<br>---<br>A doc/CHANGES-staging/func_volume.txt<br>M funcs/func_volume.c<br>M include/asterisk/frame.h<br>M include/asterisk/utils.h<br>M main/frame.c<br>5 files changed, 68 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/func_volume.txt b/doc/CHANGES-staging/func_volume.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..e73295b</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/func_volume.txt</span><br><span>@@ -0,0 +1,3 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: func_volume</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Accept decimal number as argument.</span><br><span>diff --git a/funcs/func_volume.c b/funcs/func_volume.c</span><br><span>index 533eb55..8587479 100644</span><br><span>--- a/funcs/func_volume.c</span><br><span>+++ b/funcs/func_volume.c</span><br><span>@@ -70,8 +70,8 @@</span><br><span> </span><br><span> struct volume_information {</span><br><span> struct ast_audiohook audiohook;</span><br><span style="color: hsl(0, 100%, 40%);">- int tx_gain;</span><br><span style="color: hsl(0, 100%, 40%);">- int rx_gain;</span><br><span style="color: hsl(120, 100%, 40%);">+ float tx_gain;</span><br><span style="color: hsl(120, 100%, 40%);">+ float rx_gain;</span><br><span> unsigned int flags;</span><br><span> };</span><br><span> </span><br><span>@@ -107,7 +107,7 @@</span><br><span> {</span><br><span> struct ast_datastore *datastore = NULL;</span><br><span> struct volume_information *vi = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- int *gain = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ float *gain = NULL;</span><br><span> </span><br><span> /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */</span><br><span> if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)</span><br><span>@@ -141,7 +141,7 @@</span><br><span> if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)</span><br><span> return 0;</span><br><span> /* Apply gain to frame... easy as pi */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_frame_adjust_volume(frame, *gain);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_frame_adjust_volume_float(frame, *gain);</span><br><span> }</span><br><span> </span><br><span> return 0;</span><br><span>@@ -193,9 +193,9 @@</span><br><span> }</span><br><span> </span><br><span> if (!strcasecmp(args.direction, "tx")) {</span><br><span style="color: hsl(0, 100%, 40%);">- vi->tx_gain = atoi(value);</span><br><span style="color: hsl(120, 100%, 40%);">+ vi->tx_gain = atof(value);</span><br><span> } else if (!strcasecmp(args.direction, "rx")) {</span><br><span style="color: hsl(0, 100%, 40%);">- vi->rx_gain = atoi(value);</span><br><span style="color: hsl(120, 100%, 40%);">+ vi->rx_gain = atof(value);</span><br><span> } else {</span><br><span> ast_log(LOG_ERROR, "Direction must be either RX or TX\n");</span><br><span> }</span><br><span>diff --git a/include/asterisk/frame.h b/include/asterisk/frame.h</span><br><span>index 780cba3..0ead3b8 100644</span><br><span>--- a/include/asterisk/frame.h</span><br><span>+++ b/include/asterisk/frame.h</span><br><span>@@ -597,6 +597,14 @@</span><br><span> int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ \brief Adjusts the volume of the audio samples contained in a frame.</span><br><span style="color: hsl(120, 100%, 40%);">+ \param f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)</span><br><span style="color: hsl(120, 100%, 40%);">+ \param adjustment The number of dB to adjust up or down.</span><br><span style="color: hsl(120, 100%, 40%);">+ \return 0 for success, non-zero for an error</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_frame_adjust_volume_float(struct ast_frame *f, float adjustment);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> \brief Sums two frames of audio samples.</span><br><span> \param f1 The first frame (which will contain the result)</span><br><span> \param f2 The second frame</span><br><span>diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h</span><br><span>index 2b332a5..10dfe83 100644</span><br><span>--- a/include/asterisk/utils.h</span><br><span>+++ b/include/asterisk/utils.h</span><br><span>@@ -375,11 +375,35 @@</span><br><span> *input = (short) res;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static force_inline void ast_slinear_saturated_multiply_float(short *input, float *value)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ float res;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ res = (float) *input * *value;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res > 32767)</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = 32767;</span><br><span style="color: hsl(120, 100%, 40%);">+ else if (res < -32768)</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = -32768;</span><br><span style="color: hsl(120, 100%, 40%);">+ else</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = (short) res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static force_inline void ast_slinear_saturated_divide(short *input, short *value)</span><br><span> {</span><br><span> *input /= *value;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static force_inline void ast_slinear_saturated_divide_float(short *input, float *value)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ float res = (float) *input / *value;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res > 32767)</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = 32767;</span><br><span style="color: hsl(120, 100%, 40%);">+ else if (res < -32768)</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = -32768;</span><br><span style="color: hsl(120, 100%, 40%);">+ else</span><br><span style="color: hsl(120, 100%, 40%);">+ *input = (short) res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #ifdef localtime_r</span><br><span> #undef localtime_r</span><br><span> #endif</span><br><span>diff --git a/main/frame.c b/main/frame.c</span><br><span>index f7a5222..4ae732b 100644</span><br><span>--- a/main/frame.c</span><br><span>+++ b/main/frame.c</span><br><span>@@ -43,6 +43,8 @@</span><br><span> #include "asterisk/dsp.h"</span><br><span> #include "asterisk/file.h"</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <math.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #if (defined(LOW_MEMORY) || defined(MALLOC_DEBUG)) && !defined(NO_FRAME_CACHE)</span><br><span> #define NO_FRAME_CACHE</span><br><span> #endif</span><br><span>@@ -706,6 +708,31 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_frame_adjust_volume_float(struct ast_frame *f, float adjustment)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int count;</span><br><span style="color: hsl(120, 100%, 40%);">+ short *fdata = f->data.ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+ float adjust_value = fabs(adjustment);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_cache_is_slinear(f->subclass.format))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!adjustment) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (count = 0; count < f->samples; count++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (adjustment > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_slinear_saturated_multiply_float(&fdata[count], &adjust_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (adjustment < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_slinear_saturated_divide_float(&fdata[count], &adjust_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)</span><br><span> {</span><br><span> int count;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14089">change 14089</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/14089"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I5b42b890ec4e1f6b0b3400cb44ff16522b021c8c </div>
<div style="display:none"> Gerrit-Change-Number: 14089 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Jean Aunis - Prescom <jean.aunis@prescom.fr> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-CC: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>