forked from davidnuon/TDDEmulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTYInput.java
197 lines (179 loc) · 5.07 KB
/
TTYInput.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Copyright 2012 Jeff Bush
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import javax.sound.sampled.*;
import java.util.*;
class TTYInput implements Runnable
{
static final int kBufferSize = 1600;
static final double kMarkFrequency = 1400f;
static final double kSpaceFrequency = 1800f;
static final int kBitsPerSecond = 45;
static final float kSampleRate = 8000f;
static final int kBitsPerCharacter = 5;
static final int kSamplesPerBit = (int) (kSampleRate / kBitsPerSecond);
static final long kGuardDelay = (long) (1000 * kBufferSize / kSampleRate * 2);
public TTYInput()
{
try
{
AudioFormat format = new AudioFormat(kSampleRate, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
format);
if (!AudioSystem.isLineSupported(info))
{
System.out.println("Line matching " + info + " not supported.");
return;
}
fAudioSource = (TargetDataLine) AudioSystem.getLine(info);
fAudioSource.open(format, kBufferSize);
}
catch (LineUnavailableException exc)
{
System.out.println("TTYInput: unable to open line " + exc);
}
fThread = new Thread(this);
fThread.start();
}
interface TTYInputListener
{
public void handleCode(char c);
}
void setListener(TTYInputListener handler)
{
fHandler = handler;
}
synchronized void setIgnoreInput(boolean ignore)
{
if (ignore)
{
if (fGuardTimer != null)
fGuardTimer.cancel();
fIgnoreInput = true;
}
else
{
// Some data may be in the current input buffer. Wait
// a spell for it to empty out so we don't pick up pieces
// of the last character.
fGuardTimer = new Timer();
fGuardTimer.schedule(new TimerTask() {
public void run() {
fIgnoreInput = false;
}
}, kGuardDelay);
}
}
void processCode(char code)
{
if (code == 0x1b)
fModeIsFigs = true;
else if (code == 0x1f)
fModeIsFigs = false;
else
{
char ch;
if (fModeIsFigs)
ch = kFigsTable[(int) code];
else
ch = kLtrsTable[(int) code];
if (fHandler != null)
fHandler.handleCode(ch);
}
}
public void run()
{
IIRFilter markBPF = IIRFilter.makeBandpassFilter(kMarkFrequency,
50.0, kSampleRate);
IIRFilter spaceBPF = IIRFilter.makeBandpassFilter(kSpaceFrequency,
50.0, kSampleRate);
IIRFilter markLPF = IIRFilter.makeLowPassFilter(kBitsPerSecond,
kSampleRate);
IIRFilter spaceLPF = IIRFilter.makeLowPassFilter(kBitsPerSecond,
kSampleRate);
byte[] sampleBuffer = new byte[kBufferSize];
boolean waitForStartBit = true;
int currentWord = 0;
int bitCount = 0;
int bitDurationCounter = 0;
fAudioSource.start();
while (true)
{
int got = fAudioSource.read(sampleBuffer, 0, sampleBuffer.length);
for (int i = 0; i < got; i += 2)
{
double sample = (double) ((sampleBuffer[i] << 8) | sampleBuffer[i + 1]) / 32767;
double markValue = markBPF.processSample(sample);
double spaceValue = spaceBPF.processSample(sample);
double markLevel = markLPF.processSample(Math.abs(markValue));
double spaceLevel = spaceLPF.processSample(Math.abs(spaceValue));
double nrzValue = markLevel - spaceLevel;
if (fIgnoreInput)
{
// Drop the entire buffer if we are ignoring input.
waitForStartBit = true;
break;
}
boolean isMark = nrzValue > 0;
if (waitForStartBit)
{
// Set a threshold for the start bit so we don't trigger
// due to noise when there is no signal.
if (nrzValue < -0.1)
{
waitForStartBit = false;
bitDurationCounter = (int)(kSamplesPerBit * 1.5);
bitCount = 0;
currentWord = 0;
}
}
else
{
if (--bitDurationCounter == 0)
{
bitDurationCounter = kSamplesPerBit;
if (++bitCount == 6)
{
if (isMark) // Valid stop bit
processCode((char) currentWord);
waitForStartBit = true;
}
else
currentWord = (currentWord >> 1) | ((isMark ? 1 : 0) << 4);
}
}
}
}
}
// Baudot to unicode tables
private static final char[] kLtrsTable = {
' ', 'E', '\n', 'A', ' ', 'S', 'I', 'U',
'\r', 'D', 'R', 'J', 'N', 'F', 'C', 'K',
'T', 'Z', 'L', 'W', 'H', 'Y', 'P', 'Q',
'O', 'B', 'G', ' ', 'M', 'X', 'V', ' '
};
private static final char[] kFigsTable = {
' ', '3', '\n', '-', ' ', '-', '8', '7',
'\r', '$', '4', '\'', ',', '!', ':', '(',
'5', '\"', ')', '2', '=', '6', '0', '1',
'9', '?', '+', ' ', '.', '/', ';', ' '
};
private boolean fModeIsFigs = false;
private TargetDataLine fAudioSource;
private Thread fThread;
private TTYInputListener fHandler;
private boolean fIgnoreInput = false;
private Timer fGuardTimer;
}