日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

Java SDK

更新時(shí)間:

本文介紹如何使用智能語音交互流式文本語音合成的Java SDK,包括SDK的安裝方法及SDK代碼示例等。

前提條件

在使用SDK之前,請先閱讀接口說明

下載安裝

  1. 從Maven服務(wù)器下載最新版本的SDKnls-sdk-java-demo+flowingtts+3.zip

    <dependency>
        <groupId>com.alibaba.nls</groupId>
        <artifactId>nls-sdk-tts</artifactId>
        <version>2.2.14</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.nls</groupId>
        <artifactId>nls-sdk-common</artifactId>
        <version>2.2.14</version>
    </dependency>
    重要

    Java SDK 從 2.1.7 版本開始(含2.1.7),語音合成SDK(包括實(shí)時(shí)長文本語音合成)SpeechSynthesizerwaitForComplete 接口的超時(shí)時(shí)間單位從 變更為 毫秒

  2. 解壓該ZIP文件。

  3. 在pom.xml文件所在的目錄運(yùn)行mvn package,會(huì)在target目錄生成可執(zhí)行JAR:nls-example-tts-2.0.0-jar-with-dependencies.jar。

  4. 將JAR包拷貝到您應(yīng)用所在的服務(wù)器,用于快速驗(yàn)證及壓測服務(wù)。

  5. 服務(wù)驗(yàn)證。運(yùn)行如下代碼,并按提示提供相應(yīng)參數(shù)。運(yùn)行后在命令執(zhí)行目錄生成logs/nls.log,并且將合成的音頻保存在flowingTts.wav。

    java -cp nls-example-flowing-tts-2.0.0-jar-with-dependencies.jar com.alibaba.nls.client.FlowingSpeechSynthesizerDemo <your-api-key> <your-token>

關(guān)鍵接口

  • NlsClient:語音處理客戶端,利用該客戶端可以進(jìn)行一句話識(shí)別、實(shí)時(shí)語音識(shí)別和語音合成的語音處理任務(wù)。該客戶端為線程安全,建議全局僅創(chuàng)建一個(gè)實(shí)例。

  • FlowingSpeechSynthesizer:流式文本語音合成實(shí)時(shí)語音合成處理類,通過該接口請求參數(shù),發(fā)送請求,非線程安全。

  • FlowingSpeechSynthesizerListener:流式文本語音合成實(shí)時(shí)語音合成監(jiān)聽類,監(jiān)聽返回結(jié)果。非線程安全。需要實(shí)現(xiàn)如下抽象方法:

    /**
    * 服務(wù)端檢測到了一句話的開始
    * @param response
    */
    abstract public void onSentenceBegin(FlowingSpeechSynthesizerResponse response) ;
    /**
    * 接收到語音合成音頻數(shù)據(jù)流
    * @param message 二進(jìn)制音頻數(shù)據(jù)
    */
    abstract public void onAudioData(ByteBuffer message);
    /**
    * 服務(wù)端檢測到了一句話的結(jié)束,并返回這句話的起止位置與所有時(shí)間戳
    * @param response
    */
    abstract public void onSentenceEnd(FlowingSpeechSynthesizerResponse response) ;
    /**
    * 合成結(jié)束
    * @param response
    */
    abstract public void onSynthesisComplete(FlowingSpeechSynthesizerResponse response) ;
    /**
    * 失敗處理
    * @param response
    */
    abstract public void onFail(FlowingSpeechSynthesizerResponse response) ;
    /**
    * 增量在response=>payload中返回時(shí)間戳
    * @param response
    */
    abstract public void onSentenceSynthesis(FlowingSpeechSynthesizerResponse response) ;
重要

SDK調(diào)用注意事項(xiàng):

  • NlsClient使用Netty框架,NlsClient對象的創(chuàng)建會(huì)消耗一定時(shí)間和資源,一經(jīng)創(chuàng)建可以重復(fù)使用。建議調(diào)用程序?qū)lsClient的創(chuàng)建和關(guān)閉與程序本身的生命周期相結(jié)合。

  • SpeechSynthesizer對象不可重復(fù)使用,一個(gè)語音合成任務(wù)對應(yīng)一個(gè)SpeechSynthesizer對象。例如,大模型和用戶的N次對話要進(jìn)行N次語音合成任務(wù),創(chuàng)建N個(gè)SpeechSynthesizer對象。

  • SpeechSynthesizerListener對象和SpeechSynthesizer對象是一一對應(yīng)的,不能將一個(gè)SpeechSynthesizerListener對象設(shè)置到多個(gè)SpeechSynthesizer對象中,否則無法區(qū)分各語音合成任務(wù)。

  • Java SDK依賴Netty網(wǎng)絡(luò)庫,如果您的應(yīng)用依賴Netty,其版本需更新至4.1.17.Final及以上。

