文本转语音

对于 Audio Write test step(音频写入测试步骤) 和 interactive test execution(交互式测试执行),可以配置文本转语音服务。每个服务都可以定义多个不同的语音。 默认情况下,ecu.test 通过 Windows 的 SAPI 支持语音输出。 还可以使用 UserPyModules 连接或实现其他文本转语音服务。

用户自定义语音输出

语音输出使用文本转语音服务,根据给定文本和参数生成音频文件。 随后该文件会回放到音频输出设备。 Python 脚本必须满足以下要求,才能作为有效的语音输出服务使用。

  • 脚本必须位于 Workspace(工作区)的 UserPyModules 文件夹中。

  • 必须在脚本顶层指定变量 MODULE_TYPE = ‘TextToSpeechService’。

  • 文件中必须包含类 “TextToSpeechService”。

  • 该类必须实现 __init__、GetVoices、SpeakToFile、GetCapabilities、GetFileExtension 和 GetServiceId 方法。

  • 不得存在两个服务使用相同的 service ID。

  • 服务中的语音可通过名称、语言和性别唯一标识。

新增服务在刷新用户库后即可使用。可在 ecu.test 的 “Extras” 菜单中执行此操作。

最小 TextToSpeechService 示例

这是一个用户自定义语音输出示例。

import numpy as np
import wave

MODULE_TYPE = 'TextToSpeechService'


class TextToSpeechService:
    """
    Test TextToSpeechService that only implements a parameterized sine curve instead of a
    speech output.
    """

    def __init__(self):
        pass

    def GetVoices(self) -> list[tuple[str, str, str]]:
        """
        Returns list of available voices as triple (name, language, gender)
        """
        return [
            ('Harald', 'de', 'MALE'),
            ('Harold', 'en', 'MALE'),
            ('Brigitte', 'de', 'FEMALE'),
            ('Robot', 'en', 'OTHER'),
        ]

    def _GetVoiceFreq(self, voice: str) -> float:
        match voice:
            case 'Harald':
                return 440.
            case 'Harold':
                return 600.
            case 'Brigitte':
                return 1000.
            case 'Robot':
                return 110.

        return 10

    def SpeakToFile(self,
                    text: str,
                    voice: tuple[str, str, str],
                    filePath: str,
                    parameters: dict) -> None:
        """
        This method generates a speech output for the specified text and parameters
        (speed and pitch) with the specified voice (name, language, gender) under the specified
        file path. That file will be used for audio output.
        """
        volume = 0.5
        sampleRate = 48000
        duration = (len(text) / 10.) * parameters['speed']
        f = self._GetVoiceFreq(voice[0]) * parameters['pitch']

        # generate samples
        samples = (np.sin(2 * np.pi * np.arange(sampleRate * duration) * f / sampleRate)).astype(
            np.float32)

        output_bytes = (volume * samples).tobytes()

        with wave.open(filePath, 'wb') as wavefile:
            wavefile.setnchannels(1)
            wavefile.setsampwidth(4)
            wavefile.setframerate(sampleRate)
            wavefile.writeframes(output_bytes)

    @staticmethod
    def GetCapabilities() -> list[str]:
        """
        Marks if UserTextToSpeechService module supports pitch and speed adaptation.
        """
        return ['pitch', 'speed']

    @staticmethod
    def GetFileExtension() -> str:
        """
        Extension of generated speech audio files.
        """
        return 'wav'

    @staticmethod
    def GetServiceId() -> str:
        """
        Service name as to be displayed in the voice selection.
        """
        return "FakeTTS"

可用方法说明

类 “TextToSpeechService”

Python 脚本必须实现该类。

__init__

该方法必须实现,且不带参数,但可以为空。

GetVoices

该方法返回文本转语音服务可用的语音列表。每个语音以三元组 (name, RFC 5646 language code, gender) 表示。

SpeakToFile

该方法生成语音输出文件:

  • 根据请求的 text 生成语音输出。

  • 使用模块中选定的 voice,其格式为三元组 (name, language, gender)。

  • 音频输出必须保存为定义在 filePath 中的文件。

  • parameter 字典可包含 ‘speed’ 和 ‘pitch’ 信息,生成音频输出时可加以考虑。 一旦文本转语音服务通过 GetCapabilities() 方法声明支持这些能力,就可以在 ecu.test 中调整语速和音高。

GetCapabilities

该方法返回该文本转语音模块是否支持对音高和语速进行参数化设定。

GetFileExtension

返回所创建音频文件的扩展名。之所以需要该信息,是因为每次运行的语音输出临时文件路径由 ecu.test 定义。

GetServiceId

文本转语音服务的唯一标识符。