commit 611089fd43c55640fc1e598c5eed1215f21e16b4
parent 3a48f51a79276c4a41e0acb8b91ee1d7ad77a027
Author: gstraube <gstraube@mailbox.org>
Date: Thu, 17 Aug 2017 21:04:39 +0200
Introduce fragment for listener
Diffstat:
2 files changed, 123 insertions(+), 117 deletions(-)
diff --git a/app/src/main/java/com/github/cythara/ListenerFragment.java b/app/src/main/java/com/github/cythara/ListenerFragment.java
@@ -0,0 +1,106 @@
+package com.github.cythara;
+
+import android.app.Fragment;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.os.Bundle;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import be.tarsos.dsp.AudioDispatcher;
+import be.tarsos.dsp.AudioEvent;
+import be.tarsos.dsp.pitch.FastYin;
+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.FFT_YIN;
+
+public class ListenerFragment extends Fragment {
+
+ interface TaskCallbacks {
+ void onProgressUpdate(PitchDifference percent);
+ }
+
+ private static final int SAMPLE_RATE = 44100;
+ private static final int BUFFER_SIZE = FastYin.DEFAULT_BUFFER_SIZE;
+ private static final int OVERLAP = FastYin.DEFAULT_OVERLAP;
+ private static final int MIN_ITEMS_COUNT = 15;
+ private static List<PitchDifference> pitchDifferences = new ArrayList<>();
+
+ private TaskCallbacks taskCallbacks;
+
+ @Override
+ public void onAttach(Context context) {
+ super.onAttach(context);
+
+ taskCallbacks = (TaskCallbacks) context;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setRetainInstance(true);
+
+ PitchListener pitchListener = new PitchListener();
+ pitchListener.execute();
+ }
+
+ @Override
+ public void onDetach() {
+ super.onDetach();
+ taskCallbacks = null;
+ }
+
+ private class PitchListener extends AsyncTask<Void, PitchDifference, Void> {
+
+ @Override
+ protected Void doInBackground(Void... params) {
+ PitchDetectionHandler pitchDetectionHandler = new PitchDetectionHandler() {
+ @Override
+ public void handlePitch(PitchDetectionResult pitchDetectionResult,
+ AudioEvent audioEvent) {
+ float pitch = pitchDetectionResult.getPitch();
+
+ if (pitch != -1) {
+ PitchDifference pitchDifference = PitchComparator.retrieveNote(pitch);
+
+
+ pitchDifferences.add(pitchDifference);
+
+ if (pitchDifferences.size() >= MIN_ITEMS_COUNT) {
+ PitchDifference average =
+ Sampler.calculateAverageDifference(pitchDifferences);
+
+ publishProgress(average);
+
+ pitchDifferences.clear();
+ }
+ }
+ }
+ };
+
+ PitchProcessor pitchProcessor = new PitchProcessor(FFT_YIN, SAMPLE_RATE,
+ BUFFER_SIZE, pitchDetectionHandler);
+
+ AudioDispatcher audioDispatcher = fromDefaultMicrophone(SAMPLE_RATE,
+ BUFFER_SIZE, OVERLAP);
+
+ audioDispatcher.addAudioProcessor(pitchProcessor);
+
+ audioDispatcher.run();
+
+ return null;
+ }
+
+ @Override
+ protected void onProgressUpdate(PitchDifference... pitchDifference) {
+ if (taskCallbacks != null) {
+ taskCallbacks.onProgressUpdate(pitchDifference[0]);
+ }
+ }
+ }
+}
diff --git a/app/src/main/java/com/github/cythara/MainActivity.java b/app/src/main/java/com/github/cythara/MainActivity.java
@@ -1,136 +1,36 @@
package com.github.cythara;
+import android.app.FragmentManager;
import android.os.Bundle;
-import android.os.Handler;
-import android.os.Message;
import android.support.v7.app.AppCompatActivity;
-import android.util.Log;
-import java.lang.ref.WeakReference;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
+public class MainActivity extends AppCompatActivity implements ListenerFragment.TaskCallbacks {
-import be.tarsos.dsp.AudioDispatcher;
-import be.tarsos.dsp.AudioEvent;
-import be.tarsos.dsp.pitch.FastYin;
-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.FFT_YIN;
-
-public class MainActivity extends AppCompatActivity {
-
- private static final int SAMPLE_RATE = 44100;
- private static final int BUFFER_SIZE = FastYin.DEFAULT_BUFFER_SIZE;
- private static final int OVERLAP = FastYin.DEFAULT_OVERLAP;
- private static final int MIN_ITEMS_COUNT = 15;
- private static List<PitchDifference> pitchDifferences = new ArrayList<>();
-
- final Handler updateHandler = new UpdateHandler(this);
- final PitchListener pitchListener = new PitchListener(this);
+ private static final String TAG_LISTENER_FRAGMENT = "listener_fragment";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- new Thread(pitchListener).start();
- }
+ FragmentManager fragmentManager = getFragmentManager();
+ ListenerFragment listenerFragment = (ListenerFragment)
+ fragmentManager.findFragmentByTag(TAG_LISTENER_FRAGMENT);
- private static class PitchListener implements Runnable {
-
- private final WeakReference<MainActivity> mainActivity;
-
- PitchListener(MainActivity activity) {
- mainActivity = new WeakReference<>(activity);
+ if (listenerFragment == null) {
+ listenerFragment = new ListenerFragment();
+ fragmentManager
+ .beginTransaction()
+ .add(listenerFragment, TAG_LISTENER_FRAGMENT)
+ .commit();
}
-
- @Override
- public void run() {
- final MainActivity activity = mainActivity.get();
-
- if (activity != null) {
- PitchDetectionHandler pitchDetectionHandler = new PitchDetectionHandler() {
- @Override
- public void handlePitch(PitchDetectionResult pitchDetectionResult,
- AudioEvent audioEvent) {
- float pitch = pitchDetectionResult.getPitch();
-
- if (pitch != -1) {
- PitchDifference pitchDifference = PitchComparator.retrieveNote(pitch);
-
- String msg;
-
- msg = String.format(Locale.US, "Closest: %s Diff: %f Freq: %f",
- pitchDifference.closest.name(),
- pitchDifference.deviation, pitch);
-
- log(msg);
-
- pitchDifferences.add(pitchDifference);
-
- if (pitchDifferences.size() >= MIN_ITEMS_COUNT) {
- PitchDifference average =
- Sampler.calculateAverageDifference(pitchDifferences);
-
- Message message = new Message();
- Bundle bundle = new Bundle();
- bundle.putParcelable("pitchDiff", average);
- message.setData(bundle);
-
- msg = String.format(Locale.US, "Note: %s Diff: %f",
- pitchDifference.closest.name(),
- pitchDifference.deviation);
-
- log(msg);
-
- activity.updateHandler.sendMessage(message);
-
- pitchDifferences.clear();
- }
- }
- }
- };
-
- PitchProcessor pitchProcessor = new PitchProcessor(FFT_YIN, SAMPLE_RATE,
- BUFFER_SIZE, pitchDetectionHandler);
-
- AudioDispatcher audioDispatcher = fromDefaultMicrophone(SAMPLE_RATE,
- BUFFER_SIZE, OVERLAP);
-
- audioDispatcher.addAudioProcessor(pitchProcessor);
-
- audioDispatcher.run();
- }
- }
- }
-
- private static void log(String msg) {
- Log.d("com.github.cythara", msg);
}
- private static class UpdateHandler extends Handler {
-
- private final WeakReference<MainActivity> mainActivity;
-
- UpdateHandler(MainActivity activity) {
- mainActivity = new WeakReference<>(activity);
- }
-
- public void handleMessage(Message msg) {
- MainActivity activity = mainActivity.get();
-
- if (activity != null) {
- TunerView tunerView = (TunerView) activity.findViewById(R.id.pitch);
-
- PitchDifference pitchDiff = msg.getData().getParcelable("pitchDiff");
+ @Override
+ public void onProgressUpdate(PitchDifference pitchDifference) {
+ TunerView tunerView = (TunerView) this.findViewById(R.id.pitch);
- tunerView.setPitchDifference(pitchDiff);
- tunerView.invalidate();
- }
- }
+ tunerView.setPitchDifference(pitchDifference);
+ tunerView.invalidate();
}
}