summaryrefslogtreecommitdiff
path: root/util/spkmodem_recv/spkmodem-recv.c
blob: f2737b15429e2f8a41165dba3ecd68aea56ea6ba (plain)
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
/* spkmodem-recv.c - decode spkmodem signals */
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Compilation:	gcc -o spkmodem-recv spkmodem-recv  */
/* Usage: parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */

#define SAMPLES_PER_FRAME 240
#define FREQ_SEP_MIN 5
#define FREQ_SEP_MAX 15
#define FREQ_DATA_MIN 15
#define FREQ_DATA_THRESHOLD 25
#define FREQ_DATA_MAX 60
#define THRESHOLD 500

#define DEBUG 0
#define FLUSH_TIMEOUT 1
#define ERR() (errno = errno ? errno : ECANCELED)

signed short frame[2 * SAMPLES_PER_FRAME], pulse[2 * SAMPLES_PER_FRAME];
int f1, f2, lp, ascii_bit = 7;
char ascii = 0;

void handle_audio(void);
void fetch_sample(void);
void read_frame(int ringpos);
void print_char(void);

int
main(int argc, char *argv[])
{
	int c;
#ifdef __OpenBSD__
	if (pledge("stdio", NULL) == -1)
		err(ERR(), "pledge");
#endif
	while ((c = getopt(argc, argv, "u")) != -1) {
		if (c != 'u')
			err(errno = EINVAL, NULL);
		setvbuf(stdout, NULL, _IONBF, 0);
	}
	while (!feof(stdin))
		handle_audio();
	if (errno)
		err(errno, "Unhandled error upon exit. Exit status is errno.");
	return 0;
}

void
handle_audio(void)
{
	static int llp = 0;
	if (lp > (3 * SAMPLES_PER_FRAME)) {
		ascii_bit = 7;
		ascii = lp = 0;
		++llp;
	}
	if (llp == FLUSH_TIMEOUT)
		if (fflush(stdout) == EOF)
			err(ERR(), NULL);
	if ((f2 <= FREQ_SEP_MIN) || (f2 >= FREQ_SEP_MAX)
	    || (f1 <= FREQ_DATA_MIN) || (f1 >= FREQ_DATA_MAX)) {
		fetch_sample();
		return;
	}
	print_char();

	lp = llp = 0;
	for (int sample = 0; sample < SAMPLES_PER_FRAME; sample++)
		fetch_sample();
}

void
fetch_sample(void)
{
	static int ringpos = 0;
	f1 -= pulse[ringpos];
	f1 += pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];
	f2 -= pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];

	read_frame(ringpos);
	if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0))
		++f2;
	++ringpos;
	ringpos %= 2 * SAMPLES_PER_FRAME;
	++lp;
}

void
read_frame(int ringpos)
{
	if ((fread(frame + ringpos, 1, sizeof(frame[0]), stdin)
	    != sizeof(frame[0])) || (ferror(stdin) != 0))
		err(ERR(), "Could not read from frame.");
}

void
print_char(void)
{
#if DEBUG
	long stdin_pos = 0;
	if ((stdin_pos = ftell(stdin)) == -1)
		err(ERR(), NULL);
	printf ("%d %d %d @%ld\n", f1, f2, FREQ_DATA_THRESHOLD,
	    stdin_pos - sizeof(frame));
#endif
	if (f1 < FREQ_DATA_THRESHOLD)
		ascii |= (1 << ascii_bit);
	if (!ascii_bit) {
#if DEBUG
		printf("<%c, %x>", ascii, ascii);
#else
		printf("%c", ascii);
#endif
		ascii_bit = 7;
		ascii = 0;
	}
}