Skip to content

Commit

Permalink
add audio recording test
Browse files Browse the repository at this point in the history
  • Loading branch information
delbertooo committed Mar 5, 2017
1 parent 809806c commit 8e70760
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
15 changes: 7 additions & 8 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
Expand All @@ -90,7 +90,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.delbertooo.careco.android.preheatingcontrol" >

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,16 @@ public void playSingleRect(View view) {
public void playSequence(View view) {
generateBitSequence(1, 0, 1, 0, 1, 1, 1, 1).play();
}

public void readAudio(View view) {
Recorder recorder = new Recorder();
recorder.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
recorder.stop();
Log.d("debug", recorder.getResult().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.delbertooo.careco.android.preheatingcontrol;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import java.util.ArrayList;
import java.util.List;

/**
* Created by robert on 05.03.17.
*/

public class Recorder {
private static final int RECORDER_SAMPLE_RATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
private AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, BUFFER_SIZE);
private boolean recording = false;
private Thread readThread;
private List<Short> result = new ArrayList<>();

public void start() {
if (recording) {
throw new RuntimeException("Already recording.");
}
recording = true;
recorder.startRecording();
readThread = new Thread(new Runnable() {
@Override
public void run() {
short buffer[] = new short[BUFFER_SIZE];
while (recording) {
int bytesRead = recorder.read(buffer, 0, BUFFER_SIZE);
for (int i = 0; i < bytesRead; i++) {
result.add(buffer[i]);
}
}
}
});
readThread.start();
}

public void stop() {
recording = false;
try {
readThread.join();
} catch (InterruptedException e) {
throw new RuntimeException("Failed to stop recording.", e);
}
recorder.stop();
recorder.release();
readThread = null;
}

public List<Short> getResult() {
return result;
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,15 @@
android:layout_alignParentStart="true"
android:layout_marginTop="275dp"
android:onClick="playSequence" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read 2s"
android:id="@+id/buttonread2s"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:layout_marginLeft="200dp"
android:onClick="readAudio" />
</RelativeLayout>

0 comments on commit 8e70760

Please sign in to comment.