Audio Session
minispeaker.player
Speakers
class Speakers()
Class that offers an easy interface to play audio.
Due to the supporting library implementation, each physical playback device should correspond to one Speaker class per Python process. In other words, don't try to have two Speakers with one device and expect functionality.
Attributes:
name
Optional[str], optional - The name of the speaker to playback to, found by available_speakers(). If no name is given, use the default speakers on the system. Defaults to field(default_factory=default_speaker).sample_rate
int, optional - The sample rate of the audio in Hz. Defaults to 44100.sample_format
SampleFormat, optional - The bit depth of the audio. Defaults to SampleFormat.SIGNED16.channels
int, optional - The number of audio channels. Defaults to 1.buffer_size
int, optional - The size of each audio buffer in samples. Defaults to 128.volume
float, optional - The initial volume of the speaker as a percent decimal. Defaults to 1.0.
Speakers.pause
def pause()
Pauses the speaker. Does nothing if the speaker is already paused.
Speakers.cont
def cont()
Unpauses the speaker. Does nothing if the speaker is already playing.
Speakers.mute
def mute()
Mutes the speaker. The audio will still be playing but it won't be heard. Is no-op if the speaker is already muted.
Speakers.unmute
def unmute()
Unmutes the speaker. Does nothing if the speaker is not muted.
Speakers.play
def play(audio: (str
| Iterator[memoryview | bytes | ArrayLike]
| AsyncIterator[memoryview | bytes | ArrayLike]
| Generator[memoryview | bytes | ArrayLike, int, None]
| AsyncGenerator[memoryview | bytes | ArrayLike, int]),
name: Optional[str] = None,
volume: Optional[float] = None,
paused: Optional[bool] = False,
muted: Optional[bool] = False,
realtime: Optional[bool] = False)
Plays audio to the speaker.
Arguments:
audio
str | Iterator[memoryview | bytes | ArrayLike] | AsyncIterator[memoryview | bytes | ArrayLike] | Generator[memoryview | bytes | ArrayLike, int, None] | AsyncGenerator[memoryview | bytes | ArrayLike, int] - Audio file path or audio stream. The audio stream can either be any form of async/sync iterator or generator. Keep in mind that for generators, they must be pre-initialized via next() and yield audio chunks as some form of an array, to allow the ability to send() a number into the generator and receive a corresponding number of audio frames, instead of the unknown pre-set amount. See memory_stream() for an example.name
Optional[str] - A custom name which will be accessible by self[name]. Defaults to None.volume
Optional[float] - The individual Track's volume. Defaults to None.paused
Optional[bool] - Should the audio be immediately paused before playback? Defaults to False.muted
Optional[bool] - Should the audio be immediately muted before playback? Defaults to False.realtime
Optional[bool] - Should the audio(if asynchronous) be played in realtime? Defaults to False.
Raises:
TypeError
- The audio input is not valid and must be a correct file path.
Examples:
Basic usage with context manager: python >>> with Speaker(name="My device speakers") as speaker: ... speaker.play("track.mp3", 'special name') ... speaker.play("test.mp3") # Both track.mp3 and test.mp3 are playing ... speaker['special name'].wait() # Wait until 'special name', or 'track.mp3' is finished. 'test.mp3' might still be playing. ... speaker.wait() # Wait until all the tracks are finished
Manual usage: python >>> speaker = Speakers(name="My device speakers") >>> speaker.play("track.mp3") >>> speaker.wait() # Wait until track is finished >>> speaker.stop()
Speakers.exit
def exit()
Close the speaker. After Speakers().exit() is called, any calls to play with this Speaker object will be undefined behavior.
Speakers.wait
def wait() -> bool | Coroutine[Any, Any, Literal[True]]
All Tracks
being played are done in the background. Call this function to wait
until no Track
s in Speaker
can be played.
Returns:
bool | Coroutine[Any, Any, Literal[True]]: Either a synchronous or asynchronous return result of Event.wait
Speakers.clear
def clear()
Removes all current tracks. An alert is sent indicating all the tracks are finished.
Speakers.__getitem__
def __getitem__(name: str) -> Track
Retrieves a Track
.
Arguments:
name
str - The name of theTrack
.
Returns:
Track
- ATrack
calledname
Speakers.__delitem__
def __delitem__(name: str)
Remove a Track
called name
. An alert is sent indicating thatTrack
is finished.
Arguments:
name
str - Name of theTrack
Speakers.__enter__
def __enter__() -> Speakers
Since resource initialization is done at play
request rather than through a context manager, this function is no-op.
Returns:
Speakers
- Itself
.
Speakers.__exit__
def __exit__(exc_type, exc_value, traceback)
This class instance cannot be used after closing resources.