簡體   English   中英

從 python(Watson Text To Speech TTS)中編輯 wav 文件頭以用於 QSound/pyqt5

[英]Edit wav file headers for use with QSound/pyqt5 from within python (Watson Text To Speech TTS)

來自 pyqt5 的 QSound 一直給我帶來麻煩,一些 wav 文件運行良好。 其他導致 Qt 應用程序出錯且無法運行。 我通過研究將罪魁禍首縮小到 wav 文件的標題。

如果我在 Audacity 中打開 wav 文件並將其導出為 wav 文件......導出的 wav 文件可以完美運行。 但是我需要一個從我的 python 腳本中運行的解決方案。

我從 Watson 的 Text-To-Speech api 獲取我的 wav 文件,不確定我是否可以控制它包含的標題。

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_()

一種可能的解決方案是不使用 QSound,而是使用 QMediaPlayer 來處理其他編解碼器:

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)

# ...

注意:另一種選擇是使用另一種格式,如 mp3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM