Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chan_simpleusb, chan_usbradio: Add parameter to make scaling/clipping optional, fix issue #399 #418

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
40b886a
Add RX Audio Statistics and ADC Clip Detection feature. This adds a n…
davidgsd Sep 6, 2024
e8f00d0
Move rxaudiostats functions to ../res/res_usbradio.c, add 'ast_radio_…
davidgsd Sep 8, 2024
84644e5
Add Rx Audio Stats and Clip Detect feature support to chan_usbradio.c…
davidgsd Sep 8, 2024
efe18d8
minor whitespace changes, changed one variable to a signed type.
davidgsd Sep 9, 2024
55cf18a
Removed unused variable.
davidgsd Sep 9, 2024
22bc47f
Add comments, update whitespace (if/for opening braces on same line)
davidgsd Sep 12, 2024
74dd2e6
Add braces around single if statements.
davidgsd Sep 12, 2024
7d66fc6
Rename checkrxaudio parameter to clipledgpio (which specifies the GPI…
davidgsd Sep 12, 2024
217634b
whitespace updates.
davidgsd Sep 12, 2024
c1d0d88
set clipledgpio = 0 in configs/samples/simpleusb.conf.sample.
davidgsd Sep 12, 2024
a68ca64
whitespace cleanup.
davidgsd Sep 12, 2024
67859d1
re-enable scaling code in chan_simplusb and usbradio that scales the …
davidgsd Sep 15, 2024
c9d1b41
consider 0 a valid file descriptor value in ast_radio_print_rx_audio_…
davidgsd Sep 15, 2024
5a30d09
update ast_radio_print_rx_audio_stats() function header comment to cl…
davidgsd Sep 15, 2024
6d3bece
Merge branch 'AllStarLink:master' into master
davidgsd Oct 5, 2024
70683d2
Fix issue #415
davidgsd Oct 5, 2024
0ebd611
Merge branch 'AllStarLink:master' into master
davidgsd Oct 6, 2024
1ce8306
Fix issue #399, add legacyaudioscaling parameter to allow scaling/cli…
davidgsd Oct 7, 2024
a476a0b
Updates from review comments.
davidgsd Oct 7, 2024
889be41
whitespace cleanups
davidgsd Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 18 additions & 41 deletions channels/chan_simpleusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ struct chan_simpleusb_pvt {
#define WARN_speed 2
#define WARN_frag 4

/* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
* be representable in 16 bits to avoid overflows.
*/
#define BOOST_SCALE (1<<9)
#define BOOST_MAX 40 /* slightly less than 7 bits */
int boost; /* input boost, scaled by BOOST_SCALE */
char devicenum;
char devstr[128];
int spkrmax;
Expand Down Expand Up @@ -343,14 +337,16 @@ static struct chan_simpleusb_pvt simpleusb_default = {
.queuesize = QUEUE_SIZE,
.frags = FRAGS,
.readpos = 0, /* start here on reads */
.boost = BOOST_SCALE,
.wanteeprom = 1,
.usedtmf = 1,
.rxondelay = 0,
.txoffdelay = 0,
.pager = PAGER_NONE,
.clipledgpio = 0,
.rxaudiostats.index = 0,
/* After the vast majority of existing installs have had a chance to review their
audio settings and the associated old scaling/clipping hacks are no longer in
significant use the following cfg and all related code should be deleted. */
.legacyaudioscaling = 1,
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
};

Expand Down Expand Up @@ -2108,12 +2104,14 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
/* enough to fill a frame */
memcpy(o->simpleusb_write_buf + o->simpleusb_write_dst, (char *) f1->data.ptr + src, l);

/* TBR - below is an attempt to match levels to the original CM108 IC which has
/* Below is an attempt to match levels to the original CM108 IC which has
* been out of production for over 10 years. Scaling audio to 109.375% will
* result in clipping! Any adjustments for CM1xxx gain differences should be
* made in the mixer settings, not in the audio stream.
* TODO: After the vast majority of existing installs have had a chance to review their
* audio settings and these old scaling/clipping hacks are no longer in significant use
* the legacyaudioscaling cfg and related code should be deleted.
*/
#if 1
/* Adjust the audio level for CM119 A/B devices */
if (o->legacyaudioscaling && o->devtype != C108_PRODUCT_ID) {
register int v;
Expand All @@ -2131,7 +2129,6 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
*sp++ = v;
}
}
#endif

sp = (short *) o->simpleusb_write_buf;
sp1 = outbuf;
Expand Down Expand Up @@ -2408,34 +2405,13 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
}
}

