You are viewing unmaintained documentation for an older version of OpenSesame. Click here to view the current documentation. Your version of OpenSesame:

Test the next generation of OpenSesame: 2.9.0 Hesitant Heisenberg!

Sampler functions

The sampler module provides functionality to play sound samples in .ogg and .wav format (Audacity is an excellent free tool to convert samples from other formats). For example, the following script plays the sample ‘bark.ogg’, which it retrieves from the file pool using exp.get_file():

from openexp.sampler import sampler
my_sampler = sampler(exp, exp.get_file('bark.ogg'))
my_sampler.play()

Important note: 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.

For OpenSesame 2.8.3

class sampler

Function overview:
__init__
fade_in
is_playing
pan
pause
pitch
play
resume
stop
stop_after
volume
wait

sampler.__init__(experiment, src)
Initializes the sampler with a specified file.

Arguments:
experiment → An instance of libopensesame.experiment.experiment.
src → A path to a .wav or .ogg file.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)

sampler.fade_in(ms)
Sets the fade-in time in milliseconds.

Arguments:
ms → An integer value specifying the duration in milliseconds.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.fade_in(100)

sampler.is_playing()
Checks if a sound is currently playing.

Returns:
True if a sound is playing, False if not.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
self.sleep(100)
if my_sampler.is_playing():
	print('The sampler is still playing!')

sampler.pan(p)
Sets the panning of the sample. The volume of the "unpanned" channel decreases, the volume of the other channel remains the same. To fully mute one channel specify "left" (mutes right, pans to left) or "right" (mutes left, pans to right").

Arguments:
p → Panning. A float (p < 0 = to left, p > 0 = to right) or string ("left" or "right").

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.pan('left')

sampler.pause()
Pauses playback (if any).

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
self.sleep(100)
my_sampler.pause()
self.sleep(100)
my_sampler.resume()

sampler.pitch(p)
Sets the relative pitch of the sample.

Arguments:
p → The pitch. p > 1.0 slows the sample down, p < 1.0 speeds the sample up.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.pitch(2.0)

sampler.play(block=False)
Plays the sound.

Keyword arguments:
block → If True, block until the sound is finished (default = False).

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()

sampler.resume()
Resumes playback (if any).

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
self.sleep(100)
my_sampler.pause()
self.sleep(100)
my_sampler.resume()

sampler.stop()
Stops the currently playing sound (if any).

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
self.sleep(100)
my_sampler.stop()

sampler.stop_after(ms)
Specifies a duration after which the sampler stops playing.

Arguments:
ms → An integer value specifying the duration in milliseconds.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.stop_after(100)

sampler.volume(vol)
Sets the volume.

Arguments:
vol → A volume between 0.0 and 1.0

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.volume(0.5)

sampler.wait()
Blocks until the sound has finished playing or returns right away if no sound is playing.

Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
my_sampler.wait()
print('The sampler is finished!')