aqt.sound

Contents

aqt.sound#

Attributes#

Classes#

Player

Helper class that provides a standard way to create an ABC using

SoundOrVideoPlayer

Helper class that provides a standard way to create an ABC using

SoundPlayer

Helper class that provides a standard way to create an ABC using

VideoPlayer

Helper class that provides a standard way to create an ABC using

AVPlayer

SimpleProcessPlayer

A player that invokes a new process for each tag to play.

SimpleMpvPlayer

A player that invokes a new process for each tag to play.

SimpleMplayerPlayer

A player that invokes a new process for each tag to play.

MpvManager

Class for communication with the mpv media player via unix socket

SimpleMplayerSlaveModePlayer

A player that invokes a new process for each tag to play.

Recorder

Helper class that provides a standard way to create an ABC using

QtAudioInputRecorder

Helper class that provides a standard way to create an ABC using

NativeMacRecorder

Helper class that provides a standard way to create an ABC using

RecordDialog

Functions#

is_audio_file(→ bool)

retryWait(→ int)

encode_mp3(→ None)

Encode the provided wav file to .mp3, and call on_done() with the path.

record_audio(→ None)

clearAudioQueue(→ None)

play(→ None)

playFromText(→ None)

av_refs_to_play_icons(→ str)

Add play icons into the HTML.

play_clicked_audio(→ None)

eg. if pycmd is 'play:q:0', play the first audio on the question side.

setup_audio(→ None)

cleanup_audio(→ None)

Module Contents#

aqt.sound.OnDoneCallback#
class aqt.sound.Player#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstractmethod play(tag: anki.sound.AVTag, on_done: OnDoneCallback) None#

Play a file.

When reimplementing, make sure to call gui_hooks.av_player_did_begin_playing(self, tag) on the main thread after playback begins.

abstractmethod rank_for_tag(tag: anki.sound.AVTag) int | None#

How suited this player is to playing tag.

AVPlayer will choose the player that returns the highest rank for a given tag.

If None, this player can not play the tag.

stop() None#

Optional.

If implemented, the player must not call on_done() when the audio is stopped.

seek_relative(secs: int) None#

Jump forward or back by secs. Optional.

toggle_pause() None#

Optional.

shutdown() None#

Do any cleanup required at program termination. Optional.

aqt.sound.AUDIO_EXTENSIONS#
aqt.sound.is_audio_file(fname: str) bool#
class aqt.sound.SoundOrVideoPlayer#

Bases: Player

Helper class that provides a standard way to create an ABC using inheritance.

default_rank = 0#
rank_for_tag(tag: anki.sound.AVTag) int | None#

How suited this player is to playing tag.

AVPlayer will choose the player that returns the highest rank for a given tag.

If None, this player can not play the tag.

class aqt.sound.SoundPlayer#

Bases: Player

Helper class that provides a standard way to create an ABC using inheritance.

default_rank = 0#
rank_for_tag(tag: anki.sound.AVTag) int | None#

How suited this player is to playing tag.

AVPlayer will choose the player that returns the highest rank for a given tag.

If None, this player can not play the tag.

class aqt.sound.VideoPlayer#

Bases: Player

Helper class that provides a standard way to create an ABC using inheritance.

default_rank = 0#
rank_for_tag(tag: anki.sound.AVTag) int | None#

How suited this player is to playing tag.

AVPlayer will choose the player that returns the highest rank for a given tag.

If None, this player can not play the tag.

class aqt.sound.AVPlayer#
players: list[Player] = []#
interrupt_current_audio = True#
current_caller: Any = None#
current_caller_interrupted = False#
current_player: Player | None = None#
play_tags(tags: list[anki.sound.AVTag]) None#

Clear the existing queue, then start playing provided tags.

append_tags(tags: list[anki.sound.AVTag]) None#

Append provided tags to the queue, then start playing them if the current player is idle.

queue_is_empty() bool#
stop_and_clear_queue() None#
stop_and_clear_queue_if_caller(caller: Any) None#
clear_queue_and_maybe_interrupt() None#
play_file(filename: str) None#

Play the provided path.

SECURITY: Filename may be an arbitrary path. For filenames coming from a collection, you should only ever use the os.path.basename(filename) as the filename.

play_file_with_caller(filename: str, caller: Any) None#

Play the provided path, noting down the caller.

SECURITY: Filename may be an arbitrary path. For filenames coming from a collection, you should only ever use the os.path.basename(filename) as the filename.

insert_file(filename: str) None#

Place the provided path at the top of the playlist.

SECURITY: Filename may be an arbitrary path. For filenames coming from a collection, you should only ever use the os.path.basename(filename) as the filename.

toggle_pause() None#
seek_relative(secs: int) None#
shutdown() None#
aqt.sound.av_player#
aqt.sound.si = None#
aqt.sound.retryWait(proc: subprocess.Popen) int#
class aqt.sound.SimpleProcessPlayer(taskman: aqt.taskman.TaskManager, media_folder: str | None = None)#

Bases: Player

A player that invokes a new process for each tag to play.

