summaryrefslogtreecommitdiff
path: root/util/spkmodem_recv/spkmodem-recv.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/spkmodem_recv/spkmodem-recv.c')
-rw-r--r--util/spkmodem_recv/spkmodem-recv.c138
1 files changed, 66 insertions, 72 deletions
diff --git a/util/spkmodem_recv/spkmodem-recv.c b/util/spkmodem_recv/spkmodem-recv.c
index 30e13d8a..7de911ec 100644
--- a/util/spkmodem_recv/spkmodem-recv.c
+++ b/util/spkmodem_recv/spkmodem-recv.c
@@ -19,117 +19,111 @@
#define FREQ_DATA_MAX 60
#define THRESHOLD 500
-#define DEBUG 0
-#define FLUSH_TIMEOUT 1
+#define ERR() (errno = errno ? errno : ECANCELED)
+#define reset_char() ascii = 0, ascii_bit = 7
-signed short frame[2 * SAMPLES_PER_FRAME];
-signed short pulse[2 * SAMPLES_PER_FRAME];
-int f1, f2, lp, ascii_bit = 7;
+signed short frame[2 * SAMPLES_PER_FRAME], pulse[2 * SAMPLES_PER_FRAME];
+int ringpos, debug, freq_data, freq_separator, sample_count, ascii_bit = 7;
char ascii = 0;
void handle_audio(void);
-void print_char(void);
void fetch_sample(void);
+void read_frame(void);
+int set_ascii_bit(void);
+void print_char(void);
+void print_stats(void);
int
main(int argc, char *argv[])
{
int c;
-
-#ifdef HAVE_PLEDGE
+#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1)
- err(errno, "pledge");
+ err(ERR(), "pledge");
#endif
-
- while ((c = getopt(argc, argv, "u")) != -1) {
- switch (c) {
- case 'u':
- setvbuf(stdout, NULL, _IONBF, 0);
- break;
- default:
+ while ((c = getopt(argc, argv, "d")) != -1) {
+ if (c != 'd')
err(errno = EINVAL, NULL);
- }
+ debug = 1;
}
-
+ setvbuf(stdout, NULL, _IONBF, 0);
while (!feof(stdin))
handle_audio();
-
+ if (errno && debug)
+ err(errno, "Unhandled error, errno %d", errno);
return errno;
}
void
handle_audio(void)
{
- static int llp = 0;
-
- if (lp > (3 * SAMPLES_PER_FRAME)) {
- ascii_bit = 7;
- ascii = 0;
- lp = 0;
- llp++;
- }
- if (llp == FLUSH_TIMEOUT)
- if (fflush(stdout) == EOF)
- err(errno, NULL);
-
- if ((f2 <= FREQ_SEP_MIN) || (f2 >= FREQ_SEP_MAX)
- || (f1 <= FREQ_DATA_MIN) || (f1 >= FREQ_DATA_MAX)) {
+ if (sample_count > (3 * SAMPLES_PER_FRAME))
+ sample_count = reset_char();
+ if ((freq_separator <= FREQ_SEP_MIN) || (freq_separator >= FREQ_SEP_MAX)
+ || (freq_data <= FREQ_DATA_MIN) || (freq_data >= FREQ_DATA_MAX)) {
fetch_sample();
return;
}
- print_char();
-
- lp = 0;
- llp = 0;
- for (int i = 0; i < SAMPLES_PER_FRAME; i++)
+ if (set_ascii_bit() < 0)
+ print_char();
+ sample_count = 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)];
- if (fread(frame + ringpos, 1, sizeof(frame[0]), stdin)
- != sizeof(frame[0]))
- err(errno = ECANCELED, "Could not read frame.");
+ freq_data -= pulse[ringpos];
+ freq_data += pulse[(ringpos + SAMPLES_PER_FRAME)
+ % (2 * SAMPLES_PER_FRAME)];
+ freq_separator -= pulse[(ringpos + SAMPLES_PER_FRAME)
+ % (2 * SAMPLES_PER_FRAME)];
+
+ read_frame();
+ if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0))
+ ++freq_separator;
+ ++ringpos;
+ ringpos %= 2 * SAMPLES_PER_FRAME;
+ ++sample_count;
+}
- if (abs(frame[ringpos]) > THRESHOLD) { /* rising/falling edge(pulse) */
- pulse[ringpos] = 1;
- f2++;
- } else {
- pulse[ringpos] = 0;
- }
+void
+read_frame(void)
+{
+ if ((fread(frame + ringpos, 1, sizeof(frame[0]), stdin)
+ != sizeof(frame[0])) || (ferror(stdin) != 0))
+ err(ERR(), "Could not read from frame.");
+}
- ringpos++;
- ringpos %= 2 * SAMPLES_PER_FRAME;
- lp++;
+int
+set_ascii_bit(void)
+{
+ if (debug)
+ print_stats();
+ if (freq_data < FREQ_DATA_THRESHOLD)
+ ascii |= (1 << ascii_bit);
+ --ascii_bit;
+ return ascii_bit;
}
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
+ if (debug)
printf("<%c, %x>", ascii, ascii);
-#else
+ else
printf("%c", ascii);
-#endif
- ascii_bit = 7;
- ascii = 0;
- }
+ reset_char();
+}
+
+void
+print_stats(void)
+{
+ long stdin_pos = 0;
+ 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));
}