Advertisement
Guest User

Recorder.java

a guest
Nov 13th, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package com.karma.freqsensor;
  2.  
  3. import android.media.AudioFormat;
  4. import android.media.AudioRecord;
  5. import android.media.MediaRecorder.AudioSource;
  6. import android.util.Log;
  7.  
  8. public class Recorder {
  9.     private AudioRecord _input;
  10.     private Thread _recordingThread;
  11.     private short[] _inputBuffer;
  12.     private Complex[] _fftWindow;
  13.     SignalDetectedListener signalDetectedListener;
  14.     double threshold;
  15.     int channel_config = AudioFormat.CHANNEL_IN_MONO;
  16.     int format = AudioFormat.ENCODING_PCM_16BIT;
  17.     int sampleSize = 44100;
  18.     int bufferSize = AudioRecord.getMinBufferSize(sampleSize, channel_config, format);
  19.     long lastDetectionTimeMillis;
  20.  
  21.     public interface SignalDetectedListener {
  22.         public void onSignalDetected(double magnitude);
  23.     }
  24.  
  25.     public Recorder(SignalDetectedListener signalDetectedListener, double threshold) {
  26.         this.signalDetectedListener = signalDetectedListener;
  27.         this.threshold = threshold;
  28.  
  29.         _inputBuffer = new short[bufferSize];
  30.     }
  31.  
  32.     public void start() {
  33.         if (_recordingThread == null) {
  34.             _recordingThread = new Thread(new Runnable() {
  35.                 @Override
  36.                 public void run() {
  37.                     _input = new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
  38.                     _input.startRecording();
  39.                     int counter = 0;
  40.                     while (!Thread.currentThread().isInterrupted()) {
  41.                         _input.read(_inputBuffer, 0, _inputBuffer.length);
  42.                         scaleInput(_inputBuffer, 512, 100.0);
  43.                         Complex[] fftOutput = FFT.fft(_fftWindow);
  44.                         double targetFrequency = 20000;
  45.                         int bin = (int) Math.round(targetFrequency * _fftWindow.length / 44100);
  46.                         double fft20KHzRe = fftOutput[bin].re();
  47.                         double fft20KHzIm = fftOutput[bin].im();
  48.                         double magn20KHz = Math.sqrt(fft20KHzIm * fft20KHzIm + fft20KHzRe * fft20KHzRe);
  49.                         double phase20KHz = Math.atan2(fft20KHzIm, fft20KHzRe);
  50.                         if (counter % 20 == 0)
  51.                             Log.d("FFTMagic", "Window: " + _fftWindow.length + " Bin: " + bin + " Magnitude: " + Math.round(magn20KHz)
  52.                                     + " Phase: " + Math.round(phase20KHz / Math.PI * 2));
  53.                         if (magn20KHz > threshold && signalDetectedListener != null) {
  54.                             if (lastDetectionTimeMillis == 0)
  55.                                 lastDetectionTimeMillis = System.currentTimeMillis();
  56.                             if (System.currentTimeMillis() - lastDetectionTimeMillis > 1000) {
  57.                                 signalDetectedListener.onSignalDetected(magn20KHz);
  58.                                 break;
  59.                             }
  60.                         } else if (magn20KHz < threshold)
  61.                             lastDetectionTimeMillis = 0;
  62.                         counter++;
  63.                     }
  64.  
  65.                     _input.stop();
  66.                     _input.release();
  67.                     _input = null;
  68.                 }
  69.             });
  70.             _recordingThread.start();
  71.         }
  72.     }
  73.  
  74.     public void stop() {
  75.         if (_recordingThread != null) {
  76.             _recordingThread.interrupt();
  77.             _recordingThread = null;
  78.         }
  79.     }
  80.  
  81.     private void scaleInput(short[] input, int fftWindowSize, double amplification) {
  82.         if (_fftWindow == null || _fftWindow.length != fftWindowSize) {
  83.             _fftWindow = new Complex[fftWindowSize];
  84.         }
  85.  
  86.         for (int i = 0; i < fftWindowSize; ++i) {
  87.             _fftWindow[i] = new Complex(input[i] * amplification / 32768.0, 0.0);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement