Skip to content

Commit

Permalink
Various tidying up jobs.
Browse files Browse the repository at this point in the history
V.150.1 has moved forward, but is still a work in progress.
The various modules using Goertzel filters were not using a harmonised
approach to setting thresholds and measuring power levels. This has
been improved.
  • Loading branch information
coppice-git committed Oct 18, 2023
1 parent df1282e commit 5394b2c
Show file tree
Hide file tree
Showing 112 changed files with 6,106 additions and 1,977 deletions.
6 changes: 3 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ nobase_include_HEADERS = spandsp/ademco_contactid.h \
spandsp/queue.h \
spandsp/saturated.h \
spandsp/schedule.h \
spandsp/stdbool.h \
spandsp/sig_tone.h \
spandsp/silence_gen.h \
spandsp/ssl_fax.h \
spandsp/sprt.h \
spandsp/ssl_fax.h \
spandsp/stdbool.h \
spandsp/super_tone_rx.h \
spandsp/super_tone_tx.h \
spandsp/swept_tone.h \
Expand Down Expand Up @@ -322,8 +322,8 @@ nobase_include_HEADERS = spandsp/ademco_contactid.h \
spandsp/private/schedule.h \
spandsp/private/sig_tone.h \
spandsp/private/silence_gen.h \
spandsp/private/ssl_fax.h \
spandsp/private/sprt.h \
spandsp/private/ssl_fax.h \
spandsp/private/super_tone_rx.h \
spandsp/private/super_tone_tx.h \
spandsp/private/swept_tone.h \
Expand Down
16 changes: 8 additions & 8 deletions src/ademco_contactid.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ static const struct ademco_code_s ademco_codes[] =
{-1, "???"}
};

#define GOERTZEL_SAMPLES_PER_BLOCK 55 /* We need to detect over a +-5% range */
#define GOERTZEL_SAMPLES_PER_BLOCK 55 /* We need to detect over a +-5% range */

#if defined(SPANDSP_USE_FIXED_POINT)
#define DETECTION_THRESHOLD 3035 /* -42dBm0 */
#define TONE_TO_TOTAL_ENERGY 45.2233f /* -0.85dB */
static const int detection_threshold = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
static const float tone_to_total_energy = GOERTZEL_SAMPLES_PER_BLOCK*db_to_power_ratio(-0.85f);
#else
#define DETECTION_THRESHOLD 49728296.6f /* -42dBm0 [((GOERTZEL_SAMPLES_PER_BLOCK*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2] */
#define TONE_TO_TOTAL_ENERGY 45.2233f /* -0.85dB [GOERTZEL_SAMPLES_PER_BLOCK*10^(-0.85/10.0)] */
static const float detection_threshold = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
static const float tone_to_total_energy = GOERTZEL_SAMPLES_PER_BLOCK*db_to_power_ratio(-0.85f);
#endif

