audiodelays – Support for audio delay effects
The audiodelays module contains classes to provide access to audio delay effects.
Available on these boards
- class audiodelays.Echo(max_delay_ms: int = 500, delay_ms: synthio.BlockInput = 250.0, decay: synthio.BlockInput = 0.7, mix: synthio.BlockInput = 0.5, buffer_size: int = 512, sample_rate: int = 8000, bits_per_sample: int = 16, samples_signed: bool = True, channel_count: int = 1)
An Echo effect
- Create a Echo effect where you hear the original sample play back, at a lesser volume after
a set number of millisecond delay. The delay timing of the echo can be changed at runtime with the delay_ms parameter but the delay can never exceed the max_delay_ms parameter. The maximum delay you can set is limited by available memory.
Each time the echo plays back the volume is reduced by the decay setting (echo * decay).
The mix parameter allows you to change how much of the unchanged sample passes through to the output to how much of the effect audio you hear as the output.
- Parameters:
max_delay_ms (int) – The maximum time the echo can be in milliseconds
delay_ms (synthio.BlockInput) – The current time of the echo delay in milliseconds. Must be less the max_delay_ms
decay (synthio.BlockInput) – The rate the echo fades. 0.0 = instant; 1.0 = never.
mix (synthio.BlockInput) – The mix as a ratio of the sample (0.0) to the effect (1.0).
buffer_size (int) – The total size in bytes of each of the two playback buffers to use
sample_rate (int) – The sample rate to be used
channel_count (int) – The number of channels the source samples contain. 1 = mono; 2 = stereo.
bits_per_sample (int) – The bits per sample of the effect
samples_signed (bool) – Effect is signed (True) or unsigned (False)
freq_shift (bool) – Do echos change frequency as the echo delay changes
Playing adding an echo to a synth:
import time import board import audiobusio import synthio import audiodelays audio = audiobusio.I2SOut(bit_clock=board.GP20, word_select=board.GP21, data=board.GP22) synth = synthio.Synthesizer(channel_count=1, sample_rate=44100) echo = audiodelays.Echo(max_delay_ms=1000, delay_ms=850, decay=0.65, buffer_size=1024, channel_count=1, sample_rate=44100, mix=0.7, freq_shift=False) echo.play(synth) audio.play(echo) note = synthio.Note(261) while True: synth.press(note) time.sleep(0.25) synth.release(note) time.sleep(5)
- __exit__() None
Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.
- delay_ms: synthio.BlockInput
Delay of the echo in milliseconds. (read-only)
- decay: synthio.BlockInput
The rate the echo decays between 0 and 1 where 1 is forever and 0 is no echo.
- mix: synthio.BlockInput
The rate the echo mix between 0 and 1 where 0 is only sample and 1 is all effect.
- play(sample: circuitpython_typing.AudioSample, *, loop: bool = False) None
Plays the sample once when loop=False and continuously when loop=True. Does not block. Use
playingto block.The sample must match the encoding settings given in the constructor.
- class audiodelays.PitchShift(semitones: synthio.BlockInput = 0.0, mix: synthio.BlockInput = 1.0, window: int = 1024, overlap: int = 128, buffer_size: int = 512, sample_rate: int = 8000, bits_per_sample: int = 16, samples_signed: bool = True, channel_count: int = 1)
A pitch shift effect
- Create a pitch shift effect where the original sample play back is altered to change the
the perceived pitch by a factor of semi-tones (1/12th of an octave). This effect will cause a slight delay in the output depending on the size of the window and overlap buffers.
The mix parameter allows you to change how much of the unchanged sample passes through to the output to how much of the effect audio you hear as the output.
- Parameters:
semitones (synthio.BlockInput) – The amount of pitch shifting in semitones (1/12th of an octave)
mix (synthio.BlockInput) – The mix as a ratio of the sample (0.0) to the effect (1.0)
window (int) – The total size in bytes of the window buffer used alter the playback pitch
overlap (int) – The total size in bytes of the overlap buffer used to prevent popping in the output. If set as 0, no overlap will be used.
buffer_size (int) – The total size in bytes of each of the two playback buffers to use
sample_rate (int) – The sample rate to be used
channel_count (int) – The number of channels the source samples contain. 1 = mono; 2 = stereo.
bits_per_sample (int) – The bits per sample of the effect
samples_signed (bool) – Effect is signed (True) or unsigned (False)
Shifting the pitch of a synth by 5 semitones:
import time import board import audiobusio import synthio import audiodelays audio = audiobusio.I2SOut(bit_clock=board.GP0, word_select=board.GP1, data=board.GP2) synth = synthio.Synthesizer(channel_count=1, sample_rate=44100) pitch_shift = audiodelays.PitchShift(semitones=5.0, mix=0.5, window=2048, overlap=256, buffer_size=1024, channel_count=1, sample_rate=44100) pitch_shift.play(synth) audio.play(pitch_shift) while True: for notenum in (60, 64, 67, 71): synth.press(notenum) time.sleep(0.25) synth.release_all()
- __enter__() PitchShift
No-op used by Context Managers.
- __exit__() None
Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.
- semitones: synthio.BlockInput
The amount of pitch shifting in semitones (1/12th of an octave).
- mix: synthio.BlockInput
The output mix between 0 and 1 where 0 is only sample and 1 is all effect.
- play(sample: circuitpython_typing.AudioSample, *, loop: bool = False) None
Plays the sample once when loop=False and continuously when loop=True. Does not block. Use
playingto block.The sample must match the encoding settings given in the constructor.