In PyQ5t, how to extend QMediaPlaylist whilst it's playing

52 Views Asked by At

Context

I'm building a text-to-speech (TTS) GUI with an offline real-time speech synthesizer (like espeak). Because the speech synthesizer is quite slow, I'd like to chunk up the text, get sequentially wav for each, and play them in sequence as soon as the first one is completed. The assumption is that TTS won't be any faster.

Question

Does the QPlaylist allow for extending playlist, i.e. adding new media at the end, while the playlist is being played and without stopping it?

What I'm hoping to achieve is to have a thread that generates wav files and appends each file to a playlist, and the playlist is started when a list is not empty, and stops when all files are played.

I could do this with thread that monitors the status of the QMediaPlayer and as soon as it's finished play the next file in a list (populated by other threads). But, that seems like reimplementing the playlist.

Code

The code I'm working on and would like to modify is here, or a short cleaned snipped is like

from PyQt5.QtMultimedia import QMediaPlayer, QMediaPlaylist

def read_text(text: str, player: QMediaPlayer) -> None:
    filepaths = []
    for parted_text in text.split('. '):
        filename = ask(parted_text)
        filepaths.append(filename)
    play_files(filepaths, player)
    return

def play_files(filepaths, player):
    playlist = QMediaPlaylist(player)
    for filepath in filepaths:
        url = QUrl.fromLocalFile(filepath)
        playlist.addMedia(QMediaContent(url))
    player.setPlaylist(playlist)
    player.play()

def ask(text: str):
    """Connect to local server on host localhost:port and extract wav from stream"""
    response = requests.get(URL, params={"text": text})
    if response.status_code != 200:
        raise Exception(f"Error: Unexpected response {response}")
    return response.json()["filename"]

0

There are 0 best solutions below