AliTtsConfig.java 1.86 KB
Newer Older
何处是我家's avatar
提交  
何处是我家 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ewaytek.deepseek.config;

import com.alibaba.nls.client.AccessToken;
import com.alibaba.nls.client.protocol.NlsClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author yangtq
 * @date 2025/3/28
 */
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "tts")
public class AliTtsConfig {

    private String appKey;
    private String accessKeyId;
    private String accessKeySecret;
    private String voice;

    private int volume;

    private int speech_rate;

    private int pitch_rate;

    private int longText;


    private static NlsClient client;
    private static AccessToken accessToken;

    @PostConstruct
    public void init() {
        log.info("客户端启动-------------------------->");
        synchronized (this) {
            applyAccessToken(accessKeyId, accessKeySecret);
            client = new NlsClient(accessToken.getToken());
            log.info("阿里云 NlsClient 初始化完毕");
        }
    }

    public static void applyAccessToken(String accessKeyId, String accessKeySecret) {
        accessToken = new AccessToken(accessKeyId, accessKeySecret);
        try {
            accessToken.apply();
            log.info("get token: " + accessToken.getToken() + ", expire time: " + accessToken.getExpireTime());
        } catch (IOException e) {
            log.error("https获取accessToken失败!" + e.getMessage());
        }
    }


    public static AccessToken getAccessToken() {
        return accessToken;
    }

    public static NlsClient getNlsClient() {
        return client;
    }

    public String getAppKey() {
        return appKey;
    }

}