util/spkmodem-recv: squash a few code lines

Signed-off-by: Leah Rowe <leah@libreboot.org>
fsdg20230625
Leah Rowe 2023-06-04 15:53:00 +01:00
parent 3401f287b4
commit b21c1dd5e8
1 changed files with 8 additions and 25 deletions

View File

@ -23,8 +23,7 @@
#define FLUSH_TIMEOUT 1 #define FLUSH_TIMEOUT 1
#define ERR() (errno = errno ? errno : ECANCELED) #define ERR() (errno = errno ? errno : ECANCELED)
signed short frame[2 * SAMPLES_PER_FRAME]; signed short frame[2 * SAMPLES_PER_FRAME], pulse[2 * SAMPLES_PER_FRAME];
signed short pulse[2 * SAMPLES_PER_FRAME];
int f1, f2, lp, ascii_bit = 7; int f1, f2, lp, ascii_bit = 7;
char ascii = 0; char ascii = 0;
@ -37,25 +36,17 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c; int c;
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) if (pledge("stdio", NULL) == -1)
err(ERR(), "pledge"); err(ERR(), "pledge");
#endif #endif
while ((c = getopt(argc, argv, "u")) != -1) { while ((c = getopt(argc, argv, "u")) != -1) {
switch (c) { if (c != 'u')
case 'u':
setvbuf(stdout, NULL, _IONBF, 0);
break;
default:
err(errno = EINVAL, NULL); err(errno = EINVAL, NULL);
} setvbuf(stdout, NULL, _IONBF, 0);
} }
while (!feof(stdin)) while (!feof(stdin))
handle_audio(); handle_audio();
if (errno) if (errno)
err(errno, "Unhandled error upon exit. Exit status is errno."); err(errno, "Unhandled error upon exit. Exit status is errno.");
return 0; return 0;
@ -65,7 +56,6 @@ void
handle_audio(void) handle_audio(void)
{ {
static int llp = 0; static int llp = 0;
if (lp > (3 * SAMPLES_PER_FRAME)) { if (lp > (3 * SAMPLES_PER_FRAME)) {
ascii_bit = 7; ascii_bit = 7;
ascii = lp = 0; ascii = lp = 0;
@ -74,13 +64,11 @@ handle_audio(void)
if (llp == FLUSH_TIMEOUT) if (llp == FLUSH_TIMEOUT)
if (fflush(stdout) == EOF) if (fflush(stdout) == EOF)
err(ERR(), NULL); err(ERR(), NULL);
if ((f2 <= FREQ_SEP_MIN) || (f2 >= FREQ_SEP_MAX) if ((f2 <= FREQ_SEP_MIN) || (f2 >= FREQ_SEP_MAX)
|| (f1 <= FREQ_DATA_MIN) || (f1 >= FREQ_DATA_MAX)) { || (f1 <= FREQ_DATA_MIN) || (f1 >= FREQ_DATA_MAX)) {
fetch_sample(); fetch_sample();
return; return;
} }
print_char(); print_char();
lp = llp = 0; lp = llp = 0;
@ -92,14 +80,12 @@ void
fetch_sample(void) fetch_sample(void)
{ {
static int ringpos = 0; static int ringpos = 0;
f1 -= pulse[ringpos]; f1 -= pulse[ringpos];
f1 += pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)]; f1 += pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];
f2 -= pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)]; f2 -= pulse[(ringpos + SAMPLES_PER_FRAME) % (2 * SAMPLES_PER_FRAME)];
read_frame(ringpos);
pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0; read_frame(ringpos);
if (pulse[ringpos]) if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0))
++f2; ++f2;
++ringpos; ++ringpos;
ringpos %= 2 * SAMPLES_PER_FRAME; ringpos %= 2 * SAMPLES_PER_FRAME;
@ -109,11 +95,9 @@ fetch_sample(void)
void void
read_frame(int ringpos) read_frame(int ringpos)
{ {
if (fread(frame + ringpos, 1, sizeof(frame[0]), stdin) if ((fread(frame + ringpos, 1, sizeof(frame[0]), stdin)
!= sizeof(frame[0])) != sizeof(frame[0])) || (ferror(stdin) != 0))
err(ERR(), "Could not read from frame."); err(ERR(), "Could not read from frame.");
if (ferror(stdin) != 0)
err(ERR(), "Could not read from frame");
} }
void void
@ -128,8 +112,7 @@ print_char(void)
#endif #endif
if (f1 < FREQ_DATA_THRESHOLD) if (f1 < FREQ_DATA_THRESHOLD)
ascii |= (1 << ascii_bit); ascii |= (1 << ascii_bit);
ascii_bit--; if (!ascii_bit) {
if (ascii_bit < 0) {
#if DEBUG #if DEBUG
printf("<%c, %x>", ascii, ascii); printf("<%c, %x>", ascii, ascii);
#else #else