commit bf7af7794593f51c876c43bc93a379907a8c2cf4
parent 82bd7d0108878baf63a81a2ff00734ec41557034
Author: gstraube <gstraube@mailbox.org>
Date: Sun, 21 May 2017 20:35:48 +0200
Display detected pitch in Hertz
Diffstat:
4 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/app/build.gradle b/app/build.gradle
@@ -25,5 +25,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
+ compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
@@ -1,7 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.github.cythara">
+ <uses-permission android:name="android.permission.RECORD_AUDIO" />
+
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@@ -9,7 +11,12 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
-
+ <activity android:name=".MainActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
</application>
-</manifest>
+</manifest>
+\ No newline at end of file
diff --git a/app/src/main/java/com/github/cythara/MainActivity.java b/app/src/main/java/com/github/cythara/MainActivity.java
@@ -0,0 +1,75 @@
+package com.github.cythara;
+
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.support.v7.app.AppCompatActivity;
+import android.util.Log;
+import android.widget.TextView;
+
+import be.tarsos.dsp.AudioDispatcher;
+import be.tarsos.dsp.AudioEvent;
+import be.tarsos.dsp.pitch.McLeodPitchMethod;
+import be.tarsos.dsp.pitch.PitchDetectionHandler;
+import be.tarsos.dsp.pitch.PitchDetectionResult;
+import be.tarsos.dsp.pitch.PitchProcessor;
+
+import static be.tarsos.dsp.io.android.AudioDispatcherFactory.fromDefaultMicrophone;
+import static be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm.MPM;
+
+public class MainActivity extends AppCompatActivity {
+
+ private static final int SAMPLE_RATE = 44100;
+ private static final int BUFFER_SIZE = McLeodPitchMethod.DEFAULT_BUFFER_SIZE * 7;
+ private static final int OVERLAP = McLeodPitchMethod.DEFAULT_OVERLAP * 7;
+
+ final Handler myHandler = new Handler() {
+
+ public void handleMessage(Message msg) {
+ final TextView pitchText = (TextView) findViewById(R.id.pitch);
+
+ pitchText.setText(msg.getData().getString("pitch"));
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ Runnable runnable = new Runnable() {
+ @Override
+ public void run() {
+ PitchDetectionHandler pitchDetectionHandler = new PitchDetectionHandler() {
+ @Override
+ public void handlePitch(PitchDetectionResult pitchDetectionResult,
+ AudioEvent audioEvent) {
+ float pitch = pitchDetectionResult.getPitch();
+
+ if (pitch != -1) {
+ Message message = new Message();
+ Bundle bundle = new Bundle();
+ bundle.putString("pitch", String.valueOf(pitch));
+ message.setData(bundle);
+ myHandler.sendMessage(message);
+
+ Log.d("com.github.cythara", "Pitch: " + pitch);
+ }
+ }
+ };
+
+ PitchProcessor pitchProcessor = new PitchProcessor(MPM, SAMPLE_RATE,
+ BUFFER_SIZE, pitchDetectionHandler);
+
+ AudioDispatcher audioDispatcher = fromDefaultMicrophone(SAMPLE_RATE,
+ BUFFER_SIZE, OVERLAP);
+
+ audioDispatcher.addAudioProcessor(pitchProcessor);
+
+ audioDispatcher.run();
+ }
+ };
+
+ new Thread(runnable).start();
+ }
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ tools:context="com.github.cythara.MainActivity">
+
+ <TextView
+ android:id="@+id/pitch"
+ android:layout_width="187dp"
+ android:layout_height="85dp"
+ android:layout_marginRight="112dp"
+ android:layout_marginTop="245dp"
+ android:lineSpacingExtra="8sp"
+ android:text="Listening…"
+ android:textAlignment="center"
+ android:textSize="24sp"
+ app:layout_constraintRight_toRightOf="parent"
+ app:layout_constraintTop_toTopOf="parent" />
+</android.support.constraint.ConstraintLayout>