代碼示例

package com.alibaba.nls.client;

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.FlowingSpeechSynthesizer;
import com.alibaba.nls.client.protocol.tts.FlowingSpeechSynthesizerListener;
import com.alibaba.nls.client.protocol.tts.FlowingSpeechSynthesizerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * 此示例演示了:
 *      流式文本語音合成API調(diào)用。
 */
public class FlowingSpeechSynthesizerDemo {
    private static final Logger logger = LoggerFactory.getLogger(FlowingSpeechSynthesizerDemo.class);
    private static long startTime;
    private String appKey;
    NlsClient client;
    public FlowingSpeechSynthesizerDemo(String appKey, String token, String url) {
        this.appKey = appKey;
        //創(chuàng)建NlsClient實(shí)例應(yīng)用全局創(chuàng)建一個(gè)即可。生命周期可和整個(gè)應(yīng)用保持一致,默認(rèn)服務(wù)地址為阿里云線上服務(wù)地址。
        if(url.isEmpty()) {
            client = new NlsClient(token);
        } else {
            client = new NlsClient(url, token);
        }
    }
    private static FlowingSpeechSynthesizerListener getSynthesizerListener() {
        FlowingSpeechSynthesizerListener listener = null;
        try {
            listener = new FlowingSpeechSynthesizerListener() {
                File f=new File("flowingTts.wav");
                FileOutputStream fout = new FileOutputStream(f);
                private boolean firstRecvBinary = true;

                //流式文本語音合成開始
                public void onSynthesisStart(FlowingSpeechSynthesizerResponse response) {
                    System.out.println("name: " + response.getName() +
                                       ", status: " + response.getStatus());
                }
                //服務(wù)端檢測到了一句話的開始
                public void onSentenceBegin(FlowingSpeechSynthesizerResponse response) {
                    System.out.println("name: " + response.getName() +
                                       ", status: " + response.getStatus());
                    System.out.println("Sentence Begin");
                }
                //服務(wù)端檢測到了一句話的結(jié)束,獲得這句話的起止位置和所有時(shí)間戳
                public void onSentenceEnd(FlowingSpeechSynthesizerResponse response) {
                    System.out.println("name: " + response.getName() +
                                       ", status: " + response.getStatus() + ", subtitles: " + response.getObject("subtitles"));

                }
                //流式文本語音合成結(jié)束
                @Override
                public void onSynthesisComplete(FlowingSpeechSynthesizerResponse response) {
                    // 調(diào)用onSynthesisComplete時(shí),表示所有TTS數(shù)據(jù)已經(jīng)接收完成,所有文本都已經(jīng)合成音頻并返回。
                    System.out.println("name: " + response.getName() + ", status: " + response.getStatus()+", output file :"+f.getAbsolutePath());
                }
                //收到語音合成的語音二進(jìn)制數(shù)據(jù)
                @Override
                public void onAudioData(ByteBuffer message) {
                    try {
                        if(firstRecvBinary) {
                            // 此處計(jì)算首包語音流的延遲,收到第一包語音流時(shí),即可以進(jìn)行語音播放,以提升響應(yīng)速度(特別是實(shí)時(shí)交互場景下)。
                            firstRecvBinary = false;
                            long now = System.currentTimeMillis();
                            logger.info("tts first latency : " + (now - FlowingSpeechSynthesizerDemo.startTime) + " ms");
                        }
                        byte[] bytesArray = new byte[message.remaining()];
                        message.get(bytesArray, 0, bytesArray.length);
                        System.out.println("write array:" + bytesArray.length);
                        fout.write(bytesArray);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //收到語音合成的增量音頻時(shí)間戳
                @Override
                public void onSentenceSynthesis(FlowingSpeechSynthesizerResponse response) {
                    System.out.println("name: " + response.getName() +
                            ", status: " + response.getStatus() + ", subtitles: " + response.getObject("subtitles"));
                }
                @Override
                public void onFail(FlowingSpeechSynthesizerResponse response){
                    // task_id是調(diào)用方和服務(wù)端通信的唯一標(biāo)識(shí),當(dāng)遇到問題時(shí),需要提供此task_id以便排查。
                    System.out.println(
                            "session_id: " + getFlowingSpeechSynthesizer().getCurrentSessionId() +
                                    ", task_id: " + response.getTaskId() +
                                    //狀態(tài)碼
                                    ", status: " + response.getStatus() +
                                    //錯(cuò)誤信息
                                    ", status_text: " + response.getStatusText());
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listener;
    }
    public void process(String[] textArray) {
        FlowingSpeechSynthesizer synthesizer = null;
        try {
            //創(chuàng)建實(shí)例,建立連接。
            synthesizer = new FlowingSpeechSynthesizer(client, getSynthesizerListener());
            synthesizer.setAppKey(appKey);
            //設(shè)置返回音頻的編碼格式。
            synthesizer.setFormat(OutputFormatEnum.WAV);
            //設(shè)置返回音頻的采樣率。
            synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
            //發(fā)音人。
            synthesizer.setVoice("siyue");
            //音量,范圍是0~100,可選,默認(rèn)50。
            synthesizer.setVolume(50);
            //語調(diào),范圍是-500~500,可選,默認(rèn)是0。
            synthesizer.setPitchRate(0);
            //語速,范圍是-500~500,默認(rèn)是0。
            synthesizer.setSpeechRate(0);
            //此方法將以上參數(shù)設(shè)置序列化為JSON發(fā)送給服務(wù)端,并等待服務(wù)端確認(rèn)。
            long start = System.currentTimeMillis();
            synthesizer.start();
            logger.info("tts start latency " + (System.currentTimeMillis() - start) + " ms");
            FlowingSpeechSynthesizerDemo.startTime = System.currentTimeMillis();
            //設(shè)置連續(xù)兩次發(fā)送文本的最小時(shí)間間隔(毫秒),如果當(dāng)前調(diào)用send時(shí)距離上次調(diào)用時(shí)間小于此值,則會(huì)阻塞并等待直到滿足條件再發(fā)送文本
            synthesizer.setMinSendIntervalMS(100);
            for(String text :textArray) {
                //發(fā)送流式文本數(shù)據(jù)。
                synthesizer.send(text);
            }
            //通知服務(wù)端流式文本數(shù)據(jù)發(fā)送完畢,阻塞等待服務(wù)端處理完成。
            synthesizer.stop();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //關(guān)閉連接
            if (null != synthesizer) {
                synthesizer.close();
            }
        }
    }
    public void shutdown() {
        client.shutdown();
    }
    public static void main(String[] args) throws Exception {
        String appKey = "your-api-key";
        String token = "your-token";
        // url取默認(rèn)值
        String url = "wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1";
        if (args.length == 2) {
            appKey   = args[0];
            token       = args[1];
        } else if (args.length == 3) {
            appKey   = args[0];
            token       = args[1];
            url      = args[2];
        } else {
            System.err.println("run error, need params(url is optional): " + "<app-key> <token> [url]");
            System.exit(-1);
        }
        String[] textArray = {"百草堂與三", "味書屋 魯迅 \n我家的后面有一個(gè)很", "大的園,相傳叫作百草園。現(xiàn)在是早已并屋子一起賣", "給朱文公的子孫了,連那最末次的相見也已經(jīng)",
                "隔了七八年,其中似乎確鑿只有一些野草;但那時(shí)卻是我的樂園。\n不必說碧綠的菜畦,光滑的石井欄,高大的皂莢樹,紫紅的桑葚;也不必說鳴蟬在樹葉里長吟,肥胖的黃蜂伏在菜花",
                "上,輕捷的叫天子(云雀)忽然從草間直竄向云霄里去了。\n單是周圍的短短的泥墻根一帶,就有無限趣味。油蛉在這里低唱,蟋蟀們在這里彈琴。翻開斷磚來,有時(shí)會(huì)遇見蜈蚣;還有斑",
                "蝥,倘若用手指按住它的脊梁,便會(huì)啪的一聲,\n從后竅噴出一陣煙霧。何首烏藤和木蓮藤纏絡(luò)著,木蓮有蓮房一般的果實(shí),何首烏有臃腫的根。有人說,何首烏根是有像人形的,吃了",
                "便可以成仙,我于是常常拔它起來,牽連不斷地拔起來,\n也曾因此弄壞了泥墻,卻從來沒有見過有一塊根像人樣! 如果不怕刺,還可以摘到覆盆子,像小珊瑚珠攢成的小球,又酸又甜,",
                "色味都比桑葚要好得遠(yuǎn)......"};
        FlowingSpeechSynthesizerDemo demo = new FlowingSpeechSynthesizerDemo(appKey, token, url);
        demo.process(textArray);
        demo.shutdown();
    }
}

常見問題

服務(wù)端返回“idle timeout”錯(cuò)誤,應(yīng)如何解決?

該報(bào)錯(cuò)是由于服務(wù)端在超過10s沒有收到客戶端消息,從而導(dǎo)致斷連,返回idle timeout報(bào)錯(cuò)。

可以通過調(diào)用FlowingSpeechSynthesizer.getConnection().sendPing() 定期向服務(wù)端發(fā)送ping包為連接保活。