基于百度AI的语音合成

来源:互联网 发布:西安seo技术 编辑:程序博客网 时间:2024/05/16 10:18

1.该功能基于度娘语音合成接口

2.下面代码直接粘贴到开发工具即可使用

3.APP_ID,API_KEY,SECRET_KEY这些需要你自己去度娘开发者中心申请。


public class SpeechSynthesis {

    // 设置APPID/AK/SK
    public static final String APP_ID = "xxxx";// "你的 App ID"
    public static final String API_KEY = "xxxxxx";// "你的 Api ID"
    public static final String SECRET_KEY = "xxxxxx";// "你的 Secret Key"

    public static void main(String[] args) {
        AipSpeech client = initCilent();
        HashMap<String, Object> options = initOptions();
        StringBuffer text = new StringBuffer();
        text.append(嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻");
        String textStr = text.toString();
        int textLength = textStr.length();
        int makeCount = textLength % 500 == 0 ? textLength / 500 : textLength / 500 + 1;
        int flag = 0;
        int count = 1;
        byte[] totalData = null;
        if (makeCount == 1) {
            totalData = getByteData(client, options, textStr);
        } else {
            while (count < makeCount) {
                String newText = textStr.substring(flag, flag + 500);
                System.out.println("Info:" + count + "次,本次合成内容:" + newText);
                if (count == 1) {
                    totalData = getByteData(client, options, newText);
                } else {
                    byte[] newData = getByteData(client, options, newText);
                    totalData = byteMerger(totalData, newData);
                }
                flag += 500;
                count++;
            }
            if (count == makeCount) {
                String newText = textStr.substring(flag);
                byte[] lastData = getByteData(client, options, newText);
                totalData = byteMerger(totalData, lastData);
                System.out.println("Info:" + count + "次,本次合成内容:" + newText);
            }
        }
        MakeVoice("Voice.mp3", totalData);
    }

    // 设置语音合成参数
    private static HashMap<String, Object> initOptions() {
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("spd", "5");
        options.put("pit", "5");
        options.put("vol", "5");
        options.put("per", "3");
        return options;
    }

    // 初始化一个FaceClient
    private static AipSpeech initCilent() {
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    // 发送文字,得到返回的语音字节码
    private static byte[] getByteData(AipSpeech client,
            HashMap<String, Object> options, String textStr) {
        TtsResponse res = client.synthesis(textStr, "zh", 1, options);
        byte[] data = res.getData();
        return data;
    }

    // 语音合成mp3
    private static void MakeVoice(String fileName, byte[] data) {
        File file = new File("/d:" + File.separator + fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 字节码流合成
    public static byte[] byteMerger(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }
}

原创粉丝点击