1. 语音转文字
1.1 maven导入以下包
<!-- 获取音频信息 -->
<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>
1.2 编写代码
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;import org.vosk.LogLevel;
import org.vosk.Recognizer;
import org.vosk.LibVosk;
import org.vosk.Model;public class DecoderDemo {public static void main(String[] argv) throws IOException, UnsupportedAudioFileException {LibVosk.setLogLevel(LogLevel.DEBUG);try (Model model = new Model("D:\\model\\vosk-model-small-cn-0.22");InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream("D:\\File\\badao.wav")));Recognizer recognizer = new Recognizer(model, 16000)) {int bytes;byte[] b = new byte[4096];while ((bytes = ais.read(b)) >= 0) {recognizer.acceptWaveForm(b, bytes);}System.out.println(recognizer.getFinalResult() + System.lineSeparator());}}}
注意new Model("模型路径")是你下载的模型解压后的地址
InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream("音频路径"))); 这个是你要识别的音频地址,我用的音频格式是 wav 其他格式还没试。
没有模型可以去网址: https://alphacephei.com/vosk/models 下载
选择 Chinese 下载这两个模型 (建议都下载 small模型识别快一点)
下载后记得解压在使用
2. 文字转语音
2.1 Maven导入以下包
<!-- 文字转语音 -->
<dependency><groupId>com.hynnet</groupId><artifactId>jacob</artifactId><version>1.18</version>
</dependency>
2.2 编写代码
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class TxtToSoundUtils {public static void main(String[] args) {textToSpeech("打开卧室灯泡");System.out.println("生成成功!");}/*** 语音转文字并播放** @param text*/public static void textToSpeech(String text) {ActiveXComponent ax;try {ax = new ActiveXComponent("Sapi.SpVoice");// 运行时输出语音内容Dispatch spVoice = ax.getObject();// 音量 0-100ax.setProperty("Volume", new Variant(100));// 语音朗读速度 -10 到 +10ax.setProperty("Rate", new Variant(-2));// 执行朗读Dispatch.call(spVoice, "Speak", new Variant(text));// 下面是构建文件流把生成语音文件ax = new ActiveXComponent("Sapi.SpFileStream");Dispatch spFileStream = ax.getObject();ax = new ActiveXComponent("Sapi.SpAudioFormat");Dispatch spAudioFormat = ax.getObject();// 设置音频流格式Dispatch.put(spAudioFormat, "Type", new Variant(22));// 设置文件输出流格式Dispatch.putRef(spFileStream, "Format", spAudioFormat);// 调用输出 文件流打开方法,创建一个.wav文件Dispatch.call(spFileStream, "Open", new Variant("D:\\File\\TestFile.wav"), new Variant(3), new Variant(true));// 设置声音对象的音频输出流为输出文件对象Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);// 设置音量 0到100Dispatch.put(spVoice, "Volume", new Variant(100));// 设置朗读速度Dispatch.put(spVoice, "Rate", new Variant(-2));// 开始朗读Dispatch.call(spVoice, "Speak", new Variant(text));// 关闭输出文件Dispatch.call(spFileStream, "Close");Dispatch.putRef(spVoice, "AudioOutputStream", null);spAudioFormat.safeRelease();spFileStream.safeRelease();spVoice.safeRelease();ax.safeRelease();} catch (Exception e) {e.printStackTrace();}}}
到此就完成了 语音转文字 文字转语音的功能了,希望能帮到你。有疑问评论
如Linux部署有问题可以参考这篇文章,希望对你有帮助
在Linux服务器上 运行vosk报错Could not initialize class org.vosk.LibVosk_服务器-CSDN问答