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