Libraryless. Click here for Pure Java version (178L/2K/6K).
1 | !636 |
2 | !quicknew |
3 | !standard functions |
4 | |
5 | import android.media.AudioFormat; |
6 | import android.media.AudioRecord; |
7 | import android.media.MediaRecorder; |
8 | import android.app.Activity; |
9 | import android.widget.*; |
10 | import android.graphics.*; |
11 | import android.content.*; // Intent etc. |
12 | |
13 | |
14 | |
15 | class SoundMeter { |
16 | |
17 | private AudioRecord ar = null; |
18 | private int minSize; |
19 | |
20 | public void start() { |
21 | minSize= AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); |
22 | ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize); |
23 | ar.startRecording(); |
24 | } |
25 | |
26 | public void stop() { |
27 | if (ar != null) { |
28 | ar.stop(); |
29 | } |
30 | } |
31 | |
32 | public double getAmplitude() { |
33 | short[] buffer = new short[minSize]; |
34 | ar.read(buffer, 0, minSize); |
35 | int max = 0; |
36 | for (short s : buffer) |
37 | { |
38 | if (Math.abs(s) > max) |
39 | { |
40 | max = Math.abs(s); |
41 | } |
42 | } |
43 | return max; |
44 | } |
45 | |
46 | } |
47 | |
48 | main { |
49 | static double gain=5.0; |
50 | |
51 | static Object androidContext; |
52 | |
53 | psvm { |
54 | new SoundMeter sm; |
55 | |
56 | sm.start(); |
57 | goToHomeScreen (); |
58 | double highest = 1.0; |
59 | for (int i = 0; i < 10000; i ++) { |
60 | |
61 | double amp = sm.getAmplitude(); |
62 | highest = Math.max(highest, amp); |
63 | int col = (int) Math.min(255, Math.floor(amp/highest*gain*255)); |
64 | setWidgetColor(col); |
65 | } |
66 | sm.stop(); |
67 | } |
68 | } |
Began life as a copy of #1000439
download show line numbers debug dex old transpilations
Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, exkalrxbqyxc, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, mrjhfnjfopze, nrtiiiyxqhmw, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
ID | Author/Program | Comment | Date |
---|---|---|---|
183 | #1000604 (pitcher) | 2015-08-18 00:50:10 | |
177 | #1000610 (pitcher) | 2015-08-18 00:49:56 |
Snippet ID: | #1000440 |
Snippet name: | Graphical sound meter |
Eternal ID of this version: | #1000440/1 |
Text MD5: | 2ad7ae9fdf76f8f6979b9d5d89df383e |
Transpilation MD5: | 6acec68b68fa61bf9a01c94adb4f1222 |
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-17 13:41:57 |
Source code size: | 1539 bytes / 68 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 995 / 945 |
Referenced in: | [show references] |