net.beadsproject.beads.ugens
Class SamplePlayer

java.lang.Object
  extended by net.beadsproject.beads.core.Bead
      extended by net.beadsproject.beads.core.UGen
          extended by net.beadsproject.beads.ugens.SamplePlayer
Direct Known Subclasses:
GranularSamplePlayer

public class SamplePlayer
extends UGen

SamplePlayer plays back a Sample. Playback rate and loop points can be controlled by UGens. The playback point in the Sample can also be directly controlled from UGen to perform scrubbing. The player can be set to a number of different loop modes. If constructed with a Sample argument, the number of outputs of SamplePlayer is determined by the number of channels of the Sample. Sample playback can use either linear or cubic interpolation.

Author:
ollie

Nested Class Summary
static class SamplePlayer.InterpolationType
          The Enum InterpolationType.
static class SamplePlayer.LoopType
          The Enum LoopType.
 
Field Summary
protected  Sample buffer
          The Sample.
protected  boolean forwards
          Flag for alternating loop mode to determine if playback is in forward or reverse phase.
protected  SamplePlayer.InterpolationType interpolationType
          The interpolation type.
protected  boolean killOnEnd
          Flag to determine whether the SamplePlayer should kill itself when it gets to the end of the Sample.
protected  double lastChangedPosition
          The last changed position, used to determine if the position envelope is in use.
protected  float loopCrossFade
          The loop cross fade in milliseconds.
protected  float loopEnd
          The loop end.
protected  UGen loopEndEnvelope
          The loop end envelope.
protected  float loopStart
          The loop start.
protected  UGen loopStartEnvelope
          The loop start envelope.
protected  SamplePlayer.LoopType loopType
          The loop type.
protected  double position
          The position in milliseconds.
protected  UGen positionEnvelope
          The position envelope.
protected  double positionIncrement
          The millisecond position increment per sample.
protected  float rate
          The rate.
protected  UGen rateEnvelope
          The rate envelope.
protected  float sampleRate
          The sample rate, determined by the Sample.
protected  boolean startLoop
          Flag to determine whether playback starts at the beginning of the sample or at the beginning of the loop.
 
Fields inherited from class net.beadsproject.beads.core.UGen
bufferSize, bufIn, bufOut, context, ins, outs
 
Constructor Summary
protected SamplePlayer(AudioContext context, int outs)
          Instantiates a new SamplePlayer with given number of outputs.
  SamplePlayer(AudioContext context, Sample buffer)
          Instantiates a new SamplePlayer with given Sample.
 
Method Summary
 void calculateBuffer()
          Called by the signal chain to update this UGen's ouput data.
protected  void calculateNextPosition(int i)
          Used at each sample in the perform routine to determine the next playback position.
 Sample getBuffer()
          Gets the Sample.
 SamplePlayer.InterpolationType getInterpolationType()
          Gets the interpolation type.
 boolean getKillOnEnd()
          Determines whether this SamplePlayer will kill itself at the end of the Sample it is playing.
 float getLoopCrossFade()
          Gets the loop cross fade.
 UGen getLoopEndEnvelope()
          Gets the loop end envelope.
 UGen getLoopStartEnvelope()
          Gets the loop start envelope.
 SamplePlayer.LoopType getLoopType()
          Gets the loop type.
 double getPosition()
          Gets the playback position.
 UGen getPositionEnvelope()
          Gets the position envelope.
 UGen getRateEnvelope()
          Gets the rate envelope.
 float getSampleRate()
          Gets the sample rate.
 boolean inLoop()
          Determines whether the playback position is within the loop points.
 void reset()
          Resets the position to the start of the Sample.
 void reTrigger()
          Re trigger the SamplePlayer from the beginning.
