From 933ba3c3c913beeda5b1b2efc382809dce4f3303 Mon Sep 17 00:00:00 2001 From: c vw Date: Thu, 12 Dec 2019 16:46:12 +0100 Subject: [PATCH] tx heartbeat also for new protocol --- audio.c | 57 ++++++++++++++++++++++-- audio.h | 1 + new_protocol.c | 22 +++------- old_protocol.c | 37 +--------------- portaudio.c | 116 ++++++++++++++++++++++++++++++------------------- 5 files changed, 134 insertions(+), 99 deletions(-) diff --git a/audio.c b/audio.c index 832a8ea..f9312f3 100644 --- a/audio.c +++ b/audio.c @@ -73,6 +73,14 @@ AUDIO_DEVICE input_devices[MAX_AUDIO_DEVICES]; int n_output_devices; AUDIO_DEVICE output_devices[MAX_AUDIO_DEVICES]; +// +// Ring buffer for "local microphone" samples +// +#define MICRINGLEN 2048 +float *mic_ring_buffer=NULL; +int mic_ring_read_pt=0; +int mic_ring_write_pt=0; + int audio_open_output(RECEIVER *rx) { int err; snd_pcm_hw_params_t *hw_params; @@ -239,6 +247,13 @@ g_print("audio_open_input: mic_buffer: size=%d channels=%d sample=%ld bytes\n",m break; } +g_print("audio_open_input: allocating ring buffer\n"); + mic_ring_buffer=(float *) malloc(MICRINGLEN * sizeof(float)); + mic_ring_read_pt = mic_ring_write_pt=0; + if (mic_ring_buffer == NULL) { + return -1; + } + g_print("audio_open_input: creating mic_read_thread\n"); GError *error; mic_read_thread_id = g_thread_try_new("local mic",mic_read_thread,NULL,&error); @@ -281,6 +296,10 @@ g_print("audio_close_input: free mic buffer\n"); g_free(mic_buffer); mic_buffer=NULL; } + if (mic_ring_buffer != NULL) { + free(mic_ring_buffer); + mic_ring_buffer=NULL; + } } // @@ -470,6 +489,7 @@ g_print("mic_read_thread: snd_pcm_start\n"); } } } else { + int newpt; // process the mic input for(i=0;ilocal_microphone) { - process_mic_data(mic_bytes_read); - } +// +// process mic data even if local mic is used, +// since this is our pace-maker +// + process_mic_data(mic_bytes_read); free(mic_line_buffer); } } @@ -1834,7 +1836,7 @@ static void process_mic_data(int bytes) { for(i=0;ilocal_microphone ? audio_get_next_mic_sample() : (float) sample * 0.00003051; #ifdef FREEDV if(active_receiver->freedv) { add_freedv_mic_sample(transmitter,fsample); @@ -1847,18 +1849,6 @@ static void process_mic_data(int bytes) { } } -void new_protocol_process_local_mic(float sample) { -#ifdef FREEDV - if(active_receiver->freedv) { - add_freedv_mic_sample(transmitter,sample); - } else { -#endif - add_mic_sample(transmitter,sample); -#ifdef FREEDV - } -#endif -} - void new_protocol_cw_audio_samples(short left_audio_sample,short right_audio_sample) { int rc; int mode=transmitter->mode; diff --git a/old_protocol.c b/old_protocol.c index 4c05757..b804a92 100644 --- a/old_protocol.c +++ b/old_protocol.c @@ -252,11 +252,6 @@ static unsigned char ep6_inbuffer[EP6_BUFFER_SIZE]; static unsigned char usb_buffer_block = 0; #endif -#define MICRINGLEN 2048 -float mic_ring_buffer[MICRINGLEN]; -int mic_ring_read_pt=0; -int mic_ring_write_pt=0; - void old_protocol_stop() { metis_start_stop(0); } @@ -994,22 +989,7 @@ static void process_ozy_input_buffer(unsigned char *buffer) { mic_samples++; if(mic_samples>=mic_sample_divisor) { // reduce to 48000 - if (!transmitter->local_microphone) { - // take mic sample from SDR - fsample = (float) mic_sample * 0.00003051; - } else { - // take mic sample from local mic buffer - if (mic_ring_read_pt == mic_ring_write_pt) { - // nothing in buffer: insert silence - fsample=0.0; - } else { - int newpt = mic_ring_read_pt+1; - if (newpt == MICRINGLEN) newpt=0; - fsample=mic_ring_buffer[mic_ring_read_pt]; - // atomic update of read pointer - mic_ring_read_pt=newpt; - } - } + fsample = transmitter->local_microphone ? audio_get_next_mic_sample() : (float) mic_sample * 0.00003051; #ifdef FREEDV if(active_receiver->freedv) { add_freedv_mic_sample(transmitter,fsample); @@ -1096,21 +1076,6 @@ void old_protocol_iq_samples(int isample,int qsample) { } } -// -// This function now simply puts the mic sample -// into a ring buffer -// -void old_protocol_process_local_mic(float sample) { - int newpt; // value of write pointer after a successful write - newpt=mic_ring_write_pt +1; - if (newpt == MICRINGLEN) newpt=0; - if (newpt != mic_ring_read_pt) { - // buffer space available, do the write - mic_ring_buffer[mic_ring_write_pt]=sample; - mic_ring_write_pt=newpt; - } -} - /* static void process_bandscope_buffer(char *buffer) { } diff --git a/portaudio.c b/portaudio.c index 111a524..1e009a4 100644 --- a/portaudio.c +++ b/portaudio.c @@ -49,6 +49,14 @@ int n_output_devices=0; #define BUFFER_SIZE 256 +// +// Ring buffer for "local microphone" samples +// +#define MICRINGLEN 2048 +float *mic_ring_buffer=NULL; +int mic_ring_read_pt=0; +int mic_ring_write_pt=0; + // // AUDIO_GET_CARDS // @@ -171,6 +179,12 @@ int audio_open_input() return -1; } + mic_ring_buffer=(float *) malloc(MICRINGLEN * sizeof(float)); + mic_ring_read_pt = mic_ring_write_pt=0; + if (mic_ring_buffer == NULL) { + return -1; + } + err = Pa_StartStream(record_handle); if (err != paNoError) { fprintf(stderr, "PORTAUDIO ERROR: AOI start stream:%s\n",Pa_GetErrorText(err)); @@ -188,59 +202,69 @@ int pa_mic_cb(const void *inputBuffer, void *outputBuffer, unsigned long framesP void *userdata) { float *in = (float *)inputBuffer; - int i; - gfloat sample; - + int i, newpt; + float sample; if (in == NULL) { - // - // We could just do nothing, but rather send a bunch of silence - // Note the mic samples flying in are the "heart-beat" of the TX engine. - // - for (i=0; i