Sound¶
- class aud.Sound
- class Sound(filename, stream=0)¶
Sound objects are immutable and represent a sound that can be played simultaneously multiple times. They are called factories because they create reader objects internally that are used for playback.
- Parameters:
filename (string) – Path of the file.
stream (int) – The index of the audio stream within the file if it contains multiple audio streams, 0 by default.
- ADSR()
- ADSR(attack, decay, sustain, release)¶
Attack-Decay-Sustain-Release envelopes the volume of a sound. Note: there is currently no way to trigger the release with this API.
- accumulate()
- accumulate(additive=False)¶
Accumulates a sound by summing over positive input differences thus generating a monotonic sigal. If additivity is set to true negative input differences get added too, but positive ones with a factor of two.
Note that with additivity the signal is not monotonic anymore.
- addSound(object, /)
- addSound(sound)¶
Adds a new sound to a sound list.
- Parameters:
sound (
Sound) – The sound that will be added to the list.
Note
You can only add a sound to a sound list.
- animateableTimeStretchPitchScale()
- animateableTimeStretchPitchScale(fps[, time_stretch, pitch_scale, quality, preserve_formant])¶
Applies time-stretching and pitch-scaling to the sound.
- Parameters:
fps (float) – The FPS of the animation system.
time_stretch (float or
AnimateablePropertyP) – The factor by which to stretch or compress time.pitch_scale (float or
AnimateablePropertyP:arg quality: Rubberband stretcher quality (STRETCHER_QUALITY_*).) – The factor by which to adjust the pitch.preserve_formant (bool) – Whether to preserve the vocal formants during pitch-shifting.
- Returns:
The created
Soundobject.- Return type:
- binaural()
- binaural()¶
Creates a binaural sound using another sound as source. The original sound must be mono
- classmethod buffer()
- classmethod buffer(data, rate)¶
Creates a sound from a data buffer.
- cache()
- cache()¶
Caches a sound into RAM.
This saves CPU usage needed for decoding and file access if the underlying sound reads from a file on the harddisk, but it consumes a lot of memory.
Note
Only known-length factories can be buffered.
Warning
Raw PCM data needs a lot of space, only buffer short factories.
- convolver()
- convolver()¶
Creates a sound that will apply convolution to another sound.
- data()
- data()¶
Retrieves the data of the sound as numpy array.
- Returns:
A two dimensional numpy float array.
- Return type:
numpy.ndarray
Note
Best efficiency with cached sounds.
- delay()
- delay(time)¶
Delays by playing adding silence in front of the other sound’s data.
- echo()
- Echo(delay, feedback, mix)¶
Adds Echo effect to the sound.
- envelope()
- envelope(attack, release, threshold, arthreshold)¶
Delays by playing adding silence in front of the other sound’s data.
- fadein()
- fadein(start, length)¶
Fades a sound in by raising the volume linearly in the given time interval.
- Parameters:
start (float) – Time in seconds when the fading should start.
length (float) – Time in seconds how long the fading should last.
- Returns:
The created
Soundobject.- Return type:
Note
Before the fade starts it plays silence.
- fadeout()
- fadeout(start, length)¶
Fades a sound in by lowering the volume linearly in the given time interval.
- Parameters:
start (float) – Time in seconds when the fading should start.
length (float) – Time in seconds how long the fading should last.
- Returns:
The created
Soundobject.- Return type:
Note
After the fade this sound plays silence, so that the length of the sound is not altered.
- classmethod file()
- classmethod file(filename)¶
Creates a sound object of a sound file.
- Parameters:
filename (string) – Path of the file.
- Returns:
The created
Soundobject.- Return type:
Warning
If the file doesn’t exist or can’t be read you will not get an exception immediately, but when you try to start playback of that sound.
- filter()
- filter(b, a=(1,))¶
Filters a sound with the supplied IIR filter coefficients. Without the second parameter you’ll get a FIR filter.
If the first value of the a sequence is 0, it will be set to 1 automatically. If the first value of the a sequence is neither 0 nor 1, all filter coefficients will be scaled by this value so that it is 1 in the end, you don’t have to scale yourself.
- highpass()
- highpass(frequency, Q=0.5)¶
Creates a second order highpass filter based on the transfer function \(H(s) = s^2 / (s^2 + s/Q + 1)\)
- join(object, /)
- join(sound)¶
Plays two factories in sequence.
- Parameters:
sound (
Sound) – The sound to play second.- Returns:
The created
Soundobject.- Return type:
Note
The two factories have to have the same specifications (channels and samplerate).
- length
The sample specification of the sound as a tuple with rate and channel count.
- limit()
- limit(start, end)¶
Limits a sound within a specific start and end time.
- classmethod list()
- classmethod list()¶
Creates an empty sound list that can contain several sounds.
- loop()
- loop(count)¶
Loops a sound.
- Parameters:
count (integer) – How often the sound should be looped. Negative values mean endlessly.
- Returns:
The created
Soundobject.- Return type:
Note
This is a filter function, you might consider using
Handle.loop_countinstead.
- lowpass()
- lowpass(frequency, Q=0.5)¶
Creates a second order lowpass filter based on the transfer function \(H(s) = 1 / (s^2 + s/Q + 1)\)
- mix(object, /)
- mix(sound)¶
Mixes two factories.
- Parameters:
sound (
Sound) – The sound to mix over the other.- Returns:
The created
Soundobject.- Return type:
Note
The two factories have to have the same specifications (channels and samplerate).
- modulate(object, /)
- modulate(sound)¶
Modulates two factories.
- Parameters:
sound (
Sound) – The sound to modulate over the other.- Returns:
The created
Soundobject.- Return type:
Note
The two factories have to have the same specifications (channels and samplerate).
- mutable()
- mutable()¶
Creates a sound that will be restarted when sought backwards. If the original sound is a sound list, the playing sound can change.
- pingpong()
- pingpong()¶
Plays a sound forward and then backward. This is like joining a sound with its reverse.
- pitch()
- pitch(factor)¶
Changes the pitch of a sound with a specific factor.
- Parameters:
factor (float) – The factor to change the pitch with.
- Returns:
The created
Soundobject.- Return type:
Note
This is done by changing the sample rate of the underlying sound, which has to be an integer, so the factor value rounded and the factor may not be 100 % accurate.
Note
This is a filter function, you might consider using
Handle.pitchinstead.
- rechannel()
- rechannel(channels)¶
Rechannels the sound.
- resample()
- resample(rate, quality)¶
Resamples the sound.
- reverse()
- reverse()¶
Plays a sound reversed.
Note
The sound has to have a finite length and has to be seekable. It’s recommended to use this only with factories with fast and accurate seeking, which is not true for encoded audio files, such ones should be buffered using
cache()before being played reversed.Warning
If seeking is not accurate in the underlying sound you’ll likely hear skips/jumps/cracks.
- classmethod sawtooth()
- classmethod sawtooth(frequency, rate=48000)¶
Creates a sawtooth sound which plays a sawtooth wave.
- classmethod silence()
- classmethod silence(rate=48000)¶
Creates a silence sound which plays simple silence.
- classmethod sine()
- classmethod sine(frequency, rate=48000)¶
Creates a sine sound which plays a sine wave.
- specs
The sample specification of the sound as a tuple with rate and channel count.
- classmethod square()
- classmethod square(frequency, rate=48000)¶
Creates a square sound which plays a square wave.
- threshold()
- threshold(threshold=0)¶
Makes a threshold wave out of an audio wave by setting all samples with a amplitude >= threshold to 1, all <= -threshold to -1 and all between to 0.
- timeStretchPitchScale()
- timeStretchPitchScale(time_stretch, pitch_scale, quality, preserve_formant)¶
Applies time-stretching and pitch-scaling to the sound.
- Parameters:
time_stretch (float) – The factor by which to stretch or compress time.
pitch_scale (float) – The factor by which to adjust the pitch.
quality (int) – Rubberband stretcher quality (STRETCHER_QUALITY_*).
preserve_formant (bool) – Whether to preserve the vocal formants during pitch-shifting.
- Returns:
The created
Soundobject.- Return type:
- classmethod triangle()
- classmethod triangle(frequency, rate=48000)¶
Creates a triangle sound which plays a triangle wave.
- volume()
- volume(volume)¶
Changes the volume of a sound.
Note
Should be in the range [0, 1] to avoid clipping.
Note
This is a filter function, you might consider using
Handle.volumeinstead.
- write()
- write(filename, rate, channels, format, container, codec, bitrate, buffersize)¶
Writes the sound to a file.
- Parameters:
filename (string) – The path to write to.
rate (int) – The sample rate to write with.
channels (int) – The number of channels to write with.
format (int) – The sample format to write with.
container (int) – The container format for the file.
codec (int) – The codec to use in the file.
bitrate (int) – The bitrate to write with.
buffersize (int) – The size of the writing buffer.