File size: 2,325 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
#include "zipvoice_c_api.h"
#include "zipvoice_resident_server.hpp"

#include <memory>
#include <string>

struct ZipVoiceHandle {
    std::unique_ptr<ZipVoiceResidentServer> impl;
    std::string last_error;
    std::string last_path;
};

extern "C" {

ZipVoiceHandle *zipvoice_create(const ZipVoiceConfig *cfg) {
    if (!cfg) return nullptr;
    auto *h = new ZipVoiceHandle();
    h->impl = std::make_unique<ZipVoiceResidentServer>();
    bool ok = h->impl->Init(
        cfg->root_dir ? cfg->root_dir : "",
        cfg->model_dir ? cfg->model_dir : "",
        cfg->token_file ? cfg->token_file : "",
        cfg->vocoder_model ? cfg->vocoder_model : "",
        cfg->max_tokens > 0 ? cfg->max_tokens : 384,
        cfg->max_feat_len > 0 ? cfg->max_feat_len : 1024,
        cfg->num_step > 0 ? cfg->num_step : 4,
        cfg->default_speed > 0.0f ? cfg->default_speed : 1.0f,
        cfg->seed > 0 ? cfg->seed : 42);
    if (!ok) {
        delete h;
        return nullptr;
    }
    return h;
}

void zipvoice_destroy(ZipVoiceHandle *h) {
    delete h;
}

int zipvoice_cache_prompt(ZipVoiceHandle *h,
                          const char *key,
                          const char *wav_path,
                          const char *prompt_text) {
    if (!h || !h->impl || !key || !wav_path || !prompt_text) return 0;
    bool ok = h->impl->CachePrompt(key, wav_path, prompt_text);
    if (!ok) h->last_error = "cache_prompt failed";
    return ok ? 1 : 0;
}

ZipVoiceResponseC zipvoice_infer(ZipVoiceHandle *h,
                                 const ZipVoiceRequestC *req) {
    ZipVoiceResponseC out{};
    if (!h || !h->impl || !req || !req->text || !req->output_wav) {
        out.ok = 0;
        out.error = "invalid arguments";
        return out;
    }

    ZipVoiceRequest cpp_req;
    cpp_req.text = req->text;
    cpp_req.prompt = req->prompt ? req->prompt : "zh";
    cpp_req.speed = req->speed;
    cpp_req.output_wav = req->output_wav;

    auto resp = h->impl->Infer(cpp_req);
    out.ok = resp.ok ? 1 : 0;
    h->last_path = resp.path;
    h->last_error = resp.error;
    out.path = h->last_path.c_str();
    out.error = h->last_error.empty() ? nullptr : h->last_error.c_str();
    out.wall_sec = resp.wall_sec;
    out.model_sec = resp.model_sec;
    out.rtf = resp.rtf;
    return out;
}

} // extern "C"