visualise audio input with processing for android

edited November 2013 in Share Your Work

a sample app to visualise audio signal on android phone

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


final int RECORDER_SAMPLERATE = 44100;
final int RECORDER_CHANNELG= AudioFormat.CHANNEL_IN_MONO ;



final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

short[] bufferG = null;

AudioRecord audioRecordG = null;

int bufferSizeG;
int bufferSizeD;
float volume = 0, volumeold=0;
int i;
int largeur, hauteur;
int offset=0;
float zoom=20;
void setup() {
  size(displayWidth, displayHeight, P3D);
  hauteur=displayWidth;
  largeur=displayHeight;
  orientation(PORTRAIT);
  bufferSizeG = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELG, RECORDER_AUDIO_ENCODING);


  audioRecordG = new AudioRecord(MediaRecorder.AudioSource.MIC, 
  RECORDER_SAMPLERATE, RECORDER_CHANNELG, RECORDER_AUDIO_ENCODING, bufferSizeG);



   audioRecordG.startRecording();

  bufferG = new short[bufferSizeG];



  background(0);
  stroke (128, 255, 128);
  fill(0);
}

void draw() {
  zoom=map(mouseY,10,hauteur,1,20);
  if(zoom<=0){
   zoom=1; 
  }

  int crossing=0;
  int bufferReadResultG = audioRecordG.read(bufferG, 0, (bufferSizeG));


  if (offset>=hauteur) {
    offset=0;
    noStroke();
     fill(0);
    rect (0, 0, hauteur, largeur);  

    stroke (52);
    for (int i = 0; i < 11 ; i++) {
      line (0, i*75, hauteur, i*75);
      line (i*75+25, 0, i*75+25, largeur);
    }
    stroke (0);
    line (hauteur/2, 0, hauteur/2, largeur);
    line (0, largeur/2, hauteur, largeur/2);
    stroke (128, 255, 128);
  }

  // draw the waveforms so we can see what we are monitoring
  for (int i = 0; i < (bufferSizeG) - 1 && i<hauteur+crossing; i++)
  {
    if (crossing==0&&bufferG[i]<0&&bufferG[i+1]>0) {         
      crossing=i;
    }
    if (crossing!=0) {
      line( int((i-crossing)/zoom)+offset, largeur/2 + bufferG[i]/(mouseX+1), int((i+1-crossing)/zoom)+offset, largeur/2 + bufferG[i+1]/(mouseX+1));
    }
  }

   offset=int(hauteur/zoom+offset)-1;
  if(hauteur/zoom==0){
  offset=1+offset;
  }

}

void stop() {
  audioRecordG.stop();
  audioRecordG.release();

}
Sign In or Register to comment.