Java语音转文字及文字转语音教学 (离线版)

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-100
            ax.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            ax.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到100
            Dispatch.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();
        }
    }

}

到此就完成了 语音转文字 文字转语音的功能了,希望能帮到你。有疑问评论

  • 29
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值