This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_render_server.cpp
89 lines (86 loc) · 2.72 KB
/
open_render_server.cpp
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
#include "open_render_server.h"
std::default_random_engine e;
auto temp_path = std::filesystem::temp_directory_path().string() + "\openvocaloid\\";
std::string engine_path;
bool exist(std::string& name) {
if (FILE* file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
}
else {
return false;
}
}
int main(int argc, char* argv[])
{
e.seed(time(0));
int res = mkdir(temp_path.c_str());
httplib::Server server;
server.Get("/initEngine", [](const httplib::Request& request, httplib::Response& response) {
if (init_engine(request.get_param_value("path").c_str()) == 0) {
response.status = 200;
}
else {
response.status = 400;
}
return;
});
server.Get("/kill", [](const httplib::Request& request, httplib::Response& response) {
stop();
abort();
});
server.Get("/database", [](const httplib::Request& request, httplib::Response& response) {
rapidjson::Document databases;
databases.SetArray();
auto& allocator = databases.GetAllocator();
auto db = get_databases();
for (int i = 0; i < get_database_count(); i++) {
rapidjson::Value database(rapidjson::kObjectType);
database.AddMember("id", db[i].id, allocator);
database.AddMember("name", rapidjson::Value(db[i].name, allocator).Move(), allocator);
database.AddMember("langId", db[i].lang_id, allocator);
databases.PushBack(database, allocator);
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
rapidjson::Document result;
result.SetObject();
result.AddMember("databases", databases, allocator);
result.Accept(writer);
std::string jsonString = buffer.GetString();
response.status = 200;
response.body = jsonString;
return;
});
server.Post("/render", [](const httplib::Request& request, httplib::Response& response) {
auto& body = request.body;
std::string fname = std::to_string(e());
std::ofstream file(temp_path + fname + ".json");
file << body;
file.close();
int res = render((temp_path + fname + ".json").c_str(), (temp_path + fname + ".wav").c_str());
remove((temp_path + fname + ".json").c_str());
if (res != 0) {
response.status = 400;
return;
}
std::ifstream result(temp_path + fname + ".wav", std::ios::binary);
if (result) {
result.seekg(0, std::ios::end);
size_t length = result.tellg();
result.seekg(0, std::ios::beg);
std::vector<char> buffer(length);
result.read(buffer.data(), length);
remove((temp_path + fname + ".wav").c_str());
response.set_content(buffer.data(), length, "application/octet-stream");
response.set_header("Content-Disposition", "attachment; filename=result.wav");
}
else {
response.status = 404;
}
response.status = 200;
return;
});
server.listen("0.0.0.0", atoi(argv[1]));
return 0;
}