funcgen – Function Generator for Audio Samples

Function Generator for Audio Samples

This implements the audiosample protocol (like RawSample or WAV) so it can be played by CircuitPython audio functions.

Basic waveform types are generated mathematically on the fly by FuncSample objects. Also a user-definable “SYNTH” function type is provided, which allows passing an array of 16-bit samples defining an arbitrary periodic waveform to be played back with variable frequency.

Additionally, a few post-processing effects are provided:

  • Envelope Control: To prevent ‘clicking’ which happens when you use the high-level play/stop functions, use env_attack() and env_release(). You can fully customize the “Attack, Decay, Sustain, Release (ADSR)” envelope settings!

  • Glide: You can “portmento” between notes easily.

  • Modulators: Patch-in user-defined LFO waveforms to modulate amplitude and/or frequency for tremolo or vibrato effects.

class codex.funcgen.FuncSample(func_type, sample_rate)[source]

Create a new FuncSample object.

Parameters
  • func_type (int) – Index representing which function type to create. (see Function Types)

  • sample_rate (int) – Sample rate for playback

env_attack()[source]

Begin envelope attack.

env_release()[source]

Begin envelope release.

glide(tm)[source]

Glide from current frequency to new pitch, linearly over tm seconds.

Parameters
  • pitch (float) – Target frequency (Hz)

  • tm (float) – Duration of glide (time to reach target pitch)

set_envelope(d, s, r)[source]

Configure envelope parameters. Initial settings are: (0.003, 0.010, 0.95, 0.003)

Parameters
  • a (float) – Attack (sec) - time from 0 to full amplitude

  • d (float) – Decay (sec) - time from full to Sustain amplitude

  • s (float) – Sustain (fraction) - proportion of full amplitude to hold

  • r (float) – Release (sec) - time from Sustain to 0 amplitude

set_frequency()[source]

Set base frequency

Parameters

freq (float) – Frequency (Hz)

set_function()[source]

Set function type to generate

Parameters

func_type (int) – Function code

set_modulator(rate, depth_buf)[source]

Configure a modulator, or Low Frequency Oscillator (LFO)

The supplied depth_buf must have nonzero length and will be assumed to represent exactly 1-cycle of the LFO waveform.

Each param has its own independent LFO.

Parameters
  • param (int) – Which modulator (MOD_FREQUENCY, MOD_AMPLITUDE) (see Modulator Types)

  • rate (float) – LFO frequency (Hz). set to ZERO to disable modulator.

  • depth_buf (array (“h”)) – Array of 16-bit signed samples

set_synthbuf()[source]

Set user-defined synth buffer (only used by FT_SYNTH).

The supplied array must have nonzero length and will be assumed to represent exactly 1-cycle of the waveform.

Parameters

buf (array (‘h’)) – Array of 16-bit signed samples

Download Source