stimuli.audio.backend.SoundSD🔗

class stimuli.audio.backend.SoundSD(device, sample_rate, *, clock=<class 'stimuli.time._clock.Clock'>)[source]🔗

Sounddevice backend for audio playback.

Parameters:
deviceint

Device index of the output device as provided by sounddevice.query_devices(). If None, the default output device is used.

sample_rateint

The sample rate of the audio data, which should match the sample rate of the output device. If None, the default sample rate of the device is used.

clockstimuli.time.BaseClock

Clock object to use for time measurement. By default, the stimuli.time.Clock class is used.

Attributes

clock

The clock object used for time measurement.

duration

The duration of the audio data, in seconds.

sample_rate

The sample rate of the audio data.

Methods

close()

Close the backend and release the resources.

initialize(data[, block_size])

Initialize the backend with audio data.

play([when, blocking])

Play the audio data.

stop()

Interrupt immediately the playback of the audio data.

close()[source]🔗

Close the backend and release the resources.

initialize(data, block_size=32)[source]🔗

Initialize the backend with audio data.

Parameters:
datandarray of shape (n_frames, n_channels)

The audio data to play provided as a 2 dimensional array of shape (n_frames, n_channels). The array layout must be C-contiguous. A one dimensional ndarray of shape (n_frames,) is also accepted for mono audio.

block_sizeint

The number of frames passed to the stream callback function, or the preferred block granularity for a blocking read/write stream. The special value blocksize=0 may be used to request that the stream callback will receive an optimal (and possibly varying) number of frames based on host requirements and the requested latency settings.

play(when=None, *, blocking=False)[source]🔗

Play the audio data.

Parameters:
whenfloat | None

The relative time in seconds when to start playing the audio data. For instance, 0.2 will start playing in 200 ms. If None, the audio data is played as soon as possible. A duration superior to the device latency is recommended.

blockingbool

If True, the function blocks until the audio playback is finished.

stop()[source]🔗

Interrupt immediately the playback of the audio data.

property clock🔗

The clock object used for time measurement.

property duration🔗

The duration of the audio data, in seconds.

property sample_rate🔗

The sample rate of the audio data.