// ------------------------------------------------------------ // Class for a basic sine wave oscillator. // Initialise with median value, amplitude, phaseStep (and optionally phaseOffset) // Synchronise by calling tick(). // Retrieve current output value via value(). // ------------------------------------------------------------ class Oscillator { float phase; float phaseStep; float median; float amplitude; boolean paused; // Constructor requiring only median, amplitude and phase step amount Oscillator(float median, float amplitude, float phaseStep) { this(median, amplitude, phaseStep, 0); } // Constructor median, amplitude, phase step amount and phase offset Oscillator(float median, float amplitude, float phaseStep, float phaseOffset) { this.phase = phaseOffset; this.phaseStep = phaseStep; this.median = median; this.amplitude = amplitude; this.paused = false; } // If not paused, advance the phase for the next timeslice/iteration/what-have-you void tick() { if (!paused) phase += phaseStep; } void pause() { paused = true; } void unPause() { paused = false; } void togglePause() { paused = !paused; } // Retrieve the current output value for the oscillator float value() { return median + amplitude * sin( phase ); } }