Package psychopy :: Module sound
[frames] | no frames]

Module sound

source code

Load and play sounds

There are various APIs for this, none of which are perfect. By default PsychoPy will look for and use, in this order: ['pygame','pyglet','pyaudio'] The API chosen will be stored as a string under:

sound.audioAPI

and can be set using, e.g.:

sound.setAudioAPI('pyglet')
pygame (must be version 1.8 or above):
pros: The most robust of the API options so far - it works consistently on all platforms cons: needs an additional download, poor latencies
pyglet:
pros: comes with enthought python and is already the main API for drawing in PsychoPy cons: complex model using event_dispatch, dodgy timing (just on win32?)
pyaudio:
pros: relatively low-level wrapper around portAudio cons: needs another download, rather buggy.
Classes
  Sound
Create a sound object, from one of many ways.
  SoundPygame
Create a sound object, from one of many ways.
  SoundPyglet
Create a sound object, from one of MANY ways.
  SoundPyaudio
Create a sound object, from one of MANY ways.
Functions
 
initPyaudio()
define a thread for pyaudio event pumping (needed to fill buffers)
source code
 
initPyglet()
define a thread for pyglet event pumping (needed to fill buffers)
source code
 
initPygame(rate=22050, bits=16, stereo=True, buffer=1024)
If you need a specific format for sounds you need to run this init function.
source code
 
setAudioAPI(api)
Change the API used for the presentation of sounds
source code
Variables
  mediaLocation = ''
  preferredAPI = ['pygame', 'pyglet', 'pyaudio']
  audioAPI = 'pygame'
  havePyglet = True
  havePygame = True
  pa = pyaudio.PyAudio()
  havePyaudio = False
  API = 'pygame'
Function Details

initPygame(rate=22050, bits=16, stereo=True, buffer=1024)

source code 

If you need a specific format for sounds you need to run this init function. Run this before creating your visual.Window.

The format cannot be changed once initialised or once a Window has been created.

If a Sound object is created before this function is run it will be executed with default format (signed 16bit stereo at 44KHz).

For more details see pygame help page for the mixer.

setAudioAPI(api)

source code 

Change the API used for the presentation of sounds

usage:
setAudioAPI(api)
where:
api is one of 'pygame','pyglet', pyaudio'