Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

control tones

  • Loading branch information...
commit e1d974ef08e3ceb7b0520de557162f89db461509 1 parent cbabce2
@pingpongboss authored
View
2  res/layout/main.xml
@@ -88,7 +88,7 @@
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
- android:ems="10" android:hint="Input Message"/>
+ android:ems="10" android:hint="Input Message" android:text="test"/>
<Button
android:id="@+id/send"
View
47 src/edu/berkeley/cs194/activity/AudioModemActivity.java
@@ -148,19 +148,60 @@ public void run() {
FrequencyReceiver receiver = new FrequencyReceiver() {
long lastTime = -1;
+ ArrayList<Integer> tones = new ArrayList<Integer>();
+ boolean started = false;
@Override
public void updateFrequency(double frequency, double amplitude,
HashMap<Double, Double> frequencies) {
long current = SystemClock.uptimeMillis();
if (lastTime != -1) {
- long diff = current - lastTime;
- Log.d("Receiver", "diff: " + diff);
+ // long diff = current - lastTime;
+ // Log.d("Receiver", "diff: " + diff);
}
lastTime = current;
if (amplitude > 500) {
- Log.d("Receiver", "received " + frequency);
+ // we hear a tone
+ int tone = frequency > SoundPlayer.MEDIUM ? SoundPlayer.HIGH
+ : SoundPlayer.LOW;
+ tones.add(tone);
+ if (started) {
+ // add tone to list and wait for silence
+ } else {
+ // listen for control signal
+ if (tone == SoundPlayer.HIGH) {
+ if (tones.size() == SoundPlayer.CONTROL_TONE_NUM) {
+ tones.clear();
+ started = true;
+ Log.d("Receiver",
+ "Control tone succeeded. Listening for text.");
+ } else if (tones.size() == 1) {
+ Log.d("Receiver", "Listening for "
+ + SoundPlayer.CONTROL_TONE_NUM
+ + " HIGH tones.");
+ }
+ } else {
+ tones.clear();
+ Log.d("Receiver", "Control tone failed.");
+ }
+ }
+ } else {
+ // silence
+ if (started) {
+ if (tones.isEmpty()) {
+ started = false;
+ } else {
+ String text = Utils.morseToText(tones);
+ Log.d("Receiver", "Text: " + text);
+ }
+ tones.clear();
+ } else {
+ if (!tones.isEmpty()) {
+ tones.clear();
+ Log.d("Receiver", "Control tone failed.");
+ }
+ }
}
}
};
View
6 src/edu/berkeley/cs194/audio/PitchDetector.java
@@ -27,8 +27,8 @@
private final static int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private final static int BUFFER_SIZE_IN_MS = 3000;
- private final static int CHUNK_SIZE_IN_SAMPLES_POW2 = 11;
- private final static int CHUNK_SIZE_IN_SAMPLES = 2048; // = 2 ^
+ private final static int CHUNK_SIZE_IN_SAMPLES_POW2 = 7;
+ private final static int CHUNK_SIZE_IN_SAMPLES = 128; // = 2 ^
// CHUNK_SIZE_IN_SAMPLES_POW2
private final static int CHUNK_SIZE_IN_MS = 1000 * CHUNK_SIZE_IN_SAMPLES
/ RATE;
@@ -108,7 +108,7 @@ public void run() {
}
PostToUI(best_frequency, best_amplitude, frequencies);
- // delay thread so that we update every 600 ms
+ // delay thread so that we update every duration
long duration = (long) (SoundPlayer.duration * 1000);
final long current = SystemClock.uptimeMillis();
long delay = 0;
View
13 src/edu/berkeley/cs194/audio/SoundPlayer.java
@@ -10,7 +10,9 @@
public final static int MEDIUM = (HIGH + LOW) / 2;
public final static int SILENT = -1;
- public final static double duration = .6; // seconds
+ public final static int CONTROL_TONE_NUM = 6;
+
+ public final static double duration = .35; // seconds
private final int sampleRate = 8000;
private final int numSamples = (int) (duration * sampleRate);
private double sample[];
@@ -67,10 +69,11 @@ void playSound() {
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
* 2, AudioTrack.MODE_STATIC);
-// audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRate,
-// AudioFormat.CHANNEL_CONFIGURATION_MONO,
-// AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
-// * 2, AudioTrack.MODE_STATIC);
+ // audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
+ // sampleRate,
+ // AudioFormat.CHANNEL_CONFIGURATION_MONO,
+ // AudioFormat.ENCODING_PCM_16BIT, numSamples * frequencies.length
+ // * 2, AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
View
12 src/edu/berkeley/cs194/util/Utils.java
@@ -14,7 +14,7 @@
ArrayList<Integer> result = new ArrayList<Integer>();
lastWasWhitespace = true;
- for(int i = 0; i < 25; i++) {
+ for (int i = 0; i < SoundPlayer.CONTROL_TONE_NUM; i++) {
result.add(SoundPlayer.HIGH);
}
for (int i = 0; i < strlen; i++) {
@@ -133,13 +133,13 @@
}
return ret;
}
-
+
public static String morseToText(List<Integer> samples) {
String text = "";
- for(int i = 0; i<samples.size()-25; i++) {
- text += samples.get(i).intValue()>RecorderThread.THRESHOLD ? 1:0;
+ for (int i = 0; i < samples.size(); i++) {
+ text += samples.get(i).intValue() == SoundPlayer.HIGH ? "1" : "0";
}
- return null;
-
+ return text;
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.