2023-05-16 21:44:25 +00:00
|
|
|
/* spkmodem-recv.c - decode spkmodem signals */
|
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
2023-05-16 10:31:49 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2023-05-16 21:44:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-05-16 13:53:45 +00:00
|
|
|
#include <unistd.h>
|
2023-05-16 21:44:25 +00:00
|
|
|
|
2023-05-16 06:53:23 +00:00
|
|
|
/* Compilation: gcc -o spkmodem-recv spkmodem-recv */
|
2023-05-16 21:44:25 +00:00
|
|
|
/* Usage: parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */
|
|
|
|
|
2023-05-16 07:23:19 +00:00
|
|
|
#define SAMPLES_PER_FRAME 240
|
2023-05-16 21:44:25 +00:00
|
|
|
#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
|
|
|
|
|
2023-05-16 07:42:07 +00:00
|
|
|
signed short frame[2 * SAMPLES_PER_FRAME];
|
2023-05-16 13:11:28 +00:00
|
|
|
signed short pulse[2 * SAMPLES_PER_FRAME];
|
2023-05-16 13:23:03 +00:00
|
|
|
int f1, f2, lp, ascii_bit = 7;
|
2023-05-16 10:57:53 +00:00
|
|
|
char ascii = 0;
|
2023-05-16 21:44:25 +00:00
|
|
|
|
2023-05-16 10:53:23 +00:00
|
|
|
void handle_audio(void);
|
2023-05-16 10:57:53 +00:00
|
|
|
void print_char(void);
|
2023-05-16 10:53:51 +00:00
|
|
|
void fetch_sample(void);
|
2023-05-16 21:44:25 +00:00
|
|
|
|
|
|
|
int
|
2023-05-16 07:30:24 +00:00
|
|
|
main(int argc, char *argv[])
|
2023-05-16 21:44:25 +00:00
|
|
|
{
|
2023-05-16 13:53:45 +00:00
|
|
|
int c;
|
|
|
|
|
2023-05-30 15:02:25 +00:00
|
|
|
#ifdef __OpenBSD__
|
2023-05-16 14:09:33 +00:00
|
|
|
if (pledge("stdio", NULL) == -1)
|
|
|
|
err(errno, "pledge");
|
|
|
|
#endif
|
|
|
|
|
2023-05-16 13:53:45 +00:00
|
|
|
while ((c = getopt(argc, argv, "u")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'u':
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err(errno = EINVAL, NULL);
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 07:05:23 +00:00
|
|
|
|
2023-05-16 09:50:35 +00:00
|
|
|
while (!feof(stdin))
|
2023-05-16 10:53:23 +00:00
|
|
|
handle_audio();
|
2023-05-16 09:50:35 +00:00
|
|
|
|
2023-05-16 10:37:44 +00:00
|
|
|
return errno;
|
2023-05-16 09:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-16 10:53:23 +00:00
|
|
|
handle_audio(void)
|
2023-05-16 09:50:35 +00:00
|
|
|
{
|
|
|
|
static int llp = 0;
|
2023-05-16 06:53:23 +00:00
|
|
|
|
2023-05-16 12:42:35 +00:00
|
|
|
if (lp > (3 * SAMPLES_PER_FRAME)) {
|
2023-05-16 09:50:35 +00:00
|
|
|
ascii_bit = 7;
|
|
|
|
ascii = 0;
|
|
|
|
lp = 0;
|
|
|
|
llp++;
|
|
|
|
}
|
|
|
|
if (llp == FLUSH_TIMEOUT)
|
2023-05-16 10:31:49 +00:00
|
|
|
if (fflush(stdout) == EOF)
|
|
|
|
err(errno, NULL);
|
2023-05-16 09:50:35 +00:00
|
|
|
|
2023-05-16 12:42:35 +00:00
|
|
|
if ((f2 <= FREQ_SEP_MIN) || (f2 >= FREQ_SEP_MAX)
|
|
|
|
|| (f1 <= FREQ_DATA_MIN) || (f1 >= FREQ_DATA_MAX)) {
|
2023-05-16 10:53:23 +00:00
|
|
|
fetch_sample();
|
2023-05-16 09:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-16 10:57:53 +00:00
|
|
|
|
|
|
|
print_char();
|
|
|
|
|
|
|
|
lp = 0;
|
|
|
|
llp = 0;
|
|
|
|
for (int i = 0; i < SAMPLES_PER_FRAME; i++)
|
|
|
|
fetch_sample();
|
|
|
|
}
|
|
|
|
|
2023-05-16 07:42:07 +00:00
|
|
|
void
|
2023-05-16 10:53:23 +00:00
|
|
|
fetch_sample(void)
|
2023-05-16 07:17:29 +00:00
|
|
|
{
|
2023-05-16 12:33:00 +00:00
|
|
|
static int ringpos = 0;
|
|
|
|
|
2023-05-16 07:17:29 +00:00
|
|
|
f1 -= pulse[ringpos];
|
2023-05-16 07:23:19 +00:00
|
|
|
f1 += pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];
|
|
|
|
f2 -= pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];
|
2023-05-16 10:31:49 +00:00
|
|
|
if (fread(frame + ringpos, 1, sizeof(frame[0]), stdin)
|
|
|
|
!= sizeof(frame[0]))
|
|
|
|
err(errno = ECANCELED, "Could not read frame.");
|
2023-05-16 07:17:29 +00:00
|
|
|
|
2023-05-16 07:23:19 +00:00
|
|
|
if (abs(frame[ringpos]) > THRESHOLD) { /* rising/falling edge(pulse) */
|
2023-05-16 07:17:29 +00:00
|
|
|
pulse[ringpos] = 1;
|
|
|
|
f2++;
|
|
|
|
} else {
|
|
|
|
pulse[ringpos] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ringpos++;
|
2023-05-16 07:23:19 +00:00
|
|
|
ringpos %= 2 * SAMPLES_PER_FRAME;
|
2023-05-16 07:17:29 +00:00
|
|
|
lp++;
|
|
|
|
}
|
2023-05-16 12:22:09 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
print_char(void)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
long stdin_pos = 0;
|
|
|
|
if ((stdin_pos = ftell(stdin)) == -1)
|
|
|
|
err(errno, 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);
|
|
|
|
ascii_bit--;
|
|
|
|
if (ascii_bit < 0) {
|
|
|
|
#if DEBUG
|
|
|
|
printf("<%c, %x>", ascii, ascii);
|
|
|
|
#else
|
|
|
|
printf("%c", ascii);
|
|
|
|
#endif
|
|
|
|
ascii_bit = 7;
|
|
|
|
ascii = 0;
|
|
|
|
}
|
|
|
|
}
|