Libraryless. Click here for Pure Java version (57L/1K/2K).
1 | !636 |
2 | !quicknew |
3 | |
4 | import android.media.AudioFormat; |
5 | import android.media.AudioRecord; |
6 | import android.media.MediaRecorder; |
7 | |
8 | main {
|
9 | psvm {
|
10 | new SoundMeter sm; |
11 | sm.start(); |
12 | for (int i = 0; i < 20; i ++) {
|
13 | Thread.sleep(500); |
14 | System.out.println(sm.getAmplitude()); |
15 | } |
16 | sm.stop(); |
17 | } |
18 | } |
19 | |
20 | class SoundMeter {
|
21 | |
22 | private AudioRecord ar = null; |
23 | private int minSize; |
24 | |
25 | public void start() {
|
26 | minSize= AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); |
27 | ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize); |
28 | ar.startRecording(); |
29 | } |
30 | |
31 | public void stop() {
|
32 | if (ar != null) {
|
33 | ar.stop(); |
34 | } |
35 | } |
36 | |
37 | public double getAmplitude() {
|
38 | short[] buffer = new short[minSize]; |
39 | ar.read(buffer, 0, minSize); |
40 | int max = 0; |
41 | for (short s : buffer) |
42 | {
|
43 | if (Math.abs(s) > max) |
44 | {
|
45 | max = Math.abs(s); |
46 | } |
47 | } |
48 | return max; |
49 | } |
50 | |
51 | } |
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: | 814 / 829 |
| Referenced in: | [show references] |