Sampler functions
class sampler
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).
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.
Example:
from openexp.sampler import sampler
my_sampler = sampler(exp, exp.get_file('bark.ogg'))
my_sampler.play()
Function list:
- function sampler.__init__(experiment, src)
- function sampler.fade_in(ms)
- function sampler.is_playing()
- function sampler.pan(p)
- function sampler.pause()
- function sampler.pitch(p)
- function sampler.play(block=False)
- function sampler.resume()
- function sampler.stop()
- function sampler.stop_after(ms)
- function sampler.volume(vol)
- function sampler.wait()
function sampler.__init__(experiment, src)
Initializes the sampler with a specified file.
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
Arguments:
experiment
– The experiment object.- Type: experiment
src
– The full path to a.wav
or.ogg
file.- Type: unicode, str
function sampler.fade_in(ms)
Sets a fade-in time 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)
Arguments:
ms
– An integer value specifying the duration in milliseconds.- Type: int
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.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”).
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.pan('left')
Arguments:
p
– Panning. A float (p < 0 = to left, p > 0 = to right) or string (“left” or “right”).- Type: int, float, str, unicode
function 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()
function sampler.pitch(p)
Sets the relative pitch of the sample.
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.pitch(2.0)
Arguments:
p
– The pitch. p > 1.0 slows the sample down, p < 1.0 speeds the sample up.- Type: int, float
function sampler.play(block=False)
Plays the sound.
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.play()
Keywords:
block
– If True, the function blocks until the sound is finished. If False, the function returns right away and the sound is played in the background.- Type: bool
- Default: False
function 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()
function 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()
function sampler.stop_after(ms)
Specifies a duration after which the sampler stops playing.
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.stop_after(100)
Arguments:
ms
– An integer value specifying the duration in milliseconds.- Type: int
function sampler.volume(vol)
Sets the volume.
Example:
from openexp.sampler import sampler
src = exp.get_file('my_sound.ogg')
my_sampler = sampler(exp, src)
my_sampler.volume(0.5)
Arguments:
vol
– A volume level between 0.0 (silent) and 1.0 (full).- Type: int, float
function 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!')