/* Scale the input audio.
* o->boost is hardcoded to equal BOOST_SCALE.
* This code is not executed.
* TBR: The below should be removed. Raw audio samples should never be clipped or scaled
* for any reason. Adjustments to audio levels should be made only in the USB interface
* mixer settings.
/* Raw audio samples should never be clipped or scaled for any reason. Adjustments to
* audio levels should be made only in the USB interface mixer settings.
* TODO: After the vast majority of existing installs have had a chance to review their
* audio settings and these old scaling/clipping hacks are no longer in significant use
* the legacyaudioscaling cfg and related code should be deleted.
*/
#if 0
if (o->boost != BOOST_SCALE) { /* scale and clip values */
register int i, x;
register int16_t *p = (int16_t *) f->data.ptr;
for (i = 0; i < f->samples; i++) {
x = (p[i] * o->boost) / BOOST_SCALE;
if (x > 32767) {
x = 32767;
} else if (x < -32768) {
x = -32768;
}
p[i] = x;
}
}
#endif

/* scale and clip values */
/* TBR: The below should be phased out asap. Raw audio samples should never be clipped
* or scaled for any reason. Adjustments to audio levels should be made only in the
* USB interface mixer settings. */
#if 1
if (o->legacyaudioscaling && o->rxvoiceadj > 1.0) {
register int i, x;
register float f1;
Expand All @@ -2452,7 +2428,6 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
p[i] = x;
}
}
#endif

/* Compute the peak signal if requested */
if (o->measure_enabled) {
Expand Down Expand Up @@ -3108,8 +3083,9 @@ static void _menu_print(int fd, struct chan_simpleusb_pvt *o)
ast_cli(fd, "Rx Level currently set to %d\n", o->rxmixerset);
ast_cli(fd, "Tx A Level currently set to %d\n", o->txmixaset);
ast_cli(fd, "Tx B Level currently set to %d\n", o->txmixbset);
if(o->legacyaudioscaling)
ast_cli(fd, "legacyaudioscaling is enabled (not recommended)\n");
if(o->legacyaudioscaling) {
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
ast_cli(fd, "legacyaudioscaling is enabled\n");
}
return;
}

Expand Down Expand Up @@ -3638,7 +3614,8 @@ static void mixer_write(struct chan_simpleusb_pvt *o)
ast_radio_setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_SW, 0, 0);
ast_radio_setamixer(o->devicenum, (o->newname) ? MIXER_PARAM_SPKR_PLAYBACK_SW_NEW : MIXER_PARAM_SPKR_PLAYBACK_SW, 1, 0);
ast_radio_setamixer(o->devicenum, (o->newname) ? MIXER_PARAM_SPKR_PLAYBACK_VOL_NEW : MIXER_PARAM_SPKR_PLAYBACK_VOL,
ast_radio_make_spkr_playback_value(o->spkrmax, o->txmixaset, o->devtype), ast_radio_make_spkr_playback_value(o->spkrmax, o->txmixbset, o->devtype));
ast_radio_make_spkr_playback_value(o->spkrmax, o->txmixaset, o->devtype),
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
ast_radio_make_spkr_playback_value(o->spkrmax, o->txmixbset, o->devtype));
/* adjust settings based on the device */
switch (o->devtype) {
case C119B_PRODUCT_ID:
Expand All @@ -3652,7 +3629,7 @@ static void mixer_write(struct chan_simpleusb_pvt *o)
/* get interval step size */
f = 1000.0 / (float) o->micmax;
}
ast_radio_setamixer(o->devicenum,MIXER_PARAM_MIC_CAPTURE_VOL, mic_setting, 0);
ast_radio_setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL, mic_setting, 0);
ast_radio_setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboost, 0);
ast_radio_setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_SW, 1, 0);
/* set the received voice adjustment factor */
Expand Down
48 changes: 16 additions & 32 deletions channels/chan_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,6 @@ struct chan_usbradio_pvt {
#define WARN_speed 2
#define WARN_frag 4

/* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
* be representable in 16 bits to avoid overflows.
*/
#define BOOST_SCALE (1<<9)
#define BOOST_MAX 40 /* slightly less than 7 bits */
int boost; /* input boost, scaled by BOOST_SCALE */
char devicenum;
char devstr[128];
int spkrmax;
Expand Down Expand Up @@ -425,7 +419,6 @@ static struct chan_usbradio_pvt usbradio_default = {
.queuesize = QUEUE_SIZE,
.frags = FRAGS,
.readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
.boost = BOOST_SCALE,
.wanteeprom = 1,
.usedtmf = 1,
.rxondelay = 0,
Expand All @@ -434,6 +427,9 @@ static struct chan_usbradio_pvt usbradio_default = {
.rptnum = 0,
.clipledgpio = 0,
.rxaudiostats.index = 0,
/* After the vast majority of existing installs have had a chance to review their
audio settings and the associated old scaling/clipping hacks are no longer in
significant use the following cfg and all related code should be deleted. */
.legacyaudioscaling = 1,
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
};

Expand Down Expand Up @@ -2070,13 +2066,15 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
}
}

