Manual: AudioSignal

[ javadoc ]

The AudioSignal interface is used by the Polyphonic interface and defines only two methods. These methods are:

void generate(float[] samples)
void generate(float[] samplesLeft, float[] samplesRight)

One of these methods will be called repeatedly by the Polyphonic object that an AudioSignal has been added to, depending upon whether the audio being generated is mono or stereo. The signal is expected to add itself to the signal that may or may not already be present in the arrays. It is not required that the values in the arrays remain in the range [-1, 1]. All of the signals included with Minim implement this interface and all you need to do to write your own signals is to create a class that implements this interface and then add an instance of it to an anything that is Polyphonic, such as an AudioOutput.

Code Sample (online example)

// 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);
  }
}
/**
  * 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, P3D);
 
  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();
}