import javax.imageio.*; import java.awt.image.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.*; import java.util.regex.*; import java.util.List; import java.util.*; 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; } } public class main { static double gain=5.0; static Object androidContext; public static void main(String[] args) throws Exception { SoundMeter sm = new SoundMeter(); 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(); } static void setWidgetColor(int col) { int w=1, h=1; int[] pixels = new int[w*h]; for (int i=0; i < w*h; i++) pixels[i] = col; Bitmap bitmap = Bitmap.createBitmap(pixels, w, h, Bitmap.Config.RGB_565); call(androidContext, "updateWidgetWithBitmap", bitmap); } static void goToHomeScreen() { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); call(androidContext, "startActivity", startMain); } static Object call(Object o, String method, Object... args) { try { Method m = call_findMethod(o, method, args, false); m.setAccessible(true); return m.invoke(o, args); } catch (Exception e) { throw new RuntimeException(e); } } static Object call(Class c, String method, Object... args) { try { Method m = call_findStaticMethod(c, method, args, false); m.setAccessible(true); return m.invoke(null, args); } catch (Exception e) { throw new RuntimeException(e); } } static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) { while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");; if (!m.getName().equals(method)) { if (debug) System.out.println("Method name mismatch: " + method); continue; } if ((m.getModifiers() & Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug)) continue; return m; } c = c.getSuperclass(); } throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + c.getName()); } static Method call_findMethod(Object o, String method, Object[] args, boolean debug) { Class c = o.getClass(); while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");; if (m.getName().equals(method) && call_checkArgs(m, args, debug)) return m; } c = c.getSuperclass(); } throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName()); } private static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || types[i].isInstance(args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } }