spkmodem-recv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* spkmodem-recv.c - decode spkmodem signals */
  2. /*
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * spkmodem-recv is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * spkmodem-recv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with spkmodem-recv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <err.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. /* Compilation: cc -o spkmodem-recv spkmodem-recv.c */
  25. /* Usage: parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */
  26. #define SAMPLES_PER_FRAME 240
  27. #define MAX_SAMPLES (2 * SAMPLES_PER_FRAME)
  28. #define FREQ_SEP_MIN 5
  29. #define FREQ_SEP_MAX 15
  30. #define FREQ_DATA_MIN 15
  31. #define FREQ_DATA_THRESHOLD 25
  32. #define FREQ_DATA_MAX 60
  33. #define THRESHOLD 500
  34. #define ERR() (errno = errno ? errno : ECANCELED)
  35. #define reset_char() ascii = 0, ascii_bit = 7
  36. signed short frame[MAX_SAMPLES], pulse[MAX_SAMPLES];
  37. int ringpos, debug, freq_data, freq_separator, sample_count, ascii_bit = 7;
  38. char ascii = 0;
  39. void handle_audio(void);
  40. void decode_pulse(void);
  41. int set_ascii_bit(void);
  42. void print_char(void);
  43. void print_stats(void);
  44. int
  45. main(int argc, char *argv[])
  46. {
  47. int c;
  48. #ifdef __OpenBSD__
  49. if (pledge("stdio", NULL) == -1)
  50. err(ERR(), "pledge");
  51. #endif
  52. while ((c = getopt(argc, argv, "d")) != -1)
  53. if (!(debug = (c == 'd')))
  54. err(errno = EINVAL, NULL);
  55. setvbuf(stdout, NULL, _IONBF, 0);
  56. while (!feof(stdin))
  57. handle_audio();
  58. if (errno && debug)
  59. err(errno, "Unhandled error, errno %d", errno);
  60. return errno;
  61. }
  62. void
  63. handle_audio(void)
  64. {
  65. if (sample_count > (3 * SAMPLES_PER_FRAME))
  66. sample_count = reset_char();
  67. if ((freq_separator <= FREQ_SEP_MIN) || (freq_separator >= FREQ_SEP_MAX)
  68. || (freq_data <= FREQ_DATA_MIN) || (freq_data >= FREQ_DATA_MAX)) {
  69. decode_pulse();
  70. return;
  71. }
  72. if (set_ascii_bit() < 0)
  73. print_char();
  74. sample_count = 0;
  75. for (int sample = 0; sample < SAMPLES_PER_FRAME; sample++)
  76. decode_pulse();
  77. }
  78. void
  79. decode_pulse(void)
  80. {
  81. int next_ringpos = (ringpos + SAMPLES_PER_FRAME) % MAX_SAMPLES;
  82. freq_data -= pulse[ringpos];
  83. freq_data += pulse[next_ringpos];
  84. freq_separator -= pulse[next_ringpos];
  85. fread(frame + ringpos, 1, sizeof(frame[0]), stdin);
  86. if (ferror(stdin) != 0)
  87. err(ERR(), "Could not read from frame.");
  88. if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0))
  89. ++freq_separator;
  90. ++ringpos;
  91. ringpos %= MAX_SAMPLES;
  92. ++sample_count;
  93. }
  94. int
  95. set_ascii_bit(void)
  96. {
  97. if (debug)
  98. print_stats();
  99. if (freq_data < FREQ_DATA_THRESHOLD)
  100. ascii |= (1 << ascii_bit);
  101. --ascii_bit;
  102. return ascii_bit;
  103. }
  104. void
  105. print_char(void)
  106. {
  107. if (debug)
  108. printf("<%c, %x>", ascii, ascii);
  109. else
  110. printf("%c", ascii);
  111. reset_char();
  112. }
  113. void
  114. print_stats(void)
  115. {
  116. long stdin_pos;
  117. if ((stdin_pos = ftell(stdin)) == -1)
  118. err(ERR(), NULL);
  119. printf ("%d %d %d @%ld\n", freq_data, freq_separator,
  120. FREQ_DATA_THRESHOLD, stdin_pos - sizeof(frame));
  121. }