200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 功能实现——文字转语音(TTS)

功能实现——文字转语音(TTS)

时间:2023-12-09 10:36:09

相关推荐

功能实现——文字转语音(TTS)

文字转语音(TTS)介绍

文字转语音(TTS,Text-to-Speech)是一种技术,它将文本转换为自然流畅的人类语音输出。

TTS技术可以使计算机、智能设备和应用程序能够以口头方式与用户进行沟通。

解决方案

● 针对Python语言

① 使用Python库pyttsx3

1. pyttsx3库安装

pip install pyttsx3 -i https://pypi.tuna./simple

2. pyttsx3库使用

import pyttsx3####################################################### Create a Text to Speaker Object and Set its Property######################################################speaker = pyttsx3.init()# 获取当前语音速率rate = speaker.getProperty('rate')print(f'语音速率:{rate}')# 设置新的语音速率speaker.setProperty('rate', 100)# 获取当前语音音量volume = speaker.getProperty('volume')print(f'语音音量:{volume}')# 设置新的语音音量,音量最小为 0,最大为 1speaker.setProperty('volume', 1.0)# 获取当前语音声音的详细信息voices = speaker.getProperty('voices')print(f'语音声音详细信息:{voices}')# 设置当前语音声音为女性,当前声音不能读中文speaker.setProperty('voice', voices[1].id)# 设置当前语音声音为男性,当前声音可以读中文speaker.setProperty('voice', voices[0].id)# 获取当前语音声音voice = speaker.getProperty('voice')print(f'语音声音:{voice}')####################################################### Define a Function which can Convert Text into Speech######################################################def text2speech(text):speaker.say(text)speaker.runAndWait()speaker.stop()####################################################### Take an Example to Show the Text Convert into Speech######################################################text2speech("你好")

● 针对Java语言

① 使用jar包jacob

1. jar包下载

2. jar包使用

import com.jacob.activeX.ActiveXComponent;import .Dispatch;import .Variant;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.Objects;public class Demo {public static void main(String[] args) {// readStr("你好啊!");textToSpeechIO("12e3456789");textToStr("D:/测试.txt");}/*** 字符串文本阅读* @param str 要读的文字字符串*/public static void readStr(String str){ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");//运行时输出语音内容Dispatch spVoice = ax.getObject();//设置音量 0 ~ 100ax.setProperty("Volume",new Variant(100));//设置朗读速度 -10 ~ +10ax.setProperty("Rate",new Variant(0));//执行朗读Dispatch.call(spVoice,"Speak",new Variant(str));}/*** 字符串文本转 wav格式 语音文件* @param text 要读的文字字符串*/public static void textToSpeechIO(String text){ActiveXComponent ax = null;Dispatch spFileStream = null;Dispatch spAudioFormat = null;Dispatch spVoice = null;try{ax = new ActiveXComponent("Sapi.SpFileStream");spFileStream = ax.getObject();ax = new ActiveXComponent("Sapi.SpAudioFormat");spAudioFormat = ax.getObject();spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();// 设置音频流格式Dispatch.put(spAudioFormat, "Type", new Variant(22));// 设置文件输出流格式Dispatch.putRef(spFileStream, "Format", spAudioFormat);// 调用输出 文件流打开方法,创建一个.wav文件Dispatch.call(spFileStream, "Open", new Variant("D:/voice.wav"), new Variant(3), new Variant(true));// 设置声音对象的音频输出流为输出文件对象Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);// 设置音量 0 ~ 100Dispatch.put(spVoice, "Volume", new Variant(100));// 设置朗读速度 -10 ~ +10Dispatch.put(spVoice, "Rate", new Variant(0));Dispatch.call(spVoice, "Speak", new Variant(text));System.out.println("输出语音文件成功!");}catch(Exception e){e.printStackTrace();}finally {// 关闭输出文件Dispatch.call(Objects.requireNonNull(spFileStream), "Close");Dispatch.putRef(Objects.requireNonNull(spVoice), "AudioOutputStream", null);Objects.requireNonNull(spAudioFormat).safeRelease();spFileStream.safeRelease();spVoice.safeRelease();ax.safeRelease();}}/*** txt文件转字符串* @param fileName txt文件所在位置* @return txt文件中的字符串*/public static String textToStr(String fileName){BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(fileName));StringBuilder sb = new StringBuilder();String line;while ((line=reader.readLine()) != null){sb.append(line);}return sb.toString();} catch (IOException e) {e.printStackTrace();return "";}finally {try {Objects.requireNonNull(reader).close();} catch (IOException e) {e.printStackTrace();}}}}

● 针对命令行调用

① 使用余音(Ekho)软件

1. EKho下载

2. EKho安装

针对Linux和Raspberry Pi环境

旧版本的Ekho安装可参考 官方文档 ,新版本的EKho由于依赖的是espeak-ng库而非espeak,因此需要按照以下命令安装依赖(作用等同于官方文档中的第一条命令),其后的命令与官方保持一致即可:

sudo apt-get install espeak-ng libespeak-ng-dev libsndfile1-dev libpulse-dev libncurses5-dev libestools-dev festival-dev libvorbis-dev libmp3lame-dev libdotconf-dev texinfo pulseaudio libpulse-ocaml-dev

3. EKHO使用

• 语音输出:ekho "你好"

• 语音输出并生成对应文件:ekho "你好" -o out.wav

● 针对API调用

Reference

[1] python文字转语音的五种方式win32com,pyttsx3,百度api,可使用自己的声音

[2] Java语言实现文本转语音

[3] linux系统实现TTS(文字转语音)功能

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。