【阿里云】文本转语音方言— 阿里云语音合成,文本转语音方言工具类

目录

一、导入SDK坐标

二、文本转语音方言 —步骤和工具类

1.登录阿里云账号,创建一个语音合成项目和创建用户并设置权限。

2.获取appKey、accessKeyId、accessKeySecret在相应位置进行填写。

3.相应位置填写自己需要保存的本地路径。意思就是说,自己定一个转化的语音存放在本地的一个位置。

4.测试,调用工具类中的方法uploadSoundOSS(String text,String voiceStr) ,传入一个自己需要转化的文本信息,和所在的省份名称进行测试就可以了。

三、创建JavaBean — 与文本转语音的不同


一、导入SDK坐标

<dependency>
    <groupId>com.alibaba.nls</groupId>
    <artifactId>nls-sdk-tts</artifactId>
    <version>2.2.1</version>
</dependency>

二、文本转语音方言 —步骤和工具类

1.登录阿里云账号,创建一个语音合成项目和创建用户并设置权限

2.获取appKey、accessKeyId、accessKeySecret在相应位置进行填写。

3.相应位置填写自己需要保存的本地路径。意思就是说,自己定一个转化的语音存放在本地的一个位置。

4.测试,调用工具类中的方法uploadSoundOSS(String text,String voiceStr) ,传入一个自己需要转化的文本信息,和所在的省份名称进行测试就可以了。

//文字转语音方言
public class SoundOSS_Dialect {
    private static String appKey = "自己的appKey";
    private static String accessKeyId = "自己的accessKeyId";
    private static String accessKeySecret = "自己的accessKeySecret";
    static NlsClient client ;

    public static String uploadSoundOSS(String text,String voiceStr) {
        String url = UUID.randomUUID().toString().replace("-", "");
        SpeechSynthesizerDemo("");
        //自己选一个本地路径填写
        File f =new File("需要保存在本地的路径"+url+".wav");
        process(text,f,voiceStr);
        client.shutdown();
        return f.getAbsolutePath();
    }

    public static void  SpeechSynthesizerDemo(String url) {
        AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
        try {
            accessToken.apply();
            if(url.isEmpty()) {
                client = new NlsClient(accessToken.getToken());
            }else {
                client = new NlsClient(url, accessToken.getToken());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static SpeechSynthesizerListener getSynthesizerListener(File f) {
        SpeechSynthesizerListener listener = null;
        try {
            listener = new SpeechSynthesizerListener() {
                FileOutputStream fout = new FileOutputStream(f);
                private boolean firstRecvBinary = true;
                //语音合成结束
                @Override
                public void onComplete(SpeechSynthesizerResponse response) {
                    //调用onComplete时表示所有TTS数据已接收完成,因此为整个合成数据的延迟。该延迟可能较大,不一定满足实时场景。
                    System.out.println("name: " + response.getName() +
                            ", status: " + response.getStatus()+
                            ", output file :"+ f.getAbsolutePath()
                    );
                }
                //语音合成的语音二进制数据
                @Override
                public void onMessage(ByteBuffer message) {
                    try {
                        if(firstRecvBinary) {
                            //计算首包语音流的延迟,收到第一包语音流时,即可以进行语音播放,以提升响应速度(特别是实时交互场景下)。
                            firstRecvBinary = false;
                        }
                        byte[] bytesArray = new byte[message.remaining()];
                        message.get(bytesArray, 0, bytesArray.length);
                        fout.write(bytesArray);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onFail(SpeechSynthesizerResponse response){
                    //task_id是调用方和服务端通信的唯一标识,当遇到问题时需要提供task_id以便排查。
                    System.out.println(
                            "task_id: " + response.getTaskId() +
                                    //状态码 20000000 表示识别成功
                                    ", status: " + response.getStatus() +
                                    //错误信息
                                    ", status_text: " + response.getStatusText());
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listener;
    }
    public static void process(String text,File f,String voiceStr) {
        SpeechSynthesizer synthesizer = null;
        try {
            //创建实例,建立连接。
            synthesizer = new SpeechSynthesizer(client,getSynthesizerListener(f));
            synthesizer.setAppKey(appKey);
            //设置返回音频的编码格式
            synthesizer.setFormat(OutputFormatEnum.WAV);
            //设置返回音频的采样率
            synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
            //发音人
            Sound_Voice voice = new Sound_Voice(voiceStr);
            if (voiceStr == null){
                synthesizer.setVoice("chuangirl");
            }else {
                synthesizer.setVoice(voice.getSpeaker());
            }

            //语调,范围是-500~500,可选,默认是0。
            synthesizer.setPitchRate(100);
            //语速,范围是-500~500,默认是0。
            synthesizer.setSpeechRate(100);
            //设置用于语音合成的文本
            synthesizer.setText(text);
            // 是否开启字幕功能(返回相应文本的时间戳),默认不开启,需要注意并非所有发音人都支持该参数。
            synthesizer.addCustomedParam("enable_subtitle", false);
            //此方法将以上参数设置序列化为JSON格式发送给服务端,并等待服务端确认。
            long start = System.currentTimeMillis();
            synthesizer.start();
            //等待语音合成结束
            synthesizer.waitForComplete();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            if (null != synthesizer) {
                synthesizer.close();
            }
        }
    }

}

三、创建JavaBean — 与文本转语音的不同

不同就在于方言发言人的设置,不同的地域方言阿里云有不同的发言人,可以根据所在的地域省份设置相应的发言人

 创建方言类,有参传入所在地的省份进行就可以了。通过调用getSpeaker()方法就可以获取到对应的发言人。

目前阿里云所支持的方言只有以下几种。

//方言定义类

@Data
@NoArgsConstructor
public class Sound_Voice implements Serializable {
    public String voiceStr ;
    public String speaker ;

    public Sound_Voice(String voiceStr) {
        switch (voiceStr){
            case "广东":
                this.speaker = "taozi";
                this.voiceStr = "广东";
                break;
            case "东北":
                this.speaker = "cuijie";
                this.voiceStr = "东北";
                break;
            case "天津":
                this.speaker = "aikan";
                this.voiceStr = "天津";
                break;
            case "四川":
                this.speaker = "chuangirl";
                this.voiceStr = "四川";
                break;
            case "香港":
                this.speaker = "kelly";
                this.voiceStr = "香港";
                break;
            case "台湾":
                this.speaker = "qingqing";
                this.voiceStr = "台湾";
                break;
            case "湖南":
                this.speaker = "xiaoze";
                this.voiceStr = "湖南";
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zsy3757486/article/details/130870173
今日推荐