spkmodem-recv.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. /* Compilation: gcc -o spkmodem-recv spkmodem-recv */
  22. /* Usage: parecord --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */
  23. #define SAMPLES_PER_TRAME 240
  24. #define FREQ_SEP_MIN 5
  25. #define FREQ_SEP_MAX 15
  26. #define FREQ_DATA_MIN 15
  27. #define FREQ_DATA_THRESHOLD 25
  28. #define FREQ_DATA_MAX 60
  29. #define THRESHOLD 500
  30. #define DEBUG 0
  31. #define FLUSH_TIMEOUT 1
  32. static signed short trame[2 * SAMPLES_PER_TRAME];
  33. static signed short pulse[2 * SAMPLES_PER_TRAME];
  34. static int ringpos = 0;
  35. static int pos, f1, f2;
  36. static int amplitude = 0;
  37. static int lp = 0;
  38. static void
  39. read_sample (void)
  40. {
  41. amplitude -= abs (trame[ringpos]);
  42. f1 -= pulse[ringpos];
  43. f1 += pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)];
  44. f2 -= pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)];
  45. fread (trame + ringpos, 1, sizeof (trame[0]), stdin);
  46. amplitude += abs (trame[ringpos]);
  47. if (pos ? (trame[ringpos] < -THRESHOLD)
  48. : (trame[ringpos] > +THRESHOLD))
  49. {
  50. pulse[ringpos] = 1;
  51. pos = !pos;
  52. f2++;
  53. }
  54. else
  55. pulse[ringpos] = 0;
  56. ringpos++;
  57. ringpos %= 2 * SAMPLES_PER_TRAME;
  58. lp++;
  59. }
  60. int
  61. main ()
  62. {
  63. int bitn = 7;
  64. char c = 0;
  65. int i;
  66. int llp = 0;
  67. while (!feof (stdin))
  68. {
  69. if (lp > 3 * SAMPLES_PER_TRAME)
  70. {
  71. bitn = 7;
  72. c = 0;
  73. lp = 0;
  74. llp++;
  75. }
  76. if (llp == FLUSH_TIMEOUT)
  77. fflush (stdout);
  78. if (f2 > FREQ_SEP_MIN && f2 < FREQ_SEP_MAX
  79. && f1 > FREQ_DATA_MIN && f1 < FREQ_DATA_MAX)
  80. {
  81. #if DEBUG
  82. printf ("%d %d %d @%d\n", f1, f2, FREQ_DATA_THRESHOLD,
  83. ftell (stdin) - sizeof (trame));
  84. #endif
  85. if (f1 < FREQ_DATA_THRESHOLD)
  86. c |= (1 << bitn);
  87. bitn--;
  88. if (bitn < 0)
  89. {
  90. #if DEBUG
  91. printf ("<%c, %x>", c, c);
  92. #else
  93. printf ("%c", c);
  94. #endif
  95. bitn = 7;
  96. c = 0;
  97. }
  98. lp = 0;
  99. llp = 0;
  100. for (i = 0; i < SAMPLES_PER_TRAME; i++)
  101. read_sample ();
  102. continue;
  103. }
  104. read_sample ();
  105. }
  106. return 0;
  107. }