目錄
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. 運行效果
?