Manual: Recordable

[ javadoc | examples ]

The Recordable interface defines functionality that provides a program with floating point samples of the audio passing through it. It does this using AudioListeners. You add listeners to the Recordable and then the Recordable will call the appropriate samples method of all its listeners when it has a new buffer of samples. It is also possible to query a Recordable object for its buffer size, type (mono or stereo), and audio format. Sample values are between -1 and 1, inclusive. Recordable is implemented by AudioSource and SignalSplitter.

Adding and Removing AudioListeners

addListener(AudioListener al)
removeListener(AudioListener al)

The above methods can be called on a Recordable to add or remove an instance of a class that implements AudioListener. When a listener has been added to a Recordable, one of it’s samples methods will be called each time the Recordable generates a new buffer of samples or receives a new buffer of samples. This is the best way to monitor input or output. An AudioListener cannot be added more than once to a Recordable.

Code Sample (online example)

import ddf.minim.*;
 
AudioPlayer groove;
// this is a class that implements AudioListener
WaveformRenderer waveform;
 
void setup()
{
  size(512, 200);
  // always start Minim first
  Minim.start(this);
  groove = Minim.loadFile("groove.mp3", 512);
  groove.loop();
  waveform = new WaveformRenderer();
}
 
void draw()
{
  background(0);
  waveform.draw();
}
 
void keyPressed()
{
  if ( key == 'a' )
  {
    groove.addListener(waveform);
  }
  if ( key == 'r' )
  {
    groove.removeListener(waveform);
  }  
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  groove.close();
  // always stop Minim before exiting
  Minim.stop();
 
  super.stop();
}

The format of a Recordable

A Recordable object can give you information about the kind of audio that it represents. These properties are accessed by the following methods:

int bufferSize()
int sampleRate()
int type()
AudioFormat getFormat()

bufferSize() returns the length of the float buffers sent to AudioListeners. sampleRate() returns the sample rate, in Hz, of the audio that the Recordable represents, such as 44100 or 96000. type() returns the number of channels in the audio, which will be either 1 or 2 and can be compared to Minim.MONO and Minim.STEREO for readability. getFormat() returns an AudioFormat object that contains the sample rate and type, as well as some extra information. Specifically, getFormat().getChannels() will return the same value as type() and getFormat().getSampleRate() will return the same value as sampleRate().

Code Sample (online example)

import ddf.minim.*;
 
AudioOutput out;
 
void setup()
{
  size(760, 140);
  smooth();
  textFont(loadFont("CourierNewPSMT-12.vlw"));
  // always start Minim first
  Minim.start(this);
  // this should give us a stereo output with a 1024 sample buffer, 
  // a sample rate of 44100 and a bit depth of 16
  out = Minim.getLineOut();
}
 
void draw()
{
  background(0);
  text("The output has " + out.getFormat().getChannels() + " channels.", 5, 15);
  text("The output's encoding is " + out.getFormat().getEncoding() + ".", 5, 30);
  text("The output's frame rate is " + out.getFormat().getFrameRate() + " frames per second.", 5, 45);
  text("The output's frame size is " + out.getFormat().getFrameSize() + " bytes.", 5, 60);
  text("The output's sample rate is " + out.getFormat().getSampleRate() + " Hz.", 5, 75);
  text("The output's sample size is " + out.getFormat().getSampleSizeInBits() + " bits.", 5, 90);
  String endianess = out.getFormat().isBigEndian() ? "big-endian" : "little-endian";
  text("The output's byte order is " + endianess + ".", 5, 105);
  text("The output's format as a string is " + out.getFormat(), 5, 120);
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  out.close();
  // always stop Minim before exiting
  Minim.stop();
  super.stop();
}