Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package com.example.myapplication;
  2. import android.media.MediaRecorder;
  3.  
  4. import java.io.IOException;
  5.  
  6. public class DetectNoise {
  7. static final private double EMA_FILTER = 0.6;
  8.  
  9. private MediaRecorder mRecorder = null;
  10. private double mEMA = 0.0;
  11.  
  12. public void start() {
  13.  
  14. if (mRecorder == null) {
  15. mRecorder = new MediaRecorder();
  16. mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  17. mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  18. mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  19. mRecorder.setOutputFile("/dev/null");
  20. try {
  21. mRecorder.prepare();
  22. } catch (IllegalStateException e) {
  23. e.printStackTrace();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. mRecorder.start();
  28. mEMA = 0.0;
  29. }
  30. }
  31.  
  32. public void stop() {
  33. if (mRecorder != null) {
  34. mRecorder.stop();
  35. mRecorder.release();
  36. mRecorder = null;
  37. }
  38. }
  39.  
  40. public double getAmplitude() {
  41. if (mRecorder != null)
  42. return 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);
  43. else
  44. return 0;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement