Class AbstractWave

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

public abstract class AbstractWave extends 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

    Fields
    Modifier and Type
    Field
    Description
    float
     
    float
     
    float
     
    float
    Current wave phase
    static final float
     
    static final float
     
    float
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    AbstractWave(float phase)
     
    AbstractWave(float phase, float freq)
     
    AbstractWave(float phase, float freq, float amp, float offset)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final float
    Ensures phase remains in the 0...TWO_PI interval.
    final float
    cyclePhase(float freq)
    Progresses phase and ensures it remains in the 0...TWO_PI interval.
    static final float
    hertzToRadians(float hz, float sampleRate)
    Converts a frequency in Hertz into radians.
    void
    pop()
     
    void
     
    static final float
    radiansToHertz(float f, float sampleRate)
    Converts a frequency from radians to Hertz.
    void
    Resets the wave phase to the last set phase value (via setPhase(float).
    void
    setPhase(float phase)
    Starts the wave from a new phase.
     
    abstract float
    Updates the wave and returns new value.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • PI

      public static final float PI
      See Also:
    • TWO_PI

      public static final float TWO_PI
      See Also:
    • phase

      public float phase
      Current wave phase
    • frequency

      public float frequency
    • amp

      public float amp
    • offset

      public float offset
    • value

      public float value
  • Constructor Details

    • 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 Details

    • 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 String toString()
      Overrides:
      toString in class 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