目录
1. 下载依赖ddl
2. 引入Pom依赖
3. java代码
二. 语音转文本
1. 下载中文语音转文本的模型
2. 引入pom依赖
3. java代码
4. 运行效果
1. 下载依赖ddl
文字转语音文件需要使用jacob的dll文件放在jdk安装目录下的bin文件夹下 点击官网下载录或者通过csdn下载
2. 引入Pom依赖
<dependency><groupId>com.hynnet</groupId><artifactId>jacob</artifactId><version>1.18</version></dependency>
3. java代码
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;/*** 文件转语音的工具类*/
public class JacobUtil {public static void main(String[] args) {String text = "很喜欢朗诵。上大学时,他是校广播站的播音员。母亲说你可以去市广播电台试试。他说可以吗?母亲说为什么不可以," +"只要心是明亮的,天空就是明亮的,你的世界,就是明亮的。再听到这句话时,感觉完全不一样了。" +"虽然他仍然消沉,可是偶尔当母亲说到什么有趣的事时,他也会开心得哈哈大笑。他听从母亲的建议," +"真的在某一天,去市电台应聘。本来他只想应付一下母亲,可出乎意料的是,他竟被破格录取为电台的兼职主持人," +"主持晚间的一档节目。";String filePath = "C:\\Users\\admin\\Downloads\\call.mp3";textToSpeech(text, filePath);}/*** 将文本转化为语音** @param text 文本内容* @param filePath 语音文件*/public static void textToSpeech(String text, String filePath) {ActiveXComponent activeXComponent = new ActiveXComponent("Sapi.SpVoice");Dispatch dispatch = activeXComponent.getObject();//运行时输出语音内容//文件名称activeXComponent = new ActiveXComponent("Sapi.SpFileStream");//生成语音文件Dispatch fileStreamDispatch = activeXComponent.getObject();activeXComponent = new ActiveXComponent("Sapi.SpAudioFormat");//音频Dispatch audioDispatch = activeXComponent.getObject();Dispatch.putRef(fileStreamDispatch, "Format", audioDispatch);//设置文件流格式Dispatch.put(audioDispatch, "Type", new Variant(22));//设置音频流格式//调用输出文件流打开方法,创建一个.wav .mp3 .mp4 .wma文件Dispatch.call(fileStreamDispatch, "Open", new Variant(filePath), new Variant(3), new Variant(true));Dispatch.putRef(dispatch, "AudioOutputStream", fileStreamDispatch);//设置声音对象的音频流输出流为输出文件对象Dispatch.put(dispatch, "Volume", new Variant(100));//设置音量0-100Dispatch.put(dispatch, "Rate", new Variant(0));//设置朗读速度Dispatch.call(dispatch, "Speak", new Variant(text));//开始朗读Dispatch.call(fileStreamDispatch, "Close"); //关闭输出文件流Dispatch.putRef(dispatch, "AudioOutputStream", null);audioDispatch.safeRelease();fileStreamDispatch.safeRelease();dispatch.safeRelease();activeXComponent.safeRelease();}
}
二. 语音转文本
1. 下载中文语音转文本的模型
点击下载 模型1(小) 模型2(大) 这两个模型都是中文, 如果有其他语种的识别,可以去官网下载对应语种的模型 注意 需要解压
2. 引入pom依赖
<!-- 获取音频信息 --><dependency><groupId>org</groupId><artifactId>jaudiotagger</artifactId><version>2.0.3</version></dependency><!-- 语音识别 --><dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.7.0</version></dependency><dependency><groupId>com.alphacephei</groupId><artifactId>vosk</artifactId><version>0.3.32</version></dependency>
3. java代码
package org.example.deepseek;import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioSystem;
import org.vosk.LogLevel;
import org.vosk.Recognizer;
import org.vosk.LibVosk;
import org.vosk.Model;public class VoiceUtil {public static void main(String[] argv) throws Exception {String filePath = "D:\\ai\\voice\\call.mp3";String modelPath = "D:\\ai\\voice\\vosk-model-cn-0.22";//注意模型需要解压后引入模型的根目录String content = speechToText(filePath, modelPath);System.out.println(content);}public static String speechToText(String filePath, String modelPath) throws Exception {LibVosk.setLogLevel(LogLevel.INFO);Model model = new Model(modelPath);FileInputStream fileInputStream = null;BufferedInputStream bufferedInputStream = null;InputStream inputStream = null;Recognizer recognizer = null;try {fileInputStream = new FileInputStream(filePath);bufferedInputStream = new BufferedInputStream(fileInputStream);inputStream = AudioSystem.getAudioInputStream(bufferedInputStream);recognizer = new Recognizer(model, 16000);int bytes;byte[] b = new byte[4096];while ((bytes = inputStream.read(b)) >= 0) {recognizer.acceptWaveForm(b, bytes);}return recognizer.getFinalResult();//System.lineSeparator()} finally {close(recognizer);close(inputStream);close(bufferedInputStream);close(fileInputStream);}}public static void close(AutoCloseable obj) {if (obj != null) {try {obj.close();} catch (Exception e) {e.printStackTrace();}}}
}
4. 运行效果