diff options
Diffstat (limited to 'lib/wsprd/wsprd.c')
| -rw-r--r-- | lib/wsprd/wsprd.c | 180 |
1 files changed, 131 insertions, 49 deletions
diff --git a/lib/wsprd/wsprd.c b/lib/wsprd/wsprd.c index 13f8d8b3..d442c70b 100644 --- a/lib/wsprd/wsprd.c +++ b/lib/wsprd/wsprd.c @@ -42,6 +42,7 @@ #include "nhash.h" #include "wsprd_utils.h" #include "wsprsim_utils.h" +#include "wsprd_stream.h" #define max(x,y) ((x) > (y) ? (x) : (y)) @@ -727,6 +728,23 @@ void usage(void) printf(" -v verbose mode (shows dupes)\n"); printf(" -w wideband mode - decode signals within +/- 150 Hz of center\n"); printf(" -z x (x is fano metric table bias, default is 0.45)\n"); + printf(" -0 streaming mode: read PCM audio from stdin per the\n"); + printf(" streaming framing protocol; emit decodes as NDJSON\n"); + printf(" on stdout. Replaces the positional\n"); + printf(" input file argument.\n"); +} + +static int build_data_path(char *destination, size_t capacity, + const char *data_dir, const char *filename) +{ + const char *base = data_dir ? data_dir : "."; + int written = snprintf(destination, capacity, "%s/%s", base, filename); + if (written < 0 || (size_t)written >= capacity) { + fprintf(stderr, "Error: data path for '%s' exceeds %zu bytes\n", + filename, capacity - 1); + return -1; + } + return 0; } //*************************************************************************** @@ -740,13 +758,15 @@ int main(int argc, char *argv[]) unsigned char *symbols, *decdata, *channel_symbols, *apmask, *cw; signed char message[]={-9,13,-35,123,57,-39,64,0,0,0,0}; char *callsign, *grid, *call_loc_pow; - char *ptr_to_infile,*ptr_to_infile_suffix; + char *ptr_to_infile = NULL, *ptr_to_infile_suffix = NULL; char *data_dir="."; char wisdom_fname[200],all_fname[200],spots_fname[200]; char timer_fname[200],hash_fname[200]; char uttime[5],date[7]; int c,delta,maxpts=65536,verbose=0,quickmode=0,more_candidates=0, stackdecoder=0; int usehashtable=1,wspr_type=2, ipass, nblocksize; + int stream_mode = 0; + struct wsprd_stream_config stream_cfg = {0}; int nhardmin,ihash; int writec2=0,maxdrift; int shift1, lagmin, lagmax, lagstep, ifmin, ifmax, not_decoded; @@ -777,10 +797,9 @@ int main(int argc, char *argv[]) struct result decodes[50]; char *hashtab; - hashtab=calloc(32768*13,sizeof(char)); + hashtab=calloc(WSPRD_HASH_COUNT*WSPRD_CALLSIGN_SIZE,sizeof(char)); char *loctab; - loctab=calloc(32768*5,sizeof(char)); - int nh; + loctab=calloc(WSPRD_HASH_COUNT*WSPRD_GRID4_SIZE,sizeof(char)); symbols=calloc(nbits*2,sizeof(unsigned char)); apmask=calloc(162,sizeof(unsigned char)); cw=calloc(162,sizeof(unsigned char)); @@ -818,8 +837,26 @@ int main(int argc, char *argv[]) idat=calloc(maxpts,sizeof(float)); qdat=calloc(maxpts,sizeof(float)); + if (!hashtab || !loctab || !symbols || !apmask || !cw || !decdata || + !channel_symbols || !callsign || !grid || !call_loc_pow || + !idat || !qdat) { + fprintf(stderr, "Error: unable to allocate wsprd decoder buffers\n"); + free(hashtab); + free(loctab); + free(symbols); + free(apmask); + free(cw); + free(decdata); + free(channel_symbols); + free(callsign); + free(grid); + free(call_loc_pow); + free(idat); + free(qdat); + return EXIT_FAILURE; + } - while ( (c = getopt(argc, argv, "a:BcC:de:f:HJmo:qstwvz:")) !=-1 ) { + while ( (c = getopt(argc, argv, "a:BcC:de:f:HJmo:qstwvz:0")) !=-1 ) { switch (c) { case 'a': data_dir = optarg; @@ -872,6 +909,9 @@ int main(int argc, char *argv[]) case 'z': bias=strtod(optarg,NULL); //fano metric bias (default is 0.45) break; + case '0': // --stream (long-name parity with jt9 --stream) + stream_mode = 1; + break; case '?': usage(); return 1; @@ -884,15 +924,19 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if( optind+1 > argc) { + if( !stream_mode && optind+1 > argc) { usage(); return 1; - } else { + } else if (!stream_mode) { ptr_to_infile=argv[optind]; } if( stackdecoder ) { stack=calloc(stacksize,sizeof(struct snode)); + if (!stack) { + fprintf(stderr, "Error: unable to allocate wsprd stack decoder\n"); + return EXIT_FAILURE; + } } // setup metric table @@ -902,23 +946,18 @@ int main(int argc, char *argv[]) } FILE *fp_fftwf_wisdom_file, *fall_wspr, *fwsprd, *fhash, *ftimer; - strcpy(wisdom_fname,"."); - strcpy(all_fname,"."); - strcpy(spots_fname,"."); - strcpy(timer_fname,"."); - strcpy(hash_fname,"."); - if(data_dir != NULL) { - strncpy(wisdom_fname,data_dir, sizeof wisdom_fname); - strncpy(all_fname,data_dir, sizeof all_fname); - strncpy(spots_fname,data_dir, sizeof spots_fname); - strncpy(timer_fname,data_dir, sizeof timer_fname); - strncpy(hash_fname,data_dir, sizeof hash_fname); + if (build_data_path(wisdom_fname, sizeof wisdom_fname, data_dir, + "wspr_wisdom.dat") != 0 || + build_data_path(all_fname, sizeof all_fname, data_dir, + "ALL_WSPR.TXT") != 0 || + build_data_path(spots_fname, sizeof spots_fname, data_dir, + "wspr_spots.txt") != 0 || + build_data_path(timer_fname, sizeof timer_fname, data_dir, + "wspr_timer.out") != 0 || + build_data_path(hash_fname, sizeof hash_fname, data_dir, + "hashtable.txt") != 0) { + return EXIT_FAILURE; } - strncat(wisdom_fname,"/wspr_wisdom.dat",20); - strncat(all_fname,"/ALL_WSPR.TXT",20); - strncat(spots_fname,"/wspr_spots.txt",20); - strncat(timer_fname,"/wspr_timer.out",20); - strncat(hash_fname,"/hashtable.txt",20); if ((fp_fftwf_wisdom_file = fopen(wisdom_fname, "r"))) { //Open FFTW wisdom fftwf_import_wisdom_from_file(fp_fftwf_wisdom_file); fclose(fp_fftwf_wisdom_file); @@ -938,17 +977,35 @@ int main(int argc, char *argv[]) } ftimer=fopen(timer_fname,"w"); - if( strstr(ptr_to_infile,".wav") ) { + if (stream_mode) { + // Read PCM from stdin per the streaming protocol. Configure frame + // supplies date/time/dialfreq/wspr_type; audio frames carry int16 + // PCM @ 12 kHz mono. + wsprd_stream_emit_ready(); + t0 = clock(); + npoints = wsprd_stream_read(&stream_cfg, idat, qdat); + treadwav += (float)(clock()-t0)/CLOCKS_PER_SEC; + if (npoints == 1) return 1; + wspr_type = stream_cfg.wspr_type; + dialfreq = stream_cfg.dialfreq - (dialfreq_error * 1.0e-06); + strncpy(date, stream_cfg.date, 6); date[6] = '\0'; + strncpy(uttime, stream_cfg.uttime, 4); uttime[4] = '\0'; + } else if( strstr(ptr_to_infile,".wav") ) { ptr_to_infile_suffix=strstr(ptr_to_infile,".wav"); - + t0 = clock(); npoints=readwavfile(ptr_to_infile, wspr_type, idat, qdat); treadwav += (float)(clock()-t0)/CLOCKS_PER_SEC; - + if( npoints == 1 ) { return 1; } dialfreq=dialfreq_cmdline - (dialfreq_error*1.0e-06); + // Parse date and time from given filename + strncpy(date,ptr_to_infile_suffix-11,6); + strncpy(uttime,ptr_to_infile_suffix-4,4); + date[6]='\0'; + uttime[4]='\0'; } else if ( strstr(ptr_to_infile,".c2") !=0 ) { ptr_to_infile_suffix=strstr(ptr_to_infile,".c2"); npoints=readc2file(ptr_to_infile, idat, qdat, &dialfreq, &wspr_type); @@ -956,23 +1013,34 @@ int main(int argc, char *argv[]) return 1; } dialfreq -= (dialfreq_error*1.0e-06); + // Parse date and time from given filename + strncpy(date,ptr_to_infile_suffix-11,6); + strncpy(uttime,ptr_to_infile_suffix-4,4); + date[6]='\0'; + uttime[4]='\0'; } else { printf("Error: Failed to open %s\n",ptr_to_infile); printf("WSPR file must have suffix .wav or .c2\n"); return 1; } - // Parse date and time from given filename - strncpy(date,ptr_to_infile_suffix-11,6); - strncpy(uttime,ptr_to_infile_suffix-4,4); - date[6]='\0'; - uttime[4]='\0'; - // Do windowed ffts over 2 symbols, stepped by half symbols int nffts=4*floor(npoints/512)-1; fftin=(fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex)*512); fftout=(fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex)*512); + if (!fftin || !fftout) { + fprintf(stderr, "Error: unable to allocate wsprd FFT buffers\n"); + if (fftin) fftwf_free(fftin); + if (fftout) fftwf_free(fftout); + return EXIT_FAILURE; + } PLAN3 = fftwf_plan_dft_1d(512, fftin, fftout, FFTW_FORWARD, PATIENCE); + if (!PLAN3) { + fprintf(stderr, "Error: unable to create wsprd FFT plan\n"); + fftwf_free(fftin); + fftwf_free(fftout); + return EXIT_FAILURE; + } float ps[512][nffts]; float w[512]; @@ -981,13 +1049,10 @@ int main(int argc, char *argv[]) } if( usehashtable ) { - char line[80], hcall[13], hgrid[5]; + char line[80]; if( (fhash=fopen(hash_fname,"r+")) ) { while (fgets(line, sizeof(line), fhash) != NULL) { - hgrid[0]='\0'; - sscanf(line,"%d %s %s",&nh,hcall,hgrid); - strcpy(hashtab+nh*13,hcall); - if(strlen(hgrid)>0) strcpy(loctab+nh*5,hgrid); + wsprd_load_hash_line(line, hashtab, loctab); } } else { fhash=fopen(hash_fname,"w+"); @@ -1003,7 +1068,7 @@ int main(int argc, char *argv[]) maxdrift=4; minsync2=0.12; } - if(ipass == 2 ) { + else { nblocksize=4; // try 3 blocksizes plus bitbybit normalization maxdrift=0; // no drift for smaller frequency estimator variance minsync2=0.10; @@ -1444,7 +1509,8 @@ int main(int argc, char *argv[]) if(!strcmp(callsign,allcalls[i]) && (fabs(f1-allfreqs[i]) <4.0)) dupe=1; } - if( (verbose || !dupe) && !noprint) { + if( (verbose || !dupe) && !noprint && + uniques < (int)(sizeof decodes / sizeof decodes[0]) ) { strcpy(allcalls[uniques],callsign); allfreqs[uniques]=f1; uniques++; @@ -1502,9 +1568,16 @@ int main(int argc, char *argv[]) } for (i=0; i<uniques; i++) { - printf("%4s %3.0f %4.1f %10.6f %2d %-s \n", - decodes[i].time, decodes[i].snr,decodes[i].dt, decodes[i].freq, - (int)decodes[i].drift, decodes[i].message); + if (stream_mode) { + wsprd_stream_emit_decode(date, decodes[i].time, + decodes[i].snr, decodes[i].dt, + decodes[i].freq, (int)decodes[i].drift, + decodes[i].message); + } else { + printf("%4s %3.0f %4.1f %10.6f %2d %-s \n", + decodes[i].time, decodes[i].snr,decodes[i].dt, decodes[i].freq, + (int)decodes[i].drift, decodes[i].message); + } fprintf(fall_wspr, "%6s %4s %3.0f %5.2f %11.7f %-22s %2d %5.2f %2d %2d %4d %2d %3d %5u %5d\n", decodes[i].date, decodes[i].time, decodes[i].snr, @@ -1521,7 +1594,11 @@ int main(int argc, char *argv[]) decodes[i].jitter); } - printf("<DecodeFinished>\n"); + if (stream_mode) { + wsprd_stream_emit_decode_finished(date, uttime); + } else { + printf("<DecodeFinished>\n"); + } fftwf_free(fftin); fftwf_free(fftout); @@ -1553,15 +1630,17 @@ int main(int argc, char *argv[]) fclose(fwsprd); // fclose(fdiag); fclose(ftimer); - fftwf_destroy_plan(PLAN1); - fftwf_destroy_plan(PLAN2); - fftwf_destroy_plan(PLAN3); + if (PLAN1) fftwf_destroy_plan(PLAN1); + if (PLAN2) fftwf_destroy_plan(PLAN2); + if (PLAN3) fftwf_destroy_plan(PLAN3); if( usehashtable ) { fhash=fopen(hash_fname,"w"); - for (i=0; i<32768; i++) { - if( strncmp(hashtab+i*13,"\0",1) != 0 ) { - fprintf(fhash,"%5d %s %s\n",i,hashtab+i*13,loctab+i*5); + for (i=0; i<WSPRD_HASH_COUNT; i++) { + if( strncmp(hashtab+i*WSPRD_CALLSIGN_SIZE,"\0",1) != 0 ) { + fprintf(fhash,"%5d %s %s\n",i, + hashtab+i*WSPRD_CALLSIGN_SIZE, + loctab+i*WSPRD_GRID4_SIZE); } } fclose(fhash); @@ -1570,9 +1649,12 @@ int main(int argc, char *argv[]) free(hashtab); free(loctab); free(symbols); + free(apmask); + free(cw); free(decdata); free(channel_symbols); free(callsign); + free(grid); free(call_loc_pow); free(idat); free(qdat); |
