Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.karma.freqsensor;
- import android.media.AudioFormat;
- import android.media.AudioRecord;
- import android.media.MediaRecorder.AudioSource;
- import android.util.Log;
- public class Recorder {
- private AudioRecord _input;
- private Thread _recordingThread;
- private short[] _inputBuffer;
- private Complex[] _fftWindow;
- SignalDetectedListener signalDetectedListener;
- double threshold;
- int channel_config = AudioFormat.CHANNEL_IN_MONO;
- int format = AudioFormat.ENCODING_PCM_16BIT;
- int sampleSize = 44100;
- int bufferSize = AudioRecord.getMinBufferSize(sampleSize, channel_config, format);
- long lastDetectionTimeMillis;
- public interface SignalDetectedListener {
- public void onSignalDetected(double magnitude);
- }
- public Recorder(SignalDetectedListener signalDetectedListener, double threshold) {
- this.signalDetectedListener = signalDetectedListener;
- this.threshold = threshold;
- _inputBuffer = new short[bufferSize];
- }
- public void start() {
- if (_recordingThread == null) {
- _recordingThread = new Thread(new Runnable() {
- @Override
- public void run() {
- _input = new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
- _input.startRecording();
- int counter = 0;
- while (!Thread.currentThread().isInterrupted()) {
- _input.read(_inputBuffer, 0, _inputBuffer.length);
- scaleInput(_inputBuffer, 512, 100.0);
- Complex[] fftOutput = FFT.fft(_fftWindow);
- double targetFrequency = 20000;
- int bin = (int) Math.round(targetFrequency * _fftWindow.length / 44100);
- double fft20KHzRe = fftOutput[bin].re();
- double fft20KHzIm = fftOutput[bin].im();
- double magn20KHz = Math.sqrt(fft20KHzIm * fft20KHzIm + fft20KHzRe * fft20KHzRe);
- double phase20KHz = Math.atan2(fft20KHzIm, fft20KHzRe);
- if (counter % 20 == 0)
- Log.d("FFTMagic", "Window: " + _fftWindow.length + " Bin: " + bin + " Magnitude: " + Math.round(magn20KHz)
- + " Phase: " + Math.round(phase20KHz / Math.PI * 2));
- if (magn20KHz > threshold && signalDetectedListener != null) {
- if (lastDetectionTimeMillis == 0)
- lastDetectionTimeMillis = System.currentTimeMillis();
- if (System.currentTimeMillis() - lastDetectionTimeMillis > 1000) {
- signalDetectedListener.onSignalDetected(magn20KHz);
- break;
- }
- } else if (magn20KHz < threshold)
- lastDetectionTimeMillis = 0;
- counter++;
- }
- _input.stop();
- _input.release();
- _input = null;
- }
- });
- _recordingThread.start();
- }
- }
- public void stop() {
- if (_recordingThread != null) {
- _recordingThread.interrupt();
- _recordingThread = null;
- }
- }
- private void scaleInput(short[] input, int fftWindowSize, double amplification) {
- if (_fftWindow == null || _fftWindow.length != fftWindowSize) {
- _fftWindow = new Complex[fftWindowSize];
- }
- for (int i = 0; i < fftWindowSize; ++i) {
- _fftWindow[i] = new Complex(input[i] * amplification / 32768.0, 0.0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement