-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrtl_fm_python.c
462 lines (398 loc) · 11.1 KB
/
rtl_fm_python.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
#include "rtl_fm.c"
#include "getopt/getopt.h"
#include "rtl-sdr.h"
#include "convenience/convenience.h"
#ifndef _WIN32
struct sigaction sigact;
#endif
int r, opt;
int dev_given = 0;
int custom_ppm = 0;
void lib_loop(){
while (!do_exit) {
usleep(100000);
}
}
void lib_init_first()
{
dongle_init(&dongle);
demod_init(&demod);
output_init(&output);
controller_init(&controller);
}
void no_exit_usage(void)
{
fprintf(stderr,
"rtl_fm, a simple narrow band FM demodulator for RTL2832 based DVB-T receivers\n\n"
"Use:\trtl_fm -f freq [-options] [filename]\n"
"\t-f frequency_to_tune_to [Hz]\n"
"\t use multiple -f for scanning (requires squelch)\n"
"\t ranges supported, -f 118M:137M:25k\n"
"\t[-M modulation (default: fm)]\n"
"\t fm, wbfm, raw, am, usb, lsb\n"
"\t wbfm == -M fm -s 170k -o 4 -A fast -r 32k -l 0 -E deemp\n"
"\t raw mode outputs 2x16 bit IQ pairs\n"
"\t[-s sample_rate (default: 24k)]\n"
"\t[-d device_index (default: 0)]\n"
"\t[-g tuner_gain (default: automatic)]\n"
"\t[-l squelch_level (default: 0/off)]\n"
//"\t for fm squelch is inverted\n"
//"\t[-o oversampling (default: 1, 4 recommended)]\n"
"\t[-p ppm_error (default: 0)]\n"
"\t[-E enable_option (default: none)]\n"
"\t use multiple -E to enable multiple options\n"
"\t edge: enable lower edge tuning\n"
"\t dc: enable dc blocking filter\n"
"\t deemp: enable de-emphasis filter\n"
"\t direct: enable direct sampling\n"
"\t offset: enable offset tuning\n"
"\tfilename ('-' means stdout)\n"
"\t omitting the filename also uses stdout\n\n"
"Experimental options:\n"
"\t[-r resample_rate (default: none / same as -s)]\n"
"\t[-t squelch_delay (default: 10)]\n"
"\t +values will mute/scan, -values will exit\n"
"\t[-F fir_size (default: off)]\n"
"\t enables low-leakage downsample filter\n"
"\t size can be 0 or 9. 0 has bad roll off\n"
"\t[-A std/fast/lut choose atan math (default: std)]\n"
//"\t[-C clip_path (default: off)\n"
//"\t (create time stamped raw clips, requires squelch)\n"
//"\t (path must have '\%s' and will expand to date_time_freq)\n"
//"\t[-H hop_fifo (default: off)\n"
//"\t (fifo will contain the active frequency)\n"
"\n"
"Produces signed 16 bit ints, use Sox or aplay to hear them.\n"
"\trtl_fm ... | play -t raw -r 24k -es -b 16 -c 1 -V1 -\n"
"\t | aplay -r 24k -f S16_LE -t raw -c 1\n"
"\t -M wbfm | play -r 32k ... \n"
"\t -s 22050 | multimon -t raw /dev/stdin\n\n");
}
void no_exit_sanity_checks(void)
{
if (controller.freq_len == 0) {
fprintf(stderr, "Please specify a frequency.\n");
}
if (controller.freq_len >= FREQUENCIES_LIMIT) {
fprintf(stderr, "Too many channels, maximum %i.\n", FREQUENCIES_LIMIT);
}
if (controller.freq_len > 1 && demod.squelch_level == 0) {
fprintf(stderr, "Please specify a squelch level. Required for scanning multiple frequencies.\n");
}
}
void lib_process_args(int argc, char **argv)
{
while ((opt = getopt(argc, argv, "d:f:g:s:b:l:o:t:r:p:E:F:A:M:h")) != -1) {
switch (opt) {
case 'd':
dongle.dev_index = verbose_device_search(optarg);
dev_given = 1;
break;
case 'f':
if (controller.freq_len >= FREQUENCIES_LIMIT) {
break;}
if (strchr(optarg, ':'))
{frequency_range(&controller, optarg);}
else
{
controller.freqs[controller.freq_len] = (uint32_t)atofs(optarg);
controller.freq_len++;
}
break;
case 'g':
dongle.gain = (int)(atof(optarg) * 10);
break;
case 'l':
demod.squelch_level = (int)atof(optarg);
break;
case 's':
demod.rate_in = (uint32_t)atofs(optarg);
demod.rate_out = (uint32_t)atofs(optarg);
break;
case 'r':
output.rate = (int)atofs(optarg);
demod.rate_out2 = (int)atofs(optarg);
break;
case 'o':
fprintf(stderr, "Warning: -o is very buggy\n");
demod.post_downsample = (int)atof(optarg);
if (demod.post_downsample < 1 || demod.post_downsample > MAXIMUM_OVERSAMPLE) {
fprintf(stderr, "Oversample must be between 1 and %i\n", MAXIMUM_OVERSAMPLE);}
break;
case 't':
demod.conseq_squelch = (int)atof(optarg);
if (demod.conseq_squelch < 0) {
demod.conseq_squelch = -demod.conseq_squelch;
demod.terminate_on_squelch = 1;
}
break;
case 'p':
dongle.ppm_error = atoi(optarg);
custom_ppm = 1;
break;
case 'E':
if (strcmp("edge", optarg) == 0) {
controller.edge = 1;}
if (strcmp("dc", optarg) == 0) {
demod.dc_block = 1;}
if (strcmp("deemp", optarg) == 0) {
demod.deemph = 1;}
if (strcmp("direct", optarg) == 0) {
dongle.direct_sampling = 1;}
if (strcmp("offset", optarg) == 0) {
dongle.offset_tuning = 1;}
break;
case 'F':
demod.downsample_passes = 1; /* truthy placeholder */
demod.comp_fir_size = atoi(optarg);
break;
case 'A':
if (strcmp("std", optarg) == 0) {
demod.custom_atan = 0;}
if (strcmp("fast", optarg) == 0) {
demod.custom_atan = 1;}
if (strcmp("lut", optarg) == 0) {
atan_lut_init();
demod.custom_atan = 2;}
break;
case 'M':
if (strcmp("fm", optarg) == 0) {
demod.mode_demod = &fm_demod;}
if (strcmp("raw", optarg) == 0) {
demod.mode_demod = &raw_demod;}
if (strcmp("am", optarg) == 0) {
demod.mode_demod = &am_demod;}
if (strcmp("usb", optarg) == 0) {
demod.mode_demod = &usb_demod;}
if (strcmp("lsb", optarg) == 0) {
demod.mode_demod = &lsb_demod;}
if (strcmp("wbfm", optarg) == 0) {
controller.wb_mode = 1;
demod.mode_demod = &fm_demod;
demod.rate_in = 170000;
demod.rate_out = 170000;
demod.rate_out2 = 32000;
demod.custom_atan = 1;
//demod.post_downsample = 4;
demod.deemph = 1;
demod.squelch_level = 0;}
break;
case 'h':
default:
no_exit_usage();
break;
}
}
/* quadruple sample_rate to limit to Δθ to ±π/2 */
demod.rate_in *= demod.post_downsample;
if (!output.rate) {
output.rate = demod.rate_out;}
no_exit_sanity_checks();
if (controller.freq_len > 1) {
demod.terminate_on_squelch = 0;}
if (argc <= optind) {
output.filename = "-";
} else {
output.filename = argv[optind];
}
ACTUAL_BUF_LENGTH = lcm_post[demod.post_downsample] * DEFAULT_BUF_LENGTH;
if (!dev_given) {
dongle.dev_index = verbose_device_search("0");
}
if (dongle.dev_index < 0) {
fprintf(stderr, "Device index less than zero.\n");
}
}
void lib_output_open()
{
if (strcmp(output.filename, "-") == 0) { /* Write samples to stdout */
output.file = stdout;
#ifdef _WIN32
_setmode(_fileno(output.file), _O_BINARY);
#endif
} else {
output.file = fopen(output.filename, "wb");
if (!output.file) {
fprintf(stderr, "Failed to open %s\n", output.filename);
}
}
}
void lib_input_open()
{
r = rtlsdr_open(&dongle.dev, (uint32_t)dongle.dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dongle.dev_index);
}
}
void lib_register_signal_handler(){
#ifndef _WIN32
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGPIPE, &sigact, NULL);
#else
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif
}
void lib_go()
{
if (demod.deemph) {
demod.deemph_a = (int)round(1.0/((1.0-exp(-1.0/(demod.rate_out * 75e-6)))));
}
if (dongle.gain == AUTO_GAIN) {
verbose_auto_gain(dongle.dev);
} else {
dongle.gain = nearest_gain(dongle.dev, dongle.gain);
verbose_gain_set(dongle.dev, dongle.gain);
}
verbose_ppm_set(dongle.dev, dongle.ppm_error);
verbose_reset_buffer(dongle.dev);
pthread_create(&controller.thread, NULL, controller_thread_fn, (void *)(&controller));
usleep(100000);
pthread_create(&output.thread, NULL, output_thread_fn, (void *)(&output));
pthread_create(&demod.thread, NULL, demod_thread_fn, (void *)(&demod));
pthread_create(&dongle.thread, NULL, dongle_thread_fn, (void *)(&dongle));
}
void lib_print_frequency(){
fprintf(stderr,"Frequency: %i.\n",controller.freqs[controller.freq_now]);
}
uint32_t lib_get_frequency(){
return controller.freqs[controller.freq_now];
}
int lib_get_s_level(){
int s = rms(demod.lowpassed, demod.lp_len, 1);
if (s > 0) {
return s;
} else {
return 0;
}
}
void lib_set_frequency(uint32_t new_frequency){
optimal_settings(new_frequency, demod.rate_in);
controller.freqs[controller.freq_now] = new_frequency;
rtlsdr_set_center_freq(dongle.dev, dongle.freq);
}
void lib_set_squelch_level(int l) {
demod.squelch_level = l;
}
void util_set_wbfm_options(){
controller.wb_mode = 1;
demod.custom_atan = 1;
demod.deemph = 1;
demod.squelch_level = 0;
}
void util_set_other_demod_options(){
controller.wb_mode = 0;
demod.custom_atan = 0;
demod.deemph = 0;
demod.squelch_level = 0;
}
void lib_set_demod_wbfm(){
util_set_wbfm_options();
demod.mode_demod = &fm_demod;
}
void lib_set_demod_fm(){
util_set_other_demod_options();
demod.mode_demod = &fm_demod;
}
void lib_set_demod_am(){
util_set_other_demod_options();
demod.mode_demod = &am_demod;
}
void lib_set_demod_lsb(){
util_set_other_demod_options();
demod.mode_demod = &lsb_demod;
}
void lib_set_demod_usb(){
util_set_other_demod_options();
demod.mode_demod = &usb_demod;
}
void lib_set_demod_raw(){
util_set_other_demod_options();
demod.mode_demod = &raw_demod;
}
char lib_get_demod_mode(){
if (demod.mode_demod==&fm_demod){
if (controller.wb_mode==1){
return 'w';
} else {
return 'f';
}
}
if (demod.mode_demod==&am_demod){
return 'a';
}
if (demod.mode_demod==&lsb_demod){
return 'l';
}
if (demod.mode_demod==&usb_demod){
return 'u';
}
if (demod.mode_demod==&raw_demod){
return 'r';
}
}
void lib_set_auto_gain(){
r = rtlsdr_set_tuner_gain_mode(dongle.dev, 0);
dongle.gain = -100;
}
void lib_set_gain(int g){
if (g >= 0){
int ng;
rtlsdr_set_tuner_gain_mode(dongle.dev, 1);
ng = nearest_gain(dongle.dev,g*10);
rtlsdr_set_tuner_gain(dongle.dev, ng);
dongle.gain=ng;
}
}
void lib_set_real_gain(int g){
if (g > -100){
rtlsdr_set_tuner_gain_mode(dongle.dev, 1);
rtlsdr_set_tuner_gain(dongle.dev, g);
dongle.gain=g;
} else {
lib_set_auto_gain();
}
}
int lib_get_tuner_gains_count(){
int count,r;
rtlsdr_set_tuner_gain_mode(dongle.dev, 1);
return rtlsdr_get_tuner_gains(dongle.dev, NULL);
}
void lib_get_tuner_gains(int *gains){
int count,r;
r = rtlsdr_set_tuner_gain_mode(dongle.dev, 1);
count = rtlsdr_get_tuner_gains(dongle.dev, gains);
}
int lib_get_gain(){
if (dongle.gain == -100){
return -100;
} else {
return rtlsdr_get_tuner_gain(dongle.dev);
}
}
uint32_t lib_frequency_convert(char *s){
return (uint32_t)atofs(s);
}
void lib_stop()
{
sighandler(0);
rtlsdr_cancel_async(dongle.dev);
pthread_join(dongle.thread, NULL);
safe_cond_signal(&demod.ready, &demod.ready_m);
pthread_join(demod.thread, NULL);
safe_cond_signal(&output.ready, &output.ready_m);
safe_cond_signal(&controller.hop, &controller.hop_m);
pthread_join(controller.thread, NULL);
demod_cleanup(&demod);
output_cleanup(&output);
controller_cleanup(&controller);
}
void lib_output_close()
{
if (output.file != stdout) {
fclose(output.file);}
}