11

I am trying to record the sound from the MIC and draw a live graph. I am able to record and draw the graph. The problem is the values that are recorded using the code below are not accurate for example ... the image below is what i get when there is no sound at all present. I have seen examples using the fft but I am noot sure if that will be of any help in my case as I am trying to draw a time domain graph and I see no purpose to convert it to frequency domain (for now). Others are using average power, this might be helpful but I am not sure.

Thanks for any help.

enter image description here

bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

    short data [] = new short[bufferSize];

    while (!Thread.interrupted()) {

        recorder.startRecording();

        recorder.read(data, 0, bufferSize);

        recorder.stop();

        for (short s : data)
        {
            try {
                Thread.sleep((long) 300.00);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            postUI (Math.abs(s));
        }
    }

    recorder.release();
user591124
  • 497
  • 2
  • 9
  • 23
  • hi how did you get amplitude of sound in audiorecorder please help – Aashish Bhatnagar Jul 27 '12 at 07:34
  • 2
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize); recorder.startRecording(); recorder.read(data, 0, bufferSize); recorder.stop(); for (short s : data) { average += Math.abs(s); } – user591124 Aug 10 '12 at 09:54

2 Answers2

1

Try using a higher sampling rate. The maximum is 48000, but 44100 is standard.

Also, it is possible the microphone is just recording background noise.

gregm
  • 11,719
  • 7
  • 55
  • 75
0

This is @Gourneau's answer but with code

AudioRecord recorder; // our recorder, must be initialized first
short[] buffer; // buffer where we will put captured samples
DataOutputStream output; // output stream to target file
boolean isRecording; // indicates if sound is currently being captured
ProgressBar pb; // our progress bar recieved from layout
while (isRecording) {
    double sum = 0;
    int readSize = recorder.read(buffer, 0, buffer.length);
    for (int i = 0; i < readSize; i++) {
        output.writeShort(buffer [i]);
        sum += buffer [i] * buffer [i];
    }
    if (readSize > 0) {
        final double amplitude = sum / readSize;
        pb.setProgress((int) Math.sqrt(amplitude));
    }
}

http://web.archive.org/web/20140709183733/http://developer.samsung.com/android/technical-docs/Displaying-Sound-Volume-in-Real-Time-While-Recording

Trancer
  • 717
  • 2
  • 8
  • 23