1

QSound from pyqt5 has been giving me trouble, some wav files work well. Others cause the Qt app to error and not run. I have with research narrowed the culprit down to the headers of the wav files.

If I open the wav file in Audacity and export it as a wav file... the exported wav file works perfectly. However I need a solution that runs from within my python script.

I am getting my wav files from Watson's Text-To-Speech api, not sure if I can control what headers it includes.

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtMultimedia import QSound

from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator


def list_to_speech(text, language='ja-JP_EmiV3Voice'):
    api_key = "my_api_key"
    url = "url"

    # Set up service
    authenticator = IAMAuthenticator(api_key)
    # Now TTS service
    tts = TextToSpeechV1(authenticator=authenticator)
    # Set Service URL
    tts.set_service_url(url)
    with open('text_to_speech.wav', 'wb') as audio_file:
        res = tts.synthesize(text, accept='audio/wav', voice=language).get_result()
        audio_file.write(res.content)


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.sound = QSound("text_to_speech.wav")
        self.sound.play()

        label = QLabel("This PyQt5 window will (try to) play the wav file!")
        label.setAlignment(Qt.AlignCenter)

        self.setCentralWidget(label)


if __name__ == "__main__":
    # the file saved by list_to_speech won't play as QSound(text_to_speech.wav).play()
    # (instead it crashes the app before opening)
    # 
    # if I open the text_to_speech.wav file in Audacity and export it with empty headers,
    # then comment out next line, it works.
    list_to_speech("ありがとう")
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

1 Answers1

1

A possible solution is not to use QSound but rather QMediaPlayer that allows handling other codecs:

import os
import sys

from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

# ...

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        filename = os.path.join(CURRENT_DIR, "text_to_speech.wav")

        self.player = QMediaPlayer()
        url = QUrl.fromLocalFile(filename)
        self.player.setMedia(QMediaContent(url))
        self.player.play()

        label = QLabel("This PyQt5 window will (try to) play the wav file!")
        label.setAlignment(Qt.AlignCenter)

        self.setCentralWidget(label)

# ...

Note: Another option is to use another format like mp3.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Non-working solution. - The wav file downloads - The Qt application will now open (that's new.) However, the when self.player.play() runs, no sound comes through my speakers. – John DeBaggis Dec 04 '20 at 22:32
  • @JohnDeBaggis What is your OS? Qt Multimedia uses third party libraries for codecs, do you have codecs installed?I tested your application with the url and apikey you provided and it didn't work with QSound but with QMediaPlayer (now it can no longer test since you deactivated the api key) – eyllanesc Dec 04 '20 at 22:35
  • I am on a windows10 system, I likely don't have many extra codecs installed. – John DeBaggis Dec 04 '20 at 22:38
  • @JohnDeBaggis I just tested it and it works on Linux. Try installing the k-lite codecs: https://codecguide.com/download_kl.htm – eyllanesc Dec 04 '20 at 22:40
  • @JohnDeBaggis also change to `with open(os.path.join(CURRENT_DIR, 'text_to_speech.wav'), 'wb') as audio_file:` – eyllanesc Dec 04 '20 at 22:41
  • 1
    @JohnDeBaggis Have you tried using another format like mp3? – eyllanesc Dec 04 '20 at 22:50
  • Reset computer after installing codecs, fixed with open line. No luck... I haven't tried other file formats per say... I'll try mp3 now. – John DeBaggis Dec 04 '20 at 22:52
  • mp3 worked! this is a suitable solution for me. I wanted mp3 anyways :-) – John DeBaggis Dec 04 '20 at 22:53