From 28a297f97d53174c2d780e3f6ef6457ed336adeb Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sat, 18 Mar 2023 19:49:56 +0100 Subject: [PATCH] Generate compador table only once when the application starts --- src/amps/dsp.c | 4 +++- src/cnetz/dsp.c | 3 ++- src/libcompandor/compandor.c | 22 +++++++++++++++++----- src/libcompandor/compandor.h | 3 ++- src/libdisplay/display_measurements.c | 3 ++- src/nmt/dsp.c | 4 +++- src/r2000/dsp.c | 3 ++- src/test/test_compandor.c | 3 ++- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/amps/dsp.c b/src/amps/dsp.c index a18abee..8b6f7e6 100644 --- a/src/amps/dsp.c +++ b/src/amps/dsp.c @@ -162,6 +162,8 @@ void dsp_init(void) } dsp_sync_check[0x712] = 0x00; /* no bit error */ dsp_sync_check[0x0ed] = 0x80; /* no bit error */ + + compandor_init(); } static void dsp_init_ramp(amps_t *amps) @@ -194,7 +196,7 @@ int dsp_init_sender(amps_t *amps, int tolerant) int half; /* attack (3ms) and recovery time (13.5ms) according to amps specs */ - init_compandor(&s->cstate, 8000, 3.0, 13.5); + setup_compandor(&s->cstate, 8000, 3.0, 13.5); PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for transceiver.\n"); diff --git a/src/cnetz/dsp.c b/src/cnetz/dsp.c index c5658e5..4d5e518 100644 --- a/src/cnetz/dsp.c +++ b/src/cnetz/dsp.c @@ -76,6 +76,7 @@ const char *cnetz_dsp_mode_name(enum dsp_mode mode) void dsp_init(void) { + compandor_init(); } static void dsp_init_ramp(cnetz_t *cnetz) @@ -160,7 +161,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], en /* init compandor, according to C-Netz specs, attack and recovery time * shall not exceed according to ITU G.162 */ - init_compandor(&cnetz->cstate, 8000, 5.0, 22.5); + setup_compandor(&cnetz->cstate, 8000, 5.0, 22.5); /* use duration of one bit to ramp level of last frame to current frame */ cnetz->offset_range = ceil(cnetz->fsk_bitduration); diff --git a/src/libcompandor/compandor.c b/src/libcompandor/compandor.c index b06602e..1b0797c 100644 --- a/src/libcompandor/compandor.c +++ b/src/libcompandor/compandor.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include "../libsample/sample.h" @@ -39,6 +40,7 @@ #define ENVELOPE_MAX 9.990 static double sqrt_tab[10000]; +static int compandor_initalized = 0; /* * Init compandor according to ITU-T G.162 specification @@ -46,10 +48,24 @@ static double sqrt_tab[10000]; * Hopefully this is correct * */ -void init_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms) + +void compandor_init(void) { int i; + // FIXME: make global, not at instance + for (i = 0; i < 10000; i++) + sqrt_tab[i] = sqrt(i * 0.001); + compandor_initalized = 1; +} + +void setup_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms) +{ + if (!compandor_initalized) { + fprintf(stderr, "Compandor nicht initialized.\n"); + abort(); + } + memset(state, 0, sizeof(*state)); state->c.peak = 1.0; @@ -60,10 +76,6 @@ void init_compandor(compandor_t *state, double samplerate, double attack_ms, dou state->c.step_down = pow(COMPRESS_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate); state->e.step_up = pow(EXPAND_ATTACK_FACTOR, 1000.0 / attack_ms / samplerate); state->e.step_down = pow(EXPAND_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate); - - // FIXME: make global, not at instance - for (i = 0; i < 10000; i++) - sqrt_tab[i] = sqrt(i * 0.001); } void compress_audio(compandor_t *state, sample_t *samples, int num) diff --git a/src/libcompandor/compandor.h b/src/libcompandor/compandor.h index e912609..df8e020 100644 --- a/src/libcompandor/compandor.h +++ b/src/libcompandor/compandor.h @@ -14,7 +14,8 @@ typedef struct compandor { } e; } compandor_t; -void init_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms); +void compandor_init(void); +void setup_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms); void compress_audio(compandor_t *state, sample_t *samples, int num); void expand_audio(compandor_t *state, sample_t *samples, int num); diff --git a/src/libdisplay/display_measurements.c b/src/libdisplay/display_measurements.c index e2c2f0f..543ca1c 100644 --- a/src/libdisplay/display_measurements.c +++ b/src/libdisplay/display_measurements.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "../libsample/sample.h" #include "../libdebug/debug.h" #include "../libdisplay/display.h" @@ -246,7 +247,7 @@ static void print_measurements(int on) memset(line_color + width - MAX_UNIT_LEN, 4, MAX_UNIT_LEN); /* blue */ else memset(line_color + width - MAX_UNIT_LEN, 3, MAX_UNIT_LEN); /* yellow */ - strncpy(line + width - MAX_UNIT_LEN + 1, text, (strlen(text) < MAX_UNIT_LEN) ? strlen(text) : MAX_UNIT_LEN); + memcpy(line + width - MAX_UNIT_LEN + 1, text, MAX(strlen(text), MAX_UNIT_LEN)); display_line(on, width); } } diff --git a/src/nmt/dsp.c b/src/nmt/dsp.c index e63ae45..d51bc69 100644 --- a/src/nmt/dsp.c +++ b/src/nmt/dsp.c @@ -97,6 +97,8 @@ void dsp_init(void) /* dialtone sine */ dsp_sine_dialtone[i] = s * TX_PEAK_DIALTONE; } + + compandor_init(); } static int fsk_send_bit(void *inst); @@ -109,7 +111,7 @@ int dsp_init_sender(nmt_t *nmt, double deviation_factor) int i; /* attack (3ms) and recovery time (13.5ms) according to NMT specs */ - init_compandor(&nmt->cstate, 8000, 3.0, 13.5); + setup_compandor(&nmt->cstate, 8000, 3.0, 13.5); PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for Transceiver.\n"); diff --git a/src/r2000/dsp.c b/src/r2000/dsp.c index bb3b97c..6254ca9 100644 --- a/src/r2000/dsp.c +++ b/src/r2000/dsp.c @@ -64,6 +64,7 @@ /* global init for FSK */ void dsp_init(void) { + compandor_init(); } static int fsk_send_bit(void *inst); @@ -75,7 +76,7 @@ static void super_receive_bit(void *inst, int bit, double quality, double level) int dsp_init_sender(r2000_t *r2000) { /* attack (3ms) and recovery time (13.5ms) according to NMT specs */ - init_compandor(&r2000->cstate, 8000, 3.0, 13.5); + setup_compandor(&r2000->cstate, 8000, 3.0, 13.5); PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for Transceiver.\n"); diff --git a/src/test/test_compandor.c b/src/test/test_compandor.c index b1cd51e..f91e0b4 100644 --- a/src/test/test_compandor.c +++ b/src/test/test_compandor.c @@ -72,7 +72,8 @@ int main(void) sample_t samples[SAMPLERATE * 2]; int f; - init_compandor(&cstate, SAMPLERATE, ATTACK_MS, RECOVERY_MS); + compandor_init(); + setup_compandor(&cstate, SAMPLERATE, ATTACK_MS, RECOVERY_MS); for (f = 0; f < 3; f++) { /* -16 and -4 dB */