语音生成 本地离线语音生成

前言:

最近觉得电脑主动发声提醒提示很多信息比我自己看要好很多,我希望当有信息弹出时,电脑不再是滴滴一声,而是提示我信息来源内容语音发声等等
因此本地离线文字转语音是必要的

实现原理:

微软本地就有tts语音生成,调用即可
输入文本,设置tts生成人,语速,音量,生成语音文件
生成后播放,然后删除音频文件

实现

import datetime
import os
import pygame
import edge_tts
import asyncio
import glob
import sounddevice as sd
TEXT = "你好,主人,序列7号已经准备完毕,准备接受指令,none,none,sevent,complete!"
baseMusicPath=r"D:\PGMusic\tempMusic"
def getNowTime():
    # 获取当前时间
    time_format = "%Y%m%d%H%M%S%f"
    current_time = datetime.datetime.now()
    # 格式化时间字符串,精确到毫秒位,分隔符为 "-"
    formatted_time = current_time.strftime(time_format)[:-3]
    # 将时间字符串转换为 datetime 对象
    time_obj = datetime.datetime.strptime(formatted_time, time_format)
    # 将 datetime 对象转换为时间戳(以秒为单位)
    timestamp = "{:.3f}".format(time_obj.timestamp())
    print(formatted_time,timestamp)
    return formatted_time,timestamp
async def my_function(TEXT):
    # Name: zh - CN - XiaoxiaoNeural
    # Gender: Female
    # ​
    # Name: zh - CN - XiaoyiNeural
    # Gender: Female
    # ​
    # Name: zh - CN - YunjianNeural
    # Gender: Male
    # ​
    # Name: zh - CN - YunxiNeural
    # Gender: Male
    # ​
    # Name: zh - CN - YunxiaNeural
    # Gender: Male
    # ​
    # Name: zh - CN - YunyangNeural
    # Gender: Male
    # ​
    # Name: zh - CN - liaoning - XiaobeiNeural
    # Gender: Female
    # ​
    # Name: zh - CN - shaanxi - XiaoniNeural
    # Gender: Female
    voice = 'zh-CN-YunxiNeural'
    fileName=getNowTime()[0]
    output = r'{}\{}.mp3'.format(baseMusicPath,fileName)
    rate = '-4%'
    volume = '+30%'
    tts = edge_tts.Communicate(text=TEXT, voice=voice, rate=rate, volume=volume)
    await tts.save(output)
    return output,'{}.mp3'.format(fileName)
def delete_files_except(directory, name):
    paths=glob.glob(os.path.join(directory, '*'))
    print(paths)
    print(name)
    for file in paths:
        if os.path.isfile(file) and os.path.basename(file) != name:
            # print(os.path.basename(file))
            os.remove(file)
outputPath,fileName=asyncio.run(my_function(TEXT))
print(fileName)
print(outputPath)
def play_audio(file_path):
    pygame.mixer.init()
    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.wait(1000)
play_audio(outputPath)
delete_files_except(baseMusicPath,fileName)

你可能感兴趣的:(语音识别,人工智能)