天天看點

阿裡雲智能語音互動--語音合成Java SDK使用示例

使用前提與環境準備:服務開通并購買

Step By Step

1.添加pom依賴

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

2.Code Sample

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import com.alibaba.nls.client.AccessToken;
import com.alibaba.nls.client.protocol.NlsClient;
import com.alibaba.nls.client.protocol.OutputFormatEnum;
import com.alibaba.nls.client.protocol.SampleRateEnum;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 此示例示範了:
 *      語音合成API調用。
 *      動态擷取token。
 *      流式合成TTS。
 *      首包延遲計算。
 */


//  語音合成Java SDK 調用示例

public class SpeechSynthesizerDemo {
    private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesizerDemo.class);
    private static long startTime;
    private String appKey;
    NlsClient client;
    public SpeechSynthesizerDemo(String appKey, String accessKeyId, String accessKeySecret) {
        this.appKey = appKey;
        //應用全局建立一個NlsClient執行個體,預設服務位址為阿裡雲線上服務位址。
        //擷取token,使用時注意在accessToken.getExpireTime()過期前再次擷取。
        AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
        try {
            accessToken.apply();
            System.out.println("get token: " + accessToken.getToken() + ", expire time: " + accessToken.getExpireTime());
            client = new NlsClient(accessToken.getToken());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public SpeechSynthesizerDemo(String appKey, String accessKeyId, String accessKeySecret, String url) {
        this.appKey = appKey;
        AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
        try {
            accessToken.apply();
            System.out.println("get token: " + accessToken.getToken() + ", expire time: " + accessToken.getExpireTime());
            if(url.isEmpty()) {
                client = new NlsClient(accessToken.getToken());
            }else {
                client = new NlsClient(url, accessToken.getToken());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static SpeechSynthesizerListener getSynthesizerListener() {
        SpeechSynthesizerListener listener = null;
        try {
            listener = new SpeechSynthesizerListener() {
                File f=new File("tts_test.wav");
                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;
                            long now = System.currentTimeMillis();
                            logger.info("tts first latency : " + (now - SpeechSynthesizerDemo.startTime) + " ms");
                        }
                        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 void process() {
        SpeechSynthesizer synthesizer = null;
        try {
            //建立執行個體,建立連接配接。
            synthesizer = new SpeechSynthesizer(client, getSynthesizerListener());
            synthesizer.setAppKey(appKey);
            //設定傳回音頻的編碼格式
            synthesizer.setFormat(OutputFormatEnum.WAV);
            //設定傳回音頻的采樣率
            synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
            //發音人
            synthesizer.setVoice("siyue");
            //語調,範圍是-500~500,可選,預設是0。
            synthesizer.setPitchRate(100);
            //語速,範圍是-500~500,預設是0。
            synthesizer.setSpeechRate(100);
            //設定用于語音合成的文本
            synthesizer.setText("歡迎使用阿裡巴巴智能語音合成服務,您可以說杭州明天天氣怎麼樣啊,如果天氣晴朗的話,我們大家準備去爬山野營,看看美麗的自然風景");
            // 是否開啟字幕功能(傳回相應文本的時間戳),預設不開啟,需要注意并非所有發音人都支援該參數。
            synthesizer.addCustomedParam("enable_subtitle", false);
            //此方法将以上參數設定序列化為JSON格式發送給服務端,并等待服務端确認。
            long start = System.currentTimeMillis();
            synthesizer.start();
            logger.info("tts start latency " + (System.currentTimeMillis() - start) + " ms");
            SpeechSynthesizerDemo.startTime = System.currentTimeMillis();
            //等待語音合成結束
            synthesizer.waitForComplete();
            logger.info("tts stop latency " + (System.currentTimeMillis() - start) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //關閉連接配接
            if (null != synthesizer) {
                synthesizer.close();
            }
        }
    }
    public void shutdown() {
        client.shutdown();
    }
    public static void main(String[] args) throws Exception {
        String appKey = "XXXXXXXXXX";
        String id = "XXXXXXXXXX";
        String secret = "XXXXXXXXXX";
        String url = ""; //預設值:wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1
       /* if (args.length == 3) {
            appKey   = args[0];
            id       = args[1];
            secret   = args[2];
        } else if (args.length == 4) {
            appKey   = args[0];
            id       = args[1];
            secret   = args[2];
            url      = args[3];
        } else {
            System.err.println("run error, need params(url is optional): " + "<app-key> <AccessKeyId> <AccessKeySecret> [url]");
            System.exit(-1);
        }*/
        SpeechSynthesizerDemo demo = new SpeechSynthesizerDemo(appKey, id, secret, url);
        demo.process();
        demo.shutdown();
    }
}           

3.測試結果

get token: b42af56233c84ca5a6362e6f24cfbc2e, expire time: 1640198528
name: SynthesisCompleted, status: 20000000, output file :D:\ZhinyYjh\tts_test.wav
           

更多參考

快速開始

智能語言互動:

語音合成
上一篇: HTML前端知識
下一篇: 定義事件