pyglet.media.synthesis
- class ADSREnvelope
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:
- 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.
- class FlatEnvelope
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.
- class LinearDecayEnvelope
A linearly decaying envelope.
This envelope linearly decays the amplitude from the peak value to 0, over the length of the waveform.
- Parameters:
- peakfloat
The Initial peak value of the envelope, from 0.0 to 1.0. Values outside this range will be clamped.
- __init__(peak=1.0)
- class Sawtooth
- class Silence
- class Sine
- class Square
- class SynthesisSource
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.
- __init__( )
- is_precise() bool
bool: Whether this source is considered precise.
x
bytes on sources
are considered aligned ifx % 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::rtype:bool
If
x
or more bytes are available,x
bytes.If not enough bytes are available anymore,
r
bytes wherer < x
andr
is aligned.
A source is not precise if it does any of these:
Return less than
x
bytes for an aligned request ofx
bytes although data still remains so that an additional request would return additionalAudioData
/ notNone
.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.- Returns:
bool: Whether the source is precise.
- class TremoloEnvelope
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:
- class Triangle
- class WhiteNoise
- composite_operator(*operators: Generator) Generator
Combine the output from multiple generators.
This does a simple sum & devision of the output of two or more generators. A new generator is returned.
- Return type:
- sine_operator(
- sample_rate: int = 44800,
- frequency: float = 440,
- index: float = 1,
- modulator: Generator | None = None,
- envelope: Envelope | None = None,
A sine wave generator that can be optionally modulated with another generator.
This generator represents a single FM Operator. It can be used by itself as a simple sine wave, or modulated by another waveform generator. Multiple operators can be linked together in this way. For example:
operator1 = sine_operator(samplerate=44800, frequency=1.22) operator2 = sine_operator(samplerate=44800, frequency=99, modulator=operator1) operator3 = sine_operator(samplerate=44800, frequency=333, modulator=operator2) operator4 = sine_operator(samplerate=44800, frequency=545, modulator=operator3)
- Parameters:
sample_rate – Audio samples per second. (CD quality is 44100).
frequency – The frequency, in Hz, of the waveform you wish to generate.
index – The modulation index. Defaults to 1
modulator – An optional operator to modulate this one.
envelope – An optional Envelope to apply to the waveform.