-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathminimodem.c
More file actions
1492 lines (1313 loc) · 41.8 KB
/
minimodem.c
File metadata and controls
1492 lines (1313 loc) · 41.8 KB
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
/*
* minimodem.c
*
* minimodem - software audio Bell-type or RTTY FSK modem
*
* Copyright (C) 2011-2020 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <assert.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define VERSION "unknown"
#endif
#include "simpleaudio.h"
#include "fsk.h"
#include "databits.h"
#include "baudot.h"
char *program_name = "";
int tx_transmitting = 0;
int tx_print_eot = 0;
int tx_leader_bits_len = 2;
int tx_trailer_bits_len = 2;
simpleaudio *tx_sa_out;
float tx_bfsk_mark_f;
unsigned int tx_bit_nsamples;
unsigned int tx_flush_nsamples;
void
tx_stop_transmit_sighandler( int sig )
{
// fprintf(stderr, "alarm\n");
int j;
for ( j=0; j<tx_trailer_bits_len; j++ )
simpleaudio_tone(tx_sa_out, tx_bfsk_mark_f, tx_bit_nsamples);
if ( tx_flush_nsamples )
simpleaudio_tone(tx_sa_out, 0, tx_flush_nsamples);
tx_transmitting = 0;
if ( tx_print_eot )
fprintf(stderr, "### EOT\n");
}
/*
* rudimentary BFSK transmitter
*/
static void fsk_transmit_frame(
simpleaudio *sa_out,
unsigned int bits,
unsigned int n_data_bits,
size_t bit_nsamples,
float bfsk_mark_f,
float bfsk_space_f,
float bfsk_nstartbits,
float bfsk_nstopbits,
int invert_start_stop,
int bfsk_msb_first
)
{
int i;
if ( bfsk_nstartbits > 0 )
simpleaudio_tone(sa_out, invert_start_stop ? bfsk_mark_f : bfsk_space_f,
bit_nsamples * bfsk_nstartbits); // start
for ( i=0; i<n_data_bits; i++ ) { // data
unsigned int bit;
if (bfsk_msb_first) {
bit = ( bits >> (n_data_bits - i - 1) ) & 1;
} else {
bit = ( bits >> i ) & 1;
}
float tone_freq = bit == 1 ? bfsk_mark_f : bfsk_space_f;
simpleaudio_tone(sa_out, tone_freq, bit_nsamples);
}
if ( bfsk_nstopbits > 0 )
simpleaudio_tone(sa_out, invert_start_stop ? bfsk_space_f : bfsk_mark_f,
bit_nsamples * bfsk_nstopbits); // stop
}
static void fsk_transmit_stdin(
simpleaudio *sa_out,
int tx_interactive,
float data_rate,
float bfsk_mark_f,
float bfsk_space_f,
int n_data_bits,
float bfsk_nstartbits,
float bfsk_nstopbits,
int invert_start_stop,
int bfsk_msb_first,
unsigned int bfsk_do_tx_sync_bytes,
unsigned int bfsk_sync_byte,
databits_encoder encode,
int txcarrier
)
{
size_t sample_rate = simpleaudio_get_rate(sa_out);
size_t bit_nsamples = sample_rate / data_rate + 0.5f;
tx_sa_out = sa_out;
tx_bfsk_mark_f = bfsk_mark_f;
tx_bit_nsamples = bit_nsamples;
if ( tx_interactive )
tx_flush_nsamples = sample_rate/2; // 0.5 sec of zero samples to flush
else
tx_flush_nsamples = 0;
// one-shot
struct itimerval itv = {
{0, 0}, // it_interval
{0, 1000000/(float)(data_rate+data_rate*0.03f)} // it_value
};
struct itimerval itv_zero = {
{0, 0}, // it_interval
{0, 0} // it_value
};
// arbitrary chosen timeout value: 1/25 of a second
unsigned int idle_carrier_usec = (1000000/25);
int block_input = tx_interactive && !txcarrier;
if ( block_input )
signal(SIGALRM, tx_stop_transmit_sighandler);
// Set up for select() should we need it
int fd = fileno(stdin);
fd_set fdset;
tx_transmitting = 0;
int end_of_file = 0;
unsigned char buf;
int n_read = 0;
int idle = 0;
while ( !end_of_file )
{
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
struct timeval tv_idletimeout = { 0, 0 };
if ( !tx_interactive ) {
// When stdin blocks we "emit idle tone", for a duration of
// idle_carrier_usec. If !tx_interactive (i.e. writing to an
// audio file) make the select timeout the same duration.
tv_idletimeout.tv_usec = idle_carrier_usec;
}
if( block_input || select(fd+1, &fdset, NULL, NULL, &tv_idletimeout) )
{
n_read = read(fd, &buf, sizeof(buf));
if( n_read <= 0 ) //Includes EOF (0) and errors (-1)
{
end_of_file = 1;
continue; //Do nothing else
}
idle = 0;
}
else
idle = 1;
// Cause any running timer to immediately trigger
if ( block_input )
setitimer(ITIMER_REAL, &itv_zero, NULL);
if( !idle )
{
// fprintf(stderr, "<c=%d>", c);
unsigned int nwords;
unsigned int bits[2];
unsigned int j;
nwords = encode(bits, buf);
if ( !tx_transmitting )
{
tx_transmitting = 1;
/* emit leader tone (mark) */
for ( j=0; j<tx_leader_bits_len; j++ )
simpleaudio_tone(sa_out, invert_start_stop ? bfsk_space_f : bfsk_mark_f, bit_nsamples);
}
if ( tx_transmitting < 2)
{
tx_transmitting = 2;
/* emit "preamble" of sync bytes */
for ( j=0; j<bfsk_do_tx_sync_bytes; j++ )
fsk_transmit_frame(sa_out, bfsk_sync_byte, n_data_bits,
bit_nsamples, bfsk_mark_f, bfsk_space_f,
bfsk_nstartbits, bfsk_nstopbits, invert_start_stop, 0);
}
/* emit data bits */
for ( j=0; j<nwords; j++ )
fsk_transmit_frame(sa_out, bits[j], n_data_bits,
bit_nsamples, bfsk_mark_f, bfsk_space_f,
bfsk_nstartbits, bfsk_nstopbits, invert_start_stop, bfsk_msb_first);
}
else
{
tx_transmitting = 1;
/* emit idle tone (mark) */
simpleaudio_tone(sa_out,
invert_start_stop ? bfsk_space_f : bfsk_mark_f,
idle_carrier_usec * sample_rate / 1000000);
}
if ( block_input )
setitimer(ITIMER_REAL, &itv, NULL);
}
if ( block_input ) {
setitimer(ITIMER_REAL, &itv_zero, NULL);
signal(SIGALRM, SIG_DFL);
}
if ( !tx_transmitting )
return;
tx_stop_transmit_sighandler(0);
}
static void
report_no_carrier( fsk_plan *fskp,
unsigned int sample_rate,
float bfsk_data_rate,
float frame_n_bits,
unsigned int nframes_decoded,
size_t carrier_nsamples,
float confidence_total,
float amplitude_total )
{
float nbits_decoded = nframes_decoded * frame_n_bits;
#if 0
fprintf(stderr, "nframes_decoded=%u\n", nframes_decoded);
fprintf(stderr, "nbits_decoded=%f\n", nbits_decoded);
fprintf(stderr, "carrier_nsamples=%lu\n", carrier_nsamples);
#endif
float throughput_rate =
nbits_decoded * sample_rate / (float)carrier_nsamples;
fprintf(stderr, "\n### NOCARRIER ndata=%u confidence=%.3f ampl=%.3f bps=%.2f",
nframes_decoded,
(double)(confidence_total / nframes_decoded),
(double)(amplitude_total / nframes_decoded),
(double)(throughput_rate));
#if 0
fprintf(stderr, " bits*sr=%llu rate*nsamp=%llu",
(unsigned long long)(nbits_decoded * sample_rate + 0.5),
(unsigned long long)(bfsk_data_rate * carrier_nsamples) );
#endif
if ( (unsigned long long)(nbits_decoded * sample_rate + 0.5f) == (unsigned long long)(bfsk_data_rate * carrier_nsamples) ) {
fprintf(stderr, " (rate perfect) ###\n");
} else {
float throughput_skew = (throughput_rate - bfsk_data_rate)
/ bfsk_data_rate;
fprintf(stderr, " (%.1f%% %s) ###\n",
(double)(fabsf(throughput_skew) * 100.0f),
signbit(throughput_skew) ? "slow" : "fast"
);
}
}
void
generate_test_tones( simpleaudio *sa_out, unsigned int duration_sec )
{
unsigned int sample_rate = simpleaudio_get_rate(sa_out);
unsigned int nframes = sample_rate / 10;
int i;
for ( i=0; i<(sample_rate/nframes*duration_sec); i++ ) {
simpleaudio_tone(sa_out, 1000, nframes/2);
simpleaudio_tone(sa_out, 1777, nframes/2);
}
}
static int
benchmarks()
{
fprintf(stdout, "minimodem %s benchmarks\n", VERSION);
int ret;
ret = system("sed -n -e '/^model name/{p;q}' -e '/^cpu model/{p;q}' /proc/cpuinfo");
if ( ret )
; // don't care, hush compiler.
fflush(stdout);
unsigned int sample_rate = 48000;
sa_backend_t backend = SA_BACKEND_BENCHMARK;
// backend = SA_BACKEND_SYSDEFAULT; // for test
simpleaudio *sa_out;
// enable the sine wave LUT
simpleaudio_tone_init(1024, 1.0);
sa_out = simpleaudio_open_stream(backend, NULL, SA_STREAM_PLAYBACK,
SA_SAMPLE_FORMAT_S16, sample_rate, 1,
program_name, "generate-tones-lut1024-S16-mono");
if ( ! sa_out )
return 0;
generate_test_tones(sa_out, 10);
simpleaudio_close(sa_out);
sa_out = simpleaudio_open_stream(backend, NULL, SA_STREAM_PLAYBACK,
SA_SAMPLE_FORMAT_FLOAT, sample_rate, 1,
program_name, "generate-tones-lut1024-FLOAT-mono");
if ( ! sa_out )
return 0;
generate_test_tones(sa_out, 10);
simpleaudio_close(sa_out);
// disable the sine wave LUT
simpleaudio_tone_init(0, 1.0);
sa_out = simpleaudio_open_stream(backend, NULL, SA_STREAM_PLAYBACK,
SA_SAMPLE_FORMAT_S16, sample_rate, 1,
program_name, "generate-tones-nolut-S16-mono");
if ( ! sa_out )
return 0;
generate_test_tones(sa_out, 10);
simpleaudio_close(sa_out);
sa_out = simpleaudio_open_stream(backend, NULL, SA_STREAM_PLAYBACK,
SA_SAMPLE_FORMAT_FLOAT, sample_rate, 1,
program_name, "generate-tones-nolut-FLOAT-mono");
if ( ! sa_out )
return 0;
generate_test_tones(sa_out, 10);
simpleaudio_close(sa_out);
return 1;
}
static int rx_stop = 0;
void
rx_stop_sighandler( int sig )
{
rx_stop = 1;
}
void
version()
{
printf(
"minimodem %s\n"
"Copyright (C) 2011-2020 Kamal Mostafa <kamal@whence.com>\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n\n"
"Written by Kamal Mostafa <kamal@whence.com>.\n",
VERSION);
}
void
usage()
{
fprintf(stderr,
"usage: minimodem [--tx|--rx] [options] {baudmode}\n"
" -t, --tx, --transmit, --write\n"
" -r, --rx, --receive, --read (default)\n"
" [options]\n"
" -a, --auto-carrier\n"
" -i, --inverted\n"
" -c, --confidence {min-confidence-threshold}\n"
" -l, --limit {max-confidence-search-limit}\n"
" -8, --ascii ASCII 8-N-1\n"
" -7, ASCII 7-N-1\n"
" -5, --baudot Baudot 5-N-1\n"
" -u, --usos {0|1}\n"
" -f, --file {filename.flac}\n"
" -b, --bandwidth {rx_bandwidth}\n"
" -v, --volume {amplitude or 'E'}\n"
" -M, --mark {mark_freq}\n"
" -S, --space {space_freq}\n"
" --startbits {n}\n"
" --stopbits {n.n}\n"
" --invert-start-stop\n"
" --sync-byte {0xXX}\n"
" -q, --quiet\n"
" -R, --samplerate {rate}\n"
" -V, --version\n"
" -A, --alsa[=plughw:X,Y]\n"
" -s, --sndio[=device]\n"
" --lut={tx_sin_table_len}\n"
" --float-samples\n"
" --rx-one\n"
" --benchmarks\n"
" --binary-output\n"
" --binary-raw {nbits}\n"
" --print-filter\n"
" --print-eot\n"
" --tx-carrier\n"
" {baudmode}\n"
" any_number_N Bell-like N bps --ascii\n"
" 1200 Bell202 1200 bps --ascii\n"
" 300 Bell103 300 bps --ascii\n"
" rtty RTTY 45.45 bps --baudot --stopbits=1.5\n"
" tdd TTY/TDD 45.45 bps --baudot --stopbits=2.0\n"
" same NOAA SAME 520.83 bps --sync-byte=0xAB ...\n"
" callerid Bell202 CID 1200 bps\n"
" uic{-train,-ground} UIC-751-3 Train/Ground 600 bps\n"
);
exit(1);
}
int
build_expect_bits_string( char *expect_bits_string,
int bfsk_nstartbits,
int bfsk_n_data_bits,
float bfsk_nstopbits,
int invert_start_stop,
int use_expect_bits,
unsigned long long expect_bits )
{
// example expect_bits_string
// 0123456789A
// isddddddddp i == idle bit (a.k.a. prev_stop bit)
// s == start bit d == data bits p == stop bit
// ebs = "10dddddddd1" <-- expected mark/space framing pattern
//
// NOTE! expect_n_bits ends up being (frame_n_bits+1), because
// we expect the prev_stop bit in addition to this frame's own
// (start + n_data_bits + stop) bits. But for each decoded frame,
// we will advance just frame_n_bits worth of samples, leaving us
// pointing at our stop bit -- it becomes the next frame's prev_stop.
//
// prev_stop--v
// start--v v--stop
// char *expect_bits_string = "10dddddddd1";
//
char start_bit_value = invert_start_stop ? '1' : '0';
char stop_bit_value = invert_start_stop ? '0' : '1';
int j = 0;
if ( bfsk_nstopbits != 0.0f )
expect_bits_string[j++] = stop_bit_value;
int i;
// Nb. only integer number of start bits works (for rx)
for ( i=0; i<bfsk_nstartbits; i++ )
expect_bits_string[j++] = start_bit_value;
for ( i=0; i<bfsk_n_data_bits; i++,j++ ) {
if ( use_expect_bits )
expect_bits_string[j] = ( (expect_bits>>i)&1 ) + '0';
else
expect_bits_string[j] = 'd';
}
if ( bfsk_nstopbits != 0.0f )
expect_bits_string[j++] = stop_bit_value;
expect_bits_string[j] = 0;
return j;
}
int
main( int argc, char*argv[] )
{
char *modem_mode = NULL;
int TX_mode = -1;
int quiet_mode = 0;
int output_print_filter = 0;
float band_width = 0;
float bfsk_mark_f = 0;
float bfsk_space_f = 0;
unsigned int bfsk_inverted_freqs = 0;
int bfsk_nstartbits = -1;
float bfsk_nstopbits = -1;
unsigned int bfsk_do_rx_sync = 0;
unsigned int bfsk_do_tx_sync_bytes = 0;
unsigned long long bfsk_sync_byte = -1;
unsigned int bfsk_n_data_bits = 0;
int bfsk_msb_first = 0;
char *expect_data_string = NULL;
char *expect_sync_string = NULL;
unsigned int expect_n_bits;
int invert_start_stop = 0;
int autodetect_shift;
char *filename = NULL;
float carrier_autodetect_threshold = 0.0;
// fsk_confidence_threshold : signal-to-noise squelch control
//
// The minimum SNR-ish confidence level seen as "a signal".
float fsk_confidence_threshold = 1.5;
// fsk_confidence_search_limit : performance vs. quality
//
// If we find a frame with confidence > confidence_search_limit,
// quit searching for a better frame. confidence_search_limit has a
// dramatic effect on peformance (high value yields low performance, but
// higher decode quality, for noisy or hard-to-discern signals (Bell 103,
// or skewed rates).
float fsk_confidence_search_limit = 2.3f;
// float fsk_confidence_search_limit = INFINITY; /* for test */
sa_backend_t sa_backend = SA_BACKEND_SYSDEFAULT;
char *sa_backend_device = NULL;
sa_format_t sample_format = SA_SAMPLE_FORMAT_S16;
unsigned int sample_rate = 48000;
unsigned int nchannels = 1; // FIXME: only works with one channel
float tx_amplitude = 1.0;
unsigned int tx_sin_table_len = 4096;
unsigned int rx_one = 0;
float rxnoise_factor = 0.0;
int txcarrier = 0;
int output_mode_binary = 0;
int output_mode_raw_nbits = 0;
float bfsk_data_rate = 0.0;
databits_encoder *bfsk_databits_encode;
databits_decoder *bfsk_databits_decode;
bfsk_databits_decode = databits_decode_ascii8;
bfsk_databits_encode = databits_encode_ascii8;
/* validate the default system audio mechanism */
#if !(USE_SNDIO || USE_PULSEAUDIO || USE_ALSA)
# define _MINIMODEM_NO_SYSTEM_AUDIO
# if !USE_SNDFILE
# error At least one of {USE_SNDIO,USE_PULSEAUDIO,USE_ALSA,USE_SNDFILE} must be enabled!
# endif
#endif
program_name = strrchr(argv[0], '/');
if ( program_name )
program_name++;
else
program_name = argv[0];
int c;
int option_index;
enum {
MINIMODEM_OPT_UNUSED=256, // placeholder
MINIMODEM_OPT_MSBFIRST,
MINIMODEM_OPT_STARTBITS,
MINIMODEM_OPT_STOPBITS,
MINIMODEM_OPT_INVERT_START_STOP,
MINIMODEM_OPT_SYNC_BYTE,
MINIMODEM_OPT_LUT,
MINIMODEM_OPT_FLOAT_SAMPLES,
MINIMODEM_OPT_RX_ONE,
MINIMODEM_OPT_BENCHMARKS,
MINIMODEM_OPT_BINARY_OUTPUT,
MINIMODEM_OPT_BINARY_RAW,
MINIMODEM_OPT_PRINT_FILTER,
MINIMODEM_OPT_XRXNOISE,
MINIMODEM_OPT_PRINT_EOT,
MINIMODEM_OPT_TXCARRIER
};
while ( 1 ) {
static struct option long_options[] = {
{ "version", 0, 0, 'V' },
{ "tx", 0, 0, 't' },
{ "transmit", 0, 0, 't' },
{ "write", 0, 0, 't' },
{ "rx", 0, 0, 'r' },
{ "receive", 0, 0, 'r' },
{ "read", 0, 0, 'r' },
{ "confidence", 1, 0, 'c' },
{ "limit", 1, 0, 'l' },
{ "auto-carrier", 0, 0, 'a' },
{ "inverted", 0, 0, 'i' },
{ "ascii", 0, 0, '8' },
{ "", 0, 0, '7' },
{ "baudot", 0, 0, '5' },
{ "usos", 1, 0, 'u' },
{ "msb-first", 0, 0, MINIMODEM_OPT_MSBFIRST },
{ "file", 1, 0, 'f' },
{ "bandwidth", 1, 0, 'b' },
{ "volume", 1, 0, 'v' },
{ "mark", 1, 0, 'M' },
{ "space", 1, 0, 'S' },
{ "startbits", 1, 0, MINIMODEM_OPT_STARTBITS },
{ "stopbits", 1, 0, MINIMODEM_OPT_STOPBITS },
{ "invert-start-stop", 0, 0, MINIMODEM_OPT_INVERT_START_STOP },
{ "sync-byte", 1, 0, MINIMODEM_OPT_SYNC_BYTE },
{ "quiet", 0, 0, 'q' },
{ "alsa", 2, 0, 'A' },
{ "sndio", 2, 0, 's' },
{ "samplerate", 1, 0, 'R' },
{ "lut", 1, 0, MINIMODEM_OPT_LUT },
{ "float-samples", 0, 0, MINIMODEM_OPT_FLOAT_SAMPLES },
{ "rx-one", 0, 0, MINIMODEM_OPT_RX_ONE },
{ "benchmarks", 0, 0, MINIMODEM_OPT_BENCHMARKS },
{ "binary-output", 0, 0, MINIMODEM_OPT_BINARY_OUTPUT },
{ "binary-raw", 1, 0, MINIMODEM_OPT_BINARY_RAW },
{ "print-filter", 0, 0, MINIMODEM_OPT_PRINT_FILTER },
{ "print-eot", 0, 0, MINIMODEM_OPT_PRINT_EOT },
{ "Xrxnoise", 1, 0, MINIMODEM_OPT_XRXNOISE },
{ "tx-carrier", 0, 0, MINIMODEM_OPT_TXCARRIER },
{ 0 }
};
c = getopt_long(argc, argv, "Vtrc:l:ai875u:f:b:v:M:S:T:qs::A::R:",
long_options, &option_index);
if ( c == -1 )
break;
switch( c ) {
case 'V':
version();
exit(0);
case 't':
if ( TX_mode == 0 )
usage();
TX_mode = 1;
break;
case 'r':
if ( TX_mode == 1 )
usage();
TX_mode = 0;
break;
case 'c':
fsk_confidence_threshold = atof(optarg);
break;
case 'l':
fsk_confidence_search_limit = atof(optarg);
break;
case 'a':
carrier_autodetect_threshold = 0.001;
break;
case 'i':
bfsk_inverted_freqs = 1;
break;
case 'f':
filename = optarg;
break;
case '8':
bfsk_n_data_bits = 8;
break;
case '7':
bfsk_n_data_bits = 7;
break;
case '5':
bfsk_n_data_bits = 5;
bfsk_databits_decode = databits_decode_baudot;
bfsk_databits_encode = databits_encode_baudot;
break;
case 'u':
baudot_usos = atoi(optarg);
break;
case MINIMODEM_OPT_MSBFIRST:
bfsk_msb_first = 1;
break;
case 'b':
band_width = atof(optarg);
assert( band_width != 0 );
break;
case 'v':
if ( optarg[0] == 'E' )
tx_amplitude = FLT_EPSILON;
else
tx_amplitude = atof(optarg);
assert( tx_amplitude > 0.0f );
break;
case 'M':
bfsk_mark_f = atof(optarg);
assert( bfsk_mark_f > 0 );
break;
case 'S':
bfsk_space_f = atof(optarg);
assert( bfsk_space_f > 0 );
break;
case MINIMODEM_OPT_STARTBITS:
bfsk_nstartbits = atoi(optarg);
// Note: bfsk_nstartbits is limited by arrays
// expect_bits_string[32] and fsk.c:bit_something[32]
assert( bfsk_nstartbits >= 0 && bfsk_nstartbits <= 20 );
break;
case MINIMODEM_OPT_STOPBITS:
bfsk_nstopbits = atof(optarg);
assert( bfsk_nstopbits >= 0 );
break;
case MINIMODEM_OPT_INVERT_START_STOP:
invert_start_stop = 1;
break;
case MINIMODEM_OPT_SYNC_BYTE:
bfsk_do_rx_sync = 1;
bfsk_do_tx_sync_bytes = 16;
bfsk_sync_byte = strtol(optarg, NULL, 0);
break;
case 'q':
quiet_mode = 1;
break;
case 'R':
sample_rate = atoi(optarg);
assert( sample_rate > 0 );
break;
case 'A':
#if USE_ALSA
sa_backend = SA_BACKEND_ALSA;
if ( optarg )
sa_backend_device = optarg;
#else
fprintf(stderr, "E: This build of minimodem was configured without alsa support.\n");
exit(1);
#endif
break;
case 's':
#if USE_SNDIO
sa_backend = SA_BACKEND_SNDIO;
if ( optarg )
sa_backend_device = optarg;
#else
fprintf(stderr, "E: This build of minimodem was configured without sndio support.\n");
exit(1);
#endif
break;
case MINIMODEM_OPT_LUT:
tx_sin_table_len = atoi(optarg);
break;
case MINIMODEM_OPT_FLOAT_SAMPLES:
sample_format = SA_SAMPLE_FORMAT_FLOAT;
break;
case MINIMODEM_OPT_RX_ONE:
rx_one = 1;
break;
case MINIMODEM_OPT_BENCHMARKS:
benchmarks();
exit(0);
break;
case MINIMODEM_OPT_BINARY_OUTPUT:
output_mode_binary = 1;
break;
case MINIMODEM_OPT_BINARY_RAW:
output_mode_raw_nbits = atoi(optarg);
break;
case MINIMODEM_OPT_PRINT_FILTER:
output_print_filter = 1;
break;
case MINIMODEM_OPT_XRXNOISE:
rxnoise_factor = atof(optarg);
break;
case MINIMODEM_OPT_TXCARRIER:
txcarrier = 1;
break;
case MINIMODEM_OPT_PRINT_EOT:
tx_print_eot = 1;
break;
default:
usage();
}
}
if ( TX_mode == -1 )
TX_mode = 0;
/* The receive code requires floating point samples to feed to the FFT */
if ( TX_mode == 0 )
sample_format = SA_SAMPLE_FORMAT_FLOAT;
if ( filename ) {
#if !USE_SNDFILE
fprintf(stderr, "E: This build of minimodem was configured without sndfile,\nE: so the --file flag is not supported.\n");
exit(1);
#endif
} else {
#ifdef _MINIMODEM_NO_SYSTEM_AUDIO
fprintf(stderr, "E: this build of minimodem was configured without system audio support,\nE: so only the --file mode is supported.\n");
exit(1);
#endif
}
#if 0
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
#endif
if (optind + 1 != argc) {
fprintf(stderr, "E: *** Must specify {baudmode} (try \"300\") ***\n");
usage();
}
modem_mode = argv[optind++];
if ( strncasecmp(modem_mode, "rtty",5)==0 ) {
bfsk_databits_decode = databits_decode_baudot;
bfsk_databits_encode = databits_encode_baudot;
bfsk_data_rate = 45.45;
if ( bfsk_n_data_bits == 0 )
bfsk_n_data_bits = 5;
if ( bfsk_nstopbits < 0 )
bfsk_nstopbits = 1.5;
} else if ( strncasecmp(modem_mode, "tdd",4)==0 ) {
bfsk_databits_decode = databits_decode_baudot;
bfsk_databits_encode = databits_encode_baudot;
bfsk_data_rate = 45.45;
if ( bfsk_n_data_bits == 0 )
bfsk_n_data_bits = 5;
if ( bfsk_nstopbits < 0 )
bfsk_nstopbits = 2.0;
bfsk_mark_f = 1400;
bfsk_space_f = 1800;
} else if ( strncasecmp(modem_mode, "same",5)==0 ) {
// http://www.nws.noaa.gov/nwr/nwrsame.htm
bfsk_data_rate = 520.0 + 5/6.0;
bfsk_n_data_bits = 8;
bfsk_nstartbits = 0;
bfsk_nstopbits = 0;
bfsk_do_rx_sync = 1;
bfsk_do_tx_sync_bytes = 16;
bfsk_sync_byte = 0xAB;
bfsk_mark_f = 2083.0 + 1/3.0;
bfsk_space_f = 1562.5;
band_width = bfsk_data_rate;
} else if ( strncasecmp(modem_mode, "caller",6)==0 ) {
if ( TX_mode ) {
fprintf(stderr, "E: callerid --tx mode is not supported.\n");
return 1;
}
if ( carrier_autodetect_threshold > 0.0f )
fprintf(stderr, "W: callerid with --auto-carrier is not recommended.\n");
bfsk_databits_decode = databits_decode_callerid;
bfsk_data_rate = 1200;
bfsk_n_data_bits = 8;
} else if ( strncasecmp(modem_mode, "uic", 3) == 0 ) {
if ( TX_mode ) {
fprintf(stderr, "E: uic-751-3 --tx mode is not supported.\n");
return 1;
}
// http://ec.europa.eu/transport/rail/interoperability/doc/ccs-tsi-en-annex.pdf
if (tolower(modem_mode[4]) == 't')
bfsk_databits_decode = databits_decode_uic_train;
else
bfsk_databits_decode = databits_decode_uic_ground;
bfsk_data_rate = 600;
bfsk_n_data_bits = 39;
bfsk_mark_f = 1300;
bfsk_space_f = 1700;
bfsk_nstartbits = 8;
bfsk_nstopbits = 0;
expect_data_string = "11110010ddddddddddddddddddddddddddddddddddddddd";
expect_n_bits = 47;
} else if ( strncasecmp(modem_mode, "V.21", 4) == 0 ) {
bfsk_data_rate = 300;
bfsk_mark_f = 980;
bfsk_space_f = 1180;
bfsk_n_data_bits = 8;
} else {
bfsk_data_rate = atof(modem_mode);
if ( bfsk_n_data_bits == 0 )
bfsk_n_data_bits = 8;
}
if ( bfsk_data_rate == 0.0f )
usage();
if ( output_mode_binary || output_mode_raw_nbits )
bfsk_databits_decode = databits_decode_binary;
if ( output_mode_raw_nbits ) {
bfsk_nstartbits = 0;
bfsk_nstopbits = 0;
bfsk_n_data_bits = output_mode_raw_nbits;
}
if ( bfsk_data_rate >= 400 ) {
/*
* Bell 202: baud=1200 mark=1200 space=2200
*/
autodetect_shift = - ( bfsk_data_rate * 5 / 6 );
if ( bfsk_mark_f == 0 )
bfsk_mark_f = bfsk_data_rate / 2 + 600;
if ( bfsk_space_f == 0 )
bfsk_space_f = bfsk_mark_f - autodetect_shift;
if ( band_width == 0 )
band_width = 200;
} else if ( bfsk_data_rate >= 100 ) {
/*
* Bell 103: baud=300 mark=1270 space=1070
*/
autodetect_shift = 200;
if ( bfsk_mark_f == 0 )
bfsk_mark_f = 1270;
if ( bfsk_space_f == 0 )
bfsk_space_f = bfsk_mark_f - autodetect_shift;
if ( band_width == 0 )
band_width = 50; // close enough
} else {
/*
* RTTY: baud=45.45 mark/space=variable shift=-170
*/
autodetect_shift = 170;
if ( bfsk_mark_f == 0 )
bfsk_mark_f = 1585;
if ( bfsk_space_f == 0 )
bfsk_space_f = bfsk_mark_f - autodetect_shift;
if ( band_width == 0 ) {
band_width = 10; // FIXME chosen arbitrarily
}
}
// defaults: 1 start bit, 1 stop bit
if ( bfsk_nstartbits < 0 )
bfsk_nstartbits = 1;
if ( bfsk_nstopbits < 0 )
bfsk_nstopbits = 1.0;
// n databits plus bfsk_startbit start bits plus bfsk_nstopbit stop bits:
unsigned int bfsk_frame_n_bits = bfsk_n_data_bits + bfsk_nstartbits + bfsk_nstopbits;
if ( bfsk_frame_n_bits > 64 ) {
fprintf(stderr, "E: total number of bits per frame must be <= 64.\n");
exit(1);
}
// do not transmit any leader tone if no start bits
if ( bfsk_nstartbits == 0 )
tx_leader_bits_len = 0;
if ( bfsk_inverted_freqs ) {
float t = bfsk_mark_f;
bfsk_mark_f = bfsk_space_f;
bfsk_space_f = t;
}
/* restrict band_width to <= data rate (FIXME?) */
if ( band_width > bfsk_data_rate )
band_width = bfsk_data_rate;
// sanitize confidence search limit
if ( fsk_confidence_search_limit < fsk_confidence_threshold )
fsk_confidence_search_limit = fsk_confidence_threshold;
char *stream_name = NULL;
if ( filename ) {
sa_backend = SA_BACKEND_FILE;
stream_name = filename;
}
/*
* Handle transmit mode
*/
if ( TX_mode ) {
simpleaudio_tone_init(tx_sin_table_len, tx_amplitude);
int tx_interactive = 0;
if ( ! stream_name ) {
tx_interactive = 1;
stream_name = "output audio";
}
simpleaudio *sa_out;
sa_out = simpleaudio_open_stream(sa_backend, sa_backend_device,
SA_STREAM_PLAYBACK,
sample_format, sample_rate, nchannels,
program_name, stream_name);
if ( ! sa_out )
return 1;
fsk_transmit_stdin(sa_out, tx_interactive,
bfsk_data_rate,
bfsk_mark_f, bfsk_space_f,
bfsk_n_data_bits,
bfsk_nstartbits,
bfsk_nstopbits,