File size: 2,158 Bytes
a37072b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once

#include <string>
#include <vector>
#include <map>
#include <memory>
#include <sys/time.h>

#include "ax_sys_api.h"
#include "src/tokenizer.hpp"
#include "src/fbank.hpp"
#include "src/zipvoice_engine.hpp"
#include "src/vocoder.hpp"

struct ZipVoicePromptCache {
    std::string key;          // zh / en
    std::string prompt_text;
    std::string prompt_wav;
    std::vector<int> prompt_tokens;
    std::vector<float> prompt_features;  // [1, T, 100] flattened
    int prompt_frames = 0;
    float prompt_rms = 0.0f;
};

struct ZipVoiceRequest {
    std::string id;
    std::string text;
    std::string prompt = "zh";
    float speed = 1.0f;
    int num_step = 0;  // 0 = use server default
    std::string output_wav;
};

struct ZipVoiceResponse {
    bool ok = false;
    std::string id;
    std::string path;
    std::string error;
    double wall_sec = 0.0;
    double model_sec = 0.0;
    double rtf = 0.0;
};

class ZipVoiceResidentServer {
public:
    ZipVoiceResidentServer();
    ~ZipVoiceResidentServer();

    bool Init(const std::string& root_dir,
              const std::string& model_dir,
              const std::string& token_file,
              const std::string& vocoder_model,
              int max_tokens = 384,
              int max_feat_len = 1024,
              int num_step = 4,
              float default_speed = 1.0f,
              int seed = 42);

    ZipVoiceResponse Infer(const ZipVoiceRequest& req);
    bool CachePrompt(const std::string& key, const std::string& wav_path, const std::string& text);

private:
    bool InitAxSys();
    bool LoadTokenizer();
    bool LoadRuntime();
    bool LoadVocoder();
    bool CachePrompts();

private:
    std::string root_dir_;
    std::string model_dir_;
    std::string token_file_;
    std::string vocoder_model_;

    int max_tokens_ = 384;
    int max_feat_len_ = 1024;
    int num_step_ = 4;
    float default_speed_ = 1.0f;
    int seed_ = 42;

    bool ax_inited_ = false;

    std::unique_ptr<Tokenizer> tokenizer_;
    std::unique_ptr<ZipVoiceEngine> engine_;
    std::unique_ptr<Vocoder> vocoder_;
    std::map<std::string, ZipVoicePromptCache> prompts_;
};