Skip to content
Igor Zinken edited this page Dec 27, 2018 · 4 revisions

A WaveTable is basically a wrapper for a self-contained buffer that contains a single cycle of a waveform, which can be used to synthesize repeated cycles of the wave at any given frequency. It is, strictly speaking, less accurate than synthesizing a waveform for its full duration at a specific frequency. Regardless, at higher resolutions, wave tables can sound quite "clean", if anything they will use very little memory and omit the need of (re)synthesizing complex waves which can greatly free up the strain on CPU resources.

A WaveTable can be used in conjunction with regular buffer operations (such as a Processor) where upon each sample in the buffer, its peek()-method can be invoked to retrieve a representation of the tables waveform at the given sample period for the desired wave frequency. WaveTables are for instance in use by the LFO module and the Synthesizer.

See WaveGenerator-utility for the creation of wave tables and TablePool for pooling unique waveform types.

constructor / destructor

WaveTable( int aTableLength, float aFrequency )

Where aTableLength describes the length of the wave table. This will describe the amount of samples exist in the buffer of a single cycle of the wave tables waveform. Higher amounts give cleaner results, lower amount reduce memory footprint. A value of 128 is satisfactorily used in MikroWave, a value of around a tenth of the engine's samplerate should be hi-fi enough!

aFrequency describes the frequency (in Hz) at which we would like to play/synthesize the waveform described inside the wavetable. Note that the wave table contains a single cycle of the waveform (rendered at 440 Hz), as such aFrequency describes the length of the waveforms period during rendering (see peek()), and consequently, the pitch.

Note that upon construction the WaveTable's buffer contains no audio. See getBuffer() and WaveGenerator.

~WaveTable();

Deletes the WaveTables buffer.

public methods

float getFrequency()

Returns the frequency at which the WaveTables contents should be resynthesized.

void setFrequency( float value )

Sets the frequency at which the WaveTables contents should be resynthesized to given value.

SAMPLE_TYPE getAccumulator()

Retrieves the WaveTables current accumulator position. The accumulator is basically a "playback head" for a wave table. In conjunction with the peek()-method the accumulator will represent the value the waveforms period should represent at the instance's frequency.

void setAccumulator( SAMPLE_TYPE value )

Sets the WaveTables accumulator position to given value.

SAMPLE_TYPE* getBuffer()

Returns the buffer holding the wave table values. This can be used together with the WaveGenerator to fill the wavetable.

SAMPLE_TYPE peek()

The method to invoke to render a sample from the wave table. For instance, to render a 2 second sine wave at a sample rate of 44.1 kHz, you would need 44100 * 2 = 88200 samples. You could render the sine from the wave table like so :

for ( int i = 0; i < 88200; ++i )
{
    outputBuffer[ i ] = waveTable->peek();
}

The peek()-method will keep the accumulator within bounds of the instance's tableLength so it can be repeatedly called to render the output.

void cloneTable( WaveTable* waveTable )

Clones the contents of given waveTable buffer into the instances _buffer.

WaveTable* clone()

Create a new WaveTable instance that shares the properties of the current instance, along with the _buffer s contents.

protected properties

SAMPLE_TYPE* buffer

The buffer that holds the wave table.

SAMPLE_TYPE _accumulator
SAMPLE_TYPE SR_OVER_LENGTH
float _frequency

Used to synthesize a waveform from the table's buffer using the requested frequency and staying within the bounds of the sample rate divided by the tableLength.

Clone this wiki locally