Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

control tones

  • Loading branch information...
commit e1d974ef08e3ceb7b0520de557162f89db461509 1 parent cbabce2
Mark Wei authored
2  res/layout/main.xml
@@ -88,7 +88,7 @@
88 88 android:layout_height="wrap_content"
89 89 android:layout_margin="4dp"
90 90 android:layout_weight="1"
91   - android:ems="10" android:hint="Input Message"/>
  91 + android:ems="10" android:hint="Input Message" android:text="test"/>
92 92
93 93 <Button
94 94 android:id="@+id/send"
47 src/edu/berkeley/cs194/activity/AudioModemActivity.java
@@ -148,19 +148,60 @@ public void run() {
148 148
149 149 FrequencyReceiver receiver = new FrequencyReceiver() {
150 150 long lastTime = -1;
  151 + ArrayList<Integer> tones = new ArrayList<Integer>();
  152 + boolean started = false;
151 153
152 154 @Override
153 155 public void updateFrequency(double frequency, double amplitude,
154 156 HashMap<Double, Double> frequencies) {
155 157 long current = SystemClock.uptimeMillis();
156 158 if (lastTime != -1) {
157   - long diff = current - lastTime;
158   - Log.d("Receiver", "diff: " + diff);
  159 + // long diff = current - lastTime;
  160 + // Log.d("Receiver", "diff: " + diff);
159 161 }
160 162 lastTime = current;
161 163
162 164 if (amplitude > 500) {
163   - Log.d("Receiver", "received " + frequency);
  165 + // we hear a tone
  166 + int tone = frequency > SoundPlayer.MEDIUM ? SoundPlayer.HIGH
  167 + : SoundPlayer.LOW;
  168 + tones.add(tone);
  169 + if (started) {
  170 + // add tone to list and wait for silence
  171 + } else {
  172 + // listen for control signal
  173 + if (tone == SoundPlayer.HIGH) {
  174 + if (tones.size() == SoundPlayer.CONTROL_TONE_NUM) {
  175 + tones.clear();
  176 + started = true;
  177 + Log.d("Receiver",
  178 + "Control tone succeeded. Listening for text.");
  179 + } else if (tones.size() == 1) {
  180 + Log.d("Receiver", "Listening for "
  181 + + SoundPlayer.CONTROL_TONE_NUM
  182 + + " HIGH tones.");
  183 + }
  184 + } else {
  185 + tones.clear();
  186 + Log.d("Receiver", "Control tone failed.");
  187 + }
  188 + }
  189 + } else {
  190 + // silence
  191 + if (started) {
  192 + if (tones.isEmpty()) {
  193 + started = false;
  194 + } else {
  195 + String text = Utils.morseToText(tones);
  196 + Log.d("Receiver", "Text: " + text);
  197 + }
  198 + tones.clear();
  199 + } else {
  200 + if (!tones.isEmpty()) {
  201 + tones.clear();
  202 + Log.d("Receiver", "Control tone failed.");
  203 + }
  204 + }
164 205 }
165 206 }
166 207 };
6 src/edu/berkeley/cs194/audio/PitchDetector.java
@@ -27,8 +27,8 @@
27 27 private final static int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
28 28
29 29 private final static int BUFFER_SIZE_IN_MS = 3000;
30   - private final static int CHUNK_SIZE_IN_SAMPLES_POW2 = 11;
31   - private final static int CHUNK_SIZE_IN_SAMPLES = 2048; // = 2 ^
  30 + private final static int CHUNK_SIZE_IN_SAMPLES_POW2 = 7;
  31 + private final static int CHUNK_SIZE_IN_SAMPLES = 128; // = 2 ^
32 32 // CHUNK_SIZE_IN_SAMPLES_POW2
33 33 private final static int CHUNK_SIZE_IN_MS = 1000 * CHUNK_SIZE_IN_SAMPLES
34 34 / RATE;
@@ -108,7 +108,7 @@ public void run() {
108 108 }
109 109 PostToUI(best_frequency, best_amplitude, frequencies);
110 110
111   - // delay thread so that we update every 600 ms
  111 + // delay thread so that we update every duration
112 112 long duration = (long) (SoundPlayer.duration * 1000);
113 113 final long current = SystemClock.uptimeMillis();
114 114 long delay = 0;
13 src/edu/berkeley/cs194/audio/SoundPlayer.java
@@ -10,7 +10,9 @@
10 10 public final static int MEDIUM = (HIGH + LOW) / 2;
11 11 public final static int SILENT = -1;
12 12
13   - public final static double duration = .6; // seconds
  13 + public final static int CONTROL_TONE_NUM = 6;
  14 +
  15 + public final static double duration = .35; // seconds
14 16 private final int sampleRate = 8000;
15 17 private final int numSamples = (int) (duration * sampleRate);
16 18 private double sample[];
@@ -67,10 +69,11 @@ void playSound() {
67 69 AudioFormat.CHANNEL_CONFIGURATION_MONO,
68 70 AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
69 71 * 2, AudioTrack.MODE_STATIC);
70   -// audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRate,
71   -// AudioFormat.CHANNEL_CONFIGURATION_MONO,
72   -// AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
73   -// * 2, AudioTrack.MODE_STATIC);
  72 + // audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
  73 + // sampleRate,
  74 + // AudioFormat.CHANNEL_CONFIGURATION_MONO,
  75 + // AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
  76 + // * 2, AudioTrack.MODE_STATIC);
74 77 audioTrack.write(generatedSnd, 0, generatedSnd.length);
75 78 audioTrack.play();
76 79 }
12 src/edu/berkeley/cs194/util/Utils.java
@@ -14,7 +14,7 @@
14 14
15 15 ArrayList<Integer> result = new ArrayList<Integer>();
16 16 lastWasWhitespace = true;
17   - for(int i = 0; i < 25; i++) {
  17 + for (int i = 0; i < SoundPlayer.CONTROL_TONE_NUM; i++) {
18 18 result.add(SoundPlayer.HIGH);
19 19 }
20 20 for (int i = 0; i < strlen; i++) {
@@ -133,13 +133,13 @@
133 133 }
134 134 return ret;
135 135 }
136   -
  136 +
137 137 public static String morseToText(List<Integer> samples) {
138 138 String text = "";
139   - for(int i = 0; i<samples.size()-25; i++) {
140   - text += samples.get(i).intValue()>RecorderThread.THRESHOLD ? 1:0;
  139 + for (int i = 0; i < samples.size(); i++) {
  140 + text += samples.get(i).intValue() == SoundPlayer.HIGH ? "1" : "0";
141 141 }
142   - return null;
143   -
  142 + return text;
  143 +
144 144 }
145 145 }

0 comments on commit e1d974e

Please sign in to comment.
Something went wrong with that request. Please try again.