package de.mal_richtig.toney; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.android.AudioDispatcherFactory; import be.tarsos.dsp.pitch.PitchDetectionHandler; import be.tarsos.dsp.pitch.PitchDetectionResult; import be.tarsos.dsp.pitch.PitchProcessor; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(44100,512*8,0); PitchDetectionHandler pdh = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult result,AudioEvent e) { final float pitchInHz = result.getPitch(); runOnUiThread(new Runnable() { @Override public void run() { TextView text = (TextView) findViewById(R.id.key_major); text.setText("" + pitchInHz); } }); } }; AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.DYNAMIC_WAVELET, 44100, 512*8, pdh); dispatcher.addAudioProcessor(p); new Thread(dispatcher,"Audio Dispatcher").start(); } public void processPitch(float pitchInHz) { TextView noteText = (TextView) findViewById(R.id.key_major); //pitchText.setText("" + pitchInHz); if(pitchInHz >= 110 && pitchInHz < 123.47) { //A noteText.setText("A"); } else if(pitchInHz >= 123.47 && pitchInHz < 130.81) { //B noteText.setText("B"); } else if(pitchInHz >= 130.81 && pitchInHz < 146.83) { //C noteText.setText("C"); } else if(pitchInHz >= 146.83 && pitchInHz < 164.81) { //D noteText.setText("D"); } else if(pitchInHz >= 164.81 && pitchInHz <= 174.61) { //E noteText.setText("E"); } else if(pitchInHz >= 174.61 && pitchInHz < 185) { //F noteText.setText("F"); } else if(pitchInHz >= 185 && pitchInHz < 196) { //G noteText.setText("G"); } } }