protected  void setBuffer(Sample buffer)
          Sets the Sample.
 void setInterpolationType(SamplePlayer.InterpolationType interpolationType)
          Sets the interpolation type.
 void setKillOnEnd(boolean killOnEnd)
          Sets/unsets option for SamplePlayer to kill itself when it reaches the end of the Sample it is playing.
 void setLoopCrossFade(float loopCrossFade)
          Sets the loop cross fade.
 void setLoopEndEnvelope(UGen loopEndEnvelope)
          Sets the loop end envelope.
 void setLoopPointsFraction(float start, float end)
          Sets both loop points to static values as fractions of the Sample length.
 void setLoopStartEnvelope(UGen loopStartEnvelope)
          Sets the loop start envelope.
 void setLoopType(SamplePlayer.LoopType loopType)
          Sets the loop type.
 void setPosition(double position)
          Sets the playback position.
 void setPositionEnvelope(UGen positionEnvelope)
          Sets the position envelope.
 void setRateEnvelope(UGen rateEnvelope)
          Sets the rate envelope.
 void setToEnd()
          Sets the playback position to the end of the Sample.
 void setToLoopStart()
          Sets the playback position to the loop start point.
 void start(float msPosition)
          Starts the sample at the given position.
 
Methods inherited from class net.beadsproject.beads.core.UGen
addDependent, addInput, addInput, clearInputConnections, getContext, getIns, getNumberOfConnectedUGens, getOuts, getValue, getValue, noInputs, pause, printInputList, printOutBuffers, removeAllConnections, removeDependent, setValue, update, zeroIns, zeroOuts
 
Methods inherited from class net.beadsproject.beads.core.Bead
getKillListener, getName, isDeleted, isPaused, kill, message, messageReceived, setKillListener, setName, start, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffer

protected Sample buffer
The Sample.


sampleRate

protected float sampleRate
The sample rate, determined by the Sample.


position

protected double position
The position in milliseconds.


lastChangedPosition

protected double lastChangedPosition
The last changed position, used to determine if the position envelope is in use.


positionEnvelope

protected UGen positionEnvelope
The position envelope.


rateEnvelope

protected UGen rateEnvelope
The rate envelope.


positionIncrement

protected double positionIncrement
The millisecond position increment per sample. Calculated from the ratio of the AudioContext's sample rate and the Sample's sample rate.


forwards

protected boolean forwards
Flag for alternating loop mode to determine if playback is in forward or reverse phase.


interpolationType

protected SamplePlayer.InterpolationType interpolationType
The interpolation type.


loopStartEnvelope

protected UGen loopStartEnvelope
The loop start envelope.


loopEndEnvelope

protected UGen loopEndEnvelope
The loop end envelope.


loopType

protected SamplePlayer.LoopType loopType
The loop type.


loopCrossFade

protected float loopCrossFade
The loop cross fade in milliseconds.


startLoop

protected boolean startLoop
Flag to determine whether playback starts at the beginning of the sample or at the beginning of the loop.


killOnEnd

protected boolean killOnEnd
Flag to determine whether the SamplePlayer should kill itself when it gets to the end of the Sample.


rate

protected float rate
The rate. Calculated and used internally from the rate envelope.


loopStart

protected float loopStart
The loop start. Calculated and used internally from the loop start envelope.


loopEnd

protected float loopEnd
The loop end. Calculated and used internally from the loop end envelope.

Constructor Detail

SamplePlayer

protected SamplePlayer(AudioContext context,
                       int outs)
Instantiates a new SamplePlayer with given number of outputs.

Parameters:
context - the AudioContext.
outs - the number of outputs.

SamplePlayer

public SamplePlayer(AudioContext context,
                    Sample buffer)
Instantiates a new SamplePlayer with given Sample. Number of outputs is determined by number of channels in Sample.

Parameters:
context - the AudioContext.
buffer - the Sample.
Method Detail

setBuffer

protected void setBuffer(Sample buffer)
Sets the Sample.

Parameters:
buffer - the new Sample.

getBuffer

public Sample getBuffer()
Gets the Sample.

Returns:
the Sample.

setToEnd

public void setToEnd()
Sets the playback position to the end of the Sample.


inLoop

public boolean inLoop()
Determines whether the playback position is within the loop points.

Returns:
true if the playback position is within the loop points.

setToLoopStart

public void setToLoopStart()
Sets the playback position to the loop start point.


start

public void start(float msPosition)
Starts the sample at the given position.

Parameters:
msPosition - the position in milliseconds.

reset

public void reset()
Resets the position to the start of the Sample.


getPosition

