Java语言实现文本转语音

Java语言实现文本转语音

1. 首先需要导入jacob.jar的jar包

jar包下载地址
下载
目录
注:需要将两个.dll文件放到java的JDK的bin目录下

2.编写测试代码

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.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 ~ 100
        ax.setProperty("Volume",new Variant(100));
        //设置朗读速度 -10 ~ +10
        ax.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 ~ 100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            // 设置朗读速度  -10 ~ +10
            Dispatch.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();
            }
        }
    }
}
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
要在Java中免费实现语音文字,你可以使用开源库,比如CMU Sphinx或Pocketsphinx。以下是一基本步骤: 1. 首先,下载并安装Sphinxbase和Pockets两个开源库,可以从官方网站上进行下载。 2. 在Java项目中添加Sphinx4和Pocketsphinx的依赖。 3. 将音频文件换为PCM格式,可以使用开源库WAVFile。 4. 设置语音识别的配置参数和语言模型文件路径。 5. 使用Java代码连接Sphinx4和Pocketsphinx,将音频文件发送到Sphinx,获得文本录结果。 以下是一个简单的Java代码示例,可以将Pocketsphinx用于语音文字: ```java import edu.cmu.sphinx.api.Configuration; import edu.cmu.sphinx.api.LiveSpeechRecognizer; import java.io.File; import java.io.FileInputStream; public class SpeechToTextExample { public static void main(String... args) throws Exception { String acousticModelPath = "path/to/acoustic/model"; String dictionaryPath = "path/to/dictionary/file"; String languageModelPath = "path/to/language/model"; String audioFilePath = "path/to/audio/file.wav"; // Set up the configuration Configuration configuration = new Configuration(); configuration.setAcousticModelPath(acousticModelPath); configuration.setDictionaryPath(dictionaryPath); configuration.setLanguageModelPath(languageModelPath); // Set up the live recognizer LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration); // Start recognition recognizer.startRecognition(new FileInputStream(new File(audioFilePath))); // Print the transcription result while (true) { System.out.println(recognizer.getResult().getHypothesis()); } // Stop recognition recognizer.stopRecognition(); } } ``` 这个示例使用了Sphinx4开源库中的类和方法,将音频文件换为PCM格式,并将其发送到Pocketsphinx引擎。然后,它不断地从引擎中获取录结果,并将其打印到控制台上。 需要注意的是,Sphinx4和Pocketsphinx的语音识别精度可能不如商用的语音识别API,但是它们是免费的开源工具,可以用于一些简单的语音文字应用。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小辰~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值