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.

SoundDevice backend

Pyo backend

pygame backend

class 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:
value: can be a number, string or an array:
  • If it’s a number between 37 and 32767 then a tone will be generated at that frequency in Hz.
  • It could be a string for a note (‘A’, ‘Bfl’, ‘B’, ‘C’, ‘Csh’, …). Then you may want to specify which octave as well
  • Or a string could represent a filename in the current location, or mediaLocation, or a full path combo
  • Or by giving an Nx2 numpy array of floats (-1:1) you can specify the sound yourself as a waveform
secs: duration (only relevant if the value is a note name or a

frequency value)

octave: is only relevant if the value is a note name.

Middle octave of a piano is 4. Most computers won’t output sounds in the bottom octave (1) and the top octave (8) is generally painful

sampleRate(=44100): If a sound has already been created or if the

bits(=16): Pygame uses the same bit depth for all sounds once

initialised

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:
fromStart : bool

Not yet implemented.

log : bool

Whether or not to log the playback event.

loops : int

How many times to repeat the sound after it plays once. If loops == -1, the sound will repeat indefinitely until stopped.

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


Back to top