Speaker

class psychopy.hardware.speaker.SpeakerDevice(index=None, name=None, latencyClass=1, resample=True)[source]

Class for managing a physical speaker device for audio playback.

Parameters:
  • index (int, optional) – Numeric index for the physical speaker device, according to psychtoolbox. Leave as None to find the speaker by name.

  • name (str, optional) – String name for the physical speaker device, according to your operating system. Leave as None to find the speaker by numeric index.

  • latencyClass (int) –

    One of:

    • 0: Don’t take exclusive control over the speaker, so other apps can still use it. Send

    sounds via the system mixer so that sample rates are all handled, even though this introduces latency.

    • 1: Don’t take exclusive control over the speaker, so other apps can still use it. Send

    sounds directly to reduce latency, so sounds will need to match the sample rate of the speaker. Recommended in most cases; if `resample` is True then sample rates are already handled on load!

    • 2: Take exclusive control over the speaker, so other apps can’t use it. Send sounds

    directly to reduce latency, so sounds will need to be the same sample rate as one another, but this can be any sample rate supported by the speaker.

    • 3: Take exclusive control over the speaker, so other apps can’t use it. Send sounds

    directly to reduce latency, so sounds will need to be the same sample rate as one another, but this can be any sample rate supported by the speaker. Force the system to prioritise resources towards playing sounds on this speaker for absolute minimum latency, but fallback to mode 2 if the system rejects this.

    • 4: Take exclusive control over the speaker, so other apps can’t use it. Send sounds

    directly to reduce latency, so sounds will need to be the same sample rate as one another, but this can be any sample rate supported by the speaker. Force the system to prioritise resources towards playing sounds on this speaker for absolute minimum latency, and raise an error if the system rejects this.

  • resample (bool, optional) – If the sample rate of an audio clip doesn’t match the sample rate of the speaker, should PsychoPy resample the sound on load?

close()[source]

Close the audio stream for this speaker.

createStream()[source]

Create the psychtoolbox audio stream

Calling this method will set the following attributes
profile

The profile from psychtoolbox, a dict with the following keys: Active, State, RequestedStartTime, StartTime, CaptureStartTime, RequestedStopTime, EstimatedStopTime, CurrentStreamTime, ElapsedOutSamples, PositionSecs, RecordedSecs, ReadSecs, SchedulePosition, XRuns, TotalCalls, TimeFailed, BufferSize, CPULoad, PredictedLatency, LatencyBias, SampleRate, OutDeviceIndex, InDeviceIndex

Type:

dict

index

A numeric index referring to the device. This may differ from the value of index this object was initialised with, as this will be the numeric index of the actual physical speaker best matching what was requested.

Type:

int

name

A string name referring to the device. This may differ from the value of name this object was initialised with, as this will be the system-reported name of the actual physical speaker best matching what was requested.

Type:

str

property exclusive
returns: Does PsychoPy have exclusive control of this speaker? If True then other apps will not be

able to play sounds on the same speaker.

Return type:

bool

static getAvailableDevices()[source]

Get all available devices of this type.

Returns:

List of dictionaries containing the parameters needed to initialise each device.

Return type:

list[dict]

getDeviceProfile()

Generate a dictionary describing this device by finding the profile from getAvailableDevices which represents the same physical device as this object.

Returns:

Dictionary representing this device

Return type:

dict

getJSON(asString=True)

Convert the output of getDeviceProfile to a JSON string.

Parameters:

asString (bool) – If True, then the output will be converted to a string, otherwise will simply be a JSON-friendly dict.

Returns:

JSON string (or JSON friendly dict) of getDeviceProfile.

Return type:

str or dict

property isOpen

Is this speaker “open”, i.e. is it active and ready for a Sound to play tracks on it

isSameDevice(other)[source]

Determine whether this object represents the same physical speaker as a given other object.

Parameters:

other (SpeakerDevice, dict) – Other SpeakerDevice to compare against, or a dict of params (which must include index as a key)

Returns:

True if the two objects represent the same physical device

Return type:

bool

open()[source]

Open the audio stream for this speaker so that sound can be played to it.

testDevice()[source]

Play a simple sound to check whether this device is working.


Back to top