User Defined Effect

This example is for Processing version 1.0+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

User Defined Signal by Damien Di Fede.

This sketch demonstrates how to implement your own AudioSignal for Minim. See MouseSaw.pde for the implementation.


import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
MouseSaw msaw;

void setup()
{
  size(512, 200, P2D);
  
  minim = new Minim(this);
  
  out = minim.getLineOut(Minim.STEREO, 2048);
  msaw = new MouseSaw();
  // adds the signal to the output
  out.addSignal(msaw);
}

void draw()
{
  background(0);
  stroke(255);
  // draw the waveforms
  for(int i = 0; i < out.bufferSize()-1; i++)
  {
    float x1 = map(i, 0, out.bufferSize(), 0, width);
    float x2 = map(i+1, 0, out.bufferSize(), 0, width);
    line(x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line(x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }
}

void stop()
{
  out.close();
  minim.stop();
  
  super.stop();
}



// this signal uses the mouseX and mouseY position to build a signal
class MouseSaw implements AudioSignal
{
  void generate(float[] samp)
  {
    float range = map(mouseX, 0, width, 0, 1);
    float peaks = map(mouseY, 0, height, 1, 20);
    float inter = float(samp.length) / peaks;
    for ( int i = 0; i < samp.length; i += inter )
    {
      for ( int j = 0; j < inter && (i+j) < samp.length; j++ )
      {
        samp[i + j] = map(j, 0, inter, -range, range);
      }
    }
  }
  
  // this is a stricly mono signal
  void generate(float[] left, float[] right)
  {
    generate(left);
    generate(right);
  }
}