Manual: AudioSample

[ javadoc | examples ]

AudioSample can be thought of like a sample in a drum machine. It is meant to be used for short sounds that simply need to be triggered and then forgotten about. An example of this might be all of the sound effects for a space shooter game. When you load an audio file using loadSample the audio will be loaded completely into memory and then triggered from there.

Code Sample (online example)

/**
  * This sketch demonstrates how to use the <code>trigger</code> method of an <code>AudioSample</code>. <br />
  * <code>AudioSample</code>s can only be triggered, not cue'd and looped
  * or anything else you might do with an <code>Playable</code> object. The advantage, however, is that
  * an <code>AudioSample</code> can be retriggered while it is still playing, which will cause the sample to
  * overlap with itself.
  * <p>
  * Use 'k' and 's' to trigger a kick drum sample and a snare sample, respectively. You will see their waveforms
  * drawn when they are played back.
  */
 
import ddf.minim.*;
 
Minim minim;
AudioSample kick;
AudioSample snare;
 
void setup()
{
  size(512, 200, P3D);
  minim = new Minim(this);
  // load BD.wav from the data folder
  kick = minim.loadSample("BD.mp3", 2048);
  if ( kick == null ) println("Didn't get kick!");
  // load SD.wav from the data folder
  snare = minim.loadSample("SD.wav", 2048);
  if ( snare == null ) println("Didn't get snare!");
}
 
void draw()
{
  background(0);
  stroke(255);
  // use the mix buffer to draw the waveforms.
  for (int i = 0; i < kick.bufferSize() - 1; i++)
  {
    float x1 = map(i, 0, kick.bufferSize(), 0, width);
    float x2 = map(i+1, 0, kick.bufferSize(), 0, width);
    line(x1, 50 - kick.mix.get(i)*50, x2, 50 - kick.mix.get(i+1)*50);
    line(x1, 150 - snare.mix.get(i)*50, x2, 150 - snare.mix.get(i+1)*50);
  }
}
 
void keyPressed()
{
  if ( key == 's' ) snare.trigger();
  if ( key == 'k' ) kick.trigger();
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  kick.close();
  snare.close();
  minim.stop();
 
  super.stop();
}

Sample Access

AudioSample is similar to AudioSnippet because all of the audio is loaded into memory. However, unlike AudioSnippet the audio is stored in memory as normalized floats so that the samples can be made available to the user. You can get a channel of audio from an AudioSample as a float array by using getChannel and passing in BufferedAudio.LEFT or BufferedAudio.RIGHT. This method will give you the actual samples used for playback, so you can modify the samples, as shown in the example below. The change will be heard the next time you trigger the sample. One current limitation is that you can’t change the length of the array, however support for that will probably be provided in a future release.

Code Sample (online example)

/**
  * This sketch demonstrates how to get a channel of audio from an AudioSample and then manipulate it to change the AudioSample after it
  * has been loaded.
  */
 
import ddf.minim.*;
 
Minim minim;
AudioSample jingle;
 
void setup()
{
  size(512, 200, P3D);
 
  minim = new Minim(this);
 
  jingle = minim.loadSample("jingle.mp3", 2048);
  // get the left channel of the audio as a float array
  // getChannel is defined in the interface BuffereAudio, 
  // which also defines two constants to use as an argument
  // BufferedAudio.LEFT and BufferedAudio.RIGHT
  float[] leftChannel = jingle.getChannel(BufferedAudio.LEFT);
  // now we are just going to reverse the left channel
  float[] reversed = reverse(leftChannel);
  arraycopy(reversed, 0, leftChannel, 0, leftChannel.length);
}
 
void draw()
{
  background(0);
  stroke(255);
  for(int i = 0; i < jingle.bufferSize() - 1; i++)
  {
    line(i, 50 - jingle.left.get(i)*50, i+1, 50 - jingle.left.get(i+1)*50);
    line(i, 150 - jingle.right.get(i)*50, i+1, 150 - jingle.right.get(i+1)*50);
  }
}
 
void keyPressed()
{
  jingle.trigger();
}
 
void stop()
{
  // always close Minim audio classes when you finish with them
  jingle.close();
  minim.stop();
 
  super.stop();
}

Sample Info

You can find out how long an AudioSample is in milliseconds by calling length(). However, AudioSample also allows you to get an AudioMetaData by calling getMetaData(), which will also allow you to access the length of the sample.

Other Controls

AudioSample derives from AudioSource, this is where it gets its Recordable and Effectable interfaces from. AudioSource derives from Controller and as such AudioSample has all of its methods available, which are described in the section about Controller.