-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMorseEngine.java
232 lines (185 loc) · 5.54 KB
/
MorseEngine.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.departmentofdigitalwizardry.morsetts;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.res.AssetManager;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.speech.tts.SynthesisCallback;
import android.util.Log;
import android.content.res.*;
public class MorseEngine {
public class Tone {
int SampleRate;
int Duration;
byte[] Sound;
}
public class Time {
private int factor;
public Time() {
this.factor = 25;
}
public Time(int factor) {
this.factor = factor;
}
public int Dit() {
return factor * 1;
}
public int Dash() {
return factor * 3;
}
// For between tones
public int ShortBreak() {
return factor * 1;
}
// For between letters
public int LetterBreak() {
return factor * 3;
}
// For between words, 7x dot (6x to account for short breaks after every tone)
public int LongBreak() {
return factor * 6;
}
}
private Time time;
public MorseEngine() {
this.time = new Time();
}
public void setTimeFactor(int factor) {
this.time = new Time(factor);
}
private char[] AsciiToMorse(char[] input) {
Map<Character, String> morseCode = new HashMap<Character, String>() {{
put('A', ".-");
put('B', "-...");
put('C', "-.-.");
put('D', "-..");
put('E', ".");
put('F', "..-.");
put('G', "--.");
put('H', "....");
put('I', "..");
put('J', ".---");
put('K', "-.-");
put('L', ".-..");
put('M', "--");
put('N', "-.");
put('O', "---");
put('P', ".--.");
put('Q', "--.-");
put('R', ".-.");
put('S', "...");
put('T', "-");
put('U', "..-");
put('V', "...-");
put('W', ".--");
put('X', "-..-");
put('Y', "-.--");
put('Z', "--..");
put('1', ".----");
put('2', "..---");
put('3', "...--");
put('4', "....-");
put('5', ".....");
put('6', "-....");
put('7', "--...");
put('8', "---..");
put('9', "----.");
put('0', "-----");
put(' ', " "); // Short break pass-through
}};
char[] output = new char[] {};
for (int i = 0; i < input.length; i++) {
char upperInputItem = Character.toUpperCase(input[i]);
if(morseCode.containsKey(upperInputItem)) {
char[] morse = (morseCode.get(upperInputItem) + "_").toCharArray();
char[] newOutput = new char[output.length + morse.length]; // Create a new array with sum of existing and morse arrays
System.arraycopy(output, 0, newOutput, 0, output.length); // Copy existing array into new array
System.arraycopy(morse, 0, newOutput, output.length > 0 ? output.length : 0, morse.length); // Copy morse array into new array
output = newOutput; // Set output
}
}
return output;
}
private int GetToneDuration(char input) {
Map<Character, Integer> durations = new HashMap<Character, Integer>() {{
put('.', time.Dit());
put('-', time.Dash());
put(' ', time.LongBreak());
put('_', time.LetterBreak());
}};
return durations.get(input);
}
private Tone GenerateTone(int duration, int sampleRate, double frequency) {
/*
* See http://stackoverflow.com/questions/2413426/playing-an-arbitrary-tone-with-android
*/
final int samples = (int)Math.ceil((duration / 1000D) * (double)sampleRate);
double sample[] = new double[samples];
for (int i = 0; i < samples; i++) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/frequency));
}
int index = 0;
byte generatedSound[] = new byte[2 * samples];
for (final double dVal : sample) {
final short val = (short)(dVal * 32767);
generatedSound[index++] = (byte)(val & 0x00ff);
generatedSound[index++] = (byte)((val & 0xff00) >>> 8);
}
Tone tone = this.new Tone();
tone.SampleRate = sampleRate;
tone.Sound = generatedSound;
tone.Duration = duration;
return tone;
}
public synchronized void TextToTones(String text, SynthesisCallback callback) {
final double FREQUENCY = 1000;
final int SAMPLE_RATE = 8000;
int totalBytes = 0;
// Map ASCII characters to Mores strings
char[] morseChars = AsciiToMorse(text.toCharArray());
// Map Morse characters to tones
List<Tone> tones = new ArrayList<Tone>();
for (int i = 0; i < morseChars.length; i++) {
int duration = GetToneDuration(morseChars[i]);
Tone tone = GenerateTone(duration, SAMPLE_RATE, (morseChars[i] == ' ' | morseChars[i] == '_') ? 0 : FREQUENCY);
tones.add(tone);
totalBytes += tone.Sound.length;
Tone rest = GenerateTone(time.ShortBreak(), SAMPLE_RATE, 0); // Add letter spacing
tones.add(rest);
totalBytes += rest.Sound.length;
}
ByteBuffer buffer = ByteBuffer.allocate(totalBytes);
for (int j = 0; j < tones.size(); j++)
{
Tone tone = tones.get(j);
buffer.put(tone.Sound);
}
buffer.rewind();
while (buffer.hasRemaining()) {
int chunk = Math.min(callback.getMaxBufferSize(), buffer.remaining());
byte[] reader = new byte[chunk];
buffer.get(reader);
callback.audioAvailable(reader, 0, chunk);
}
//return tones.toArray(new Tone[tones.size()]);
}
public void PlaySound(Tone tone) {
final AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
tone.SampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
tone.Sound.length,
AudioTrack.MODE_STATIC
);
audioTrack.write(tone.Sound, 0, tone.Sound.length);
audioTrack.play();
}
}