static int tone_rx_init = false;
Expand Down Expand Up @@ -906,17 +906,17 @@ SPAN_DECLARE(int) ademco_contactid_sender_rx(ademco_contactid_sender_state_t *s,
energy_1400 = goertzel_result(&s->tone_1400);
energy_2300 = goertzel_result(&s->tone_2300);
hit = 0;
if (energy_1400 > DETECTION_THRESHOLD || energy_2300 > DETECTION_THRESHOLD)
if (energy_1400 > detection_threshold || energy_2300 > detection_threshold)
{
if (energy_1400 > energy_2300)
{
if (energy_1400 > TONE_TO_TOTAL_ENERGY*s->energy)
if (energy_1400 > tone_to_total_energy*s->energy)
hit = 1;
/*endif*/
}
else
{
if (energy_2300 > TONE_TO_TOTAL_ENERGY*s->energy)
if (energy_2300 > tone_to_total_energy*s->energy)
hit = 2;
/*endif*/
}
Expand Down
50 changes: 25 additions & 25 deletions src/bell_r2_mf.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,26 @@ static const mf_digit_tones_t socotel_tones[] =
static char socotel_mf_tone_codes[] = "1234567890ABCDEFG";
#endif

#if defined(SPANDSP_USE_FIXED_POINT)
#define BELL_MF_THRESHOLD 204089 /* -30.5dBm0 */
#define BELL_MF_TWIST 3.981f /* 6dB */
#define BELL_MF_RELATIVE_PEAK 12.589f /* 11dB */
#define BELL_MF_SAMPLES_PER_BLOCK 120

#define R2_MF_THRESHOLD 62974 /* -36.5dBm0 */
#define R2_MF_TWIST 5.012f /* 7dB */
#define R2_MF_RELATIVE_PEAK 12.589f /* 11dB */
#define R2_MF_SAMPLES_PER_BLOCK 133

#if defined(SPANDSP_USE_FIXED_POINT)
static const int bell_mf_threshold = goertzel_threshold_dbm0(BELL_MF_SAMPLES_PER_BLOCK, -30.5f);
static const float bell_mf_twist = db_to_power_ratio(6.0f);
static const float bell_mf_relative_peak = db_to_power_ratio(11.0f);

static const int r2_mf_threshold = goertzel_threshold_dbm0(R2_MF_SAMPLES_PER_BLOCK, -36.5f);
static const float r2_mf_twist = db_to_power_ratio(7.0f);
static const float r2_mf_relative_peak = db_to_power_ratio(11.0f);
#else
#define BELL_MF_THRESHOLD 3343803100.0f /* -30.5dBm0 [((120.0*32768.0/1.4142)*10^((-30.5 - DBM0_MAX_SINE_POWER)/20.0))^2 => 3343803100.0] */
#define BELL_MF_TWIST 3.981f /* 6dB [10^(6/10) => 3.981] */
#define BELL_MF_RELATIVE_PEAK 12.589f /* 11dB */
#define BELL_MF_SAMPLES_PER_BLOCK 120
static const float bell_mf_threshold = goertzel_threshold_dbm0(BELL_MF_SAMPLES_PER_BLOCK, -30.5f);
static const float bell_mf_twist = db_to_power_ratio(6.0f);
static const float bell_mf_relative_peak = db_to_power_ratio(11.0f);

#define R2_MF_THRESHOLD 1031766650.0f /* -36.5dBm0 [((133.0*32768.0/1.4142)*10^((-36.5 - DBM0_MAX_SINE_POWER)/20.0))^2 => 1031766650.0] */
#define R2_MF_TWIST 5.012f /* 7dB */
#define R2_MF_RELATIVE_PEAK 12.589f /* 11dB */
#define R2_MF_SAMPLES_PER_BLOCK 133
static const float r2_mf_threshold = goertzel_threshold_dbm0(R2_MF_SAMPLES_PER_BLOCK, -36.5f);
static const float r2_mf_twist = db_to_power_ratio(7.0f);
static const float r2_mf_relative_peak = db_to_power_ratio(11.0f);
#endif

static goertzel_descriptor_t bell_mf_detect_desc[6];
Expand Down Expand Up @@ -561,21 +561,21 @@ SPAN_DECLARE(int) bell_mf_rx(bell_mf_rx_state_t *s, const int16_t amp[], int sam
/*endfor*/
/* Basic signal level and twist tests */
hit = 0;
if (energy[best] >= BELL_MF_THRESHOLD
if (energy[best] >= bell_mf_threshold
&&
energy[second_best] >= BELL_MF_THRESHOLD
energy[second_best] >= bell_mf_threshold
&&
energy[best] < energy[second_best]*BELL_MF_TWIST
energy[best] < energy[second_best]*bell_mf_twist
&&
energy[best]*BELL_MF_TWIST > energy[second_best])
energy[best]*bell_mf_twist > energy[second_best])
{
/* Relative peak test */
hit = 'X';
for (i = 0; i < 6; i++)
{
if (i != best && i != second_best)
{
if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best])
if (energy[i]*bell_mf_relative_peak >= energy[second_best])
{
/* The best two are not clearly the best */
hit = 0;
Expand Down Expand Up @@ -801,21 +801,21 @@ SPAN_DECLARE(int) r2_mf_rx(r2_mf_rx_state_t *s, const int16_t amp[], int samples
/*endfor*/
/* Basic signal level and twist tests */
hit = false;
if (energy[best] >= R2_MF_THRESHOLD
if (energy[best] >= r2_mf_threshold
&&
energy[second_best] >= R2_MF_THRESHOLD
energy[second_best] >= r2_mf_threshold
&&
energy[best] < energy[second_best]*R2_MF_TWIST
energy[best] < energy[second_best]*r2_mf_twist
&&
energy[best]*R2_MF_TWIST > energy[second_best])
energy[best]*r2_mf_twist > energy[second_best])
{
/* Relative peak test */
hit = true;
for (i = 0; i < 6; i++)
{
if (i != best && i != second_best)
{
if (energy[i]*R2_MF_RELATIVE_PEAK >= energy[second_best])
if (energy[i]*r2_mf_relative_peak >= energy[second_best])
{
/* The best two are not clearly the best */
hit = false;
Expand Down
87 changes: 39 additions & 48 deletions src/dtmf.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,31 @@
#include "spandsp/private/tone_generate.h"
#include "spandsp/private/dtmf.h"

#define DEFAULT_DTMF_TX_LEVEL -10
#define DEFAULT_DTMF_TX_ON_TIME 50
#define DEFAULT_DTMF_TX_OFF_TIME 55
#define DEFAULT_DTMF_TX_LEVEL -10 /* In dBm0 */
#define DEFAULT_DTMF_TX_ON_TIME 50 /* in ms */
#define DEFAULT_DTMF_TX_OFF_TIME 55 /* in ms */

#define DTMF_SAMPLES_PER_BLOCK 102

#if defined(SPANDSP_USE_FIXED_POINT)
/* The fixed point version scales the 16 bit signal down by 7 bits, so the Goertzels will fit in a 32 bit word */
#define FP_SCALE(x) ((int16_t) (x/128.0 + ((x >= 0.0) ? 0.5 : -0.5)))
#define DTMF_THRESHOLD 10438 /* -42dBm0 [((DTMF_SAMPLES_PER_BLOCK*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0)/128.0)^2]*/
#define DTMF_NORMAL_TWIST 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_REVERSE_TWIST 2.512f /* 4dB [10.0^(4.0/10.0)] */
#define DTMF_RELATIVE_PEAK_ROW 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_RELATIVE_PEAK_COL 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_TO_TOTAL_ENERGY 83.868f /* -0.85dB [DTMF_SAMPLES_PER_BLOCK*10^(-0.85/10.0)] */
#define DTMF_POWER_OFFSET 68.251f /* 10*log(((32768.0/128.0)^2)*DTMF_SAMPLES_PER_BLOCK) */
#define DTMF_SAMPLES_PER_BLOCK 102
#define FP_SCALE(x) ((int16_t) (x/128.0 + ((x >= 0.0) ? 0.5 : -0.5)))
static const float dtmf_threshold = goertzel_threshold_dbm0(DTMF_SAMPLES_PER_BLOCK, -42.0f);
static const float dtmf_normal_twist = db_to_power_ratio(8.0f);
static const float dtmf_reverse_twist = db_to_power_ratio(4.0f);
static const float dtmf_relative_peak_row = db_to_power_ratio(8.0f);
static const float dtmf_relative_peak_col = db_to_power_ratio(8.0f);
static const float dtmf_to_total_energy = DTMF_SAMPLES_PER_BLOCK*db_to_power_ratio(-0.85f);
static const float dtmf_power_offset = (power_ratio_to_db(256.0f*256.0f*DTMF_SAMPLES_PER_BLOCK) - DBM0_MAX_SINE_POWER);
#else
#define FP_SCALE(x) (x)
#define DTMF_THRESHOLD 171032462.0f /* -42dBm0 [((DTMF_SAMPLES_PER_BLOCK*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2] */
#define DTMF_NORMAL_TWIST 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_REVERSE_TWIST 2.512f /* 4dB [10.0^(4.0/10.0)] */
#define DTMF_RELATIVE_PEAK_ROW 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_RELATIVE_PEAK_COL 6.309f /* 8dB [10.0^(8.0/10.0)] */
#define DTMF_TO_TOTAL_ENERGY 83.868f /* -0.85dB [DTMF_SAMPLES_PER_BLOCK*10^(-0.85/10.0)] */
#define DTMF_POWER_OFFSET 110.395f /* 10*log((32768.0^2)*DTMF_SAMPLES_PER_BLOCK) */
#define DTMF_SAMPLES_PER_BLOCK 102
#define FP_SCALE(x) (x)
static const float dtmf_threshold = goertzel_threshold_dbm0(DTMF_SAMPLES_PER_BLOCK, -42.0f);
static const float dtmf_normal_twist = db_to_power_ratio(8.0f);
static const float dtmf_reverse_twist = db_to_power_ratio(4.0f);
static const float dtmf_relative_peak_row = db_to_power_ratio(8.0f);
static const float dtmf_relative_peak_col = db_to_power_ratio(8.0f);
static const float dtmf_to_total_energy = DTMF_SAMPLES_PER_BLOCK*db_to_power_ratio(-0.85f);
static const float dtmf_power_offset = (power_ratio_to_db(32768.0f*32768.0f*DTMF_SAMPLES_PER_BLOCK) - DBM0_MAX_SINE_POWER);
#endif

static const float dtmf_row[] =
Expand Down Expand Up @@ -217,9 +217,9 @@ SPAN_DECLARE(int) dtmf_rx(dtmf_rx_state_t *s, const int16_t amp[], int samples)
/* Relative peak test ... */
for (i = 0; i < 4; i++)
{
if ((i != best_col && col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col])
if ((i != best_col && col_energy[i]*dtmf_relative_peak_col > col_energy[best_col])
||
(i != best_row && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row]))
(i != best_row && row_energy[i]*dtmf_relative_peak_row > row_energy[best_row]))
{
break;
}
Expand All @@ -229,27 +229,27 @@ SPAN_DECLARE(int) dtmf_rx(dtmf_rx_state_t *s, const int16_t amp[], int samples)
/* ... and fraction of total energy test */
if (i >= 4
&&
(row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy)
(row_energy[best_row] + col_energy[best_col]) > dtmf_to_total_energy*s->energy)
{
/* Got a hit */
hit = dtmf_positions[(best_row << 2) + best_col];
}
/*endif*/
}
/*endif*/
if (span_log_test(&s->logging, SPAN_LOG_FLOW))
if (span_log_test(&s->logging, SPAN_LOG_DEBUG))
{
/* Log information about the quality of the signal, to aid analysis of detection problems */
/* Logging at this point filters the total no-hoper frames out of the log, and leaves
anything which might feasibly be a DTMF digit. The log will then contain a list of the
total, row and coloumn power levels for detailed analysis of detection problems. */
span_log(&s->logging,
SPAN_LOG_FLOW,
SPAN_LOG_DEBUG,
"Potentially '%c' - total %.2fdB, row %.2fdB, col %.2fdB, duration %d - %s\n",
dtmf_positions[(best_row << 2) + best_col],
log10f(s->energy)*10.0f - DTMF_POWER_OFFSET + DBM0_MAX_POWER,
log10f(row_energy[best_row]/DTMF_TO_TOTAL_ENERGY)*10.0f - DTMF_POWER_OFFSET + DBM0_MAX_POWER,
log10f(col_energy[best_col]/DTMF_TO_TOTAL_ENERGY)*10.0f - DTMF_POWER_OFFSET + DBM0_MAX_POWER,
power_ratio_to_db(s->energy) - dtmf_power_offset,
power_ratio_to_db(row_energy[best_row]/dtmf_to_total_energy) - dtmf_power_offset,
power_ratio_to_db(col_energy[best_col]/dtmf_to_total_energy) - dtmf_power_offset,
s->duration,
(hit) ? "hit" : "miss");
}
Expand Down Expand Up @@ -291,7 +291,7 @@ SPAN_DECLARE(int) dtmf_rx(dtmf_rx_state_t *s, const int16_t amp[], int samples)
/* Avoid reporting multiple no digit conditions on flaky hits */
if (s->in_digit || hit)
{
i = (s->in_digit && !hit) ? -99 : lfastrintf(log10f(s->energy)*10.0f - DTMF_POWER_OFFSET + DBM0_MAX_POWER);
i = (s->in_digit && !hit) ? -99 : lfastrintf(power_ratio_to_db(s->energy) - dtmf_power_offset);
s->realtime_callback(s->realtime_callback_data, hit, i, s->duration);
s->duration = 0;
}
Expand Down Expand Up @@ -404,8 +404,6 @@ SPAN_DECLARE(void) dtmf_rx_parms(dtmf_rx_state_t *s,
float reverse_twist,
float threshold)
{
float x;

if (filter_dialtone >= 0)
{
s->z350[0] = 0.0f;
Expand All @@ -415,21 +413,14 @@ SPAN_DECLARE(void) dtmf_rx_parms(dtmf_rx_state_t *s,
s->filter_dialtone = filter_dialtone;
}
/*endif*/
if (twist >= 0)
s->normal_twist = powf(10.0f, twist/10.0f);
if (twist >= 0.0f)
s->normal_twist = db_to_power_ratio(twist);
/*endif*/
if (reverse_twist >= 0)
s->reverse_twist = powf(10.0f, reverse_twist/10.0f);
if (reverse_twist >= 0.0f)
s->reverse_twist = db_to_power_ratio(reverse_twist);
/*endif*/
if (threshold > -99)
{
#if defined(SPANDSP_USE_FIXED_POINT)
x = (DTMF_SAMPLES_PER_BLOCK*32768.0f/(128.0f*1.4142f))*powf(10.0f, (threshold - DBM0_MAX_SINE_POWER)/20.0f);
#else
x = (DTMF_SAMPLES_PER_BLOCK*32768.0f/1.4142f)*powf(10.0f, (threshold - DBM0_MAX_SINE_POWER)/20.0f);
#endif
s->threshold = x*x;
}
if (threshold > -99.0f)
s->threshold = goertzel_threshold_dbm0(DTMF_SAMPLES_PER_BLOCK, threshold);
/*endif*/
}
/*- End of function --------------------------------------------------------*/
Expand Down Expand Up @@ -461,9 +452,9 @@ SPAN_DECLARE(dtmf_rx_state_t *) dtmf_rx_init(dtmf_rx_state_t *s,
s->realtime_callback = NULL;
s->realtime_callback_data = NULL;
s->filter_dialtone = false;
s->normal_twist = DTMF_NORMAL_TWIST;
s->reverse_twist = DTMF_REVERSE_TWIST;
s->threshold = DTMF_THRESHOLD;
s->normal_twist = dtmf_normal_twist;
s->reverse_twist = dtmf_reverse_twist;
s->threshold = dtmf_threshold;

s->in_digit = 0;
s->last_hit = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/fax.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static void fax_set_rx_type(void *user_data, int type, int bit_rate, int short_t
{
span_log(&s->logging, SPAN_LOG_FLOW, "Set fallback rx type %d%s\n", type, use_hdlc ? " (HDLC)" : "");
fax_modems_set_rx_handler(t, (span_rx_handler_t) &sslfax_rx, &s->t30.sslfax, (span_rx_fillin_handler_t) NULL, &s->t30.sslfax);
sslfax_setup(&s->t30.sslfax, &s->t30, t30_non_ecm_put, t30_non_ecm_get, t30_hdlc_accept, hdlc_underflow_handler, s->t30.sslfax.tx_use_hdlc, use_hdlc, fax_get_phase);
sslfax_setup(&s->t30.sslfax, t30_non_ecm_put, t30_non_ecm_get, t30_hdlc_accept, hdlc_underflow_handler, s->t30.sslfax.tx_use_hdlc, use_hdlc, fax_get_phase, &s->t30);
t->rx_bit_rate = bit_rate;
t->current_rx_type = type;
if (use_hdlc)
Expand Down Expand Up @@ -334,7 +334,7 @@ static void fax_set_tx_type(void *user_data, int type, int bit_rate, int short_t
span_log(&s->logging, SPAN_LOG_FLOW, "Set fallback tx type %d%s\n", type, use_hdlc ? " (HDLC)" : "");
fax_modems_set_tx_handler(t, (span_tx_handler_t) &sslfax_tx, &s->t30.sslfax);
fax_modems_set_next_tx_handler(t, (span_tx_handler_t) &sslfax_tx, &s->t30.sslfax);
sslfax_setup(&s->t30.sslfax, &s->t30, t30_non_ecm_put, t30_non_ecm_get, t30_hdlc_accept, hdlc_underflow_handler, use_hdlc, s->t30.sslfax.rx_use_hdlc, fax_get_phase);
sslfax_setup(&s->t30.sslfax, t30_non_ecm_put, t30_non_ecm_get, t30_hdlc_accept, hdlc_underflow_handler, use_hdlc, s->t30.sslfax.rx_use_hdlc, fax_get_phase, &s->t30);
t->transmit = true;
t->tx_bit_rate = bit_rate;
t->current_tx_type = type;
Expand Down
Loading

9 comments on commit 5394b2c

@kraj
Copy link

@kraj kraj commented on 5394b2c Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replacing defines with static const caused build failures when using clang compiler. see https://snips.sh/f/qCR86Rwwz1

@kraj
Copy link

@kraj kraj commented on 5394b2c Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making all in src
make[1]: Entering directory '/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux-musl/spandsp/3.0.0+git/git/src'
make  all-am
make[2]: Entering directory '/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux-musl/spandsp/3.0.0+git/git/src'
/bin/sh ../libtool  --tag=CC   --mode=compile clang -DHAVE_CONFIG_H -I.  -I.. -I/usr/include/libxml2 -DNDEBUG -Wunused-but-set-variable -D_XOPEN_SOURCE=700 -std=c99 -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes   -MT ademco_contactid.lo -MD -MP -MF .deps/ademco_contactid.Tpo -c -o ademco_contactid.lo ademco_contactid.c
libtool: compile:  clang -DHAVE_CONFIG_H -I. -I.. -I/usr/include/libxml2 -DNDEBUG -Wunused-but-set-variable -D_XOPEN_SOURCE=700 -std=c99 -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -MT ademco_contactid.lo -MD -MP -MF .deps/ademco_contactid.Tpo -c ademco_contactid.c  -fPIC -DPIC -o .libs/ademco_contactid.o
ademco_contactid.c:452:51: error: initializer element is not a compile-time constant
  452 | static const float detection_threshold          = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
      |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./spandsp/tone_detect.h:66:49: note: expanded from macro 'goertzel_threshold_dbm0'
   66 | #define goertzel_threshold_dbm0(len,thresh)     (float) ((len*len*32768.0*32768.0/2.0)*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
      |                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ademco_contactid.c:453:77: error: initializer element is not a compile-time constant
  453 | static const float tone_to_total_energy         = GOERTZEL_SAMPLES_PER_BLOCK*db_to_power_ratio(-0.85f);
      |                                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
make[2]: *** [Makefile:1089: ademco_contactid.lo] Error 1
make[2]: Leaving directory '/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux-musl/spandsp/3.0.0+git/git/src'
make[1]: *** [Makefile:870: all] Error 2
make[1]: Leaving directory '/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux-musl/spandsp/3.0.0+git/git/src'
make: *** [Makefile:511: all-recursive] Error 1

@shr-project
Copy link

@shr-project shr-project commented on 5394b2c Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only with clang, gcc fails similarly`(#67 also reports failure with gcc)

libtool: compile:  ccache arm-oe-linux-gnueabi-gcc -mthumb -mfpu=neon -mfloat-abi=softfp -mcpu=cortex-a9 -mtune=cortex-a9 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror=return-type -funwind-tables -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64 --sysroot=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/lib32-recipe-sysroot -DHAVE_CONFIG_H -I. -I../../git/src -I.. -DNDEBUG -Wunused-but-set-variable -std=gnu99 -ffast-math -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -fvisibility=hidden -DHAVE_VISIBILITY=1 -O2 -pipe -g -feliminate-unused-debug-types -fcanon-prefix-map -fmacro-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/git=/usr/src/debug/lib32-spandsp/3.0.0+git -fdebug-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/git=/usr/src/debug/lib32-spandsp/3.0.0+git -fmacro-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/build=/usr/src/debug/lib32-spandsp/3.0.0+git -fdebug-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/build=/usr/src/debug/lib32-spandsp/3.0.0+git -fdebug-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/lib32-recipe-sysroot= -fmacro-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/lib32-recipe-sysroot= -fdebug-prefix-map=TOPDIR/BUILD/work/mach-oe-linux-gnueabi/lib32-spandsp/3.0.0+git/recipe-sysroot-native= -c ../../git/src/t38_terminal.c  -fPIC -DPIC -o .libs/t38_terminal.o
../../git/src/ademco_contactid.c:449:110: error: expected ')' before ';' token
  449 | static const int detection_threshold            = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
      |                                                                                                              ^
In file included from ../../git/src/ademco_contactid.c:66:
../../git/src/spandsp/tone_detect.h:63:55: note: to match this '('
   63 | #define goertzel_threshold_dbm0(len,thresh)     (int) ((len*len*256.0*256.0/2.0*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
      |                                                       ^
../../git/src/ademco_contactid.c:449:51: note: in expansion of macro 'goertzel_threshold_dbm0'
  449 | static const int detection_threshold            = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
      |                                                   ^~~~~~~~~~~~~~~~~~~~~~~
../../git/src/ademco_contactid.c:449:18: warning: 'detection_threshold' defined but not used [-Wunused-const-variable=]
  449 | static const int detection_threshold            = goertzel_threshold_dbm0(GOERTZEL_SAMPLES_PER_BLOCK, -42.0f);
      |                  ^~~~~~~~~~~~~~~~~~~
../../git/src/ademco_contactid.c:210:35: warning: 'ademco_codes' defined but not used [-Wunused-const-variable=]
  210 | static const struct ademco_code_s ademco_codes[] =
      |                                   ^~~~~~~~~~~~
make[2]: *** [Makefile:1091: ademco_contactid.lo] Error 1
``

@coppice-git
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shr-project: Which version of GCC gave those errors? Things build and run correctly with the versions I have tried.

I have made various changes so the code now seems to build and run OK for the first time when compiled with clang. I removed my recent additions of initializers using intrinsic functions, which don't seem to work with a number of compilers. Hopefully Windows and clang builds now work as well as GCC ones.

@shr-project
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coppice-git this was with gcc-13.2

@coppice-git
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shr-project Interesting. That's one of the versions I have been using. I wonder if its related to you compiling for an ARM?

@shr-project
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if I build it for aarch64 then it builds fine.

If I check -E output I see this in arm build:

static const float dtmf_threshold = (int) ((102*102*256.0*256.0/2.0*                                                                                                                                                                          
# 76 "../../git/src/dtmf.c" 3 4                                                                                                                                                                                                               
                                                 __builtin_tgmath (powf, pow, powl, cpowf, cpow, cpowl, ((                                                                                                                                    
# 76 "../../git/src/dtmf.c"                                                                                                                                                                                                                   
                                                 10.0                                                                                                                                                                                         
# 76 "../../git/src/dtmf.c" 3 4                                                                                                                                                                                                               
                                                 )), ((                                                                                                                                                                                       
# 76 "../../git/src/dtmf.c"                                                                                                                                                                                                                   
                                                 (-42.0f - (3.14f))/10.0                                                                                                                                                                      
# 76 "../../git/src/dtmf.c" 3 4                                                                                                                                                                                                               
                                                 )))                                                                                                                                                                                          
# 76 "../../git/src/dtmf.c"                                                                                                                                                                                                                   
                                                 );

while aarch64 build has:

static const float dtmf_threshold = (float) ((102*102*32768.0*32768.0/2.0)*
# 85 "../../git/src/dtmf.c" 3 4
                                                 __builtin_tgmath (powf, pow, powl, cpowf, cpow, cpowl, ((
# 85 "../../git/src/dtmf.c"
                                                 10.0
# 85 "../../git/src/dtmf.c" 3 4
                                                 )), ((
# 85 "../../git/src/dtmf.c"
                                                 (-42.0f - (3.14f))/10.0
# 85 "../../git/src/dtmf.c" 3 4
                                                 )))
# 85 "../../git/src/dtmf.c"
                                                 );

See the missing ')' on first line, that's from the energy_threshold_dbm0 macro when SPANDSP_USE_FIXED_POINT is used, will send PR to fix this shortly.

@shr-project
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#75

@coppice-git
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, GNU C allows intrinsic functions (e.g. powf) in static initializers. Clang and MS don't seem to. I thought that was added to the C standard at some point. Maybe not. I've now separated out alternate definitions with #ifs.

Please sign in to comment.