File size: 3,850 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**************************************************************************************************
 *
 * Copyright (c) 2019-2026 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
 *
 * This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
 * may not be copied or distributed in any isomorphic form without the prior
 * written consent of Axera Semiconductor (Ningbo) Co., Ltd.
 *
 **************************************************************************************************/
#pragma once

#include <map>
#include <string>

#include "httplib.h"
#include "api/ax_tts_api.h"
#include "openai_err.hpp"

#define DEFAULT_PORT    8080
#define TTS_ENDPOINT    "/v1/audio/speech"

/* OpenAI-Compatible server
   Following API docs from: https://platform.openai.com/docs/api-reference/audio/createSpeech
   post http://IP:PORT/v1/audio/speech

 * Client usage:
 * - cURL:
 *      curl https://api.openai.com/v1/audio/speech \
        -H "Authorization: Bearer $OPENAI_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
            "model": "gpt-4o-mini-tts",
            "input": "The quick brown fox jumped over the lazy dog.",
            "voice": "alloy"
        }' \
        --output speech.mp3

 *
 * - Python:
 *  from pathlib import Path
    import openai

    speech_file_path = Path(__file__).parent / "speech.mp3"
    with openai.audio.speech.with_streaming_response.create(
        model="gpt-4o-mini-tts",
        voice="alloy",
        input="The quick brown fox jumped over the lazy dog."
        ) as response:
        response.stream_to_file(speech_file_path)


 * - JavaScript:
    import fs from "fs";
    import path from "path";
    import OpenAI from "openai";

    const openai = new OpenAI();

    const speechFile = path.resolve("./speech.mp3");

    async function main() {
    const mp3 = await openai.audio.speech.create({
        model: "gpt-4o-mini-tts",
        voice: "alloy",
        input: "Today is a wonderful day to build something people love!",
    });
    console.log(speechFile);
    const buffer = Buffer.from(await mp3.arrayBuffer());
    await fs.promises.writeFile(speechFile, buffer);
    }
    main();


 * Response:
 * Error: check https://platform.openai.com/docs/guides/error-codes
 *  {
        "error": {
            "message": "You exceeded your current quota, please check your plan and billing details.",
            "type": "insufficient_quota",
            "param": null,
            "code": 402
        }
    }

* Success:
    {
        "file": stream of pcm
    }
*/
class TTSServer {
public:
    TTSServer() = default;
    ~TTSServer() = default;

    bool init(const std::string& model_path,
              const std::string& espeak_data_path = "",
              const std::string& jieba_dict_path = "");
    void start(int port = DEFAULT_PORT);
    void stop();

private:
    void setup_routes_();
    AX_TTS_HANDLE load_tts_(const std::string& model_name);
    // 设置CORS头
    void set_CORS_headers_(httplib::Response& res);
    
    /*
     * request: Content-Type: multipart/form-data
     * {
     *      "input": "The text to generate audio for."
     *      "instructions": We use instructions for language choice for compatibilty. Supports zh, en, ja, etc.
     *      "model": Supports kokoro and melotts
     *      "speed": The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
     *      "response_format": The format to audio in. Supported formats are mp3, wav. Default to mp3.
     * }
    */
    bool check_request_(const httplib::Request& req, httplib::Response& res, nlohmann::json& json_data);

private:
    std::map<std::string, AX_TTS_HANDLE> handles_;
    httplib::Server srv_;
    std::string model_path_;
    std::string espeak_data_path_;
    std::string jieba_dict_path_;
};