psychopy.sound
- play various forms of sound¶Sound
¶PsychoPy currently supports a choice of three sound libraries: pyo, sounddevice or pygame. Select which will be used via the audioLib preference. sound.Sound() will then refer to one of SoundDevice SoundPyo or SoundPygame. This can be set on a per-experiment basis by importing preferences, and setting the audioLib option to use.
- The pygame backend is the oldest and should always work without errors, but has the least good performance. Use it if latencies foryour audio don’t mattter.
- The pyo library is, in theory, the highest performer, but in practice it has ften had issues (at least on macOS) with crashes and freezing of experiments, or causing them not to finish properly. If those issues aren’t affecting your studies then this could be the one for you.
- The sounddevice library looks like the way of the future. The performance appears to be good (although this might be less so in cases where you have complex rendering being done as well because it operates from the same computer core as the main experiment code). It’s newer than pyo and so more prone to bugs and we haven’t yet added microphone support to record your participants.
Sounds are actually generated by a variety of classes, depending on which “backend” you use (like pyo or sounddevice) and these different backends can have slightly different attributes, as below.
The user should typically do:
from psychopy.sound import Sound
but the class that gets imported will then be an alias of one of the following.
psychopy.sound.backend_pygame.
SoundPygame
(value='C', secs=0.5, octave=4, sampleRate=44100, bits=16, name='', autoLog=True, loops=0, stereo=True)¶Create a sound object, from one of many ways.
Parameters: |
sampleRate(=44100): If a sound has already been created or if the
|
---|
fadeOut
(mSecs)¶fades out the sound (when playing) over mSecs. Don’t know why you would do this in psychophysics but it’s easy and fun to include as a possibility :)
getDuration
()¶Get’s the duration of the current sound in secs
getVolume
()¶Returns the current volume of the sound (0.0:1.0)
play
(fromStart=True, log=True, loops=None)¶Starts playing the sound on an available channel.
Parameters: |
|
---|---|
Notes: | If no sound channels are available, it will not play and return None. This runs off a separate thread i.e. your code won’t wait for the sound to finish before continuing. You need to use a psychopy.core.wait() command if you want things to pause. If you call play() whiles something is already playing the sounds will be played over each other. |
setVolume
(newVol, log=True)¶Sets the current volume of the sound (0.0:1.0)
stop
(log=True)¶Stops the sound immediately