Supported by Supported by

Sampler functions

class sampler

The sampler module provides functionality to play sound samples.

Example:

src = pool['bark.ogg']
my_sampler = sampler(src, volume=.5)
my_sampler.play()

Things to know

Sampling rate

If you find that your sample plays to 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 back-end settings.

Supported file formats

Sound files in .wav and .ogg format are supported. If you need to convert samples from a different format, you can use Audacity.

Backwards incompatible changes from 2.9 to 3.0

The following are now properties, as described under

  • [sampler.block]
  • [sampler.duration]
  • [sampler.fade_in]
  • [sampler.pan]
  • [sampler.pitch]
  • [sampler.volume]

Therefore, the following will no longer work:

sampler.volume(.5)

And has to be changed to:

sampler.volume = .5

Playback keywords

Functions that accept **playback_args take the following keyword arguments:

  • volume specifies a volume between 0.0 (silent) and 1.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 the sound in milliseconds, or is set to 0 or None to play the full sound.
  • fade_in specifies the fade-in time (or attack) of the sound, or is set to 0 or None to disable fade-in.
  • block indicates whether the experiment should block (True) during playback or not (False).
src = exp.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.__init__). To change the behavior for all subsequent operations, set the playback properties directly:

src = exp.pool['bark.ogg']
my_sampler = sampler(src)
my_sampler.volume = .5
my_sampler.pan = 'left'
my_sampler.play()
my_sampler.play()

Or pass the playback keywords to sampler.__init__:

src = exp.pool['bark.ogg']
my_sampler = sampler(src, volume=.5, pan='left')
my_sampler.play()
my_sampler.play()

function sampler.__init__(experiment, src, **playback_args)

Constructor to create a new sampler object. You do not generally call this constructor directly, but use the sampler() function, which is described here: /python/sampler/.

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src, volume=.5)

Arguments:

  • experiment -- The experiment object.
    • Type: experiment
  • src -- The full path to a .wav or .ogg file.
    • Type: unicode, str

Keyword dict:

  • **playback_args: Optional [playbac

function sampler.close_sound(experiment)

Closes the mixer after the experiment is finished.

Arguments:

  • experiment -- The experiment object.
    • Type: experiment

function sampler.init_sound(experiment)

Initializes the pygame mixer before the experiment begins.

Arguments:

  • experiment -- The experiment object.
    • Type: experiment

function sampler.is_playing()

Checks if a sound is currently playing.

Returns:

True if a sound is playing, False if not.

  • Type: bool

function sampler.pause()

Pauses playback (if any).

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src)
my_sampler.play()
sleep(100)
my_sampler.pause()
sleep(100)
my_sampler.resume()

function sampler.play(**playback_args)

Plays the sound.

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src)
my_sampler.play(pitch=.5, block=True)

Keyword dict:

function sampler.resume()

Resumes playback (if any).

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src)
my_sampler.play()
sleep(100)
my_sampler.pause()
sleep(100)
my_sampler.resume()

function sampler.stop()

Stops the currently playing sound (if any).

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src)
my_sampler.play()
sleep(100)
my_sampler.stop()

function sampler.wait()

Blocks until the sound has finished playing or returns right away if no sound is playing.

Example:

src = exp.pool[u'my_sound.ogg']
my_sampler = sampler(src)
my_sampler.play()
my_sampler.wait()
print('The sampler is finished!')