How to reverse audio wave using processing

1.9k views Asked by At

Is there a way to analyize the audio recorded by the application and reverse its wave? for example in Analog Audio the wave of sound is like a sinwave either 0,1,-1. I want to reverse that so that 1 will be -1 and the -1 be 1. How to do that using processing software?

1

There are 1 answers

0
Adam Tindale On

Nikos is correct that the operation you are looking for is called Invert and not reverse. This achieved simply by multiplying every sample by -1.

The best way to do this is to use Minim, processing's audio library. You can extend the UGen class in order to make a new effects processor that flips every sample that goes through it. I've included an example below that works with a sine wave. You can change this around to be some other audio source and to draw it however you like.

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;

void setup()
{
  size(300, 200, P2D);

  minim = new Minim(this);
  out = minim.getLineOut();

  Oscil osc;
  Invert inv;
  Constant cutoff;

  // initialize the oscillator 
  // (a sawtooth wave has energy across the spectrum)
  osc = new Oscil(500, 0.2, Waves.SINE);  
  inv = new Invert();
  osc.patch(inv).patch(out);
}

void draw()
{
  background( 0 );

}

public class Invert extends UGen{

  public UGenInput audio;

  Invert()
  {
    audio = new UGenInput(InputType.AUDIO);
  }

  @Override
  protected void uGenerate (float[] channels)
  {
    if ( audio.isPatched() )
    {
      for (int i = 0; i < channels.length; i++){
        // this is where we multiple each sample by -1
        channels[i] = audio.getLastValues()[i] * -1;
      } 
    }  
  }
}