2023-09-25 01:49:34 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/* SPDX-FileCopyrightText: 2013 Free Software Foundation, Inc. */
|
|
|
|
/* Usage: parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */
|
2023-05-16 21:44:25 +00:00
|
|
|
|
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 07:23:19 +00:00
|
|
|
#define SAMPLES_PER_FRAME 240
|
2023-06-13 19:47:17 +00:00
|
|
|
#define MAX_SAMPLES (2 * SAMPLES_PER_FRAME)
|
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
|
|
|
|
|
2023-06-04 14:12:14 +00:00
|
|
|
#define ERR() (errno = errno ? errno : ECANCELED)
|
2023-06-05 01:05:36 +00:00
|
|
|
#define reset_char() ascii = 0, ascii_bit = 7
|
2023-05-16 21:44:25 +00:00
|
|
|
|
2023-06-13 19:47:17 +00:00
|
|
|
signed short frame[MAX_SAMPLES], pulse[MAX_SAMPLES];
|
2023-06-05 14:38:36 +00:00
|
|
|
int ringpos, debug, freq_data, freq_separator, sample_count, 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-06-14 07:43:54 +00:00
|
|
|
void decode_pulse(void);
|
2023-06-04 16:16:26 +00:00
|
|
|
int set_ascii_bit(void);
|
2023-06-04 14:38:03 +00:00
|
|
|
void print_char(void);
|
2023-06-05 00:57:20 +00:00
|
|
|
void print_stats(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)
|
2023-06-04 14:23:51 +00:00
|
|
|
err(ERR(), "pledge");
|
2023-05-16 14:09:33 +00:00
|
|
|
#endif
|
2023-06-13 20:01:15 +00:00
|
|
|
while ((c = getopt(argc, argv, "d")) != -1)
|
|
|
|
if (!(debug = (c == 'd')))
|
2023-06-05 00:36:06 +00:00
|
|
|
err(errno = EINVAL, NULL);
|
2023-06-05 00:05:38 +00:00
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
2023-05-16 09:50:35 +00:00
|
|
|
while (!feof(stdin))
|
2023-05-16 10:53:23 +00:00
|
|
|
handle_audio();
|
2023-06-05 00:53:24 +00:00
|
|
|
if (errno && debug)
|
|
|
|
err(errno, "Unhandled error, errno %d", errno);
|
|
|
|
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
|
|
|
{
|
2023-06-05 01:05:36 +00:00
|
|
|
if (sample_count > (3 * SAMPLES_PER_FRAME))
|
|
|
|
sample_count = reset_char();
|
2023-06-04 23:34:44 +00:00
|
|
|
if ((freq_separator <= FREQ_SEP_MIN) || (freq_separator >= FREQ_SEP_MAX)
|
|
|
|
|| (freq_data <= FREQ_DATA_MIN) || (freq_data >= FREQ_DATA_MAX)) {
|
2023-06-14 07:43:54 +00:00
|
|
|
decode_pulse();
|
2023-05-16 09:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-05 00:05:38 +00:00
|
|
|
|
2023-06-08 11:32:17 +00:00
|
|
|
if (set_ascii_bit() < 0)
|
2023-06-04 16:16:26 +00:00
|
|
|
print_char();
|
2023-06-05 00:05:38 +00:00
|
|
|
sample_count = 0;
|
2023-06-04 14:23:51 +00:00
|
|
|
for (int sample = 0; sample < SAMPLES_PER_FRAME; sample++)
|
2023-06-14 07:43:54 +00:00
|
|
|
decode_pulse();
|
2023-05-16 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 07:42:07 +00:00
|
|
|
void
|
2023-06-14 07:43:54 +00:00
|
|
|
decode_pulse(void)
|
2023-05-16 07:17:29 +00:00
|
|
|
{
|
2023-06-13 19:47:17 +00:00
|
|
|
int next_ringpos = (ringpos + SAMPLES_PER_FRAME) % MAX_SAMPLES;
|
2023-06-04 23:34:44 +00:00
|
|
|
freq_data -= pulse[ringpos];
|
2023-06-13 19:47:17 +00:00
|
|
|
freq_data += pulse[next_ringpos];
|
|
|
|
freq_separator -= pulse[next_ringpos];
|
2023-05-16 07:17:29 +00:00
|
|
|
|
2023-06-13 21:50:27 +00:00
|
|
|
fread(frame + ringpos, 1, sizeof(frame[0]), stdin);
|
|
|
|
if (ferror(stdin) != 0)
|
|
|
|
err(ERR(), "Could not read from frame.");
|
|
|
|
|
2023-06-04 14:53:00 +00:00
|
|
|
if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0))
|
2023-06-04 23:34:44 +00:00
|
|
|
++freq_separator;
|
2023-06-04 14:31:23 +00:00
|
|
|
++ringpos;
|
2023-06-13 19:47:17 +00:00
|
|
|
ringpos %= MAX_SAMPLES;
|
2023-06-04 23:34:44 +00:00
|
|
|
++sample_count;
|
2023-05-16 07:17:29 +00:00
|
|
|
}
|
2023-05-16 12:22:09 +00:00
|
|
|
|
2023-06-04 16:16:26 +00:00
|
|
|
int
|
|
|
|
set_ascii_bit(void)
|
2023-05-16 12:22:09 +00:00
|
|
|
{
|
2023-06-05 00:57:20 +00:00
|
|
|
if (debug)
|
|
|
|
print_stats();
|
2023-06-04 23:34:44 +00:00
|
|
|
if (freq_data < FREQ_DATA_THRESHOLD)
|
2023-05-16 12:22:09 +00:00
|
|
|
ascii |= (1 << ascii_bit);
|
2023-06-08 11:32:17 +00:00
|
|
|
--ascii_bit;
|
2023-06-04 16:16:26 +00:00
|
|
|
return ascii_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_char(void)
|
|
|
|
{
|
2023-06-05 00:36:06 +00:00
|
|
|
if (debug)
|
|
|
|
printf("<%c, %x>", ascii, ascii);
|
|
|
|
else
|
|
|
|
printf("%c", ascii);
|
2023-06-05 01:05:36 +00:00
|
|
|
reset_char();
|
2023-05-16 12:22:09 +00:00
|
|
|
}
|
2023-06-05 00:57:20 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
print_stats(void)
|
|
|
|
{
|
2023-06-13 21:29:34 +00:00
|
|
|
long stdin_pos;
|
2023-06-05 00:57:20 +00:00
|
|
|
if ((stdin_pos = ftell(stdin)) == -1)
|
|
|
|
err(ERR(), NULL);
|
|
|
|
printf ("%d %d %d @%ld\n", freq_data, freq_separator,
|
|
|
|
FREQ_DATA_THRESHOLD, stdin_pos - sizeof(frame));
|
|
|
|
}
|