Supplementary¶
This section contains reference documentation for the more advanced concepts and types in SonoLink. If you’re new to SonoLink, you should start with the SonoLink Documentation and the Guides before diving into this section.
Queue¶
- class sonolink.History(settings: HistorySettings | None = None)[source]¶
A specialized queue for tracking playback history.
This class is intended to be read-only for users. Mutation methods will raise
AttributeError.
- class sonolink.Queue(*, mode: QueueMode = QueueMode.NORMAL, history_settings: HistorySettings | None = None)[source]¶
- Attributes
A queue implementation for managing playable tracks.
- clear()¶
Remove all items from the queue.
- copy()[source]¶
Create a shallow copy of the queue.
- Returns:
A shallow copy of the queue with the same items, mode, shuffle state, and history.
- Return type:
- dedupe(*, key: ~typing.Callable[[~sonolink.models.track.Playable], ~typing.Any] = <function Queue.<lambda>>)[source]¶
Remove duplicate tracks in place, keeping the first occurrence of each.
- Parameters:
key (
Callable[[Playable],Any]) – A function that returns the value used to determine duplicates. Defaults to comparing byidentifier.- Returns:
The number of tracks removed.
- Return type:
- get(*, key: Callable[[Playable], Any] | None = None)[source]¶
Get the next track from the queue, respecting the current queue mode.
User-added tracks always take priority over AutoPlay-discovered tracks. If the user lane is empty, falls back to the AutoPlay lane. If the queue is in
LOOPmode, returns the current track. If the queue is inLOOP_ALLmode and empty, restores tracks from history. Ifshuffle_modeisShuffleMode.PERSISTENT, a random track is popped from the user lane instead of the head.- Parameters:
key (
Callable[[Playable],Any] |None) –If provided and
current_trackis set, the picked track is skipped in favour of the next candidate whosekey(track)differs fromkey(current_track). Useful to avoid repeats, e.g.key=lambda track: track.authorto avoid picking a track by the currently playing artist. If every remaining candidate matches, the match is allowed rather than raising. Only applies to the user lane. Defaults toNone.Added in version 1.3.0.
- Returns:
The retrieved track.
- Return type:
- Raises:
QueueEmpty – Both the user queue and AutoPlay queue are empty.
- async get_wait(*, key: Callable[[Playable], Any] | None = None)[source]¶
Asynchronously get a track from the queue, waits if necessary.
This method will wait indefinitely until a track is available in the queue. This method can be used to implement a system that waits for a next track to play after the current track finishes, for example.
- move(old: int, new: int)[source]¶
Move a track from one index to another, shifting other tracks.
- Parameters:
- Returns:
The moved track.
- Return type:
- Raises:
QueueEmpty – The queue is empty.
IndexError – One or both indices are out of range.
- pop()[source]¶
Remove and return the next track from the queue.
The returned track is set as the current track. If history is enabled, the previous current track is added to history.
- Returns:
The popped track.
- Return type:
- Raises:
QueueEmpty – The queue is empty.
- pop_at(index: int)[source]¶
Remove and return a track from a specific queue index.
The returned track is set as the current track. If history is enabled, the previous current track is added to history.
- Parameters:
index (
int) – The index to pop from.- Returns:
The popped track.
- Return type:
- Raises:
QueueEmpty – The queue is empty.
IndexError – There is no item at the given index.
- previous()[source]¶
Pop the most recent track from history and set it as current.
The current track is pushed back to the front of the queue.
- Returns:
The track retrieved from history.
- Return type:
- Raises:
QueueEmpty – The history is empty.
- put(tracks: Iterable[Playable] | Playable | Playlist, /, *, atomic: bool = True)[source]¶
Put one or more tracks at the end of the queue.
- Parameters:
tracks (
Iterable[Playable] |Playable|Playlist) – The track(s) or playlist to add to the queue.atomic (
bool) – Whether to insert the items atomically. IfTrue, all items must be Playable or a TypeError is raised and nothing is added. IfFalse, non-Playable items are filtered out. Defaults toTrue.
- Returns:
The number of tracks added to the queue.
- Return type:
- Raises:
TypeError – When
atomic=Trueand a non-Playable item is encountered.
- put_at(index: int, tracks: Iterable[Playable] | Playable | Playlist, /, *, atomic: bool = True)¶
Put one or more tracks at
index.- Parameters:
index (
int) – The index to insert the track(s) to.tracks (
Iterable[Playable] |Playable|Playlist) – The track(s) or playlist to add to the queue.atomic (
bool) – Whether to insert the items atomically. IfTrue, all items must be Playable or a TypeError is raised and nothing is added. IfFalse, non-Playable items are filtered out. Defaults toTrue.
- Returns:
The number of tracks added to the queue.
- Return type:
- Raises:
TypeError – When
atomic=Trueand a non-Playable item is encountered.
- put_autoplay(tracks: Iterable[Playable] | Playable, /)[source]¶
Add AutoPlay-discovered tracks to the AutoPlay lane.
These tracks are only played once all user-added tracks are exhausted. Each track is automatically tagged with
autoplay.
- async put_wait(tracks: Iterable[Playable] | Playable | Playlist, /, *, atomic: bool = True)[source]¶
Asynchronously put one or more tracks into the queue.
This method is thread-safe and maintains insert order through a lock.
- Parameters:
tracks (
Iterable[Playable] |Playable|Playlist) – The track(s) or playlist to add to the queue.atomic (
bool) – Whether to insert the items atomically. IfTrue, all items must be Playable or a TypeError is raised and nothing is added. IfFalse, non-Playable items are filtered out. Defaults toTrue.
- Returns:
The number of tracks added to the queue.
- Return type:
- Raises:
TypeError – When
atomic=Trueand a non-Playable item is encountered.
- remove(tracks: Iterable[Playable] | Playable | Playlist, /, *, remove_all: bool = True, key: Callable[[Playable], Any] | None = None)¶
Remove one or more tracks from this queue.
- Parameters:
tracks (
Iterable[Playable] |Playable|Playlist) – The track(s) or playlist to remove from the queue.remove_all (
bool) – Whether to remove all occurrences of a track from this queue. When set toFalse, only the first occurrence of each track is removed. Defaults toTrue.key (
Callable[[Playable],Any] |None) –If provided, tracks are matched by comparing
key(track)against each value intracksinstead of using equality directly. This lets you remove tracks without holding the originalPlayableinstance, e.g.queue.remove([track.identifier], key=lambda t: t.identifier). Whenkeyis set, items intracksare used as-is and are not coerced toPlayable. Defaults toNone.Added in version 1.3.0.
- Returns:
The number of tracks removed from the queue.
- Return type:
- remove_at(index: int)[source]¶
Remove and return a track at the given index without side effects.
Unlike
pop_at(), this method does not set the removed track as the current track or push the previous current track to history.Added in version 1.3.0.
- Parameters:
index (
int) – The index of the track to remove.- Returns:
The removed track.
- Return type:
- Raises:
QueueEmpty – The queue is empty.
IndexError – There is no item at the given index.
- async remove_wait(tracks: Iterable[Playable] | Playable | Playlist, /, *, remove_all: bool = True, key: Callable[[Playable], Any] | None = None)[source]¶
Asynchronously remove one or more tracks from the queue.
This method is thread-safe.
- Parameters:
tracks (
Iterable[Playable] |Playable|Playlist) – The track(s) or playlist to remove from the queue.remove_all (
bool) – Whether to remove all occurrences of a track from this queue. When set toFalse, only the first occurrence of each track is removed. Defaults toTrue.key (
Callable[[Playable],Any] |None) –If provided, tracks are matched by comparing
key(track)against each value intracksinstead of using equality directly. Defaults toNone.Added in version 1.3.0.
- Returns:
The number of tracks removed from the queue.
- Return type:
- reset()[source]¶
Reset the queue to its default state.
This will:
Clear all items from the queue
Clear AutoPlay-discovered tracks
Clear history
Reset the mode to
QueueMode.NORMALReset
shuffle_modetoShuffleMode.DEFAULTClear the current track
Cancel all waiting futures
- shuffle()[source]¶
Shuffle the queue in place, once.
This permanently reorders
tracks; there is no way to undo it or recover the previous order afterwards. This is unrelated to, and does not change,shuffle_mode– for a toggleable shuffle that can be turned back off without losing the original order, setshuffle_modetoShuffleMode.PERSISTENTinstead.This does not return anything.
- swap(old: int, new: int)[source]¶
Swap two tracks in the queue by index.
- Parameters:
- Raises:
IndexError – One or both indices are out of range.
- property autoplay_tracks: list[Playable]¶
The list of AutoPlay-discovered tracks currently staged in the queue.
These tracks are only played once all user-added tracks are exhausted. Modifying this list will not affect the actual queue.
Added in version 1.1.0.
- Returns:
A list of AutoPlay tracks.
- Return type:
list[
Playable]
- property count: int¶
The number of tracks in this collection.
- Returns:
The total number of tracks currently held.
- Return type:
- property current_track: Playable | None¶
The currently loaded track.
This track is typically the one being played or most recently played.
This is also used when
modeis set toQueueMode.LOOPto determine which track to repeat. This can be manually set too.- Returns:
The current track, or
Noneif not set.- Return type:
sonolink.models.Playable| None
- property history: History | None¶
The queue history.
- Returns:
The queue history if history is enabled, otherwise
None.- Return type:
History| None
- property mode: QueueMode¶
The current queue mode.
QueueMode.NORMAL: Tracks are played in order and removed from the queue.QueueMode.LOOP:current_trackis repeated indefinitely until manually changed.QueueMode.LOOP_ALL: When the queue is empty, all tracks from history are restoredto the queue and played again.
- Returns:
The current queue mode.
- Return type:
- property shuffle_mode: ShuffleMode¶
The queue’s current shuffle state.
When
ShuffleMode.PERSISTENT,get()pops a random track from the user lane (tracks) instead of always popping the head. This is independent ofshuffle(), and never touchestracksitself — setting this back toShuffleMode.DEFAULTdoes not need to restore anything, since the underlying order was never changed while persistent shuffle was on.This is independent of
modeand composes with anyQueueMode. It only affects the user lane; the AutoPlay lane is unaffected.Added in version 1.3.0.
- Returns:
The queue’s current shuffle state.
- Return type:
- property tracks: list[Playable]¶
The list of tracks currently in the queue.
This does not include the
current_trackor AutoPlay-discovered tracks. Modifying this list will not affect the actual queue; use methods likeput()orpop_at()for modifications.- Returns:
A list of tracks in the queue.
- Return type:
list[
Playable]
Network¶
Messages¶
- class sonolink.network.message.MessageType(*values)[source]¶
- classmethod from_bytes(bytes, byteorder='big', *, signed=False)¶
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
- signed
Indicates whether two’s complement is used to represent the integer.
- as_integer_ratio()¶
Return a pair of integers, whose ratio is equal to the original int.
The ratio is in lowest terms and has a positive denominator.
>>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1)
- bit_count()¶
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- bit_length()¶
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- conjugate()¶
Returns self, the complex conjugate of any int.
- is_integer()¶
Returns True. Exists for duck type compatibility with float.is_integer.
- to_bytes(length=1, byteorder='big', *, signed=False)¶
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- BINARY = 2¶
- CLOSE = 8¶
- CONTINUATION = 0¶
- PING = 9¶
- PONG = 10¶
- TEXT = 1¶
- denominator¶
the denominator of a rational number in lowest terms
- imag¶
the imaginary part of a complex number
- numerator¶
the numerator of a rational number in lowest terms
- real¶
the real part of a complex number
Utilities API¶
- sonolink.utils.cached_property(attribute: str)[source]¶
Create a cached property that stores the result under
class.attribute.