SoundDevice

class SoundDevice[source]

Bases: SoundDeviceBase

Handles audio playback using sounddevice.

samplerate

Audio sample rate.

Type:

int

channels

Number of audio channels.

Type:

int

latency

Latency setting for sounddevice.

Type:

str

index

Index of the used sound device.

Type:

int

error

Error message.

Type:

str

stream

The active audio stream (internal).

Type:

sd.OutputStream

sound

The current sound buffer.

Type:

np.ndarray

command_queue

Queue for processing audio commands in a thread.

Type:

queue.Queue

thread

Background thread for audio processing.

Type:

threading.Thread

thread_running

Flag to control the background thread.

Type:

bool

__init__() None[source]

Initializes the SoundDevice with settings.

Methods

__init__()

Initializes the SoundDevice with settings.

create_sound_vec(left, right)

Interleaves left and right channels into a stereo array.

load(left, right)

Loads sound data into the playback queue.

load_wav(file)

Loads a WAV file into the playback queue.

play()

Triggers playback of the loaded sound.

shutdown()

Shuts down the audio thread and stream.

stop()

Stops playback.

Attributes

samplerate: int = 44100
error: str = 'Error connecting to the sound_device '
load(left: Any, right: Any) None[source]

Loads sound data into the playback queue.

Parameters:
  • left (Any) – Left channel data (array-like).

  • right (Any) – Right channel data (array-like).

Raises:

ValueError – If inputs are invalid or lengths differ.

load_wav(file: str) None[source]

Loads a WAV file into the playback queue.

Parameters:

file (str) – Filename of the WAV file in the media directory.

Raises:
  • FileNotFoundError – If the file does not exist.

  • ValueError – If sample rate mismatches or channel count is unsupported.

play() None[source]

Triggers playback of the loaded sound.

stop() None[source]

Stops playback.

shutdown() None[source]

Shuts down the audio thread and stream.

static create_sound_vec(left: numpy.ndarray, right: numpy.ndarray) numpy.ndarray.numpy.float32[source]

Interleaves left and right channels into a stereo array.

Parameters:
  • left (np.ndarray) – Left channel data.

  • right (np.ndarray) – Right channel data.

Returns:

Interleaved stereo data.

Return type:

np.ndarray[np.float32]