java 实现文字转语音 -仅限windows系统

1. 下载资源jacob

将.dll文件添加到jre下的bin文件夹下
64位操作系统选择jacob-1.18-x64.dll文件
32位操作系统选择jacob-1.18-x86.dll
链接:https://pan.baidu.com/s/1bppqj07 密码:hdcv

MSTTSSpeech.java

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
  * 调用MSTTS将字符串转换成语音信息
  * 调用windows speech API(SAPI)
  * @author suyunlong
  *
  */
public class MSTTSSpeech
{
    private int volume=100;// 声音:1到100
    private int rate=0;// 频率:-10到10
    private int voice=0;// 语音库序号
    private int audio=0;// 输出设备序号
    private ActiveXComponent ax=null;
    private Dispatch spVoice=null;// 声音对象
    private Dispatch spFileStream=null;// 音频文件输出流对象,在读取或保存音频文件时使用
    private Dispatch spAudioFormat=null;// 音频格式对象
    private Dispatch spMMAudioOut=null;// 音频输出对象
    private int formatType=22;// 音频的输出格式,默认为:SAFT22kHz16BitMono
    public MSTTSSpeech() 
    {
        ComThread.InitSTA();
        if(ax==null)
        {
            ax=new ActiveXComponent("Sapi.SpVoice");
            spVoice=ax.getObject();
        }
    }
    /**
     * 改变语音库
     * @param voice 语音库序号
     */
    public void changeVoice(int voice)
    {
        if(this.voice != voice)
        {
            this.voice=voice;
        }
        try
        {
            Dispatch voiceItems=Dispatch.call(spVoice,"GetVoices").toDispatch();
            int count=Integer.valueOf(Dispatch.call(voiceItems,"Count").toString());
            if(count>0)
            {
                Dispatch voiceItem=Dispatch.call(voiceItems,"Item",new Variant(this.voice)).toDispatch();
                Dispatch.put(spVoice,"Voice",voiceItem);
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 改变音频输出设备
     * @param audio 音频设备序号
     */
    public void changeAudioOutput(int audio)
    {
        if(this.audio != audio)
        {
            this.audio=audio;
        }
        try
        {
            Dispatch audioOutputs=Dispatch.call(spVoice,"GetAudioOutputs").toDispatch();
            int count=Integer.valueOf(Dispatch.call(audioOutputs,"Count").toString());
            if(count > 0)
            {
                Dispatch audioOutput=Dispatch.call(audioOutputs,"Item",new Variant(this.audio)).toDispatch();
                Dispatch.put(spVoice,"AudioOutput",audioOutput);
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 播放语音 
     * @param text 要转换成语音的文本
     */
    public void speak(String text)
    {
        this.speak(text,0);
    }
    /**
     * 停止播放语音
     */
    public void stop()
    {
        // this.speak("", 1);
        Dispatch.call(spVoice,"Pause");
    }
    /**
     * 播放语音
     * @param text 要转换成语音的文本
     * @param type 类型0:播放,1:停止
     */
    private void speak(String text,int type)
    {
        switch(type) 
        {
            case 0:
                try
                {
                    // 调整音量和读的速度
                    Dispatch.put(spVoice,"Volume",new Variant(this.volume));// 设置音量
                    Dispatch.put(spVoice,"Rate",new Variant(this.rate));// 设置速率
                    // 设置音频格式类型
                    if(spAudioFormat==null)
                    {
                        ax=new ActiveXComponent("Sapi.SpAudioFormat");
                        spAudioFormat=ax.getObject();
                        ax=new ActiveXComponent("Sapi.SpMMAudioOut");
                        spMMAudioOut=ax.getObject();
                    }
                    Dispatch.put(spAudioFormat,"Type",new Variant(this.formatType));
                    Dispatch.putRef(spMMAudioOut,"Format",spAudioFormat);
                    Dispatch.put(spVoice,"AllowAudioOutputFormatChangesOnNextSet",new Variant(false));
                    Dispatch.putRef(spVoice,"AudioOutputStream",spMMAudioOut);
                    // 开始朗读
                    Dispatch.call(spVoice,"Speak",new Variant(text));
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
                break;
            case 1:
                try
                {
                    Dispatch.call(spVoice,"Speak",new Variant(text),new Variant(2));
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }
    /**
     * 获取系统中所有的语音库名称数组 
     * @return String[]
     */
    public String[] getVoices()
    {
        String[] voices=null;
        try
        {
            Dispatch voiceItems=Dispatch.call(spVoice,"GetVoices").toDispatch();
            int count=Integer.valueOf(Dispatch.call(voiceItems,"Count").toString());
            if(count > 0)
            {
                voices=new String[count];
                for(int i=0;i 0)
            {
                result=new String[count];
                for(int i=0;i
     * SAFTDefault = -1
* SAFTNoAssignedFormat = 0
* SAFTText = 1
* SAFTNonStandardFormat = 2
* SAFTExtendedAudioFormat = 3
* // Standard PCM wave formats
* SAFT8kHz8BitMono = 4
* SAFT8kHz8BitStereo = 5
* SAFT8kHz16BitMono = 6
* SAFT8kHz16BitStereo = 7
* SAFT11kHz8BitMono = 8
* SAFT11kHz8BitStereo = 9
* SAFT11kHz16BitMono = 10
* SAFT11kHz16BitStereo = 11
* SAFT12kHz8BitMono = 12
* SAFT12kHz8BitStereo = 13
* SAFT12kHz16BitMono = 14
* SAFT12kHz16BitStereo = 15
* SAFT16kHz8BitMono = 16
* SAFT16kHz8BitStereo = 17
* SAFT16kHz16BitMono = 18
* SAFT16kHz16BitStereo = 19
* SAFT22kHz8BitMono = 20
* SAFT22kHz8BitStereo = 21
* SAFT22kHz16BitMono = 22
* SAFT22kHz16BitStereo = 23
* SAFT24kHz8BitMono = 24
* SAFT24kHz8BitStereo = 25
* SAFT24kHz16BitMono = 26
* SAFT24kHz16BitStereo = 27
* SAFT32kHz8BitMono = 28
* SAFT32kHz8BitStereo = 29
* SAFT32kHz16BitMono = 30
* SAFT32kHz16BitStereo = 31
* SAFT44kHz8BitMono = 32
* SAFT44kHz8BitStereo = 33
* SAFT44kHz16BitMono = 34
* SAFT44kHz16BitStereo = 35
* SAFT48kHz8BitMono = 36
* SAFT48kHz8BitStereo = 37
* SAFT48kHz16BitMono = 38
* SAFT48kHz16BitStereo = 39
*
* // TrueSpeech format
* SAFTTrueSpeech_8kHz1BitMono = 40
* // A-Law formats
* SAFTCCITT_ALaw_8kHzMono = 41
* SAFTCCITT_ALaw_8kHzStereo = 42
* SAFTCCITT_ALaw_11kHzMono = 43
* SAFTCCITT_ALaw_11kHzStereo = 4
* SAFTCCITT_ALaw_22kHzMono = 44
* SAFTCCITT_ALaw_22kHzStereo = 45
* SAFTCCITT_ALaw_44kHzMono = 46
* SAFTCCITT_ALaw_44kHzStereo = 47
*
* // u-Law formats
* SAFTCCITT_uLaw_8kHzMono = 48
* SAFTCCITT_uLaw_8kHzStereo = 49
* SAFTCCITT_uLaw_11kHzMono = 50
* SAFTCCITT_uLaw_11kHzStereo = 51
* SAFTCCITT_uLaw_22kHzMono = 52
* SAFTCCITT_uLaw_22kHzStereo = 53
* SAFTCCITT_uLaw_44kHzMono = 54
* SAFTCCITT_uLaw_44kHzStereo = 55
* SAFTADPCM_8kHzMono = 56
* SAFTADPCM_8kHzStereo = 57
* SAFTADPCM_11kHzMono = 58
* SAFTADPCM_11kHzStereo = 59
* SAFTADPCM_22kHzMono = 60
* SAFTADPCM_22kHzStereo = 61
* SAFTADPCM_44kHzMono = 62
* SAFTADPCM_44kHzStereo = 63
*
* // GSM 6.10 formats
* SAFTGSM610_8kHzMono = 64
* SAFTGSM610_11kHzMono = 65
* SAFTGSM610_22kHzMono = 66
* SAFTGSM610_44kHzMono = 67
* // Other formats
* SAFTNUM_FORMATS = 68
* * @param formatType * 音频输出格式类型 */ public void setFormatType(int formatType) { this.formatType=formatType; } public static void main(String[] args) { MSTTSSpeech speech=new MSTTSSpeech(); String text="这是我的测试,物理内存至少需要512MB,建议2GB以上,虚拟内存是主机物理内存的两倍,不要设到系统盘,硬盘空闲空间大于4.77GB."; speech.setFormatType(6); // speech.setRate(-1); speech.saveToWav(text,"D:/test.wav"); //speech.speak(text); SoundUtil.wavToPcm("D:/test.wav"); } }

SoundUtil.java


import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * 与声音有关的工具类
 * @author suyunlong
 *
 */
public class SoundUtil
{
    /**
     * 将WAV格式的音频文件转换为PCM格式的文件
     * @param wavFilePath WAV文件路径
     * @throws UnsupportedAudioFileException
     * @throws IOException
     */
    public static void wavToPcm(String wavFilePath)
    {
        try
        {
            File wavFile=new File(wavFilePath);
            AudioInputStream sourceAudioInputStream=AudioSystem.getAudioInputStream(wavFile);
            // 此处的转换必需是16bit的音频文件
            AudioInputStream targetAudioInputStream=AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW,
                    sourceAudioInputStream);
            String newFilePath=wavFilePath.substring(0,wavFilePath.lastIndexOf(".")+1)+"pcm";
            AudioSystem.write(targetAudioInputStream,AudioFileFormat.Type.WAVE,new File(newFilePath));
        }
        catch(UnsupportedAudioFileException e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 获取音频文件的编码格式 
     * @param wavFilePath 音频文件格式
     * @return String
     */
    public static String getWavFormat(String wavFilePath)
    {
        File wavFile=new File(wavFilePath);
        AudioInputStream ais;
        String result="";
        try
        {
            ais=AudioSystem.getAudioInputStream(wavFile);
            AudioFormat af=ais.getFormat();
            result=af.toString();
        }
        catch(UnsupportedAudioFileException e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args)
    {
        System.out.println(SoundUtil.getWavFormat("D:/test.wav"));
        SoundUtil.wavToPcm("D:/test.wav");
    }
}

你可能感兴趣的:(java 实现文字转语音 -仅限windows系统)