How to make opposite wave player

edited December 2017 in Android Mode

Hi. I'm trying to make a program that plays the exact opposite of a sound wave. But before that, I need to learn how to properly access the microphone. Here's the sketch I'm trying:

import android.os.Bundle;
short[] buffer = null;
AudioRecord audioRecord = null;
int bufferSize= 1024;
float volume = 0;
int buflen;
void setup() {
  int freq =44100;
  int chan = AudioFormat.CHANNEL_IN_MONO;
  int enc  = AudioFormat.ENCODING_PCM_16BIT;
  int src  = MediaRecorder.AudioSource.MIC;
  buflen = AudioRecord.getMinBufferSize(freq, chan, enc);
  bufferSize = buflen * 2;
  audioRecord = new AudioRecord(src, freq, chan, enc, buflen);
  buffer = new short[bufferSize];
void draw() {
  background(200);, 0, bufferSize);
  volume = abs(buffer[0]);
  text("" + volume, 100, 100);
  //draw a box if you blow the mic
  if (volume>300) {
    rect(10, 10, 20, 20);
void stop() {
  audioRecord = null;

But when I tried it, I got this exeption:

ION: Animation Thread
Process: processing.test.sketch_171211c, PID: 11072
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
    at processing.test.sketch_171211c.sketch_171211c.setup(
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PSurfaceNone.callDraw(Unknown Source)
    at processing.core.PSurfaceNone$ Source)

How should I fix it?

I am running a BLU phone.



  • @KTibow===

    • Are you ok with permissions?

    • try to verify that your audiobuffer length is not <0 (use try catch for that)

    • try to add some condition boolean before start recording (the audioRecord class has constant for that)

    something like: if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {"notReady"=true}

    • try to create a thread for recording (though your message error is not about that)
  • Okay, I'll look at my permissions. And I'll see what else there might be.

Sign In or Register to comment.