Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1000439 // Sound meter

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (57L/1K/2K).

!636
!quicknew

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

main {
  psvm {
    new SoundMeter sm;
    sm.start();
    for (int i = 0; i < 20; i ++) {
    Thread.sleep(500);
    System.out.println(sm.getAmplitude());
    }
    sm.stop();
  }
}

class SoundMeter {

    private AudioRecord ar = null;
    private int minSize;

    public void start() {
        minSize= AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);
        ar.startRecording();
    }

    public void stop() {
        if (ar != null) {
            ar.stop();
        }
    }

    public double getAmplitude() {
        short[] buffer = new short[minSize];
        ar.read(buffer, 0, minSize);
        int max = 0;
        for (short s : buffer)
        {
            if (Math.abs(s) > max)
            {
                max = Math.abs(s);
            }
        }
        return max;
    }

}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, exkalrxbqyxc, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000439
Snippet name: Sound meter
Eternal ID of this version: #1000439/1
Text MD5: 9e5b0659ec3fdad2dca4fbb77bf10fe5
Transpilation MD5: 2d4c0c56aaa8d69d31936c0b0ab089ef
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-06 03:25:05
Source code size: 1154 bytes / 51 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 516 / 458
Referenced in: [show references]