Sampler functions
class Sampler
The Sampler
class provides functionality to play sound samples. You
generally create a Sampler
object with the Sampler()
factory function,
as described in the section Creating a Sampler.
Example:
src = pool['bark.ogg']
my_sampler = Sampler(src, volume=.5)
my_sampler.play()
Things to know
Creating a Sampler
You generally create a Sampler
with the Sampler()
factory function, which
takes the full path to a sound file as the first argument.
src = pool['bark.ogg']
my_sampler = Sampler(src)
Optionally, you can pass Playback keywords to Sampler()
to set the default behavior:
src = pool['bark.ogg']
my_sampler = Sampler(src, volume=.5)
Sampling rate
If you find that your sample plays too slowly (low pitch) or too quickly (high pitch), make sure that the sampling rate of your sample matches the sampling rate of the sampler back-end as specified under backend settings.
Supported file formats
Sound files in .wav
, .mp3
, and .ogg
format are supported. If you need to
convert samples from a different format, you can use
Audacity.
Playback keywords
Functions that accept **playback_args
take the following keyword arguments:
volume
specifies a volume between0.0
(silent) and1.0
(maximum).pitch
specifies a pitch (or playback speed), where values > 1 indicate a higher pitch, and values < 1 indicate a lower pitch.pan
specifies a panning, where values < 0 indicate panning to the left, and values > 0 indicate panning to the right. Alternatively, you can set pan to 'left' or 'right' to play only a single channel.duration
specifies the duration of the sound in milliseconds, or is set to0
orNone
to play the full sound.fade_in
specifies the fade-in time (or attack) of the sound, or is set to0
orNone
to disable fade-in.block
indicates whether the experiment should block (True
) during playback or not (False
).
src = pool['bark.ogg']
my_sampler = Sampler(src)
my_sampler.play(volume=.5, pan='left')
Playback keywords only affect the current operation (except when passed to
Sampler()
when creating the object). To change the behavior for all
subsequent operations, set the playback properties directly:
src = pool['bark.ogg']
my_sampler = Sampler(src)
my_sampler.volume = .5
my_sampler.pan = 'left'
my_sampler.play()
Or pass the playback keywords to Sampler()
when creating the object:
src = pool['bark.ogg']
my_sampler = Sampler(src, volume=.5, pan='left')
my_sampler.play()
close_sound(experiment)
Closes the mixer after the experiment is finished.
Parameters
- experiment: The experiment object.
init_sound(experiment)
Initializes the pygame mixer before the experiment begins.
Parameters
- experiment: The experiment object.
is_playing()
Checks if a sound is currently playing.
Returns
- True if a sound is playing, False if not.
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play()
sleep(100)
if my_sampler.is_playing():
print('The sampler is still playing!')
pause()
Pauses playback (if any).
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play()
sleep(100)
my_sampler.pause()
sleep(100)
my_sampler.resume()
play(*arglist, **kwdict)
Plays the sound.
Parameters
- **playback_args: Optional playback keywords that will be used
for this call to
Sampler.play()
. This does not affect subsequent operations.
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play(pitch=.5, block=True)
resume()
Resumes playback (if any).
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play()
sleep(100)
my_sampler.pause()
sleep(100)
my_sampler.resume()
set_config(**cfg)
Updates the configurables.
Parameters
- **cfg: The to-be-updated configurables.
stop()
Stops the currently playing sound (if any).
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play()
sleep(100)
my_sampler.stop()
wait()
Blocks until the sound has finished playing or returns right away if no sound is playing.
Example
src = pool['my_sound.ogg']
my_sampler = Sampler(src)
my_sampler.play()
my_sampler.wait()
print('The sampler is finished!')