public double getPosition()
Gets the playback position.

Returns:
the position in milliseconds.

setPosition

public void setPosition(double position)
Sets the playback position.

Parameters:
position - the new position in milliseconds.

getPositionEnvelope

public UGen getPositionEnvelope()
Gets the position envelope.

Returns:
the position envelope.

setPositionEnvelope

public void setPositionEnvelope(UGen positionEnvelope)
Sets the position envelope. Setting the position envelope means that the position is then controlled by this envelope. If the envelope is null, or unchanging, the position continues to be modified by the SamplePlayer's internal playback or by calls to change the position.

Parameters:
positionEnvelope - the new position envelope.

getRateEnvelope

public UGen getRateEnvelope()
Gets the rate envelope.

Returns:
the rate envelope.

setRateEnvelope

public void setRateEnvelope(UGen rateEnvelope)
Sets the rate envelope.

Parameters:
rateEnvelope - the new rate envelope.

getInterpolationType

public SamplePlayer.InterpolationType getInterpolationType()
Gets the interpolation type.

Returns:
the interpolation type.

setInterpolationType

public void setInterpolationType(SamplePlayer.InterpolationType interpolationType)
Sets the interpolation type.

Parameters:
interpolationType - the new interpolation type.

getLoopCrossFade

public float getLoopCrossFade()
Gets the loop cross fade.

Returns:
the loop cross fade in milliseconds.

setLoopCrossFade

public void setLoopCrossFade(float loopCrossFade)
Sets the loop cross fade.

Parameters:
loopCrossFade - the new loop cross fade in milliseconds.

getLoopEndEnvelope

public UGen getLoopEndEnvelope()
Gets the loop end envelope.

Returns:
the loop end envelope.

setLoopEndEnvelope

public void setLoopEndEnvelope(UGen loopEndEnvelope)
Sets the loop end envelope.

Parameters:
loopEndEnvelope - the new loop end envelope.

getLoopStartEnvelope

public UGen getLoopStartEnvelope()
Gets the loop start envelope.

Returns:
the loop start envelope

setLoopStartEnvelope

public void setLoopStartEnvelope(UGen loopStartEnvelope)
Sets the loop start envelope.

Parameters:
loopStartEnvelope - the new loop start envelope.

setLoopPointsFraction

public void setLoopPointsFraction(float start,
                                  float end)
Sets both loop points to static values as fractions of the Sample length.

Parameters:
start - the start value, as fraction of the Sample length.
end - the end value, as fraction of the Sample length.

getLoopType

public SamplePlayer.LoopType getLoopType()
Gets the loop type.

Returns:
the loop type.

setLoopType

public void setLoopType(SamplePlayer.LoopType loopType)
Sets the loop type.

Parameters:
loopType - the new loop type.

getSampleRate

public float getSampleRate()
Gets the sample rate.

Returns:
the sample rate, in samples per second.

calculateBuffer

public void calculateBuffer()
Description copied from class: UGen
Called by the signal chain to update this UGen's ouput data. Subclassses of UGen should implement the UGen's DSP perform routine here. In general this involves grabbing data from UGen.bufIn and putting data into UGen.bufOut in some way. UGen.bufIn and UGen.bufOut are 2D arrays of floats of the form float[numChannels][bufferSize]. The length of the buffers is given by UGen.bufferSize, and the number of channels of the input and output buffers are given by UGen.ins and UGen.outs respectively.

Specified by:
calculateBuffer in class UGen

setKillOnEnd

public void setKillOnEnd(boolean killOnEnd)
Sets/unsets option for SamplePlayer to kill itself when it reaches the end of the Sample it is playing.

Parameters:
killOnEnd - true to kill on end.

getKillOnEnd

public boolean getKillOnEnd()
Determines whether this SamplePlayer will kill itself at the end of the Sample it is playing.

Returns:
true of SamplePlayer will kill itself at the end of the Sample it is playing.

reTrigger

public void reTrigger()
Re trigger the SamplePlayer from the beginning.


calculateNextPosition

protected void calculateNextPosition(int i)
Used at each sample in the perform routine to determine the next playback position.

Parameters:
i - the index within the buffer loop.