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