Minim core
ugens
analysis
Name FilePlayer
Description The FilePlayer UGen provides a way for you to play audio files in the same way that AudioPlayer does, allowing you to patch them into a UGen graph any way you choose. The constructor for FilePlayer takes an AudioRecordingStream, which you can get from a Minim object by calling the loadFileStream method.
Examples
/**
  This is an example of how to use a FilePlayer UGen to play an audio file. It support all of the same formats that 
  AudioPlayer does, but allows you to insert the audio from the file into a UGen chain. FilePlayer provides all the 
  same methods that AudioPlayer does for controlling the file playback: play(), loop(), cue(int position), etc.
  <p>
  Press any key to pause and unpause playback!
  <p>
  For more information about Minim and additional features, visit http://code.compartmental.net/minim/
  <p>  
  author: Damien Di Fede
*/

import ddf.minim.*;
import ddf.minim.spi.*; // for AudioRecordingStream
import ddf.minim.ugens.*;

// declare everything we need to play our file
Minim minim;
FilePlayer filePlayer;
AudioOutput out;

// you can use your own file by putting it in the data directory of this sketch
// and changing the value assigned to fileName here.
String fileName = "groove.mp3";

void setup()
{
  // setup the size of the app
  size(640, 240);
  
  // create our Minim object for loading audio
  minim = new Minim(this);
                                                  
  // a FilePlayer reads from an AudioRecordingStream, which we 
  // can easily get from Minim using loadFileStream
  filePlayer = new FilePlayer( minim.loadFileStream(fileName) );
  // and then we'll tell the recording to loop indefinitely
  filePlayer.loop();
  
  // get a line out from Minim. It's important that the file is the same audio format 
  // as our output (i.e. same sample rate, number of channels, etc).
  out = minim.getLineOut();
  
  // patch the file player to the output
  filePlayer.patch(out);
                        
}

// keyPressed is called whenever a key on the keyboard is pressed
void keyPressed()
{
  // you can query whether the file is playing or not
  // playing simply means that it is generating sound
  // this will be true if you tell it to play() or loop()
  if ( filePlayer.isPlaying() )
  {
    // pauses playback of the file
    filePlayer.pause();
  }
  else
  {
    // starts the file looping again, picking up where we left off
    filePlayer.loop();
  }
}

// draw is run many times
void draw()
{
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i < out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    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);
  }  
  
  text( "loopCount: " + filePlayer.loopCount(), 15, 15 );
}
Constructors
FilePlayer(iFileStream);
Parameters
iFileStream   AudioRecordingStream: the stream this should read from
Methods
channelCount ( )   Returns the number of channels this UGen has been configured to generate.

close ( )   Calling close will close the AudioRecordingStream that this wraps, which is proper cleanup for using the stream.

cue ( )   Sets the position to millis milliseconds from the beginning.

getLastValues ( )   Return the last values generated by this UGen. This will most often be used by sub-classes when pulling data from their inputs.

getMetaData ( )   Returns the meta data for the recording being played by this player.

getStream ( )   Returns the underlying AudioRecordingStream.

isLooping ( )   Returns true if this is currently playing and has more than one loop left to play.

isPlaying ( )   Returns true if this currently playing.

length ( )   Returns the length of the sound in milliseconds.

loop ( )   Start looping playback of the file.

loopCount ( )   Returns the number of loops left to do.

patch ( )   Send the output of this UGen to another UGen, UGenInput, or AudioOutput.

pause ( )   Pauses playback.

play ( )   Starts playback from the current position. If this was previously set to loop, looping will be disabled.

position ( )   Returns the current position of the "playhead" (ie how much of the sound has already been played)

printInputs ( )   Prints all inputs connected to this UGen (for debugging)

rewind ( )   Rewinds to the beginning. This does not stop playback.

sampleRate ( )   Returns the sample rate of this UGen.

setChannelCount ( )   Let this UGen know how many channels of audio you will be asking it for.

setLoopPoints ( )   Sets the loop points used when looping.

setSampleRate ( )   Set the sample rate for this UGen.

skip ( )   Skips millis from the current position.

tick ( )   Generates one sample frame for this UGen.

unpatch ( )   Unpatch this UGen from an AudioOutput or other UGen.

Usage Web & Application
Related Minim
AudioPlayer
UGen