Refactor global variables for signal processing

These are:
	device, sample rate, buffer, latency
Called now:
	dsp_device, dsp_samplerate, dsp_buffer, dsp_latency
Call audio device:
	call_device, call_samplerate, call_buffer
This commit is contained in:
Andreas Eversberg
2021-09-18 11:43:01 +02:00
parent de685b3cb6
commit 6fa74a1296
57 changed files with 379 additions and 368 deletions

View File

@@ -35,10 +35,10 @@
/* presets */
const char *station_id = "6681739";
const char *dialing;
const char *audiodev = "hw:0,0";
int samplerate = 48000;
const char *dsp_audiodev = "hw:0,0";
int dsp_samplerate = 48000;
const char *write_tx_wave = NULL;
int latency = 50;
int dsp_buffer = 50;
#define TONE_DONE -1
#define TONE_SILENCE 0
@@ -53,7 +53,6 @@ static struct dial_string {
int length;
} dial_string[2048];
static int dial_pos = 0;
static int latspl;
/* instances */
#ifdef HAVE_ALSA
@@ -82,10 +81,10 @@ static void print_help(const char *arg0)
printf(" 7 Digits of ID of mobile station (default = '%s')\n", station_id);
#ifdef HAVE_ALSA
printf(" -a --audio-device hw:<card>,<device>\n");
printf(" Sound card and device number (default = '%s')\n", audiodev);
printf(" Sound card and device number (default = '%s')\n", dsp_audiodev);
#endif
printf(" -s --samplerate <rate>\n");
printf(" Sample rate of sound device (default = '%d')\n", samplerate);
printf(" Sample rate of sound device (default = '%d')\n", dsp_samplerate);
printf(" -w --write-tx-wave <file>\n");
printf(" Write audio to given wave file also.\n");
}
@@ -109,10 +108,10 @@ static int handle_options(int short_option, int __attribute__((unused)) argi, ch
station_id = options_strdup(argv[argi]);
break;
case 'a':
audiodev = options_strdup(argv[argi]);
dsp_audiodev = options_strdup(argv[argi]);
break;
case 's':
samplerate = atoi(argv[argi]);
dsp_samplerate = atoi(argv[argi]);
break;
case 'w':
write_tx_wave = options_strdup(argv[argi]);
@@ -148,7 +147,7 @@ again:
default:
for (i = 0; i < count; i++) {
samples[i] = cos(2.0 * M_PI * (double)dial_string[dial_pos].tone * phase);
phase += 1.0 / samplerate;
phase += 1.0 / dsp_samplerate;
}
}
@@ -173,18 +172,18 @@ again:
/* loop that gets audio from encoder and forwards it to sound card.
* alternatively a sound file is written.
*/
static void process_signal(void)
static void process_signal(int buffer_size)
{
sample_t buff[latspl], *samples[1] = { buff };
uint8_t pbuff[latspl], *power[1] = { pbuff };
sample_t buff[buffer_size], *samples[1] = { buff };
uint8_t pbuff[buffer_size], *power[1] = { pbuff };
int count;
int __attribute__((unused)) rc;
while (dial_string[dial_pos].tone != TONE_DONE) {
#ifdef HAVE_ALSA
count = sound_get_tosend(audio, latspl);
count = sound_get_tosend(audio, buffer_size);
#else
count = samplerate / 1000;
count = dsp_samplerate / 1000;
#endif
if (count < 0) {
PDEBUG(DDSP, DEBUG_ERROR, "Failed to get number of samples in buffer (rc = %d)!\n", count);
@@ -215,12 +214,13 @@ static void process_signal(void)
int main(int argc, char *argv[])
{
int i, d, p, pulses, tone = 0;
int buffer_size;
int rc, argi;
memset(dial_string, 0, sizeof(dial_string));
/* latency of send buffer in samples */
latspl = samplerate * latency / 1000;
/* size of send buffer in samples */
buffer_size = dsp_samplerate * dsp_buffer / 1000;
/* handle options / config file */
add_options();
@@ -248,7 +248,7 @@ int main(int argc, char *argv[])
dialing = argv[argi];
d = 0;
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 0.600 * (double)samplerate; /* pause */
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 0.600 * (double)dsp_samplerate; /* pause */
if (!!strcasecmp(dialing, "disconnect")) {
/* check for valid phone number */
if (strlen(dialing) > 64) {
@@ -262,11 +262,11 @@ int main(int argc, char *argv[])
}
}
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.350 * (double)samplerate; /* off-hook */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.350 * (double)dsp_samplerate; /* off-hook */
dial_string[d].console = 's';
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.050 * (double)samplerate; /* seize */
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.050 * (double)dsp_samplerate; /* seize */
dial_string[d].console = '-';
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 1.000 * (double)samplerate; /* pause */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 1.000 * (double)dsp_samplerate; /* pause */
for (i = 0; station_id[i]; i++) {
pulses = station_id[i] - '0';
if (pulses == 0)
@@ -277,37 +277,37 @@ int main(int argc, char *argv[])
tone = TONE_SILENCE;
else
tone = TONE_GUARD;
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.025 * (double)samplerate; /* mark */
dial_string[d].tone = tone; dial_string[d++].length = 0.025 * (double)samplerate; /* space */
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.025 * (double)dsp_samplerate; /* mark */
dial_string[d].tone = tone; dial_string[d++].length = 0.025 * (double)dsp_samplerate; /* space */
}
dial_string[d].tone = tone; dial_string[d++].length = 0.190 * (double)samplerate; /* after digit */
dial_string[d].tone = tone; dial_string[d++].length = 0.190 * (double)dsp_samplerate; /* after digit */
}
dial_string[d].console = '-';
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 2.000 * (double)samplerate; /* pause */
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 2.000 * (double)dsp_samplerate; /* pause */
for (i = 0; dialing[i]; i++) {
pulses = dialing[i] - '0';
if (pulses == 0)
pulses = 10;
dial_string[d].console = dialing[i];
for (p = 1; p <= pulses; p++) {
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.060 * (double)samplerate; /* mark */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.040 * (double)samplerate; /* space */
dial_string[d].tone = TONE_CONNECT; dial_string[d++].length = 0.060 * (double)dsp_samplerate; /* mark */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.040 * (double)dsp_samplerate; /* space */
}
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.400 * (double)samplerate; /* after digit */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.400 * (double)dsp_samplerate; /* after digit */
}
dial_string[d].console = '\n';
} else {
for (i = 0; i < 750; i += 50) {
dial_string[d].tone = TONE_DISCONNECT; dial_string[d++].length = 0.025 * (double)samplerate; /* mark */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.025 * (double)samplerate; /* space */
dial_string[d].tone = TONE_DISCONNECT; dial_string[d++].length = 0.025 * (double)dsp_samplerate; /* mark */
dial_string[d].tone = TONE_GUARD; dial_string[d++].length = 0.025 * (double)dsp_samplerate; /* space */
}
}
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 0.600 * (double)samplerate; /* pause */
dial_string[d].tone = TONE_SILENCE; dial_string[d++].length = 0.600 * (double)dsp_samplerate; /* pause */
dial_string[d].tone = TONE_DONE; dial_string[d++].length = 0; /* end */
#ifdef HAVE_ALSA
/* init sound */
audio = sound_open(audiodev, NULL, NULL, NULL, 1, 0.0, samplerate, latspl, 1.0, 4000.0, 2.0);
audio = sound_open(dsp_audiodev, NULL, NULL, NULL, 1, 0.0, dsp_samplerate, dsp_buffer, 1.0, 1.0, 4000.0, 2.0);
if (!audio) {
PDEBUG(DBNETZ, DEBUG_ERROR, "No sound device!\n");
goto exit;
@@ -316,7 +316,7 @@ int main(int argc, char *argv[])
/* open wave */
if (write_tx_wave) {
rc = wave_create_record(&wave_tx_rec, write_tx_wave, samplerate, 1, 1.0);
rc = wave_create_record(&wave_tx_rec, write_tx_wave, dsp_samplerate, 1, 1.0);
if (rc < 0) {
PDEBUG(DBNETZ, DEBUG_ERROR, "Failed to create WAVE recoding instance!\n");
goto exit;
@@ -336,7 +336,7 @@ int main(int argc, char *argv[])
PDEBUG(DBNETZ, DEBUG_ERROR, "Start audio after pause...\n");
process_signal();
process_signal(buffer_size);
exit:
/* close wave */

View File

@@ -274,7 +274,7 @@ static void imts_paging(imts_t *imts, const char *dial_string, int loopback);
static void imts_detector_test(imts_t *imts, double length_1, double length_2, double length_3);
/* Create transceiver instance and link to a list. */
int imts_create(const char *kanal, const char *audiodev, int use_sdr, int samplerate, double rx_gain, double tx_gain, int pre_emphasis, int de_emphasis, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback, double squelch_db, int ptt, int station_length, double fast_seize, enum mode mode, const char *operator, double length_1, double length_2, double length_3)
int imts_create(const char *kanal, const char *device, int use_sdr, int samplerate, double rx_gain, double tx_gain, int pre_emphasis, int de_emphasis, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback, double squelch_db, int ptt, int station_length, double fast_seize, enum mode mode, const char *operator, double length_1, double length_2, double length_3)
{
imts_t *imts;
int rc;
@@ -317,7 +317,7 @@ int imts_create(const char *kanal, const char *audiodev, int use_sdr, int sample
/* init general part of transceiver */
/* do not enable emphasis, since it is done by imts code, not by common sender code */
rc = sender_create(&imts->sender, kanal, imts_channel2freq(kanal, 0), imts_channel2freq(kanal, 1), audiodev, use_sdr, samplerate, rx_gain, tx_gain, 0, 0, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback, PAGING_SIGNAL_NONE);
rc = sender_create(&imts->sender, kanal, imts_channel2freq(kanal, 0), imts_channel2freq(kanal, 1), device, use_sdr, samplerate, rx_gain, tx_gain, 0, 0, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback, PAGING_SIGNAL_NONE);
if (rc < 0) {
PDEBUG(DIMTS, DEBUG_ERROR, "Failed to init 'Sender' processing!\n");
goto error;

View File

@@ -129,7 +129,7 @@ typedef struct imts {
void imts_list_channels(void);
double imts_channel2freq(const char *kanal, int uplink);
int imts_init(void);
int imts_create(const char *channel, const char *audiodev, int use_sdr, int samplerate, double rx_gain, double tx_gain, int pre_emphasis, int de_emphasis, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback, double squelch_db, int ptt, int station_length, double fast_seize, enum mode mode, const char *operator, double length_1, double length_2, double length_3);
int imts_create(const char *channel, const char *device, int use_sdr, int samplerate, double rx_gain, double tx_gain, int pre_emphasis, int de_emphasis, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback, double squelch_db, int ptt, int station_length, double fast_seize, enum mode mode, const char *operator, double length_1, double length_2, double length_3);
void imts_destroy(sender_t *sender);
void imts_loss_indication(imts_t *imts, double loss_time);
void imts_signal_indication(imts_t *imts);

View File

@@ -50,7 +50,7 @@ static double detector_test_length_3 = 0.0;
void print_help(const char *arg0)
{
main_mobile_print_help(arg0, "-b 5 ");
main_mobile_print_help(arg0, "-b 5 -i 0.1 ");
/* - - */
printf(" -S --squelch <dB> | auto\n");
printf(" Use given RF level to detect loss of signal. When the signal gets lost\n");
@@ -199,14 +199,14 @@ int main(int argc, char *argv[])
goto fail;
}
if (use_sdr) {
/* set audiodev */
/* set device */
for (i = 0; i < num_kanal; i++)
audiodev[i] = "sdr";
num_audiodev = num_kanal;
dsp_device[i] = "sdr";
num_device = num_kanal;
}
if (num_kanal == 1 && num_audiodev == 0)
num_audiodev = 1; /* use default */
if (num_kanal != num_audiodev) {
if (num_kanal == 1 && num_device == 0)
num_device = 1; /* use default */
if (num_kanal != num_device) {
fprintf(stderr, "You need to specify as many sound devices as you have channels.\n");
exit(0);
}
@@ -226,10 +226,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "*******************************************************************************\n");
}
if (mode == MODE_IMTS && !fast_seize && latency > 5 && loopback == 0) {
if (mode == MODE_IMTS && !fast_seize && dsp_buffer > 5 && loopback == 0) {
fprintf(stderr, "*******************************************************************************\n");
fprintf(stderr, "It is required to have a low latency in order to respond to phone's seizure\n");
fprintf(stderr, "fast enough! Please reduce buffer size to 5 ms via option: '-b 5'\n");
fprintf(stderr, "fast enough! Please reduce buffer size to 5 ms via option: '-b 5 -i 0.1'\n");
fprintf(stderr, "If this causes buffer underruns, use the 'Fast Seize' mode, see help.\n");
fprintf(stderr, "*******************************************************************************\n");
exit(0);
@@ -266,7 +266,7 @@ int main(int argc, char *argv[])
/* create transceiver instance */
for (i = 0; i < num_kanal; i++) {
rc = imts_create(kanal[i], audiodev[i], use_sdr, samplerate, rx_gain, tx_gain, do_pre_emphasis, do_de_emphasis, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback, squelch_db, ptt, station_length, fast_seize, mode, operator, detector_test_length_1, detector_test_length_2, detector_test_length_3);
rc = imts_create(kanal[i], dsp_device[i], use_sdr, dsp_samplerate, rx_gain, tx_gain, do_pre_emphasis, do_de_emphasis, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback, squelch_db, ptt, station_length, fast_seize, mode, operator, detector_test_length_1, detector_test_length_2, detector_test_length_3);
if (rc < 0) {
fprintf(stderr, "Failed to create \"Sender\" instance. Quitting!\n");
goto fail;
@@ -274,7 +274,7 @@ int main(int argc, char *argv[])
printf("Base station on channel %s ready, please tune transmitter to %.3f MHz and receiver to %.3f MHz. (%.3f MHz offset)\n", kanal[i], imts_channel2freq(kanal[i], 0) / 1e6, imts_channel2freq(kanal[i], 1) / 1e6, imts_channel2freq(kanal[i], 2) / 1e6);
}
main_mobile((mode == MODE_IMTS) ? "imts" : "mts", &quit, latency, interval, NULL, station_id, station_length);
main_mobile((mode == MODE_IMTS) ? "imts" : "mts", &quit, NULL, station_id, station_length);
fail:
/* destroy transceiver instance */