-
Notifications
You must be signed in to change notification settings - Fork 170
/
csdr.c
executable file
·3631 lines (3253 loc) · 141 KB
/
csdr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This software is part of libcsdr, a set of simple DSP routines for
Software Defined Radio.
Copyright (c) 2014, Andras Retzler <randras@sdr.hu>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDRAS RETZLER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _POSIX_C_SOURCE 199309L
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include "libcsdr.h"
#include "libcsdr_gpl.h"
#include "ima_adpcm.h"
#include <sched.h>
#include <math.h>
#include <strings.h>
#include <errno.h>
#include "fastddc.h"
#include <assert.h>
char usage[]=
"csdr - a simple commandline tool for Software Defined Radio receiver DSP.\n\n"
"usage: \n\n"
" csdr function_name <function_param1> <function_param2> [optional_param] ...\n\n"
"list of functions:\n\n"
" convert_u8_f\n"
" convert_f_u8\n"
" convert_s8_f\n"
" convert_f_s8\n"
" convert_f_s16\n"
" convert_s16_f\n"
" convert_f_s24 [--bigendian]\n"
" convert_s24_f [--bigendian]\n"
" realpart_cf\n"
" clipdetect_ff\n"
" limit_ff [max_amplitude]\n"
" gain_ff <gain>\n"
" clone\n"
" none\n"
" yes_f <to_repeat> [buf_times]\n"
" detect_nan_ff\n"
" dump_f\n"
" shift_math_cc <rate>\n"
" shift_math_cc --fifo <fifo_path>\n"
" shift_addition_cc <rate>\n"
" shift_addition_cc --fifo <fifo_path>\n"
" shift_addition_cc_test\n"
" shift_table_cc <rate> [table_size]\n"
" shift_addition_fc <rate>\n"
" shift_addition_fc --fifo <fifo_path>\n"
" decimating_shift_addition_cc <rate> [decimation]\n"
" dcblock_ff\n"
" fastdcblock_ff\n"
" fmdemod_atan_cf\n"
" fmdemod_quadri_cf\n"
" fmdemod_quadri_novect_cf\n"
" deemphasis_wfm_ff <sample_rate> <tau>\n"
" deemphasis_nfm_ff <one_of_the_predefined_sample_rates>\n"
" amdemod_cf\n"
" amdemod_estimator_cf\n"
" fir_decimate_cc <decimation_factor> [transition_bw [window]]\n"
" fir_interpolate_cc <interpolation_factor> [transition_bw [window]]\n"
" firdes_lowpass_f <cutoff_rate> <length> [window [--octave]]\n"
" firdes_bandpass_c <low_cut> <high_cut> <length> [window [--octave]]\n"
" agc_ff [hang_time [reference [attack_rate [decay_rate [max_gain [attack_wait [filter_alpha]]]]]]]\n"
" fastagc_ff [block_size [reference]]\n"
" rational_resampler_ff <interpolation> <decimation> [transition_bw [window]]\n"
" old_fractional_decimator_ff <decimation_rate> [transition_bw [window]]\n"
" fractional_decimator_ff <decimation_rate> [num_poly_points ( [transition_bw [window]] | --prefilter )]\n"
" fft_cc <fft_size> <out_of_every_n_samples> [window [--octave] [--benchmark]]\n"
" fft_fc <fft_size> <out_of_every_n_samples> [window [--benchmark]]\n"
" logpower_cf [add_db]\n"
" fft_benchmark <fft_size> <fft_cycles> [--benchmark]\n"
" bandpass_fir_fft_cc <low_cut> <high_cut> <transition_bw> [window]\n"
" bandpass_fir_fft_cc --fifo <fifo_path> <transition_bw> [window]\n"
" encode_ima_adpcm_s16_u8\n"
" decode_ima_adpcm_u8_s16\n"
" compress_fft_adpcm_f_u8 <fft_size>\n"
" flowcontrol <data_rate> <reads_per_second>\n"
" through\n"
" dsb_fc [q_value]\n"
" convert_f_samperf <wait_for_this_sample> \n"
" fmmod_fc\n"
" fixed_amplitude_cc <new_amplitude>\n"
" mono2stereo_s16\n"
" setbuf <buffer_size>\n"
" fft_exchange_sides_ff <fft_size>\n"
" squelch_and_smeter_cc --fifo <squelch_fifo> --outfifo <smeter_fifo> <use_every_nth> <report_every_nth>\n"
" fifo <buffer_size> <number_of_buffers>\n"
" invert_u8_u8\n"
" rtty_line_decoder_u8_u8\n"
" rtty_baudot2ascii_u8_u8\n"
" serial_line_decoder_f_u8 <samples_per_bits> [databits [stopbits]]\n"
" octave_complex_c <samples_to_plot> <out_of_n_samples> [--2d]\n"
" timing_recovery_cc <algorithm> <decimation> [mu [max_error [--add_q [--output_error | --output_indexes | --octave <show_every_nth> | --octave_save <show_every_nth> <directory> ]]]] \n"
" psk31_varicode_encoder_u8_u8\n"
" psk31_varicode_decoder_u8_u8\n"
" differential_encoder_u8_u8\n"
" differential_decoder_u8_u8\n"
" dump_u8\n"
" psk_modulator_u8_c <n_psk>\n"
" psk31_interpolate_sine_cc <interpolation>\n"
" duplicate_samples_ntimes_u8_u8 <sample_size_bytes> <ntimes>\n"
" bpsk_costas_loop_cc <loop_bandwidth> <damping_factor> [--dd | --decision_directed] [--output_error | --output_dphase | --output_nco | --output_combined <error_file> <dphase_file> <nco_file>]\n"
" binary_slicer_f_u8\n"
" simple_agc_cc <rate> [reference [max_gain]]\n"
" firdes_peak_c <rate> <length> [window [--octave]]\n"
" peaks_fir_cc <taps_length> [peak_rate × N]\n"
" repeat_u8 <data_bytes × N>\n"
" uniform_noise_f\n"
" gaussian_noise_c\n"
" awgn_cc <snr_db> [--awgnfile <file>] [--snrshow]\n"
" pack_bits_8to1_u8_u8\n"
" pack_bits_1to8_u8_u8\n"
" firdes_pulse_shaping_filter_f (RRC <samples_per_symbol> <num_taps> <beta> | COSINE <samples_per_symbol>)\n"
" pulse_shaping_filter_cc (RRC <samples_per_symbol> <num_taps> <beta> | COSINE <samples_per_symbol>)\n"
" add_n_zero_samples_at_beginning_f <n_zero_samples>\n"
" generic_slicer_f_u8 <n_symbols>\n"
" plain_interpolate_cc <interpolation>\n"
" add_const_cc <i> <q>\n"
" tee <path> [buffers]\n"
" pll_cc (1 [alpha] |2 [bandwidth [damping_factor [ko [kd]]]])\n"
" pattern_search_u8_u8 <values_after> <pattern_values × N>\n"
" dbpsk_decoder_c_u8\n"
" bfsk_demod_cf <spacing> <filter_length>\n"
" normalized_timing_variance_u32_f <samples_per_symbol> <initial_sample_offset> [--debug]\n"
" ?<search_the_function_list>\n"
" ??<jump_to_function_docs_on_github>\n"
" =<evaluate_python_expression>\n"
" shift_addfast_cc <rate> #only if system supports NEON \n"
" shift_unroll_cc <rate>\n"
" logaveragepower_cf <add_db> <fft_size> <avgnumber>\n"
" fft_one_side_ff <fft_size>\n"
" convert_f_samplerf <wait_for_this_sample>\n"
" add_dcoffset_cc\n"
" fastddc_fwd_cc <decimation> [transition_bw [window]]\n"
" fastddc_inv_cc <shift_rate> <decimation> [transition_bw [window]]\n"
" _fft2octave <fft_size>\n"
" convert_f_i16 #deprecated, use instead: convert_f_s16\n"
" convert_i16_f #deprecated, use instead: convert_s16_f\n"
" floatdump_f #deprecated, use instead: dump_f\n"
" mono2stereo_i16 #deprecated, use instead: mono2stereo_s16\n"
" decode_ima_adpcm_u8_i16 #deprecated, use instead: decode_ima_adpcm_u8_s16\n"
" encode_ima_adpcm_i16_u8 #deprecated, use instead: encode_ima_adpcm_i16_u8\n"
" \n"
;
//change on 2015-08-29: we rather dynamically determine the bufsize
//#define BUFSIZE (1024)
//#define BIG_BUFSIZE (1024*16)
//should be multiple of 16! (size of double complex)
//also, keep in mind that shift_addition_cc works better the smaller this buffer is.
int env_csdr_fixed_bufsize = 1024;
int env_csdr_fixed_big_bufsize = 1024*16;
int env_csdr_dynamic_bufsize_on = 0;
int env_csdr_print_bufsizes = 0;
int bigbufs = 0;
//change on on 2015-08-29: we don't yield at all. fread() will do it if it blocks
#define YIELD_EVERY_N_TIMES 3
//#define TRY_YIELD if(++yield_counter%YIELD_EVERY_N_TIMES==0) sched_yield()
#define TRY_YIELD fflush(stdout);sched_yield()
//unsigned yield_counter=0;
char **argv_global;
int argc_global;
int errhead()
{
fprintf(stderr, "%s%s%s: ", argv_global[0], ((argc_global>=2)?" ":""), ((argc_global>=2)?argv_global[1]:""));
}
int badsyntax(char* why)
{
if(why==0) fprintf(stderr, "%s", usage);
else
{
errhead();
fprintf(stderr, "%s\n", why);
}
return -1;
}
int clipdetect_ff(float* input, int input_size)
{
for(int i=0;i<input_size;i++)
{
if(input[i]<-1.0) { errhead(); fprintf(stderr, "Signal value below -1.0!\n"); return -1; }
if(input[i]>1.0) { errhead(); fprintf(stderr, "Signal value above 1.0!\n"); return 1; }
}
return 0;
}
int clone_(int bufsize_param)
{
unsigned char* clone_buffer;
clone_buffer = (unsigned char*)malloc(bufsize_param*sizeof(unsigned char));
for(;;)
{
fread(clone_buffer, sizeof(unsigned char), bufsize_param, stdin);
fwrite(clone_buffer, sizeof(unsigned char), bufsize_param, stdout);
TRY_YIELD;
}
}
#define FREAD_U8 fread (input_buffer, sizeof(unsigned char), the_bufsize, stdin)
#define FWRITE_U8 fwrite (output_buffer, sizeof(unsigned char), the_bufsize, stdout)
#define FREAD_R fread (input_buffer, sizeof(float), the_bufsize, stdin)
#define FREAD_C fread (input_buffer, sizeof(float)*2, the_bufsize, stdin)
#define FWRITE_R fwrite (output_buffer, sizeof(float), the_bufsize, stdout)
#define FWRITE_C fwrite (output_buffer, sizeof(float)*2, the_bufsize, stdout)
#define FEOF_CHECK if(feof(stdin)) return 0
//#define BIG_FREAD_C fread(input_buffer, sizeof(float)*2, BIG_BUFSIZE, stdin)
//#define BIG_FWRITE_C fwrite(output_buffer, sizeof(float)*2, BIG_BUFSIZE, stdout)
int init_fifo(int argc, char *argv[])
{
if(argc>=4)
{
if(!strcmp(argv[2],"--fifo"))
{
errhead(); fprintf(stderr,"fifo control mode on\n");
int fd = open(argv[3], O_RDONLY);
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
return fd;
}
else if(!strcmp(argv[2],"--fd"))
{
//to use this:
//1. Create a pipe(pipedesc) in your process.
//2. fork() and execl() your process to run csdr, and give pipedesc[0] as parameter after --fd
// Note: when forking, the child process will get a copy of the file descriptor table! That's why this
// works at all, as file descriptor indexes are normally not transferable between processes, except for a *NIX socket way which is quite complicated...
//3. From your parent process, write into pipedesc[1].
//This is implemented in ddcd, check there to see how to do it!
int fd;
if(sscanf(argv[3], "%d",&fd)<=0) return 0;
errhead();
fprintf(stderr,"fd control mode on, fd=%d\n", fd);
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
return fd;
}
}
return 0;
}
#define RFCTL_BUFSIZE 1024
int read_fifo_ctl(int fd, char* format, ...)
{
if(!fd) return 0;
static char buffer[RFCTL_BUFSIZE];
static int buffer_index=0;
int bytes_read=read(fd,buffer+buffer_index,(RFCTL_BUFSIZE-buffer_index)*sizeof(char));
if(bytes_read<=0) return 0;
int prev_newline_at=0;
int last_newline_at=0;
for(int i=0;i<buffer_index+bytes_read;i++)
{
if(buffer[i]=='\n')
{
prev_newline_at=last_newline_at;
last_newline_at=i+1;
}
}
if(last_newline_at)
{
//fprintf(stderr,"pna=%d lna=%d\n",prev_newline_at,last_newline_at);
va_list vl;
va_start(vl,format);
vsscanf(buffer+prev_newline_at,format,vl);
va_end(vl);
memmove(buffer,buffer+last_newline_at,buffer_index+bytes_read-last_newline_at);
buffer_index=bytes_read-last_newline_at;
return 1;
}
else
{
buffer_index+=bytes_read;
return 0;
}
}
#define SETBUF_PREAMBLE "csdr"
#define SETBUF_DEFAULT_BUFSIZE 1024
#define STRINGIFY_VALUE(x) STRINGIFY_NAME(x)
#define STRINGIFY_NAME(x) #x
int getbufsize()
{
if(!env_csdr_dynamic_bufsize_on) return (bigbufs) ? env_csdr_fixed_big_bufsize : env_csdr_fixed_bufsize;
int recv_first[2];
fread(recv_first, sizeof(int), 2, stdin);
if(memcmp(recv_first, SETBUF_PREAMBLE, sizeof(char)*4)!=0)
{ badsyntax("warning! Did not match preamble on the beginning of the stream. You should put \"csdr setbuf <buffer size>\" at the beginning of the chain! Falling back to default buffer size: " STRINGIFY_VALUE(SETBUF_DEFAULT_BUFSIZE)); return SETBUF_DEFAULT_BUFSIZE; }
if(recv_first[1]<=0) { badsyntax("warning! Invalid buffer size." ); return 0; }
return recv_first[1];
}
float* input_buffer;
unsigned char* buffer_u8;
float *output_buffer;
short *buffer_i16;
float *temp_f;
int the_bufsize = 0;
#define UNITROUND_UNIT 4
int unitround(int what)
{
if(what<=0) return UNITROUND_UNIT;
return ((what-1)&~(UNITROUND_UNIT-1))+UNITROUND_UNIT;
}
int initialize_buffers()
{
if(!(the_bufsize=getbufsize())) return 0;
the_bufsize=unitround(the_bufsize);
if(env_csdr_print_bufsizes) { errhead(); fprintf(stderr,"buffer size set to %d\n", the_bufsize); }
input_buffer = (float*) malloc(the_bufsize*sizeof(float) * 2); //need the 2× because we might also put complex floats into it
output_buffer = (float*) malloc(the_bufsize*sizeof(float) * 2);
buffer_u8 = (unsigned char*)malloc(the_bufsize*sizeof(unsigned char));
buffer_i16 = (short*) malloc(the_bufsize*sizeof(short));
temp_f = (float*) malloc(the_bufsize*sizeof(float) * 4);
if(the_bufsize<=4096) //this is hacky, should be done correctly
{
fcntl(STDIN_FILENO, F_SETPIPE_SZ, 4096);
fcntl(STDOUT_FILENO, F_SETPIPE_SZ, 4096);
}
return the_bufsize;
}
int sendbufsize(int size)
{
if(size<=4096)
{
fcntl(STDOUT_FILENO, F_SETPIPE_SZ, 4096);
}
//The first word is a preamble, "csdr".
//If the next csdr process detects it, sets the buffer size according to the second word
if(!env_csdr_dynamic_bufsize_on) return env_csdr_fixed_bufsize;
if(env_csdr_print_bufsizes) { errhead(); fprintf(stderr,"next process proposed input buffer size is %d\n", size); }
int send_first[2];
memcpy((char*)send_first, SETBUF_PREAMBLE, 4*sizeof(char));
send_first[1] = size;
fwrite(send_first, sizeof(int), 2, stdout);
return size;
}
int parse_env()
{
char* envtmp;
envtmp=getenv("CSDR_DYNAMIC_BUFSIZE_ON");
//fprintf(stderr, "envtmp: %s\n",envtmp);
if(envtmp)
{
env_csdr_dynamic_bufsize_on = !!atoi(envtmp);
env_csdr_fixed_bufsize = 0;
}
else
{
envtmp=getenv("CSDR_FIXED_BUFSIZE");
if(envtmp)
{
env_csdr_fixed_big_bufsize = env_csdr_fixed_bufsize = atoi(envtmp);
}
}
envtmp=getenv("CSDR_PRINT_BUFSIZES");
if(envtmp)
{
env_csdr_print_bufsizes = atoi(envtmp);
}
}
int main(int argc, char *argv[])
{
parse_env();
argv_global=argv;
argc_global=argc;
if(argc<=1) return badsyntax(0);
if(!strcmp(argv[1],"--help")) return badsyntax(0);
fcntl(STDIN_FILENO, F_SETPIPE_SZ, 65536*32);
fcntl(STDOUT_FILENO, F_SETPIPE_SZ, 65536*32);
//fprintf(stderr, "csdr: F_SETPIPE_SZ\n");
if(!strcmp(argv[1],"setbuf"))
{
if(argc<=2) return badsyntax("need required parameter (buffer size)");
sscanf(argv[2],"%d",&the_bufsize);
if(the_bufsize<=0) return badsyntax("buffer size <= 0 is invalid");
sendbufsize(the_bufsize);
clone_(the_bufsize); //After sending the buffer size out, just copy stdin to stdout
}
if(!strcmp(argv[1],"clone") || !strcmp(argv[1],"REM"))
{
if(!sendbufsize(initialize_buffers())) return -2;
clone_(the_bufsize);
}
#define SET_NONBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK)
if(!strcmp(argv[1],"fifo"))
{
if(!sendbufsize(initialize_buffers())) return -2;
int fifo_buffer_size;
if(argc<=2) return badsyntax("need required parameter (buffer_size)");
sscanf(argv[2],"%d",&fifo_buffer_size);
int fifo_num_buffers;
if(argc<=3) return badsyntax("need required parameter (number of buffers)");
sscanf(argv[3],"%d",&fifo_num_buffers);
char** fifo_buffers = (char**)malloc(sizeof(char*)*fifo_num_buffers);
for(int i=0;i<fifo_num_buffers;i++) fifo_buffers[i]=(char*)malloc(sizeof(char)*fifo_buffer_size);
SET_NONBLOCK(STDIN_FILENO);
SET_NONBLOCK(STDOUT_FILENO);
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
fd_set write_fds;
FD_ZERO(&write_fds);
FD_SET(STDOUT_FILENO, &write_fds);
int highfd = ((STDOUT_FILENO > STDIN_FILENO) ? STDOUT_FILENO : STDIN_FILENO) + 1;
int fifo_actual_buffer_wr = fifo_num_buffers - 1;
int fifo_actual_buffer_rd = 0;
int fifo_actual_buffer_wr_pos = 0;
int fifo_actual_buffer_rd_pos = 0;
int fifo_error = 0;
int fifo_overrun_shown = 0;
for(;;)
{
select(highfd, &read_fds, NULL, NULL, NULL);
//try to read until buffer is full
if(FD_ISSET(STDIN_FILENO, &read_fds)) for(;;)
{
int read_bytes=read(STDIN_FILENO, fifo_buffers[fifo_actual_buffer_rd]+fifo_actual_buffer_rd_pos, fifo_buffer_size-fifo_actual_buffer_rd_pos);
//fprintf(stderr, "r %d %d | %d %d\n", read_bytes, fifo_buffer_size-fifo_actual_buffer_rd_pos, fifo_actual_buffer_rd, fifo_actual_buffer_rd_pos);
if(!read_bytes || ((read_bytes<0)&&(fifo_error=read_bytes)) ) break;
fifo_actual_buffer_rd_pos+=read_bytes;
if(!((fifo_actual_buffer_rd==fifo_actual_buffer_wr-1)||(fifo_actual_buffer_wr==0&&fifo_actual_buffer_rd==fifo_num_buffers-1)))
{
if(fifo_actual_buffer_rd_pos==fifo_buffer_size)
{
fifo_overrun_shown = 0;
fifo_actual_buffer_rd++;
fifo_actual_buffer_rd_pos = 0;
if(fifo_actual_buffer_rd>=fifo_num_buffers) fifo_actual_buffer_rd=0;
}
}
else
{
if(fifo_actual_buffer_rd_pos==fifo_buffer_size)
{
fifo_actual_buffer_rd_pos = 0; //rewrite same buffer
if(!fifo_overrun_shown) { fifo_overrun_shown=1; errhead(); fprintf(stderr, "circular buffer full, dropping samples\n"); }
}
}
}
//try to write until buffer is empty
if(FD_ISSET(STDOUT_FILENO, &write_fds)) for(;;)
{
if(fifo_actual_buffer_wr == fifo_actual_buffer_rd) break;
int written_bytes=write(STDOUT_FILENO, fifo_buffers[fifo_actual_buffer_wr]+fifo_actual_buffer_wr_pos, fifo_buffer_size-fifo_actual_buffer_wr_pos);
//fprintf(stderr, "w %d %d | %d %d\n", written_bytes, fifo_buffer_size-fifo_actual_buffer_wr_pos, fifo_actual_buffer_wr, fifo_actual_buffer_wr_pos);
if(!written_bytes || ((written_bytes<0)&&(fifo_error=written_bytes)) ) break;
fifo_actual_buffer_wr_pos+=written_bytes;
if(fifo_actual_buffer_wr_pos==fifo_buffer_size)
{
fifo_actual_buffer_wr++;
fifo_actual_buffer_wr_pos = 0;
if(fifo_actual_buffer_wr>=fifo_num_buffers) fifo_actual_buffer_wr=0;
}
}
if(fifo_error&&errno!=11) { errhead(); fprintf(stderr,"fifo_error (%d)", errno); return -1; }
}
return -1;
}
if(!strcmp(argv[1],"convert_u8_f"))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
fread(buffer_u8, sizeof(unsigned char), the_bufsize, stdin);
convert_u8_f(buffer_u8, output_buffer, the_bufsize);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"convert_f_u8")) //not tested
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
convert_f_u8(input_buffer, buffer_u8, the_bufsize);
fwrite(buffer_u8, sizeof(unsigned char), the_bufsize, stdout);
TRY_YIELD;
}
}
if(!strcmp(argv[1],"convert_s8_f"))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
fread((signed char*)buffer_u8, sizeof(signed char), the_bufsize, stdin);
convert_s8_f((signed char*)buffer_u8, output_buffer, the_bufsize);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"convert_f_s8")) //not tested
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
convert_f_s8(input_buffer, (signed char*)buffer_u8, the_bufsize);
fwrite((signed char*)buffer_u8, sizeof(signed char), the_bufsize, stdout);
TRY_YIELD;
}
}
if((!strcmp(argv[1],"convert_f_i16")) || (!strcmp(argv[1],"convert_f_s16")))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
convert_f_i16(input_buffer, buffer_i16, the_bufsize);
fwrite(buffer_i16, sizeof(short), the_bufsize, stdout);
TRY_YIELD;
}
}
if((!strcmp(argv[1],"convert_i16_f")) || (!strcmp(argv[1],"convert_s16_f")))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
fread(buffer_i16, sizeof(short), the_bufsize, stdin);
convert_i16_f(buffer_i16, output_buffer, the_bufsize);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"convert_f_s24"))
{
int bigendian = (argc>2) && (!strcmp(argv[2],"--bigendian"));
unsigned char* s24buffer = (unsigned char*)malloc(sizeof(unsigned char)*the_bufsize*3);
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
convert_f_s24(input_buffer, s24buffer, the_bufsize, bigendian);
fwrite(s24buffer, sizeof(unsigned char)*3, the_bufsize, stdout);
TRY_YIELD;
}
}
if(!strcmp(argv[1],"convert_s24_f"))
{
int bigendian = (argc>2) && (!strcmp(argv[2],"--bigendian"));
unsigned char* s24buffer = (unsigned char*)malloc(sizeof(unsigned char)*the_bufsize*3);
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
fread(s24buffer, sizeof(unsigned char)*3, the_bufsize, stdin);
convert_s24_f(s24buffer, output_buffer, the_bufsize, bigendian);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"realpart_cf"))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_C;
for(int i=0;i<the_bufsize;i++) output_buffer[i]=iof(input_buffer,i);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"clipdetect_ff"))
{
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
clipdetect_ff(input_buffer, the_bufsize);
fwrite(input_buffer, sizeof(float), the_bufsize, stdout);
TRY_YIELD;
}
}
if(!strcmp(argv[1],"gain_ff"))
{
if(argc<=2) return badsyntax("need required parameter (gain)");
float gain;
sscanf(argv[2],"%g",&gain);
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
gain_ff(input_buffer, output_buffer, the_bufsize, gain);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"limit_ff"))
{
float max_amplitude=1.0;
if(argc>=3) sscanf(argv[2],"%g",&max_amplitude);
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
limit_ff(input_buffer, output_buffer, the_bufsize, max_amplitude);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"yes_f"))
{
if(argc<=2) return badsyntax("need required parameter (to_repeat)");
float to_repeat;
sscanf(argv[2],"%g",&to_repeat);
int buf_times = 0;
if(argc>=4) sscanf(argv[3],"%d",&buf_times);
if(!sendbufsize(initialize_buffers())) return -2;
for(int i=0;i<the_bufsize;i++) output_buffer[i]=to_repeat;
for(int i=0;(!buf_times)||i<buf_times;i++)
{
fwrite(output_buffer, sizeof(float), the_bufsize, stdout);
TRY_YIELD;
}
return 0;
}
if(!strcmp(argv[1],"shift_math_cc"))
{
if(argc<=2) return badsyntax("need required parameter (rate)");
float starting_phase=0;
float rate;
sscanf(argv[2],"%g",&rate);
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
starting_phase=shift_math_cc((complexf*)input_buffer, (complexf*)output_buffer, the_bufsize, rate, starting_phase);
FWRITE_C;
TRY_YIELD;
}
return 0;
}
//speed tests:
//csdr yes_f 1 1000000 | time csdr shift_math_cc 0.2 >/dev/null
//csdr yes_f 1 1000000 | time csdr shift_addition_cc 0.2 >/dev/null
//csdr yes_f 1 1000000 | time csdr shift_table_cc 0.2 >/dev/null
if(!strcmp(argv[1],"shift_table_cc"))
{
bigbufs=1;
if(argc<=2) return badsyntax("need required parameter (rate)");
float starting_phase=0;
float rate;
int table_size=65536;
sscanf(argv[2],"%g",&rate);
if(argc>3) sscanf(argv[3],"%d",&table_size);
if(!sendbufsize(initialize_buffers())) return -2;
shift_table_data_t table_data=shift_table_init(table_size);
errhead();
fprintf(stderr,"LUT initialized\n");
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
starting_phase=shift_table_cc((complexf*)input_buffer, (complexf*)output_buffer, the_bufsize, rate, table_data, starting_phase);
FWRITE_C;
TRY_YIELD;
}
return 0;
}
if(!strcmp(argv[1],"shift_addfast_cc"))
{
bigbufs=1;
float starting_phase=0;
float rate;
int fd;
if(fd=init_fifo(argc,argv))
{
while(!read_fifo_ctl(fd,"%g\n",&rate)) usleep(10000);
}
else
{
if(argc<=2) return badsyntax("need required parameter (rate)");
sscanf(argv[2],"%g",&rate);
}
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
shift_addfast_data_t data=shift_addfast_init(rate);
errhead();
fprintf(stderr,"reinitialized to %g\n",rate);
int remain, current_size;
float* ibufptr;
float* obufptr;
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
remain=the_bufsize;
ibufptr=input_buffer;
obufptr=output_buffer;
while(remain)
{
current_size=(remain>1024)?1024:remain;
starting_phase=shift_addfast_cc((complexf*)ibufptr, (complexf*)obufptr, current_size, &data, starting_phase);
ibufptr+=current_size*2;
obufptr+=current_size*2;
remain-=current_size;
}
FWRITE_C;
if(read_fifo_ctl(fd,"%g\n",&rate)) break;
TRY_YIELD;
}
}
return 0;
}
if(!strcmp(argv[1],"shift_unroll_cc"))
{
bigbufs=1;
float starting_phase=0;
float rate;
int fd;
if(fd=init_fifo(argc,argv))
{
while(!read_fifo_ctl(fd,"%g\n",&rate)) usleep(10000);
}
else
{
if(argc<=2) return badsyntax("need required parameter (rate)");
sscanf(argv[2],"%g",&rate);
}
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
shift_unroll_data_t data=shift_unroll_init(rate, 1024);
errhead();
fprintf(stderr,"reinitialized to %g\n",rate);
int remain, current_size;
float* ibufptr;
float* obufptr;
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
remain=the_bufsize;
ibufptr=input_buffer;
obufptr=output_buffer;
while(remain)
{
current_size=(remain>1024)?1024:remain;
starting_phase=shift_unroll_cc((complexf*)ibufptr, (complexf*)obufptr, current_size, &data, starting_phase);
ibufptr+=current_size*2;
obufptr+=current_size*2;
remain-=current_size;
}
FWRITE_C;
if(read_fifo_ctl(fd,"%g\n",&rate)) break;
TRY_YIELD;
}
}
return 0;
}
#ifdef LIBCSDR_GPL
if(!strcmp(argv[1],"decimating_shift_addition_cc"))
{
bigbufs=1;
if(argc<=2) return badsyntax("need required parameter (rate)");
float starting_phase=0;
float rate;
int decimation=1;
sscanf(argv[2],"%g",&rate);
if(argc>3) sscanf(argv[3],"%d",&decimation);
if(!initialize_buffers()) return -2;
sendbufsize(the_bufsize/decimation);
shift_addition_data_t d=decimating_shift_addition_init(rate, decimation);
decimating_shift_addition_status_t s;
s.decimation_remain=0;
s.starting_phase=0;
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
s=decimating_shift_addition_cc((complexf*)input_buffer, (complexf*)output_buffer, the_bufsize, d, decimation, s);
fwrite(output_buffer, sizeof(float)*2, s.output_size, stdout);
TRY_YIELD;
}
return 0;
}
if(!strcmp(argv[1],"shift_addition_cc"))
{
bigbufs=1;
float starting_phase=0;
float rate;
int fd;
if(fd=init_fifo(argc,argv))
{
while(!read_fifo_ctl(fd,"%g\n",&rate)) usleep(10000);
}
else
{
if(argc<=2) return badsyntax("need required parameter (rate)");
sscanf(argv[2],"%g",&rate);
}
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
shift_addition_data_t data=shift_addition_init(rate);
errhead();
fprintf(stderr,"reinitialized to %g\n",rate);
int remain, current_size;
float* ibufptr;
float* obufptr;
for(;;)
{
FEOF_CHECK;
if(!FREAD_C) break;
remain=the_bufsize;
ibufptr=input_buffer;
obufptr=output_buffer;
while(remain)
{
current_size=(remain>1024)?1024:remain;
starting_phase=shift_addition_cc((complexf*)ibufptr, (complexf*)obufptr, current_size, data, starting_phase);
ibufptr+=current_size*2;
obufptr+=current_size*2;
remain-=current_size;
}
FWRITE_C;
if(read_fifo_ctl(fd,"%g\n",&rate)) break;
TRY_YIELD;
}
}
return 0;
}
if(!strcmp(argv[1],"shift_addition_cc_test"))
{
if(argc<=2) return badsyntax("need required parameter (rate)");
float rate;
sscanf(argv[2],"%g",&rate);
//if(initialize_buffers()) return -2; //most likely we don't need this here
shift_addition_data_t data=shift_addition_init(rate);
shift_addition_cc_test(data);
return 0;
}
#endif
if(!strcmp(argv[1],"dcblock_ff"))
{
static dcblock_preserve_t dcp; //will be 0 as .bss is set to 0
if(!sendbufsize(initialize_buffers())) return -2;
for(;;)
{
FEOF_CHECK;
FREAD_R;
dcp=dcblock_ff(input_buffer, output_buffer, the_bufsize, 0, dcp);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"fastdcblock_ff"))
{
int dcblock_bufsize=SETBUF_DEFAULT_BUFSIZE;
if(argc>=3) sscanf(argv[2],"%d",&dcblock_bufsize);
float* dcblock_buffer=(float*)malloc(sizeof(float)*dcblock_bufsize);
static float last_dc_level=0.0;
getbufsize(); //it is just dummy
sendbufsize(dcblock_bufsize);
for(;;)
{
FEOF_CHECK;
fread(dcblock_buffer, sizeof(float), dcblock_bufsize, stdin);
last_dc_level=fastdcblock_ff(dcblock_buffer, dcblock_buffer, dcblock_bufsize, last_dc_level);
fwrite(dcblock_buffer, sizeof(float), dcblock_bufsize, stdout);
TRY_YIELD;
}
}
if(!strcmp(argv[1],"fmdemod_atan_cf"))
{
if(!sendbufsize(initialize_buffers())) return -2;
float last_phase=0;
for(;;)
{
FEOF_CHECK;
FREAD_C;
if(feof(stdin)) return 0;
last_phase=fmdemod_atan_cf((complexf*)input_buffer, output_buffer, the_bufsize, last_phase);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"fmdemod_quadri_cf"))
{
if(!sendbufsize(initialize_buffers())) return -2;
complexf last_sample;
last_sample.i=0.;
last_sample.q=0.;
for(;;)
{
FEOF_CHECK;
FREAD_C;
last_sample=fmdemod_quadri_cf((complexf*)input_buffer, output_buffer, the_bufsize, temp_f, last_sample);
FWRITE_R;
TRY_YIELD;
}
}
if(!strcmp(argv[1],"fmdemod_quadri_novect_cf"))
{