Java對接訊飛語音合成(流式版)WebAPI 經驗分享

最近在開發字幕醬(https://www.zimujiang.com/),一個自動生成字幕的在線工具,遇到使用訊飛語音合成的需求,這邊記錄下

訊飛語音合成流式接口將文字信息轉化爲聲音信息,同時提供了衆多極具特色的發音人(音庫)供您選擇,可以在 這裏 在線體驗發音人效果。
該語音能力是通過Websocket API的方式給開發者提供一個通用的接口。Websocket API具備流式傳輸能力,適用於需要流式數據傳輸的AI服務場景。相較於SDK,API具有輕量、跨語言的特點;相較於HTTP API,Websocket API協議有原生支持跨域的優勢。

原WebAPI普通版本接口(http[s]: //api.xfyun.cn/v1/service/v1/tts) 不再對外開放,已經使用WebAPI普通版本的用戶仍可使用,同時也歡迎體驗新版流式接口並儘快完成遷移~

小語種及少數民族方言

  • 目前新增:韓語、日語、維吾爾語、藏語、法語、俄語、西班牙語、印地語、德語、越南語、巴西葡萄牙語、意大利語、葡萄牙語、泰語、烏爾都語;
  • 使用小語種時上傳文本編碼必須使用unicode編碼且設置tte=unicode(unicode即utf16小端的編碼方式);
  • 需先在控制檯開啓小語種發音人才可合成小語種,否則報錯11200。
package com.iflytek.voicecloud.webapi.demo;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import okhttp3.*;
import okio.ByteString;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 語音合成流式 WebAPI 接口調用示例 接口文檔(必看):https://www.xfyun.cn/doc/tts/online_tts/API.html
 * 語音合成流式WebAPI 服務,發音人使用方式:登陸開放平臺https://www.xfyun.cn/後,到控制檯-我的應用-語音合成-添加試用或購買發音人,添加後即顯示該發音人蔘數值
 * 錯誤碼鏈接:https://www.xfyun.cn/document/error-code (code返回錯誤碼時必看)
 * 小語種需要傳輸小語種文本、使用小語種發音人vcn、ent=mtts、tte=unicode以及修改文本編碼方式
 * @author iflytek
 */

public class WebTTSWS {
    private static final String hostUrl = "https://tts-api.xfyun.cn/v2/tts"; //http url 不支持解析 ws/wss schema
    private static final String appid = "XXXXXXXX";//到控制檯-語音合成頁面獲取
    private static final String apiSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";//到控制檯-語音合成頁面獲取
    private static final String apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";//到控制檯-語音合成頁面獲取
    private static final String text = "訊飛開放平臺";
    public static final Gson json = new Gson();
    public static void main(String[] args) throws Exception {
        // 構建鑑權url
        String authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);
        OkHttpClient client = new OkHttpClient.Builder().build();
        //將url中的 schema http://和https://分別替換爲ws:// 和 wss://
        String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
        Request request = new Request.Builder().url(url).build();
        // 存放音頻的文件
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String date = sdf.format(new Date());
        File f = new File("resource/tts/" + date + ".pcm");
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream os = new FileOutputStream(f);
        WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                super.onOpen(webSocket, response);
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //發送數據
                JsonObject frame = new JsonObject();
                JsonObject business = new JsonObject();
                JsonObject common = new JsonObject();
                JsonObject data = new JsonObject();
                // 填充common
                common.addProperty("app_id", appid);
                //填充business
                business.addProperty("aue", "raw");
                business.addProperty("tte", "UTF8");//小語種必須使用UNICODE編碼
                business.addProperty("ent", "intp65");
                business.addProperty("vcn", "xiaoyan");//到控制檯-我的應用-語音合成-添加試用或購買發音人,添加後即顯示該發音人蔘數值,若試用未添加的發音人會報錯11200
                business.addProperty("pitch", 50);
                business.addProperty("speed", 50);
                //填充data
                data.addProperty("status", 2);//固定位2
                try {
                    data.addProperty("text", Base64.getEncoder().encodeToString(text.getBytes("utf8")));
					//使用小語種須使用下面的代碼,此處的unicode指的是 utf16小端的編碼方式,即"UTF-16LE"”
                    //data.addProperty("text", Base64.getEncoder().encodeToString(text.getBytes("UTF-16LE")));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //填充frame
                frame.add("common", common);
                frame.add("business", business);
                frame.add("data", data);
                webSocket.send(frame.toString());
            }
            @Override
            public void onMessage(WebSocket webSocket, String text) {
                super.onMessage(webSocket, text);
                //處理返回數據
                System.out.println("receive=>" + text);
                ResponseData resp = null;
                try {
                    resp = json.fromJson(text, ResponseData.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (resp != null) {
                    if (resp.getCode() != 0) {
                        System.out.println("error=>" + resp.getMessage() + " sid=" + resp.getSid());
                        return;
                    }
                    if (resp.getData() != null) {
                        String result = resp.getData().audio;
                        byte[] audio = Base64.getDecoder().decode(result);
                        try {
                            os.write(audio);
                            os.flush();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (resp.getData().status == 2) {
                            // todo  resp.data.status ==2 說明數據全部返回完畢,可以關閉連接,釋放資源
                            System.out.println("session end ");
                            System.out.println("合成的音頻文件保存在:" + f.getPath());
                            webSocket.close(1000, "");
                            try {
                                os.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            @Override
            public void onMessage(WebSocket webSocket, ByteString bytes) {
                super.onMessage(webSocket, bytes);
            }
            @Override
            public void onClosing(WebSocket webSocket, int code, String reason) {
                super.onClosing(webSocket, code, reason);
                System.out.println("socket closing");
            }
            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                super.onClosed(webSocket, code, reason);
                System.out.println("socket closed");
            }
            @Override
            public void onFailure(WebSocket webSocket, Throwable t, Response response) {
                super.onFailure(webSocket, t, response);
                System.out.println("connection failed");
            }
        });
    }
    public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
        URL url = new URL(hostUrl);
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        String date = format.format(new Date());
        StringBuilder builder = new StringBuilder("host: ").append(url.getHost()).append("\n").//
                append("date: ").append(date).append("\n").//
                append("GET ").append(url.getPath()).append(" HTTP/1.1");
        Charset charset = Charset.forName("UTF-8");
        Mac mac = Mac.getInstance("hmacsha256");
        SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(charset), "hmacsha256");
        mac.init(spec);
        byte[] hexDigits = mac.doFinal(builder.toString().getBytes(charset));
        String sha = Base64.getEncoder().encodeToString(hexDigits);
        String authorization = String.format("hmac username=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);
        HttpUrl httpUrl = HttpUrl.parse("https://" + url.getHost() + url.getPath()).newBuilder().//
                addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(charset))).//
                addQueryParameter("date", date).//
                addQueryParameter("host", url.getHost()).//
                build();
        return httpUrl.toString();
    }
    public static class ResponseData {
        private int code;
        private String message;
        private String sid;
        private Data data;
        public int getCode() {
            return code;
        }
        public String getMessage() {
            return this.message;
        }
        public String getSid() {
            return sid;
        }
        public Data getData() {
            return data;
        }
    }
    public static class Data {
        private int status;  //標誌音頻是否返回結束  status=1,表示後續還有音頻返回,status=2表示所有的音頻已經返回
        private String audio;  //返回的音頻,base64 編碼
        private String ced;  // 合成進度
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章