File size: 1,983 Bytes
71e354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "api/ax_tts_api.h"

int main(int argc, char** argv) {
    if (argc < 4) {
        fprintf(stderr, "Usage: %s <model_dir> <espeak_data_dir> <text> [output.wav] [lang]\n", argv[0]);
        return 1;
    }
    AX_TTS_INIT_CONFIG init_cfg = {0};
    init_cfg.max_seq_len = 96;
    strncpy(init_cfg.model_path, argv[1], AX_TTS_MAX_STR_LEN - 1);
    strncpy(init_cfg.espeak_data_path, argv[2], AX_TTS_MAX_STR_LEN - 1);
    snprintf(init_cfg.jieba_dict_path, AX_TTS_MAX_STR_LEN, "%s/dict", argv[1], AX_TTS_MAX_STR_LEN - 1);

    AX_TTS_HANDLE handle = AX_TTS_Init(AX_KOKORO, &init_cfg);
    if (!handle) { fprintf(stderr, "Init failed\n"); return 1; }

    AX_TTS_RUN_CONFIG run_cfg = {0};
    run_cfg.speed = 1.0f; run_cfg.sample_rate = 24000;
    strncpy(run_cfg.voice, "af_heart", AX_TTS_MAX_STR_LEN - 1);
    const char* lang = argc > 5 ? argv[5] : "en";
    strncpy(run_cfg.language, lang, AX_TTS_MAX_STR_LEN - 1);

    AX_TTS_AUDIO* audio = NULL;
    int ret = AX_TTS_Run(handle, argv[3], &run_cfg, &audio);
    if (ret == 0 && audio) {
        fprintf(stderr, "OK: %d samples %.2fs\n", audio->num_samples, (float)audio->num_samples / audio->sample_rate);
        const char* out = argc > 4 ? argv[4] : "output.wav";
        FILE* f = fopen(out, "wb");
        if (f) {
            int ds = audio->num_samples * 4, cs = 36 + ds;
            fwrite("RIFF",1,4,f); fwrite(&cs,4,1,f); fwrite("WAVE",1,4,f);
            fwrite("fmt ",1,4,f); int fms=16; short af=3,ch=1,ba=4,bt=32; int sr=audio->sample_rate,br=sr*4;
            fwrite(&fms,4,1,f); fwrite(&af,2,1,f); fwrite(&ch,2,1,f); fwrite(&sr,4,1,f);
            fwrite(&br,4,1,f); fwrite(&ba,2,1,f); fwrite(&bt,2,1,f);
            fwrite("data",1,4,f); fwrite(&ds,4,1,f);
            fwrite(audio->data,4,audio->num_samples,f); fclose(f);
        }
        free(audio);
    } else { fprintf(stderr, "Run failed\n"); }
    AX_TTS_Uninit(handle);
    return ret;
}