138 lines
3.9 KiB
C++
Raw Normal View History

2024-01-08 23:37:00 +08:00
#include "api_adapter.h"
#include "ebyutil.h"
2024-04-02 15:36:52 +08:00
namespace ebyroid
{
namespace
{
template <class T>
inline T LoadProc(const HINSTANCE &handle, const char *proc_name)
{
FARPROC proc = GetProcAddress(handle, proc_name);
if (proc == nullptr)
{
FreeLibrary(handle);
char m[64];
std::snprintf(m, 64, "Could not find '%s' in the library.", proc_name);
throw std::runtime_error(m);
}
return reinterpret_cast<T>(proc);
}
} // namespace
ApiAdapter *ApiAdapter::Create(const char *dllpath)
{
printf("dllpath %s\n", dllpath);
HINSTANCE handle = LoadLibraryA(dllpath);
if (handle == nullptr)
{
char m[128];
printf("load error\n");
std::snprintf(m,
128,
"LoadLibrary failed with code %d (Check out the voiceroid path setting)",
GetLastError());
throw new std::runtime_error(m);
}
ApiAdapter *adapter = new ApiAdapter(handle);
adapter->init_ = LoadProc<ApiInit>(handle, "_AITalkAPI_Init@4");
adapter->end_ = LoadProc<ApiEnd>(handle, "_AITalkAPI_End@0");
adapter->voice_load_ = LoadProc<ApiVoiceLoad>(handle, "_AITalkAPI_VoiceLoad@4");
adapter->voice_clear_ = LoadProc<ApiVoiceClear>(handle, "_AITalkAPI_VoiceClear@0");
adapter->set_param_ = LoadProc<ApiSetParam>(handle, "_AITalkAPI_SetParam@4");
adapter->get_param_ = LoadProc<ApiGetParam>(handle, "_AITalkAPI_GetParam@8");
adapter->lang_load_ = LoadProc<ApiLangLoad>(handle, "_AITalkAPI_LangLoad@4");
adapter->text_to_kana_ = LoadProc<ApiTextToKana>(handle, "_AITalkAPI_TextToKana@12");
adapter->close_kana_ = LoadProc<ApiCloseKana>(handle, "_AITalkAPI_CloseKana@8");
adapter->get_kana_ = LoadProc<ApiGetKana>(handle, "_AITalkAPI_GetKana@20");
adapter->text_to_speech_ = LoadProc<ApiTextToSpeech>(handle, "_AITalkAPI_TextToSpeech@12");
adapter->close_speech_ = LoadProc<ApiCloseSpeech>(handle, "_AITalkAPI_CloseSpeech@8");
adapter->get_data_ = LoadProc<ApiGetData>(handle, "_AITalkAPI_GetData@16");
printf("load dll ok\n");
return adapter;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
ApiAdapter::~ApiAdapter()
{
BOOL result = FreeLibrary(dll_instance_);
if (!result)
{
Eprintf("FreeLibrary(HMODULE) failed. Though the program will go on, may lead to fatal error.");
}
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::Init(TConfig *config)
{
return init_(config);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::End()
{
return end_();
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::VoiceLoad(const char *voice_name)
{
return voice_load_(voice_name);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::VoiceClear()
{
return voice_clear_();
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::SetParam(IntPtr p_param)
{
return set_param_(p_param);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::GetParam(IntPtr p_param, uint32_t *size)
{
return get_param_(p_param, size);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::LangLoad(const char *dir_lang)
{
return lang_load_(dir_lang);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::TextToKana(int32_t *job_id, TJobParam *param, const char *text)
{
return text_to_kana_(job_id, param, text);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::CloseKana(int32_t job_id, int32_t use_event)
{
return close_kana_(job_id, use_event);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::GetKana(int32_t job_id,
char *text_buf,
uint32_t len_buf,
uint32_t *size,
uint32_t *pos)
{
return get_kana_(job_id, text_buf, len_buf, size, pos);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::TextToSpeech(int32_t *job_id, TJobParam *param, const char *text)
{
return text_to_speech_(job_id, param, text);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::CloseSpeech(int32_t job_id, int32_t use_event)
{
return close_speech_(job_id, use_event);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
ResultCode ApiAdapter::GetData(int32_t job_id, int16_t *raw_buf, uint32_t len_buf, uint32_t *size)
{
return get_data_(job_id, raw_buf, len_buf, size);
}
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
} // namespace ebyroid