pyglet.media.synthesis

class ADSREnvelope(
attack: float,
decay: float,
release: float,
sustain_amplitude: float = 0.5,
)

A four-part Attack, Decay, Suspend, Release envelope.

This is a four part ADSR envelope. The attack, decay, and release parameters should be provided in seconds. For example, a value of 0.1 would be 100ms. The sustain_amplitude parameter affects the sustain volume. This defaults to a value of 0.5, but can be provided on a scale from 0.0 to 1.0.

Parameters:
  • attack (float) – The attack time, in seconds.

  • decay (float) – The decay time, in seconds.

  • release (float) – The release time, in seconds.

  • sustain_amplitude (float) – The sustain amplitude (volume), from 0.0 to 1.0.

get_generator(sample_rate: float, duration: float) → Generator[float]

Get a generator instance.

Parameters:
  • sample_rate – The sample rate of the Source this will be applied to.

  • duration – The duration of the Source. This is used to calculate the number of bytes for some Envelopes.

class Algorithm(
name: str,
carriers: tuple[int, ...],
modulations: tuple[Modulation, ...],
feedback: int | None = None,
) → None
carriers: tuple[int, ...]

A tuple of indices of carrier Operators.

feedback: int | None

Index of which Operator to apply feedback to.

modulations: tuple[Modulation, ...]

A tuple of Modulation objects, Operator interaction indices.

name: str

The name of the algorithm.

class Envelope

Base class for SynthesisSource amplitude envelopes.

Custom Envelopes need only provide a single get_generator method that takes the sample rate, and duration as arguments.

get_generator(sample_rate: float, duration: float) → Generator[float]

Get a generator instance.

Parameters:
  • sample_rate – The sample rate of the Source this will be applied to.

  • duration – The duration of the Source. This is used to calculate the number of bytes for some Envelopes.

class FlatEnvelope(amplitude: float = 0.5)

A flat envelope, providing basic amplitude setting.

Parameters:

amplitude (float) – The amplitude (volume) of the wave, from 0.0 to 1.0. Values outside this range will be clamped.

get_generator(
sample_rate: float | None = None,
duration: float | None = None,
) → Generator[float]

Get a generator instance.

Parameters:
  • sample_rate – The sample rate of the Source this will be applied to.

  • duration – The duration of the Source. This is used to calculate the number of bytes for some Envelopes.

class LinearDecayEnvelope(peak: float = 1.0)

A linearly decaying envelope.

This envelope linearly decays the amplitude from the peak value to 0, over the length of the waveform.

Parameters:

peak (float) – The Initial peak value of the envelope, from 0.0 to 1.0. Values outside this range will be clamped.

get_generator(
sample_rate: float,
duration: float,
) → Generator[float]

Get a generator instance.

Parameters:
  • sample_rate – The sample rate of the Source this will be applied to.

  • duration – The duration of the Source. This is used to calculate the number of bytes for some Envelopes.

class Modulation(source: int, target: int) → None

Modulation mappint between two Operators, by index.

source: int
target: int
class Operator(
frequency: float = 440.0,
index: float = 1.0,
feedback: float = 0.0,
envelope: Envelope | None = None,
)

An FM operator (oscillator + phase modulation + optional feedback).

add_modulator(op: Operator) → None

Connect another operator as a modulator.

Return type:

None

reset() → None

Reset phase and feedback state (needed for seeking).

Return type:

None

modulators: list[Operator]
sample_rate: int = 44800
class Sawtooth(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a Sawtooth waveform.

class Silence(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a Silent waveform.

class Sine(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a sinusoid (sine) waveform.

class Square(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a Square (pulse) waveform.

class SynthesisSource(
generator: Generator,
duration: float,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Base class for synthesized waveforms.

Parameters:
  • generator (Generator) – A waveform generator that produces a stream of floats from (-1.0, 1.0)

  • duration (float) – The length, in seconds, of audio that you wish to generate.

  • sample_rate (int) – Audio samples per second. (CD quality is 44100).

  • envelope (Envelope | None) – An optional Envelope to apply to the waveform.

get_audio_data(
num_bytes: int,
) → AudioData | None

Return num_bytes bytes of audio data.

Return type:

AudioData | None

is_precise() → bool

Whether this source is considered precise.

x bytes on source s are considered aligned if x % s.audio_format.bytes_per_frame == 0, so there’d be no partial audio frame in the returned data.

A source is precise if - for an aligned request of x bytes - it returns:\

  • If x or more bytes are available, x bytes.

  • If not enough bytes are available anymore, r bytes where r < x and r is aligned.

A source is not precise if it does any of these:

  • Return less than x bytes for an aligned request of x bytes although data still remains so that an additional request would return additional AudioData / not None.

  • Return more bytes than requested.

  • Return an unaligned amount of bytes for an aligned request.

pyglet’s internals are guaranteed to never make unaligned requests, or requests of less than 1024 bytes.

If this method returns False, pyglet will wrap the source in an alignment-forcing buffer creating additional overhead.

If this method is overridden to return True although the source does not comply with the requirements above, audio playback may be negatively impacted at best and memory access violations occur at worst.

Return type:

bool

Returns:

Whether the source is precise.

seek(timestamp: float) → None

Seek to given timestamp.

Parameters:

timestamp (float) – Time where to seek in the source. The timestamp will be clamped to the duration of the source.

Return type:

None

class TremoloEnvelope(depth: float, rate: float, amplitude: float = 0.5)

A tremolo envelope, for modulation amplitude.

A tremolo envelope that modulates the amplitude of the waveform with a sinusoidal pattern. The depth and rate of modulation can be specified. Depth is calculated as a percentage of the maximum amplitude. For example: a depth of 0.2 and amplitude of 0.5 will fluctuate the amplitude between 0.4 an 0.5.

Parameters:
  • depth (float) – The amount of fluctuation, from 0.0 to 1.0.

  • rate (float) – The fluctuation frequency, in seconds.

  • amplitude (float) – The peak amplitude (volume), from 0.0 to 1.0.

get_generator(
sample_rate: float,
duration: float,
) → Generator[float]

Get a generator instance.

Parameters:
  • sample_rate – The sample rate of the Source this will be applied to.

  • duration – The duration of the Source. This is used to calculate the number of bytes for some Envelopes.

class Triangle(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a Triangle waveform.

class WhiteNoise(
duration: float,
frequency: int = 440,
sample_rate: int = 44800,
envelope: Envelope | None = None,
)

Create a random white noise waveform.

noise_generator(frequency: float, sample_rate: float) → Generator[float]
pulse_generator(
frequency: float,
sample_rate: float,
duty_cycle: float = 50.0,
) → Generator[float]
sawtooth_generator(frequency: float, sample_rate: float) → Generator[float]
silence_generator(frequency: float, sample_rate: float) → Generator[float]
sine_generator(frequency: float, sample_rate: float) → Generator[float]
triangle_generator(frequency: float, sample_rate: float) → Generator[float]