0

I want to get frequency from my audio recording simultaneously

here is my code:

My SoundProcessing .java and Main class

public class SoundProcessing extends Activity {

private WaveformView mRealtimeWaveformView;
private RecordingThread mRecordingThread;
private PlaybackThread mPlaybackThread;
private static final int REQUEST_RECORD_AUDIO = 13;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_sound_processing);

    mRealtimeWaveformView = (WaveformView) findViewById(R.id.waveformView);
    mRecordingThread = new RecordingThread(new AudioDataReceivedListener() {
        @Override
        public void onAudioDataReceived(short[] data) {
            mRealtimeWaveformView.setSamples(data);
        }
    });
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!mRecordingThread.recording()) {
                startAudioRecordingSafe();
            } else {
                mRecordingThread.stopRecording();
            }
        }
    });
}

and My RecordingThread.java:

public class RecordingThread {

private static final String LOG_TAG = RecordingThread.class.getSimpleName();
private static final int SAMPLE_RATE = 44100;

public RecordingThread(AudioDataReceivedListener listener) {
    mListener = listener;
}

private boolean mShouldContinue;
private AudioDataReceivedListener mListener;
private Thread mThread;

public boolean recording() {
    return mThread != null;
}

public void startRecording() {
    if (mThread != null)
        return;

    mShouldContinue = true;
    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            record();
        }
    });
    mThread.start();
}

public void stopRecording() {
    if (mThread == null)
        return;

    mShouldContinue = false;
    mThread = null;
}

private void record() {
    Log.v(LOG_TAG, "Start");
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);

    // buffer size in bytes
    int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    if (bufferSize == AudioRecord.ERROR || bufferSize == AudioRecord.ERROR_BAD_VALUE) {
        bufferSize = SAMPLE_RATE * 2;
    }

    short[] audioBuffer = new short[bufferSize / 2];

    AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
            SAMPLE_RATE,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            bufferSize);

    if (record.getState() != AudioRecord.STATE_INITIALIZED) {
        Log.e(LOG_TAG, "Audio Record can't initialize!");
        return;
    }
    record.startRecording();

    Log.v(LOG_TAG, "Start recording");

    long shortsRead = 0;
    while (mShouldContinue) {
        int numberOfShort = record.read(audioBuffer, 0, audioBuffer.length);
        shortsRead += numberOfShort;

        // Notify waveform
        mListener.onAudioDataReceived(audioBuffer);
    }

    record.stop();
    record.release();

    Log.v(LOG_TAG, String.format("Recording stopped. Samples read: %d", shortsRead));
}
}

and my AudioDataReceivedListener.java

public interface AudioDataReceivedListener {

    void onAudioDataReceived(short[] data);
}
Nikoo
  • 1
  • 3

1 Answers1

1

In order to get the frequency of a signal you have to analyse it for example with a Fourier transformation. In technical applications you use the Fast/Discrete Fourier Transformation (FFT). Raw audio is a signal recorded over time, the FFT enables you to see which frequencies are included in your signal.

In your case in means you need to apply a FFT on audioBuffer. There are Java classes and also C/C++ code (with NDK). For real time applications C/C++ should be faster.

Martin
  • 80
  • 8