0

enter code hereI am trying to get realtime amplitude. The data is coming from a temp sensor connected to audiojack via MIC. I need to use AudioRecord as I need the data as it is(Raw data). The code I used is as below:

public class MainActivity extends Activity {
private static final String LOG_TAG = "AudioRecordTest";
private static final float VISUALIZER_HEIGHT_DIP = 50f;
private static String mFileName = null;
private float temperature;

double sDataMax = 0;
String sDataString = "";

private boolean isRecording = false;

private Button mRecordButton = null;
private boolean mStartRecording = true;
private AudioRecord mRecorder = null;

private TextView Output;

private static final int samplingRate = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING =      AudioFormat.ENCODING_PCM_16BIT;


private void onRecord(boolean start) {
    if (start) {
        startRecording();

    } else {
        stopRecording();
    }
}
int FileSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnRecord, !isRecording);

}


public void startRecording() {

     mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
          samplingRate, RECORDER_CHANNELS,
        RECORDER_AUDIO_ENCODING, FileSize*10);


    mRecorder.startRecording();
    isRecording = true;


}
private void writeAudioDataToFile(File logFile) {
    // Write the output audio in byte

    short sData[] = new short[FileSize];
    while (isRecording) {
        // gets the voice output from microphone to byte format

        mRecorder.read(sData, 0, FileSize);


        for (int i = 0; i < sData.length; i++) {
            if (Math.abs(sData[i]) >= sDataMax) {
                sDataMax = Math.abs(sData[i]);
            }
            ;
            sDataString += "\n" + sDataMax;

        }
SidK
  • 11
  • 1

0 Answers0