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

68
LINES

< > BotCompany Repo | #1000440 // Graphical sound meter

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

Libraryless. Click here for Pure Java version (178L/2K/6K).

!636
!quicknew
!standard functions

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.app.Activity;
import android.widget.*;
import android.graphics.*;
import android.content.*; // Intent etc.



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;
    }

}

main {
  static double gain=5.0;

  static Object androidContext;

  psvm {
    new SoundMeter sm;

    sm.start();
    goToHomeScreen ();
double highest = 1.0;
    for (int i = 0; i < 10000; i ++) {
    
      double amp = sm.getAmplitude();
      highest = Math.max(highest, amp);
      int col = (int) Math.min(255, Math.floor(amp/highest*gain*255));
      setWidgetColor(col);
    }
    sm.stop();
  }
}

Author comment

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

Comments [hide]

ID Author/Program Comment Date
183 #1000604 (pitcher) 2015-08-18 00:50:10
177 #1000610 (pitcher) 2015-08-18 00:49:56

add comment

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: 909 / 841
Referenced in: [show references]