Skip to content

Commit

Permalink
C++のexampleでmodel/を読む
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Aug 26, 2023
1 parent ba9520b commit ffdcd91
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example/cpp/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16)
project(SimpleTTS)

add_executable(simple_tts simple_tts.cpp)
set_property(TARGET simple_tts PROPERTY CXX_STANDARD 11)
set_property(TARGET simple_tts PROPERTY CXX_STANDARD 17)

file(GLOB ONNXRUNTIME_SHARED_LIB ./libonnxruntime.so.* ./libonnxruntime.*.dylib)
target_link_directories(simple_tts PRIVATE ./voicevox_core)
Expand Down
34 changes: 21 additions & 13 deletions example/cpp/unix/simple_tts.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

#include "voicevox_core/voicevox_core.h"

#define STYLE_ID 0
#define OUTPUT_WAV_NAME "audio.wav"

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -32,26 +34,32 @@ int main(int argc, char *argv[]) {
}
voicevox_open_jtalk_rc_delete(open_jtalk);

VoicevoxVoiceModel* model;
result = voicevox_voice_model_new_from_path("..\\..\\..\\model\\sample.vvm");
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
std::cerr << voicevox_error_result_to_message(result) << std::endl;
return 0;
}
result = voicevox_synthesizer_load_voice_model(synthesizer, model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
std::cerr << voicevox_error_result_to_message(result) << std::endl;
return 0;
for (auto const& entry :
std::filesystem::directory_iterator{"./voicevox_core/model"}) {
const auto path = entry.path();
if (path.extension() != ".vvm") {
continue;
}
VoicevoxVoiceModel* model;
result = voicevox_voice_model_new_from_path(path.c_str(), &model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
std::cerr << voicevox_error_result_to_message(result) << std::endl;
return 0;
}
result = voicevox_synthesizer_load_voice_model(synthesizer, model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
std::cerr << voicevox_error_result_to_message(result) << std::endl;
return 0;
}
voicevox_voice_model_delete(model);
}
voicevox_voice_model_delete(model);

std::cout << "音声生成中..." << std::endl;

int64_t style_id = 0;
size_t output_wav_size = 0;
uint8_t *output_wav = nullptr;

result = voicevox_synthesizer_tts(synthesizer,text.c_str(), style_id,
result = voicevox_synthesizer_tts(synthesizer,text.c_str(), STYLE_ID,
voicevox_make_default_tts_options(),
&output_wav_size, &output_wav);
if (result != VOICEVOX_RESULT_OK) {
Expand Down
41 changes: 30 additions & 11 deletions example/cpp/windows/simple_tts/simple_tts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#include <codecvt>
#include <iostream>
#include <vector>
#include <filesystem>
#include <fstream>

#include "voicevox_core.h"

#define OPENJTALK_DICT_NAME L"open_jtalk_dic_utf_8-1.11"
#define MODEL_DIR_NAME L"model"

int main() {
std::wcout.imbue(std::locale(""));
Expand Down Expand Up @@ -45,18 +47,24 @@ int main() {
}
voicevox_open_jtalk_rc_delete(open_jtalk);

VoicevoxVoiceModel* model;
result = voicevox_voice_model_new_from_path("..\\..\\..\\model\\sample.vvm");
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
OutErrorMessage(result);
return 0;
}
result = voicevox_synthesizer_load_voice_model(synthesizer, model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
OutErrorMessage(result);
return 0;
for (auto const& entry : std::filesystem::directory_iterator{GetModelDir()}) {
const auto path = entry.path();
if (path.extension() != ".vvm") {
continue;
}
VoicevoxVoiceModel* model;
result = voicevox_voice_model_new_from_path(path.c_str(), &model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
OutErrorMessage(result);
return 0;
}
result = voicevox_synthesizer_load_voice_model(synthesizer, model);
if (result != VoicevoxResultCode::VOICEVOX_RESULT_OK) {
OutErrorMessage(result);
return 0;
}
voicevox_voice_model_delete(model);
}
voicevox_voice_model_delete(model);

std::wcout << L"音声生成中" << std::endl;
int32_t style_id = 0;
Expand Down Expand Up @@ -97,6 +105,17 @@ std::string GetOpenJTalkDict() {
return retVal;
}

/// <summary>
/// VVMを含むディレクトリのパスを取得します。
/// </summary>
/// <returns>ディレクトのパス</returns>
std::string GetModelDir() {
wchar_t buff[MAX_PATH] = {0};
PathCchCombine(buff, MAX_PATH, GetExeDirectory().c_str(), MODEL_DIR_NAME);
std::string retVal = wide_to_utf8_cppapi(buff);
return retVal;
}

/// <summary>
/// 音声ファイル名を取得します。
/// </summary>
Expand Down

0 comments on commit ffdcd91

Please sign in to comment.