-
Notifications
You must be signed in to change notification settings - Fork 10
/
vis.c
175 lines (138 loc) · 5.51 KB
/
vis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdlib.h>
#include <math.h>
#include <gtk/gtk.h>
#include <alsa/asoundlib.h>
#include <fftw3.h>
#include "common.h"
/*
*
* Detect VIS & frequency shift
*
* Each bit lasts 30 ms (1323 samples)
*
*/
guchar GetVIS () {
int selmode, ptr=0;
int VIS = 0, Parity = 0, HedrPtr = 0;
guint FFTLen = 2048, i=0, j=0, k=0, MaxBin = 0;
double Power[2048] = {0}, HedrBuf[100] = {0}, tone[100] = {0}, Hann[882] = {0};
gboolean gotvis = FALSE;
guchar Bit[8] = {0}, ParityBit = 0;
for (i = 0; i < FFTLen; i++) fft.in[i] = 0;
// Create 20ms Hann window
for (i = 0; i < 882; i++) Hann[i] = 0.5 * (1 - cos( (2 * M_PI * (double)i) / 881 ) );
ManualActivated = FALSE;
printf("Waiting for header\n");
gdk_threads_enter();
gtk_statusbar_push( GTK_STATUSBAR(gui.statusbar), 0, "Listening" );
gdk_threads_leave();
while ( TRUE ) {
if (Abort || ManualResync) return(0);
// Read 10 ms from sound card
readPcm(441);
// Apply Hann window
for (i = 0; i < 882; i++) fft.in[i] = pcm.Buffer[pcm.WindowPtr + i - 441] / 32768.0 * Hann[i];
// FFT of last 20 ms
fftw_execute(fft.Plan2048);
// Find the bin with most power
MaxBin = 0;
for (i = 0; i <= GetBin(6000, FFTLen); i++) {
Power[i] = power(fft.out[i]);
if ( (i >= GetBin(500,FFTLen) && i < GetBin(3300,FFTLen)) &&
(MaxBin == 0 || Power[i] > Power[MaxBin]))
MaxBin = i;
}
// Find the peak frequency by Gaussian interpolation
if (MaxBin > GetBin(500, FFTLen) && MaxBin < GetBin(3300, FFTLen) &&
Power[MaxBin] > 0 && Power[MaxBin+1] > 0 && Power[MaxBin-1] > 0)
HedrBuf[HedrPtr] = MaxBin + (log( Power[MaxBin + 1] / Power[MaxBin - 1] )) /
(2 * log( pow(Power[MaxBin], 2) / (Power[MaxBin + 1] * Power[MaxBin - 1])));
else HedrBuf[HedrPtr] = HedrBuf[(HedrPtr-1) % 45];
// In Hertz
HedrBuf[HedrPtr] = HedrBuf[HedrPtr] / FFTLen * 44100;
// Header buffer holds 45 * 10 msec = 450 msec
HedrPtr = (HedrPtr + 1) % 45;
// Frequencies in the last 450 msec
for (i = 0; i < 45; i++) tone[i] = HedrBuf[(HedrPtr + i) % 45];
// Is there a pattern that looks like (the end of) a calibration header + VIS?
// Tolerance ±25 Hz
CurrentPic.HedrShift = 0;
gotvis = FALSE;
for (i = 0; i < 3; i++) {
if (CurrentPic.HedrShift != 0) break;
for (j = 0; j < 3; j++) {
if ( (tone[1*3+i] > tone[0+j] - 25 && tone[1*3+i] < tone[0+j] + 25) && // 1900 Hz leader
(tone[2*3+i] > tone[0+j] - 25 && tone[2*3+i] < tone[0+j] + 25) && // 1900 Hz leader
(tone[3*3+i] > tone[0+j] - 25 && tone[3*3+i] < tone[0+j] + 25) && // 1900 Hz leader
(tone[4*3+i] > tone[0+j] - 25 && tone[4*3+i] < tone[0+j] + 25) && // 1900 Hz leader
(tone[5*3+i] > tone[0+j] - 725 && tone[5*3+i] < tone[0+j] - 675) && // 1200 Hz start bit
// ...8 VIS bits...
(tone[14*3+i] > tone[0+j] - 725 && tone[14*3+i] < tone[0+j] - 675) // 1200 Hz stop bit
) {
// Attempt to read VIS
gotvis = TRUE;
for (k = 0; k < 8; k++) {
if (tone[6*3+i+3*k] > tone[0+j] - 625 && tone[6*3+i+3*k] < tone[0+j] - 575) Bit[k] = 0;
else if (tone[6*3+i+3*k] > tone[0+j] - 825 && tone[6*3+i+3*k] < tone[0+j] - 775) Bit[k] = 1;
else { // erroneous bit
gotvis = FALSE;
break;
}
}
if (gotvis) {
CurrentPic.HedrShift = tone[0+j] - 1900;
VIS = Bit[0] + (Bit[1] << 1) + (Bit[2] << 2) + (Bit[3] << 3) + (Bit[4] << 4) +
(Bit[5] << 5) + (Bit[6] << 6);
ParityBit = Bit[7];
printf(" VIS %d (%02Xh) @ %+d Hz\n", VIS, VIS, CurrentPic.HedrShift);
Parity = Bit[0] ^ Bit[1] ^ Bit[2] ^ Bit[3] ^ Bit[4] ^ Bit[5] ^ Bit[6];
if (VISmap[VIS] == R12BW) Parity = !Parity;
if (Parity != ParityBit) {
printf(" Parity fail\n");
gotvis = FALSE;
} else if (VISmap[VIS] == UNKNOWN) {
printf(" Unknown VIS\n");
gotvis = FALSE;
} else {
gdk_threads_enter();
gtk_combo_box_set_active (GTK_COMBO_BOX(gui.combo_mode), VISmap[VIS]-1);
gtk_spin_button_set_value (GTK_SPIN_BUTTON(gui.spin_shift), CurrentPic.HedrShift);
gdk_threads_leave();
break;
}
}
}
}
}
if (gotvis)
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gui.tog_rx))) break;
// Manual start
if (ManualActivated) {
gdk_threads_enter();
gtk_widget_set_sensitive( gui.frame_manual, FALSE );
gtk_widget_set_sensitive( gui.combo_card, FALSE );
gdk_threads_leave();
selmode = gtk_combo_box_get_active (GTK_COMBO_BOX(gui.combo_mode)) + 1;
CurrentPic.HedrShift = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(gui.spin_shift));
VIS = 0;
for (i=0; i<0x80; i++) {
if (VISmap[i] == selmode) {
VIS = i;
break;
}
}
break;
}
if (++ptr == 10) {
setVU(Power, 2048, 6, FALSE);
ptr = 0;
}
pcm.WindowPtr += 441;
}
// Skip the rest of the stop bit
readPcm(20e-3 * 44100);
pcm.WindowPtr += 20e-3 * 44100;
if (VISmap[VIS] != UNKNOWN) return VISmap[VIS];
else printf(" No VIS found\n");
return 0;
}