args: list[str] = []#
env: dict[str, str] | None = None#
play(tag: anki.sound.AVTag, on_done: OnDoneCallback) None#

Play a file.

When reimplementing, make sure to call gui_hooks.av_player_did_begin_playing(self, tag) on the main thread after playback begins.

stop() None#

Optional.

If implemented, the player must not call on_done() when the audio is stopped.

class aqt.sound.SimpleMpvPlayer(taskman: aqt.taskman.TaskManager, base_folder: str, media_folder: str)#

Bases: SimpleProcessPlayer, VideoPlayer

A player that invokes a new process for each tag to play.

default_rank = 1#
class aqt.sound.SimpleMplayerPlayer(taskman: aqt.taskman.TaskManager, media_folder: str | None = None)#

Bases: SimpleProcessPlayer, SoundOrVideoPlayer

A player that invokes a new process for each tag to play.

class aqt.sound.MpvManager(base_path: str, media_folder: str)#

Bases: aqt.mpv.MPV, SoundOrVideoPlayer

Class for communication with the mpv media player via unix socket based JSON IPC. It adds a few usable methods and a callback API.

To automatically register methods as event callbacks, subclass this class and define specially named methods as follows:

def on_file_loaded(self):

# This is called for every ‘file-loaded’ event. …

def on_property_time_pos(self, position):

# This is called whenever the ‘time-pos’ property is updated. …

Please note that callbacks are executed inside a separate thread. The MPV class itself is completely thread-safe. Requests from different threads to the same MPV instance are synchronized.

default_argv = ['--idle', '--no-terminal', '--force-window=no', '--ontop', '--audio-display=no',...#
media_folder#
executable#
on_init() None#
play(tag: anki.sound.AVTag, on_done: OnDoneCallback) None#

Play a file.

When reimplementing, make sure to call gui_hooks.av_player_did_begin_playing(self, tag) on the main thread after playback begins.

stop() None#

Optional.

If implemented, the player must not call on_done() when the audio is stopped.

toggle_pause() None#

Optional.

seek_relative(secs: int) None#

Jump forward or back by secs. Optional.

on_property_idle_active(value: bool) None#
shutdown() None#

Do any cleanup required at program termination. Optional.

togglePause#
seekRelative#
queueFile(file: str) None#
clearQueue() None#
class aqt.sound.SimpleMplayerSlaveModePlayer(taskman: aqt.taskman.TaskManager, media_folder: str)#

Bases: SimpleMplayerPlayer

A player that invokes a new process for each tag to play.

media_folder#
command(*args: Any) None#

Send a command over the slave interface.

The trailing newline is automatically added.

seek_relative(secs: int) None#

Jump forward or back by secs. Optional.

toggle_pause() None#

Optional.

aqt.sound.encode_mp3(mw: aqt.AnkiQt, src_wav: str, on_done: collections.abc.Callable[[str], None]) None#

Encode the provided wav file to .mp3, and call on_done() with the path.

class aqt.sound.Recorder(output_path: str)#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

STARTUP_DELAY = 0.3#
output_path#
start(on_done: collections.abc.Callable[[], None]) None#

Start recording, then call on_done() when started.

stop(on_done: collections.abc.Callable[[str], None]) None#

Stop recording, then call on_done() when finished.

duration() float#

Seconds since recording started.

on_timer() None#

Will be called periodically.

class aqt.sound.QtAudioInputRecorder(output_path: str, mw: aqt.AnkiQt, parent: QWidget)#

Bases: Recorder

Helper class that provides a standard way to create an ABC using inheritance.

mw#
start(on_done: collections.abc.Callable[[], None]) None#

Start recording, then call on_done() when started.

stop(on_done: collections.abc.Callable[[str], None]) None#

Stop recording, then call on_done() when finished.

class aqt.sound.NativeMacRecorder(output_path: str)#

Bases: Recorder

Helper class that provides a standard way to create an ABC using inheritance.

start(on_done: collections.abc.Callable[[], None]) None#

Start recording, then call on_done() when started.

stop(on_done: collections.abc.Callable[[str], None]) None#

Stop recording, then call on_done() when finished.

class aqt.sound.RecordDialog(parent: QWidget, mw: aqt.AnkiQt, on_success: collections.abc.Callable[[str], None])#

Bases: QDialog

mw#
accept() None#
reject() None#
aqt.sound.record_audio(parent: QWidget, mw: aqt.AnkiQt, encode: bool, on_done: collections.abc.Callable[[str], None]) None#
aqt.sound.clearAudioQueue() None#
aqt.sound.play(filename: str) None#
aqt.sound.playFromText(text: Any) None#
aqt.sound.mpvManager: MpvManager | None = None#
aqt.sound.av_refs_to_play_icons(text: str) str#

Add play icons into the HTML.

When clicked, the icon will call eg pycmd(‘play:q:1’).

aqt.sound.play_clicked_audio(pycmd: str, card: anki.cards.Card) None#

eg. if pycmd is ‘play:q:0’, play the first audio on the question side.

aqt.sound.setup_audio(taskman: aqt.taskman.TaskManager, base_folder: str, media_folder: str) None#
aqt.sound.cleanup_audio() None#