编辑python中的wav文件头以与QSound / pyqt5一起使用(Watson文本到语音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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在pyqt5 Python中的行编辑中添加背景文本

在Python中编辑文本文件中的特定行

使用Microsoft认知WAV文件的Android语音到文本

解码WAV文件头

将WAV录制到IBM Watson语音到文本

如何从Visual Studio编辑器中检索文本以与Roslyn SyntaxTree一起使用?

使用pyinstaller在Python中嵌入.wav文件

PyQt5使用QFileDialog将QlineEdit保存到文本文件

如何使用python编辑Visio文件(vsdx)中的文本?

IBM Watson语音到文本Python,“ DetailedResponse”对象没有属性“ getResult”

为什么在一个环境中而不是在其他环境中进行测试时,TTS(文本到语音)提示会正常播放?

从python3中的.wav文件中删除文件头

与regex一起使用sed从文件中删除文本

使用Python编辑文件中的单词

使用MATLAB读写文件头

将Python Shell与任何文本编辑器一起使用

在python中编辑文件中的文本

在Python中截断文件头

将QML中的QSortFilterProxyModel与PyQt5一起使用

使用python 3.5进行语音文本到语音转换

如何使用python编辑文本文件中的数据?

Watson NarrowBand语音到文本不接受ogg文件

使用python编辑文件中的文本

如何使用 Python 和 PyQt5 将行编辑中的值转换为另一个窗口的行编辑?

使用 Python 的 PYQT5

如何以编程方式或使用编辑器编辑 Parquet 文件头?

如何使用 PyQt5 中垂直布局的 QScrollArea 使小部件堆叠在一起?

如何在 Python 中删除超过 5 分钟的 wav 文件

PyQt5:将 loadUi 与 Qt Designer 中的提升小部件一起使用