Source code for funcgen

"""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.
      
"""

[docs]class FuncSample: """ """ # FT_SINE = 0 # FT_SQUARE = 1 # FT_SAWTOOTH = 2 # FT_TRIANGLE = 3 # FT_RANDOM = 4 # FT_ORGAN = 5 # FT_SYNTH = 6 # MOD_FREQUENCY = 0 # MOD_AMPLITUDE = 1 def __init__(self, func_type, sample_rate): """Create a new FuncSample object. Args: func_type (int): Index representing which function type to create. (see :py:attr:`Function Types <codec.FT_ORGAN>`) sample_rate (int): Sample rate for playback """ # Implemented in C pass
[docs] def set_frequency(freq): """Set base frequency Args: freq (float): Frequency (Hz) """ pass
[docs] def set_function(func_type): """Set function type to generate Args: func_type (int): Function code """ pass
[docs] def set_synthbuf(buf): """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. Args: buf ( `array` ('h')): Array of 16-bit signed samples """ pass
[docs] def glide(pitch, tm): """Glide from current frequency to new pitch, linearly over tm seconds. Args: pitch (float): Target frequency (Hz) tm (float): Duration of glide (time to reach target pitch) """ pass
[docs] def env_attack(): """Begin envelope attack. """ pass
[docs] def env_release(): """Begin envelope release. """ pass
[docs] def set_envelope(a, d, s, r): """Configure envelope parameters. Initial settings are: (0.003, 0.010, 0.95, 0.003) Args: 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 """ pass
[docs] def set_modulator(param, rate, depth_buf): """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. Args: param (int): Which modulator (MOD_FREQUENCY, MOD_AMPLITUDE) (see :py:attr:`Modulator Types <codec.MOD_AMPLITUDE>`) rate (float): LFO frequency (Hz). set to ZERO to disable modulator. depth_buf ( `array` ("h")): Array of 16-bit signed samples """ pass