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:
@@ -93,7 +93,8 @@ typedef struct sdr {
|
||||
int channels; /* number of frequencies */
|
||||
double amplitude; /* amplitude of each carrier */
|
||||
int samplerate; /* sample rate of audio data */
|
||||
int latspl; /* latency in audio samples */
|
||||
int buffer_size; /* buffer in audio samples */
|
||||
double interval; /* how often to process the loop */
|
||||
wave_rec_t wave_rx_rec;
|
||||
wave_rec_t wave_tx_rec;
|
||||
wave_play_t wave_rx_play;
|
||||
@@ -136,7 +137,7 @@ static void show_spectrum(const char *direction, double halfbandwidth, double ce
|
||||
PDEBUG(DSDR, DEBUG_INFO, "Frequency P = %.4f MHz (Paging Frequency)\n", paging_frequency / 1e6);
|
||||
}
|
||||
|
||||
void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_frequency, double *rx_frequency, int *am, int channels, double paging_frequency, int samplerate, int latspl, double max_deviation, double max_modulation, double modulation_index)
|
||||
void *sdr_open(const char __attribute__((__unused__)) *device, double *tx_frequency, double *rx_frequency, int *am, int channels, double paging_frequency, int samplerate, int buffer_size, double interval, double max_deviation, double max_modulation, double modulation_index)
|
||||
{
|
||||
sdr_t *sdr;
|
||||
int threads = 1, oversample = 1; /* always use threads */
|
||||
@@ -179,13 +180,14 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq
|
||||
sdr->channels = channels;
|
||||
sdr->amplitude = 1.0 / (double)channels;
|
||||
sdr->samplerate = samplerate;
|
||||
sdr->latspl = latspl;
|
||||
sdr->buffer_size = buffer_size;
|
||||
sdr->interval = interval;
|
||||
sdr->threads = threads; /* always required, because write may block */
|
||||
sdr->oversample = oversample;
|
||||
|
||||
if (threads) {
|
||||
memset(&sdr->thread_read, 0, sizeof(sdr->thread_read));
|
||||
sdr->thread_read.buffer_size = sdr->latspl * 2 * sdr->oversample + 2;
|
||||
sdr->thread_read.buffer_size = sdr->buffer_size * 2 * sdr->oversample + 2;
|
||||
sdr->thread_read.buffer = calloc(sdr->thread_read.buffer_size, sizeof(*sdr->thread_read.buffer));
|
||||
if (!sdr->thread_read.buffer) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "No mem!\n");
|
||||
@@ -202,7 +204,7 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq
|
||||
iir_lowpass_init(&sdr->thread_read.lp[1], samplerate / 2.0, sdr_config->samplerate, 2);
|
||||
}
|
||||
memset(&sdr->thread_write, 0, sizeof(sdr->thread_write));
|
||||
sdr->thread_write.buffer_size = sdr->latspl * 2 + 2;
|
||||
sdr->thread_write.buffer_size = sdr->buffer_size * 2 + 2;
|
||||
sdr->thread_write.buffer = calloc(sdr->thread_write.buffer_size, sizeof(*sdr->thread_write.buffer));
|
||||
if (!sdr->thread_write.buffer) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "No mem!\n");
|
||||
@@ -221,32 +223,32 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq
|
||||
}
|
||||
|
||||
/* alloc fm modulation buffers */
|
||||
sdr->modbuff = calloc(sdr->latspl * 2, sizeof(*sdr->modbuff));
|
||||
sdr->modbuff = calloc(sdr->buffer_size * 2, sizeof(*sdr->modbuff));
|
||||
if (!sdr->modbuff) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
}
|
||||
sdr->modbuff_I = calloc(sdr->latspl, sizeof(*sdr->modbuff_I));
|
||||
sdr->modbuff_I = calloc(sdr->buffer_size, sizeof(*sdr->modbuff_I));
|
||||
if (!sdr->modbuff_I) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
}
|
||||
sdr->modbuff_Q = calloc(sdr->latspl, sizeof(*sdr->modbuff_Q));
|
||||
sdr->modbuff_Q = calloc(sdr->buffer_size, sizeof(*sdr->modbuff_Q));
|
||||
if (!sdr->modbuff_Q) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
}
|
||||
sdr->modbuff_carrier = calloc(sdr->latspl, sizeof(*sdr->modbuff_carrier));
|
||||
sdr->modbuff_carrier = calloc(sdr->buffer_size, sizeof(*sdr->modbuff_carrier));
|
||||
if (!sdr->modbuff_carrier) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
}
|
||||
sdr->wavespl0 = calloc(sdr->latspl, sizeof(*sdr->wavespl0));
|
||||
sdr->wavespl0 = calloc(sdr->buffer_size, sizeof(*sdr->wavespl0));
|
||||
if (!sdr->wavespl0) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
}
|
||||
sdr->wavespl1 = calloc(sdr->latspl, sizeof(*sdr->wavespl1));
|
||||
sdr->wavespl1 = calloc(sdr->buffer_size, sizeof(*sdr->wavespl1));
|
||||
if (!sdr->wavespl1) {
|
||||
PDEBUG(DSDR, DEBUG_ERROR, "NO MEM!\n");
|
||||
goto error;
|
||||
@@ -567,7 +569,7 @@ static void *sdr_write_child(void *arg)
|
||||
}
|
||||
|
||||
/* delay some time */
|
||||
usleep(1000);
|
||||
usleep(sdr->interval * 1000.0);
|
||||
}
|
||||
|
||||
PDEBUG(DSDR, DEBUG_DEBUG, "Thread received exit!\n");
|
||||
@@ -619,7 +621,7 @@ static void *sdr_read_child(void *arg)
|
||||
}
|
||||
|
||||
/* delay some time */
|
||||
usleep(1000);
|
||||
usleep(sdr->interval * 1000.0);
|
||||
}
|
||||
|
||||
PDEBUG(DSDR, DEBUG_DEBUG, "Thread received exit!\n");
|
||||
@@ -757,8 +759,8 @@ int sdr_write(void *inst, sample_t **samples, uint8_t **power, int num, enum pag
|
||||
int c, s, ss;
|
||||
int sent = 0;
|
||||
|
||||
if (num > sdr->latspl) {
|
||||
fprintf(stderr, "exceeding maximum size given by sdr_latspl, please fix!\n");
|
||||
if (num > sdr->buffer_size) {
|
||||
fprintf(stderr, "exceeding maximum size given by sdr->buffer_size, please fix!\n");
|
||||
abort();
|
||||
}
|
||||
if (channels != sdr->channels && channels != 0) {
|
||||
@@ -858,8 +860,8 @@ int sdr_read(void *inst, sample_t **samples, int num, int channels, double *rf_l
|
||||
int count = 0;
|
||||
int c, s, ss;
|
||||
|
||||
if (num > sdr->latspl) {
|
||||
fprintf(stderr, "exceeding maximum size given by sdr_latspl, please fix!\n");
|
||||
if (num > sdr->buffer_size) {
|
||||
fprintf(stderr, "exceeding maximum size given by sdr->buffer_size, please fix!\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
@@ -981,19 +983,19 @@ int sdr_read(void *inst, sample_t **samples, int num, int channels, double *rf_l
|
||||
return count;
|
||||
}
|
||||
|
||||
/* how much do we need to send (in audio sample duration) to get the target delay (latspl) */
|
||||
int sdr_get_tosend(void *inst, int latspl)
|
||||
/* how much do we need to send (in audio sample duration) to get the target delay (buffer size) */
|
||||
int sdr_get_tosend(void *inst, int buffer_size)
|
||||
{
|
||||
sdr_t *sdr = (sdr_t *)inst;
|
||||
int count = 0;
|
||||
|
||||
#ifdef HAVE_UHD
|
||||
if (sdr_config->uhd)
|
||||
count = uhd_get_tosend(latspl * sdr->oversample);
|
||||
count = uhd_get_tosend(buffer_size * sdr->oversample);
|
||||
#endif
|
||||
#ifdef HAVE_SOAPY
|
||||
if (sdr_config->soapy)
|
||||
count = soapy_get_tosend(latspl * sdr->oversample);
|
||||
count = soapy_get_tosend(buffer_size * sdr->oversample);
|
||||
#endif
|
||||
if (count < 0)
|
||||
return count;
|
||||
|
@@ -2,10 +2,10 @@
|
||||
enum paging_signal;
|
||||
|
||||
int sdr_start(void *inst);
|
||||
void *sdr_open(const char *audiodev, double *tx_frequency, double *rx_frequency, int *am, int channels, double paging_frequency, int samplerate, int latspl, double max_deviation, double max_modulation, double modulation_index);
|
||||
void *sdr_open(const char *audiodev, double *tx_frequency, double *rx_frequency, int *am, int channels, double paging_frequency, int samplerate, int buffer_size, double interval, double max_deviation, double max_modulation, double modulation_index);
|
||||
void sdr_close(void *inst);
|
||||
int sdr_write(void *inst, sample_t **samples, uint8_t **power, int num, enum paging_signal *paging_signal, int *on, int channels);
|
||||
int sdr_read(void *inst, sample_t **samples, int num, int channels, double *rf_level_db);
|
||||
int sdr_get_tosend(void *inst, int latspl);
|
||||
int sdr_get_tosend(void *inst, int buffer_size);
|
||||
void calibrate_bias(void);
|
||||
|
||||
|
@@ -27,9 +27,9 @@
|
||||
*
|
||||
* If a RX time stamp is valid and first chunk is to be transmitted (tosend()
|
||||
* is called), TX time stamp becomes valid and is set to RX time stamp, but
|
||||
* advanced by the duration of the latency (latspl). tosend() always returns
|
||||
* advanced by the duration of the buffer size. tosend() always returns
|
||||
* the number of samples that are needed, to make TX time stamp advance RX time
|
||||
* stamp by given latency.
|
||||
* stamp by given buffer size.
|
||||
*
|
||||
* If chunk is transmitted to SDR, the TX time stamp is advanced by the
|
||||
* duration of the transmitted chunk.
|
||||
@@ -557,7 +557,7 @@ int soapy_receive(float *buff, int max)
|
||||
}
|
||||
|
||||
/* estimate number of samples that can be sent */
|
||||
int soapy_get_tosend(int latspl)
|
||||
int soapy_get_tosend(int buffer_size)
|
||||
{
|
||||
int tosend;
|
||||
|
||||
@@ -567,23 +567,23 @@ int soapy_get_tosend(int latspl)
|
||||
|
||||
/* RX time stamp is valid the first time, set the TX time stamp in advance */
|
||||
if (!tx_valid) {
|
||||
tx_timeNs = rx_timeNs + latspl * Ns_per_sample;
|
||||
tx_timeNs = rx_timeNs + buffer_size * Ns_per_sample;
|
||||
tx_valid = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* we check how advance our transmitted time stamp is */
|
||||
pthread_mutex_lock(×tamp_mutex);
|
||||
tosend = latspl - (tx_timeNs - rx_timeNs) / Ns_per_sample;
|
||||
tosend = buffer_size - (tx_timeNs - rx_timeNs) / Ns_per_sample;
|
||||
pthread_mutex_unlock(×tamp_mutex);
|
||||
|
||||
/* in case of underrun */
|
||||
if (tosend > latspl) {
|
||||
if (tosend > buffer_size) {
|
||||
PDEBUG(DSOAPY, DEBUG_ERROR, "SDR TX underrun, seems we are too slow. Use lower SDR sample rate.\n");
|
||||
tosend = latspl;
|
||||
tosend = buffer_size;
|
||||
}
|
||||
|
||||
/* race condition and routing errors may cause TX time stamps to be in advance of slightly more than latspl */
|
||||
/* race condition and routing errors may cause TX time stamps to be in advance of slightly more than buffer_size */
|
||||
if (tosend < 0)
|
||||
tosend = 0;
|
||||
|
||||
|
@@ -4,5 +4,5 @@ int soapy_start(void);
|
||||
void soapy_close(void);
|
||||
int soapy_send(float *buff, int num);
|
||||
int soapy_receive(float *buff, int max);
|
||||
int soapy_get_tosend(int latspl);
|
||||
int soapy_get_tosend(int buffer_size);
|
||||
|
||||
|
@@ -631,7 +631,7 @@ int uhd_receive(float *buff, int max)
|
||||
}
|
||||
|
||||
/* estimate number of samples that can be sent */
|
||||
int uhd_get_tosend(int latspl)
|
||||
int uhd_get_tosend(int buffer_size)
|
||||
{
|
||||
double advance;
|
||||
int tosend;
|
||||
@@ -645,7 +645,7 @@ int uhd_get_tosend(int latspl)
|
||||
tx_time_secs = rx_time_secs;
|
||||
tx_time_fract_sec = rx_time_fract_sec;
|
||||
if (tx_timestamps) {
|
||||
tx_time_fract_sec += (double)latspl / samplerate;
|
||||
tx_time_fract_sec += (double)buffer_size / samplerate;
|
||||
if (tx_time_fract_sec >= 1.0) {
|
||||
tx_time_fract_sec -= 1.0;
|
||||
tx_time_secs++;
|
||||
@@ -660,7 +660,7 @@ int uhd_get_tosend(int latspl)
|
||||
PDEBUG(DSOAPY, DEBUG_ERROR, "SDR TX underrun, seems we are too slow. Use lower SDR sample rate.\n");
|
||||
advance = 0;
|
||||
}
|
||||
tosend = latspl - (int)(advance * samplerate);
|
||||
tosend = buffer_size - (int)(advance * samplerate);
|
||||
if (tosend < 0)
|
||||
tosend = 0;
|
||||
|
||||
|
@@ -4,5 +4,5 @@ int uhd_start(void);
|
||||
void uhd_close(void);
|
||||
int uhd_send(float *buff, int num);
|
||||
int uhd_receive(float *buff, int max);
|
||||
int uhd_get_tosend(int latspl);
|
||||
int uhd_get_tosend(int buffer_size);
|
||||
|
||||
|
Reference in New Issue
Block a user