Table Of Contents

Previous topic

psychopy.serial - functions for interacting with the serial port

Next topic

For Developers

This Page

Quick links

psychopy.sound - play various forms of sound

Sound

class psychopy.sound.SoundPygame(value='C', secs=0.5, octave=4, sampleRate=44100, bits=16, name='', autoLog=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): only used for sounds using pyglet. Pygame uses one rate for all sounds

sample rate for all sounds (once initialised)

bits(=16): Only 8- and 16-bits supported so far.

Only used for sounds using pyglet. Pygame uses the same sample rate 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)

Starts playing the sound on an available channel. 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)

Sets the current volume of the sound (0.0:1.0)

stop()

Stops the sound immediately