/* TBR - below is an attempt to match levels to the original CM108 IC which has been
/* Below is an attempt to match levels to the original CM108 IC which has been
* out of production for over 10 years. Scaling all rx audio to 80% results in a 20%
* loss in dynamic range, added quantization noise, a 2dB reduction in outgoing IAX
* audio levels, and inconsistency with Simpleusb. Adjustments for CM1xxx IC gain
* differences should be made in the mixer settings, not in the audio stream.
* TODO: After the vast majority of existing installs have had a chance to review their
* audio settings and these old scaling/clipping hacks are no longer in significant use
* the legacyaudioscaling cfg and related code should be deleted.
*/
#if 1
/* Decrease the audio level for CM119 A/B devices */
if (o->legacyaudioscaling && o->devtype != C108_PRODUCT_ID) {
/* Subtract res from o->readpos in below assignment (o->readpos was incremented
Expand All @@ -2090,7 +2088,6 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
*sp++ = (int) v;
}
}
#endif

#if 1
if (o->txkeyed || o->txtestkey || o->echoing) {
Expand Down Expand Up @@ -2140,12 +2137,14 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
}
#endif

/* TBR - below is an attempt to match levels to the original CM108 IC which has been
/* Below is an attempt to match levels to the original CM108 IC which has been
* out of production for over 10 years. Scaling audio to 110% will result in clipping!
* Any adjustments for CM1xxx IC gain differences should be made in the mixer
* settings, not in the audio stream.
* TODO: After the vast majority of existing installs have had a chance to review their
* audio settings and these old scaling/clipping hacks are no longer in significant use
* the legacyaudioscaling cfg and related code should be deleted.
*/
#if 1
/* For the CM108 adjust the audio level */
if (o->legacyaudioscaling && o->devtype != C108_PRODUCT_ID) {
register short *sp = (short *) o->usbradio_write_buf;
Expand All @@ -2162,7 +2161,7 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
*sp++ = (int) v;
}
}
#endif

/* Write the received audio to the sound card */
soundcard_writeframe(o, (short *) o->usbradio_write_buf);

Expand Down Expand Up @@ -2414,23 +2413,7 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
}
}
}
/* Scale the input audio.
* o->boost is hardcoded to equal BOOST_SCALE.
* This code is not executed.
*/
if (o->boost != BOOST_SCALE) { /* scale and clip values */
register int i, x;
register int16_t *p = (int16_t *) f->data.ptr;
for (i = 0; i < f->samples; i++) {
x = (p[i] * o->boost) / BOOST_SCALE;
if (x > 32767) {
x = 32767;
} else if (x < -32768) {
x = -32768;
}
p[i] = x;
}
}

