-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathunixinput.c
More file actions
1346 lines (1195 loc) · 42.1 KB
/
unixinput.c
File metadata and controls
1346 lines (1195 loc) · 42.1 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
/*
* unixinput.c -- input sound samples
*
* Copyright (C) 1996
* Thomas Sailer (sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu)
*
* Copyright (C) 2012-2026
* Elias Oenal (multimon-ng@eliasoenal.com)
*
* Copyright (C) 2024
* Jason Lingohr (jason@lucid.net.au)
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* ---------------------------------------------------------------------- */
#include "multimon.h"
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
/* Compatibility shim for C11 timespec_get()
*
* timespec_get() is a C11 function not universally available:
* - MSVC: available since VS2015
* - MinGW: generally missing -> use GetSystemTimeAsFileTime()
* - Android/Bionic: missing -> use POSIX clock_gettime()
* - Older BSDs/Solaris: may be missing -> use POSIX clock_gettime()
* - glibc >= 2.16, musl, macOS 10.15+, FreeBSD 11+: available
*
* CMake detects availability via check_symbol_exists and defines
* HAVE_TIMESPEC_GET. When absent, we provide a platform-appropriate fallback:
* a Windows API shim on _WIN32, otherwise a POSIX clock_gettime() shim.
*/
#if defined(_WIN32)
/* Windows strftime implementations may not support %F and %T format specifiers */
#define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
#else
#define ISO8601_FORMAT "%FT%T"
#endif
#ifndef HAVE_TIMESPEC_GET
#ifndef TIME_UTC
#define TIME_UTC 1
#endif
#if defined(_WIN32)
#include <windows.h>
/* Provide timespec_get for Windows (MinGW, or MSVC older than VS2015)
* using the Windows API. clock_gettime() is not available on Windows.
*/
static inline int timespec_get(struct timespec *ts, int base)
{
if (base != TIME_UTC) return 0;
FILETIME ft;
ULARGE_INTEGER ui;
GetSystemTimeAsFileTime(&ft);
ui.LowPart = ft.dwLowDateTime;
ui.HighPart = ft.dwHighDateTime;
/* Convert from 100-nanosecond intervals since 1601-01-01 to Unix epoch */
const uint64_t EPOCH_DIFF = 116444736000000000ULL;
uint64_t tmp = ui.QuadPart - EPOCH_DIFF;
ts->tv_sec = (time_t)(tmp / 10000000ULL);
ts->tv_nsec = (long)((tmp % 10000000ULL) * 100);
return TIME_UTC;
}
#else /* !_WIN32 */
/* Provide timespec_get on POSIX systems that lack it (e.g. Android/Bionic).
* clock_gettime(CLOCK_REALTIME, ...) is POSIX.1-2001 and available on
* Linux (all libcs), macOS, all BSDs, Solaris, and Android.
*/
static inline int timespec_get(struct timespec *ts, int base)
{
if (base != TIME_UTC) return 0;
if (clock_gettime(CLOCK_REALTIME, ts) != 0) return 0;
return TIME_UTC;
}
#endif /* _WIN32 */
#endif /* !HAVE_TIMESPEC_GET */
#ifdef SUN_AUDIO
#include <sys/audioio.h>
#include <stropts.h>
#include <sys/conf.h>
#elif PULSE_AUDIO
#include <pulse/simple.h>
#include <pulse/error.h>
#elif COREAUDIO
#include <AudioToolbox/AudioQueue.h>
#include <CoreAudio/CoreAudio.h>
#ifdef HAS_PROCESSTAP
/* External functions from macos_audio.m for system audio capture */
extern int macos_screencapture_available(void);
extern int macos_start_system_audio(unsigned int sample_rate, void (*callback)(short *, int));
extern void macos_stop_system_audio(void);
extern void macos_audio_run_loop(double seconds);
extern void macos_set_quiet(int quiet);
#endif
#elif WIN32_AUDIO
//see win32_soundin.c
#elif DUMMY_AUDIO
// NO AUDIO
#else /* SUN_AUDIO */
#include <sys/soundcard.h>
#include <sys/ioctl.h>
//#include <sys/wait.h>
#endif /* SUN_AUDIO */
#ifndef ONLY_RAW
#include <sys/wait.h>
#endif
/* ---------------------------------------------------------------------- */
static const char *allowed_types[] = {
"raw", "aiff", "au", "hcom", "sf", "voc", "cdr", "dat",
"smp", "wav", "maud", "vwe", "mp3", "mp4", "ogg", "flac", NULL
};
/* Extension to type mapping (extensions that differ from type name) */
static const struct { const char *ext; const char *type; } ext_map[] = {
{ "aif", "aiff" },
{ NULL, NULL }
};
/* Detect input type from filename extension, returns NULL if unknown */
static const char *detect_type_from_extension(const char *fname)
{
if (!fname || !strcmp(fname, "-"))
return NULL;
const char *dot = strrchr(fname, '.');
if (!dot || dot == fname)
return NULL;
const char *ext = dot + 1;
size_t ext_len = strlen(ext);
if (ext_len == 0 || ext_len > 8)
return NULL;
/* Convert extension to lowercase for comparison */
char ext_lower[16];
for (size_t i = 0; i <= ext_len && i < sizeof(ext_lower) - 1; i++)
ext_lower[i] = (ext[i] >= 'A' && ext[i] <= 'Z') ? ext[i] + 32 : ext[i];
/* Check extension map first (for aliases like .aif -> aiff) */
for (int i = 0; ext_map[i].ext; i++) {
if (!strcmp(ext_lower, ext_map[i].ext))
return ext_map[i].type;
}
/* Check if extension matches a known type directly */
for (const char **t = allowed_types; *t; t++) {
if (!strcmp(ext_lower, *t))
return *t;
}
return NULL;
}
/* Check if sox is available, returns 1 if found, 0 if not */
static int check_sox_available(void)
{
#ifdef WIN32
/* On Windows, just try to run it - harder to check PATH reliably */
return 1;
#else
/* Check common locations and PATH */
if (access("/usr/bin/sox", X_OK) == 0) return 1;
if (access("/usr/local/bin/sox", X_OK) == 0) return 1;
if (access("/opt/homebrew/bin/sox", X_OK) == 0) return 1;
/* Check PATH using which */
int ret = system("which sox >/dev/null 2>&1");
return (ret == 0);
#endif
}
/* ---------------------------------------------------------------------- */
static const struct demod_param *dem[] = { ALL_DEMOD };
#define NUMDEMOD (sizeof(dem)/sizeof(dem[0]))
static struct demod_state dem_st[NUMDEMOD];
static unsigned int dem_mask[(NUMDEMOD+31)/32];
#define MASK_SET(n) dem_mask[(n)>>5] |= 1<<((n)&0x1f)
#define MASK_RESET(n) dem_mask[(n)>>5] &= ~(1<<((n)&0x1f))
#define MASK_ISSET(n) (dem_mask[(n)>>5] & 1<<((n)&0x1f))
#define MAX_VAR_ARGS 100
/* ---------------------------------------------------------------------- */
static int verbose_level = 0;
static int repeatable_sox = 1; /* Always use sox -R for deterministic output */
static int mute_sox = 0;
static int type_explicit = 0; /* Track if -t was explicitly provided */
static int integer_only = true;
static bool dont_flush = false;
static bool is_startline = true;
static int timestamp = 0;
static int iso8601 = 0;
static char *label = NULL;
int json_mode = 0;
extern bool fms_justhex;
extern int pocsag_mode;
extern int pocsag_invert_input;
extern int pocsag_error_correction;
extern int pocsag_show_partial_decodes;
extern int pocsag_heuristic_pruning;
extern int pocsag_prune_empty;
extern int pocsag_polarity;
extern bool pocsag_init_charset(char *charset);
extern int aprs_mode;
extern int cw_dit_length;
extern int cw_gap_length;
extern int cw_threshold;
extern bool cw_disable_auto_threshold;
extern bool cw_disable_auto_timing;
extern int flex_disable_timestamp;
void quit(void);
/* ---------------------------------------------------------------------- */
void _verbprintf(int verb_level, const char *fmt, ...)
{
char time_buf[20];
if (verb_level > verbose_level)
return;
va_list args;
va_start(args, fmt);
if (is_startline)
{
if (label != NULL)
fprintf(stdout, "%s: ", label);
if (timestamp) {
if(iso8601)
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
strftime(time_buf, sizeof time_buf, ISO8601_FORMAT, gmtime(&ts.tv_sec)); //2024-09-13T20:35:30
fprintf(stdout, "%s.%06ld: ", time_buf, ts.tv_nsec/1000); //2024-09-13T20:35:30.156337
}
else
{
time_t t;
struct tm* tm_info;
t = time(NULL);
tm_info = localtime(&t);
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info);
fprintf(stdout, "%s: ", time_buf);
}
}
is_startline = false;
}
if (NULL != strchr(fmt,'\n')) /* detect end of line in stream */
is_startline = true;
vfprintf(stdout, fmt, args);
if(!dont_flush)
fflush(stdout);
va_end(args);
}
/* ---------------------------------------------------------------------- */
void addJsonTimestamp(cJSON *json_output)
{
if (json_output==NULL) return;
if (!timestamp) return;
char json_temp[100];
if(iso8601)
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
strftime(json_temp, sizeof json_temp, ISO8601_FORMAT, gmtime(&ts.tv_sec)); //2024-09-13T20:35:30
snprintf(json_temp + strlen(json_temp), sizeof json_temp - strlen(json_temp), ".%06ld", ts.tv_nsec/1000); //2024-09-13T20:35:30.156337
}
else
{
time_t t;
struct tm* tm_info;
t = time(NULL);
tm_info = localtime(&t);
strftime(json_temp, sizeof(json_temp), "%Y-%m-%d %H:%M:%S", tm_info);
}
cJSON_AddStringToObject(json_output, "timestamp", json_temp);
}
/* ---------------------------------------------------------------------- */
void process_buffer(float *float_buf, short *short_buf, unsigned int len)
{
for (int i = 0; (unsigned int) i < NUMDEMOD; i++)
if (MASK_ISSET(i) && dem[i]->demod)
{
buffer_t buffer = {short_buf, float_buf};
dem[i]->demod(dem_st+i, buffer, len);
}
}
/* ---------------------------------------------------------------------- */
#ifdef SUN_AUDIO
static void input_sound(unsigned int sample_rate, unsigned int overlap,
const char *ifname)
{
audio_info_t audioinfo;
audio_info_t audioinfo2;
audio_device_t audiodev;
int fd;
short buffer[8192];
float fbuf[16384];
unsigned int fbuf_cnt = 0;
int i;
short *sp;
if ((fd = open(ifname ? ifname : "/dev/audio", O_RDONLY)) < 0) {
perror("open");
exit (10);
}
if (ioctl(fd, AUDIO_GETDEV, &audiodev) == -1) {
perror("ioctl: AUDIO_GETDEV");
exit (10);
}
AUDIO_INITINFO(&audioinfo);
audioinfo.record.sample_rate = sample_rate;
audioinfo.record.channels = 1;
audioinfo.record.precision = 16;
audioinfo.record.encoding = AUDIO_ENCODING_LINEAR;
/*audioinfo.record.gain = 0x20;
audioinfo.record.port = AUDIO_LINE_IN;
audioinfo.monitor_gain = 0;*/
if (ioctl(fd, AUDIO_SETINFO, &audioinfo) == -1) {
perror("ioctl: AUDIO_SETINFO");
exit (10);
}
if (ioctl(fd, I_FLUSH, FLUSHR) == -1) {
perror("ioctl: I_FLUSH");
exit (10);
}
if (ioctl(fd, AUDIO_GETINFO, &audioinfo2) == -1) {
perror("ioctl: AUDIO_GETINFO");
exit (10);
}
fprintf(stdout, "Audio device: name %s, ver %s, config %s, "
"sampling rate %d\n", audiodev.name, audiodev.version,
audiodev.config, audioinfo.record.sample_rate);
for (;;) {
i = read(fd, sp = buffer, sizeof(buffer));
if (i < 0 && errno != EAGAIN) {
perror("read");
exit(4);
}
if (!i)
break;
if (i > 0) {
if(integer_only)
{
fbuf_cnt = i/sizeof(buffer[0]);
}
else
{
for (; i >= sizeof(buffer[0]); i -= sizeof(buffer[0]), sp++)
fbuf[fbuf_cnt++] = (*sp) * (1.0/32768.0);
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
}
if (fbuf_cnt > overlap) {
process_buffer(fbuf, buffer, fbuf_cnt-overlap);
memmove(fbuf, fbuf+fbuf_cnt-overlap, overlap*sizeof(fbuf[0]));
fbuf_cnt = overlap;
}
}
}
close(fd);
}
#elif DUMMY_AUDIO
static void input_sound(unsigned int sample_rate, unsigned int overlap,
const char *ifname)
{
(void)sample_rate;
(void)overlap;
(void)ifname;
}
#elif COREAUDIO
/* macOS Core Audio implementation using AudioQueue */
/* Shared state for audio queue callback */
static struct {
short *buffer;
unsigned int buffer_size;
volatile int data_ready;
volatile int running;
} ca_state;
static void ca_input_callback(void *userdata, AudioQueueRef queue,
AudioQueueBufferRef buf,
const AudioTimeStamp *start_time,
UInt32 num_packets,
const AudioStreamPacketDescription *desc)
{
(void)userdata;
(void)start_time;
(void)desc;
if (num_packets > 0 && ca_state.running) {
unsigned int bytes = num_packets * sizeof(short);
if (bytes > ca_state.buffer_size)
bytes = ca_state.buffer_size;
memcpy(ca_state.buffer, buf->mAudioData, bytes);
ca_state.data_ready = bytes / sizeof(short);
}
/* Re-enqueue the buffer for continuous recording */
if (ca_state.running)
AudioQueueEnqueueBuffer(queue, buf, 0, NULL);
}
static void input_sound(unsigned int sample_rate, unsigned int overlap,
const char *ifname)
{
AudioQueueRef queue = NULL;
AudioQueueBufferRef buffers[3];
OSStatus status;
short buffer[8192];
float fbuf[16384];
unsigned int fbuf_cnt = 0;
short *sp;
int i;
(void)ifname; /* Device selection not implemented yet */
/* Set up audio format: 16-bit signed integer, mono */
AudioStreamBasicDescription format = {
.mSampleRate = sample_rate,
.mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
.mBytesPerPacket = sizeof(short),
.mFramesPerPacket = 1,
.mBytesPerFrame = sizeof(short),
.mChannelsPerFrame = 1,
.mBitsPerChannel = 16,
.mReserved = 0
};
/* Initialize shared state */
ca_state.buffer = buffer;
ca_state.buffer_size = sizeof(buffer);
ca_state.data_ready = 0;
ca_state.running = 1;
/* Create audio input queue */
status = AudioQueueNewInput(&format, ca_input_callback, NULL,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes,
0, &queue);
if (status != noErr) {
fprintf(stderr, "AudioQueueNewInput failed: %d\n", (int)status);
exit(10);
}
/* Allocate and enqueue buffers */
const unsigned int buf_size = sizeof(buffer);
for (int b = 0; b < 3; b++) {
status = AudioQueueAllocateBuffer(queue, buf_size, &buffers[b]);
if (status != noErr) {
fprintf(stderr, "AudioQueueAllocateBuffer failed: %d\n", (int)status);
exit(10);
}
AudioQueueEnqueueBuffer(queue, buffers[b], 0, NULL);
}
/* Start recording */
status = AudioQueueStart(queue, NULL);
if (status != noErr) {
fprintf(stderr, "AudioQueueStart failed: %d\n", (int)status);
exit(10);
}
fprintf(stdout, "Core Audio: recording at %u Hz\n", sample_rate);
/* Main loop: process audio from callback */
for (;;) {
/* Run the run loop to process callbacks */
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
if (ca_state.data_ready > 0) {
i = ca_state.data_ready;
sp = buffer;
ca_state.data_ready = 0;
if (integer_only) {
fbuf_cnt = i;
} else {
for (; i > 0; i--, sp++)
fbuf[fbuf_cnt++] = (*sp) * (1.0f / 32768.0f);
}
if (fbuf_cnt > overlap) {
process_buffer(fbuf, buffer, fbuf_cnt - overlap);
memmove(fbuf, fbuf + fbuf_cnt - overlap, overlap * sizeof(fbuf[0]));
fbuf_cnt = overlap;
}
}
}
/* Cleanup (unreachable in normal operation, but good practice) */
ca_state.running = 0;
AudioQueueStop(queue, true);
AudioQueueDispose(queue, true);
}
#ifdef HAS_PROCESSTAP
/* System audio capture using Core Audio ProcessTap (macOS 14.2+) */
static unsigned int sck_overlap;
static float sck_fbuf[16384];
static short sck_sbuf[8192];
static unsigned int sck_fbuf_cnt = 0;
static void sck_audio_callback(short *samples, int count)
{
if (count <= 0)
return;
/* Copy samples to our buffer */
if (count > 8192)
count = 8192;
memcpy(sck_sbuf, samples, count * sizeof(short));
if (integer_only) {
sck_fbuf_cnt = count;
} else {
for (int i = 0; i < count; i++)
sck_fbuf[sck_fbuf_cnt++] = samples[i] * (1.0f / 32768.0f);
}
if (sck_fbuf_cnt > sck_overlap) {
process_buffer(sck_fbuf, sck_sbuf, sck_fbuf_cnt - sck_overlap);
memmove(sck_fbuf, sck_fbuf + sck_fbuf_cnt - sck_overlap, sck_overlap * sizeof(sck_fbuf[0]));
sck_fbuf_cnt = sck_overlap;
}
}
static void input_system_audio(unsigned int sample_rate, unsigned int overlap)
{
sck_overlap = overlap;
sck_fbuf_cnt = 0;
if (!macos_screencapture_available()) {
fprintf(stderr, "System audio capture requires macOS 14.2 or later\n");
exit(10);
}
if (macos_start_system_audio(sample_rate, sck_audio_callback) != 0) {
fprintf(stderr, "Failed to start system audio capture\n");
fprintf(stderr, "Make sure to grant screen recording permission in System Preferences\n");
exit(10);
}
/* Keep running - the callback handles audio processing */
for (;;) {
macos_audio_run_loop(0.1);
}
macos_stop_system_audio();
}
#endif /* HAS_PROCESSTAP */
#elif WIN32_AUDIO
//Implemented in win32_soundin.c
void input_sound(unsigned int sample_rate, unsigned int overlap, const char *ifname);
#elif PULSE_AUDIO
static void input_sound(unsigned int sample_rate, unsigned int overlap,
const char *ifname)
{
short buffer[8192];
float fbuf[16384];
unsigned int fbuf_cnt = 0;
int i;
int error;
short *sp;
(void) ifname; // Suppress the warning.
// Init stuff from pa.org
pa_simple *s;
pa_sample_spec ss;
ss.format = PA_SAMPLE_S16NE;
ss.channels = 1;
ss.rate = sample_rate;
/* Create the recording stream */
if (!(s = pa_simple_new(NULL, "multimon-ng", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, "unixinput.c: pa_simple_new() failed: %s\n", pa_strerror(error));
exit(4);
}
for (;;) {
i = pa_simple_read(s, sp = buffer, sizeof(buffer), &error);
if (i < 0 && errno != EAGAIN) {
perror("read");
fprintf(stderr, "error 1\n");
exit(4);
}
i=sizeof(buffer);
if (!i)
break;
if (i > 0) {
if(integer_only)
{
fbuf_cnt = i/sizeof(buffer[0]);
}
else
{
for (; (unsigned int) i >= sizeof(buffer[0]); i -= sizeof(buffer[0]), sp++)
fbuf[fbuf_cnt++] = (*sp) * (1.0/32768.0);
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
}
if (fbuf_cnt > overlap) {
process_buffer(fbuf, buffer, fbuf_cnt-overlap);
memmove(fbuf, fbuf+fbuf_cnt-overlap, overlap*sizeof(fbuf[0]));
fbuf_cnt = overlap;
}
}
}
pa_simple_free(s);
}
#else /* SUN_AUDIO */
/* ---------------------------------------------------------------------- */
static void input_sound(unsigned int sample_rate, unsigned int overlap,
const char *ifname)
{
int sndparam;
int fd;
union {
short s[8192];
unsigned char b[8192];
} b;
float fbuf[16384];
unsigned int fbuf_cnt = 0;
int i;
short *sp;
unsigned char *bp;
int fmt = 0;
if ((fd = open(ifname ? ifname : "/dev/dsp", O_RDONLY)) < 0) {
perror("open");
exit (10);
}
sndparam = AFMT_S16_NE; /* we want 16 bits/sample signed */
if (ioctl(fd, SNDCTL_DSP_SETFMT, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit (10);
}
if (sndparam != AFMT_S16_NE) {
fmt = 1;
sndparam = AFMT_U8;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit (10);
}
if (sndparam != AFMT_U8) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit (10);
}
}
sndparam = 0; /* we want only 1 channel */
if (ioctl(fd, SNDCTL_DSP_STEREO, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_STEREO");
exit (10);
}
if (sndparam != 0) {
fprintf(stderr, "soundif: Error, cannot set the channel "
"number to 1\n");
exit (10);
}
sndparam = sample_rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SPEED");
exit (10);
}
if ((10*abs(sndparam-sample_rate)) > sample_rate) {
perror("ioctl: SNDCTL_DSP_SPEED");
exit (10);
}
if (sndparam != sample_rate) {
fprintf(stderr, "Warning: Sampling rate is %u, "
"requested %u\n", sndparam, sample_rate);
}
#if 0
sndparam = 4;
if (ioctl(fd, SOUND_PCM_SUBDIVIDE, &sndparam) == -1) {
perror("ioctl: SOUND_PCM_SUBDIVIDE");
}
if (sndparam != 4) {
perror("ioctl: SOUND_PCM_SUBDIVIDE");
}
#endif
for (;;) {
if (fmt) {
perror("ioctl: 8BIT SAMPLES NOT SUPPORTED!");
exit (10);
// i = read(fd, bp = b.b, sizeof(b.b));
// if (i < 0 && errno != EAGAIN) {
// perror("read");
// exit(4);
// }
// if (!i)
// break;
// if (i > 0) {
// for (; i >= sizeof(b.b[0]); i -= sizeof(b.b[0]), sp++)
// fbuf[fbuf_cnt++] = ((int)(*bp)-0x80) * (1.0/128.0);
// if (i)
// fprintf(stderr, "warning: noninteger number of samples read\n");
// if (fbuf_cnt > overlap) {
// process_buffer(fbuf, fbuf_cnt-overlap);
// memmove(fbuf, fbuf+fbuf_cnt-overlap, overlap*sizeof(fbuf[0]));
// fbuf_cnt = overlap;
// }
// }
} else {
i = read(fd, sp = b.s, sizeof(b.s));
if (i < 0 && errno != EAGAIN) {
perror("read");
exit(4);
}
if (!i)
break;
if (i > 0) {
if(integer_only)
{
fbuf_cnt = i/sizeof(b.s[0]);
}
else
{
for (; i >= sizeof(b.s[0]); i -= sizeof(b.s[0]), sp++)
fbuf[fbuf_cnt++] = (*sp) * (1.0/32768.0);
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
}
if (fbuf_cnt > overlap) {
process_buffer(fbuf, b.s, fbuf_cnt-overlap);
memmove(fbuf, fbuf+fbuf_cnt-overlap, overlap*sizeof(fbuf[0]));
fbuf_cnt = overlap;
}
}
}
}
close(fd);
}
#endif /* SUN_AUDIO */
/* ---------------------------------------------------------------------- */
static void input_file(unsigned int sample_rate, unsigned int overlap,
const char *fname, const char *type)
{
struct stat statbuf;
int pipedes[2];
int pid = 0, soxstat;
int fd;
int i;
short buffer[8192];
float fbuf[16384];
unsigned int fbuf_cnt = 0;
short *sp;
/*
* if the input type is not raw, sox is started to convert the
* samples to the requested format
*/
if (!strcmp(fname, "-"))
{
// read from stdin and force raw input
fd = 0;
type = "raw";
#ifdef WINDOWS
setmode(fd, O_BINARY);
#endif
}
else if (!type || !strcmp(type, "raw")) {
#ifdef WINDOWS
if ((fd = open(fname, O_RDONLY | O_BINARY)) < 0) {
#else
if ((fd = open(fname, O_RDONLY)) < 0) {
#endif
perror("open");
exit(10);
}
}
#ifndef ONLY_RAW
else {
if (stat(fname, &statbuf)) {
perror("stat");
exit(10);
}
if (pipe(pipedes)) {
perror("pipe");
exit(10);
}
if (!(pid = fork())) {
char srate[8];
/*
* child starts here... first set up filedescriptors,
* then start sox...
*/
sprintf(srate, "%d", sample_rate);
close(pipedes[0]); /* close reading pipe end */
close(1); /* close standard output */
if (dup2(pipedes[1], 1) < 0)
perror("dup2");
close(pipedes[1]); /* close writing pipe end */
execlp("sox", "sox", repeatable_sox?"-R":"-V2", mute_sox?"-V1":"-V2",
"--ignore-length",
"-t", type, fname,
"-t", "raw", "-esigned-integer", "-b16", "-r", srate, "-", "remix", "1",
NULL);
perror("execlp");
exit(10);
}
if (pid < 0) {
perror("fork");
exit(10);
}
close(pipedes[1]); /* close writing pipe end */
fd = pipedes[0];
}
#endif
/*
* demodulate
*/
for (;;) {
i = read(fd, sp = buffer, sizeof(buffer));
if (i < 0 && errno != EAGAIN) {
perror("read");
exit(4);
}
if (!i)
break;
if (i > 0) {
if(integer_only)
{
fbuf_cnt = i/sizeof(buffer[0]);
}
else
{
for (; (unsigned int) i >= sizeof(buffer[0]); i -= sizeof(buffer[0]), sp++)
fbuf[fbuf_cnt++] = (*sp) * (1.0f/32768.0f);
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
}
if (fbuf_cnt > overlap) {
process_buffer(fbuf, buffer, fbuf_cnt-overlap);
memmove(fbuf, fbuf+fbuf_cnt-overlap, overlap*sizeof(fbuf[0]));
fbuf_cnt = overlap;
}
}
}
close(fd);
#ifndef ONLY_RAW
waitpid(pid, &soxstat, 0);
#endif
}
void quit(void)
{
int i = 0;
for (i = 0; (unsigned int) i < NUMDEMOD; i++)
{
if(MASK_ISSET(i))
if (dem[i]->deinit)
dem[i]->deinit(dem_st+i);
}
}
/* ---------------------------------------------------------------------- */
static const char usage_str[] = "\n"
"Usage: %s [file] [file] [file] ...\n"
" If no [file] is given, input will be read from your default sound\n"
" hardware. A filename of \"-\" denotes standard input.\n"
" -t <type> : Input file type (auto-detected from extension if not specified)\n"
" Types other than raw require sox. Supported: hw (hardware input),\n"
#ifdef HAS_PROCESSTAP
" system (capture system audio output, macOS 14.2+),\n"
#endif
" raw, wav, flac, mp3, ogg, aiff, au, etc.\n"
" -a <demod> : Add demodulator\n"
" -s <demod> : Subtract demodulator\n"
" -c : Remove all demodulators (must be added with -a <demod>)\n"
" -q : Quiet\n"
" -v <level> : Level of verbosity (e.g. '-v 3')\n"
" For POCSAG and MORSE_CW '-v1' prints decoding statistics.\n"
" -h : This help\n"
" -A : APRS mode (TNC2 text output)\n"
" -m : Mute SoX warnings\n"
" -r : (Deprecated) Repeatable mode is now always enabled.\n"
" -n : Don't flush stdout, increases performance.\n"
" -j : FMS: Just output hex data and CRC, no parsing.\n"
" -e : POCSAG: Hide empty messages.\n"
" -u : POCSAG: Heuristically prune unlikely decodes.\n"
" -i : POCSAG: (Deprecated) Polarity is now auto-detected.\n"
" -p : POCSAG: Show partially received messages.\n"
" -P <mode> : POCSAG: Polarity (auto/normal/inverted, default: auto).\n"
" -f <mode> : POCSAG: Overrides standards and forces decoding of data as <mode>\n"
" (<mode> can be 'numeric', 'alpha', 'skyper' or 'auto')\n"
" -b <level> : POCSAG: BCH bit error correction level. Set 0 to disable, default is 2.\n"
" Lower levels increase performance and lower false positives.\n"
" -C <cs> : POCSAG: Set Charset.\n"
" -o : CW: Set threshold for dit detection (default: 500)\n"
" -d : CW: Dit length in ms (default: 50)\n"
" -g : CW: Gap length in ms (default: 50)\n"
" -x : CW: Disable auto threshold detection\n"
" -y : CW: Disable auto timing detection\n"
" --timestamp : Add a time stamp in front of every printed line\n"
" --iso8601 : Use UTC timestamp in ISO 8601 format that includes microseconds\n"
" --label : Add a label to the front of every printed line\n"
" --flex-no-ts : FLEX: Do not add a timestamp to the FLEX demodulator output\n"
" --json : Format output as JSON. Supported by the following demodulators:\n"
" DTMF, EAS, FLEX, POCSAG. (Other demodulators will silently ignore this flag.)\n"
"\n"
" Raw input requires one channel, 16 bit, signed integer (platform-native)\n"
" samples at the demodulator's input sampling rate, which is\n"
" usually 22050 Hz. Raw input is assumed and required if piped input is used.\n";
int main(int argc, char *argv[])
{
int c;
int errflg = 0;
int quietflg = 0;
int i;
char **itype;
int mask_first = 1;
int sample_rate = -1;
unsigned int overlap = 0;
#ifdef HAS_PROCESSTAP
char *input_type = "system"; /* Default to system audio capture on macOS */
#else
char *input_type = "hw";