For API level < 23, don't forget to add the correct permissions to the manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />For API level > 23, the check needs to be done at runtime (see Activity.java)
For API level < 23, don't forget to add the correct permissions to the manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />For API level > 23, the check needs to be done at runtime (see Activity.java)
| package com.example.admin.sound; | |
| import android.Manifest; | |
| import android.content.pm.PackageManager; | |
| import android.os.Build; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| 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 Activity extends AppCompatActivity { | |
| private final static String TAG = "SOUND"; | |
| private AudioDispatcher dispatcher; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| requestRecordAudioPermission(); | |
| dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0); | |
| AudioProcessor soundPressureProcessor = new AudioProcessor() { | |
| @Override | |
| public boolean process(AudioEvent audioEvent) { | |
| float[] buffer = audioEvent.getFloatBuffer(); | |
| double power = 0.0; | |
| for (float el : buffer) { | |
| power += el * el; | |
| } | |
| double value = Math.pow(power, 0.5) / buffer.length; | |
| final double pressure = 20.0 * Math.log10(value); | |
| runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| TextView text = (TextView) findViewById(R.id.pressure); | |
| text.setText(String.valueOf(pressure) + "Db"); | |
| } | |
| }); | |
| return true; | |
| } | |
| @Override | |
| public void processingFinished() { | |
| } | |
| }; | |
| dispatcher.addAudioProcessor(soundPressureProcessor); | |
| PitchDetectionHandler pitchDetection = new PitchDetectionHandler() { | |
| @Override | |
| public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) { | |
| final float pitch = pitchDetectionResult.getPitch(); | |
| runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| TextView text = (TextView) findViewById(R.id.pitch); | |
| text.setText(String.valueOf(pitch) + "Hz"); | |
| } | |
| }); | |
| } | |
| }; | |
| AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pitchDetection); | |
| dispatcher.addAudioProcessor(pitchProcessor); | |
| new Thread(dispatcher, "Dispatcher").start(); | |
| } | |
| // see https://developer.android.com/training/permissions/requesting.html | |
| private void requestRecordAudioPermission() { | |
| int apiVersion = Build.VERSION.SDK_INT; | |
| if (apiVersion > Build.VERSION_CODES.LOLLIPOP) { | |
| ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.RECORD_AUDIO}, 1); | |
| } | |
| } | |
| @Override | |
| public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { | |
| switch (requestCode) { | |
| case 1: { | |
| if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| Log.d(TAG, "Permission granted"); | |
| } | |
| else { | |
| Log.d(TAG, "Permission denied"); | |
| finish(); | |
| } | |
| } | |
| } | |
| } | |
| } |