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)

import ddf.minim.*;
import ddf.minim.signals.*;
 
// 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);
  }
}
 
AudioOutput out;
MouseSaw msaw;
 
void setup()
{
  size(512, 200);
 
  Minim.start(this);
  out = Minim.getLineOut(Minim.STEREO, 512);
  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.left.size()-1; i++)
  {
    line(i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50);
    line(i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50);
  }
}
 
void stop()
{
  // always close minim classes when you are done with them
  out.close();
  // always stop Minim before exiting
  Minim.stop();
 
  super.stop();
}