toxi.math.waves
Class AbstractWave

java.lang.Object
  extended by toxi.math.waves.AbstractWave
Direct Known Subclasses:
AMFMSineWave, ConstantWave, FMHarmonicSquareWave, FMSawtoothWave, FMSineWave, FMSquareWave, FMTriangleWave, SineWave

public abstract class AbstractWave
extends java.lang.Object

Abstract wave oscillator type which needs to be subclassed to implement different waveforms. Please note that the frequency unit is radians, but conversion methods to & from Hertz (hertzToRadians(float, float)) are included in this base class.


Field Summary
 float amp
           
 float frequency
           
 float offset
           
 float phase
          Current wave phase
static float PI
           
static float TWO_PI
           
 float value
           
 
Constructor Summary
AbstractWave()
           
AbstractWave(float phase)
           
AbstractWave(float phase, float freq)
           
AbstractWave(float phase, float freq, float amp, float offset)
           
 
Method Summary
 float cyclePhase()
          Ensures phase remains in the 0...TWO_PI interval.
 float cyclePhase(float freq)
          Progresses phase and ensures it remains in the 0...TWO_PI interval.
static float hertzToRadians(float hz, float sampleRate)
          Converts a frequency in Hertz into radians.
 void pop()
           
 void push()
           
static float radiansToHertz(float f, float sampleRate)
          Converts a frequency from radians to Hertz.
 void reset()
          Resets the wave phase to the last set phase value (via setPhase(float).
 void setPhase(float phase)
          Starts the wave from a new phase.
 java.lang.String toString()
           
abstract  float update()
          Updates the wave and returns new value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PI

public static final float PI
See Also:
Constant Field Values

TWO_PI

public static final float TWO_PI
See Also:
Constant Field Values

phase

public float phase
Current wave phase


frequency

public float frequency

amp

public float amp

offset

public float offset

value

public float value
Constructor Detail

AbstractWave

public AbstractWave()

AbstractWave

public AbstractWave(float phase)
Parameters:
phase -

AbstractWave

public AbstractWave(float phase,
                    float freq)
Parameters:
phase -
freq -

AbstractWave

public AbstractWave(float phase,
                    float freq,
                    float amp,
                    float offset)
Parameters:
phase -
freq -
amp -
offset -
Method Detail

hertzToRadians

public static final float hertzToRadians(float hz,
                                         float sampleRate)
Converts a frequency in Hertz into radians.

Parameters:
hz - frequency to convert (in Hz)
sampleRate - sampling rate in Hz (equals period length @ 1 Hz)
Returns:
frequency in radians

radiansToHertz

public static final float radiansToHertz(float f,
                                         float sampleRate)
Converts a frequency from radians to Hertz.

Parameters:
f - frequency in radians
sampleRate - sampling rate in Hz (equals period length @ 1 Hz)
Returns:
freq in Hz

cyclePhase

public final float cyclePhase()
Ensures phase remains in the 0...TWO_PI interval.

Returns:
current phase

cyclePhase

public final float cyclePhase(float freq)
Progresses phase and ensures it remains in the 0...TWO_PI interval.

Parameters:
freq - normalized progress frequency
Returns:
update phase value

pop

public void pop()

push

public void push()

reset

public void reset()
Resets the wave phase to the last set phase value (via setPhase(float).


setPhase

public void setPhase(float phase)
Starts the wave from a new phase. The new phase position will also be used for any later call to {reset()

Parameters:
phase - new phase

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

update

public abstract float update()
Updates the wave and returns new value. Implementing classes should manually ensure the phase remains in the 0...TWO_PI interval or by calling cyclePhase().

Returns:
current (newly calculated) wave value