if (o->pmrChan->b.txCtcssReady) {
struct ast_frame wf = { AST_FRAME_TEXT };
char msg[32];
Expand Down Expand Up @@ -3704,8 +3687,9 @@ static void _menu_print(int fd, struct chan_usbradio_pvt *o)
ast_cli(fd, "Tx Voice Level currently set to %d\n", o->txmixaset);
ast_cli(fd, "Tx Tone Level currently set to %d\n", o->txctcssadj);
ast_cli(fd, "Rx Squelch currently set to %d\n", o->rxsquelchadj);
if(o->legacyaudioscaling)
ast_cli(fd, "legacyaudioscaling is enabled (not recommended)\n");
if(o->legacyaudioscaling) {
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
ast_cli(fd, "legacyaudioscaling is enabled\n");
}
return;
}

Expand Down
6 changes: 3 additions & 3 deletions configs/rpt/simpleusb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ clipledgpio = 1 ; Enable ADC Clip Detect feature to use a GP
; when clipping detected. Value = GPIO# to use (GPIO1 recommended)

legacyaudioscaling = no ; If yes, continue to do raw audio sample scaling and clipping, resulting in Tx audio levels increasing
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
; by 0.78dB and Rx audio levels increasing by 0-1.5dB. This should be set to no unless you have an
; existing node with precisely adjusted audio levels but are unable to adjust them. If set to
; yes, degraded audio quality will result
; by 0.78dB and Rx audio levels increasing by 0-1.5dB. Should be set to no unless you have an existing
; node with precisely adjusted audio levels and are unable to adjust them. This parameter and associated
; scaling/clipping code will be deleted once existing installs have been able to verify their audio levels

;;; End of node-main template

Expand Down
6 changes: 3 additions & 3 deletions configs/rpt/usbradio.conf
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ clipledgpio = 0 ; Enable ADC Clip Detect feature to use a GPIO outpu
; when clipping detected. Value = GPIO# to use (GPIO1 recommended)

legacyaudioscaling = no ; If yes, continue to do raw audio sample scaling and clipping, resulting in Tx audio levels increasing
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
; by 0.83dB and Rx audio levels decreasing by 1.94dB. This should be set to no unless you have an
; existing node with precisely adjusted audio levels but are not yet able to adjust them. If set to
; yes, degraded audio quality will result
; by 0.83dB and Rx audio levels decreasing by 1.94dB. Should be set to no unless you have an existing
; node with precisely adjusted audio levels and are unable to adjust them. This parameter and associated
; scaling/clipping code will be deleted once existing installs have been able to verify their audio levels

;;; End of node-main template

Expand Down
6 changes: 3 additions & 3 deletions configs/samples/simpleusb.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
;ppX=in ; printer port pin(x) [x is 10,12,13,15] input pin [in,cor,ctcss]

legacyaudioscaling = no ; If yes, continue to do raw audio sample scaling and clipping, resulting in Tx audio levels increasing
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
; by 0.78dB and Rx audio levels increasing by 0-1.5dB. This should be set to no unless you have an
; existing node with precisely adjusted audio levels but are unable to adjust them. If set to
; yes, degraded audio quality will result
; by 0.78dB and Rx audio levels increasing by 0-1.5dB. Should be set to no unless you have an existing
; node with precisely adjusted audio levels and are unable to adjust them. This parameter and associated
; scaling/clipping code will be deleted once existing installs have been able to verify their audio levels

;[usb]
; First channel unique configuration
Expand Down
6 changes: 3 additions & 3 deletions configs/samples/usbradio.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@
;ppX=in ; printer port pin(x) [x is 10,12,13,15] input pin [in,cor,ctcss]

legacyaudioscaling = no ; If yes, continue to do raw audio sample scaling and clipping, resulting in Tx audio levels increasing
davidgsd marked this conversation as resolved.
Show resolved Hide resolved
; by 0.83dB and Rx audio levels decreasing by 1.94dB. This should be set to no unless you have an
; existing node with precisely adjusted audio levels but are not yet able to adjust them. If set to
; yes, degraded audio quality will result
; by 0.83dB and Rx audio levels decreasing by 1.94dB. Should be set to no unless you have an existing
; node with precisely adjusted audio levels and are unable to adjust them. This parameter and associated
; scaling/clipping code will be deleted once existing installs have been able to verify their audio levels

;[usb]
; First channel unique configuration
Expand Down