From 4d2ac7bf109bbff3cf8a62660fe138980dbba3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:29:37 +0800 Subject: [PATCH] mp3 --- .../LunaTranslator/myutils/utils.py | 13 +-- .../LunaTranslator/winsharedutils.py | 15 ++++ plugins/shareddllproxy/CMakeLists.txt | 5 +- plugins/shareddllproxy/shareddllproxy.cpp | 4 +- plugins/winsharedutils/CMakeLists.txt | 5 +- plugins/winsharedutils/mp3enc.cpp | 82 +++++++++++++++++++ 6 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 plugins/winsharedutils/mp3enc.cpp diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index e353d997..2fb519b9 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -781,12 +781,13 @@ class loopbackrecorder: if os.path.exists(filewav) == False: callback("") return - filemp3 = filewav.replace(".wav", ".mp3") - cmd = './files/plugins/shareddllproxy32.exe mainmp3 "{}" "{}"'.format( - filewav, filemp3 - ) - subproc_w(cmd, run=True) - if os.path.exists(filemp3): + with open(filewav, "rb") as ff: + wav = ff.read() + mp3 = winsharedutils.encodemp3(wav) + if mp3: + filemp3 = filewav[:-3] + "mp3" + with open(filemp3, "wb") as ff: + ff.write(mp3) os.remove(filewav) callback(filemp3) else: diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index 76e3c0f8..56d2bfdc 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -361,3 +361,18 @@ clipboard_callback.restype = HWND clipboard_callback_stop = utilsdll.clipboard_callback_stop clipboard_callback_stop.argtypes = (HWND,) clipboard_callback_type = CFUNCTYPE(None, c_wchar_p, c_bool) + +_encodemp3 = utilsdll.encodemp3 +_encodemp3.argtypes = c_void_p, c_size_t, c_void_p + + +def encodemp3(wav): + ret = [] + + def cb(ptr, size): + ret.append(cast(ptr, POINTER(c_char))[:size]) + + _encodemp3(wav, len(wav), CFUNCTYPE(None, c_void_p, c_size_t)(cb)) + if len(ret): + return ret[0] + return None diff --git a/plugins/shareddllproxy/CMakeLists.txt b/plugins/shareddllproxy/CMakeLists.txt index 35a06e58..4ca95ccb 100644 --- a/plugins/shareddllproxy/CMakeLists.txt +++ b/plugins/shareddllproxy/CMakeLists.txt @@ -19,11 +19,10 @@ if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) target_link_libraries(shareddllproxy Mfplat mfuuid ${Detours}) set_target_properties(shareddllproxy PROPERTIES OUTPUT_NAME "shareddllproxy64") else() - add_library(tinymp3 ../libs/tinymp3/shine_mp3.c) add_subdirectory(voiceroid2) - add_library(x86lib eztrans.cpp dreye.cpp jbj7.cpp kingsoft.cpp le.cpp neospeech.cpp ../implsapi.cpp LR.cpp mp3.cpp) + add_library(x86lib eztrans.cpp dreye.cpp jbj7.cpp kingsoft.cpp le.cpp neospeech.cpp ../implsapi.cpp LR.cpp) target_precompile_headers(voiceroid2 REUSE_FROM pch) target_precompile_headers(x86lib REUSE_FROM pch) - target_link_libraries(shareddllproxy Mfplat mfuuid x86lib tinymp3 voiceroid2 ${Detours}) + target_link_libraries(shareddllproxy Mfplat mfuuid x86lib voiceroid2 ${Detours}) set_target_properties(shareddllproxy PROPERTIES OUTPUT_NAME "shareddllproxy32") endif() diff --git a/plugins/shareddllproxy/shareddllproxy.cpp b/plugins/shareddllproxy/shareddllproxy.cpp index 9a2fbdda..6faacd15 100644 --- a/plugins/shareddllproxy/shareddllproxy.cpp +++ b/plugins/shareddllproxy/shareddllproxy.cpp @@ -5,7 +5,7 @@ int ntleaswmain(int argc, wchar_t *wargv[]); int updatewmain(int argc, wchar_t *wargv[]); bool checkisapatch(); #ifndef _WIN64 -int mainmp3(int argc, wchar_t *argv[]); +// int mainmp3(int argc, wchar_t *argv[]); int LRwmain(int argc, wchar_t *argv[]); int jbjwmain(int argc, wchar_t *argv[]); int dreyewmain(int argc, wchar_t *argv[]); @@ -65,7 +65,7 @@ int wmain(int argc, wchar_t *argv[]) {L"listpm", listprocessmodule}, {L"update", updatewmain}, #ifndef _WIN64 - {L"mainmp3", mainmp3}, + // {L"mainmp3", mainmp3}, {L"LR", LRwmain}, {L"le", lewmain}, {L"jbj7", jbjwmain}, diff --git a/plugins/winsharedutils/CMakeLists.txt b/plugins/winsharedutils/CMakeLists.txt index b26794e9..c6c691df 100644 --- a/plugins/winsharedutils/CMakeLists.txt +++ b/plugins/winsharedutils/CMakeLists.txt @@ -10,9 +10,10 @@ generate_product_version( VERSION_MINOR ${VERSION_MINOR} VERSION_PATCH ${VERSION_PATCH} ) - -add_library(winsharedutils MODULE webview2_extra.cpp AreoAcrylic.cpp screenshot.cpp ../implsapi.cpp hwnd.cpp globalmessagelistener.cpp theme.cpp version.cpp otsu.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp ${versioninfo}) +add_library(tinymp3 ../libs/tinymp3/shine_mp3.c) +add_library(winsharedutils MODULE mp3enc.cpp webview2_extra.cpp AreoAcrylic.cpp screenshot.cpp ../implsapi.cpp hwnd.cpp globalmessagelistener.cpp theme.cpp version.cpp otsu.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp ${versioninfo}) target_precompile_headers(winsharedutils REUSE_FROM pch) +target_link_libraries(winsharedutils tinymp3) if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) set_target_properties(winsharedutils PROPERTIES OUTPUT_NAME "winsharedutils64") else() diff --git a/plugins/winsharedutils/mp3enc.cpp b/plugins/winsharedutils/mp3enc.cpp new file mode 100644 index 00000000..ea6cf385 --- /dev/null +++ b/plugins/winsharedutils/mp3enc.cpp @@ -0,0 +1,82 @@ +#include "define.h" + +#include +#include +#include +#include +#include +#include + +#define DR_WAV_IMPLEMENTATION +#include "dr_wav.h" + +int stereo = STEREO; + +DECLARE void encodemp3(void *ptr, size_t size, void (*cb)(void *ptr, size_t size)) +{ + shine_config_t config; + shine_t s; + int written; + unsigned char *data; + /* Set the default MPEG encoding paramters - basically init the struct */ + shine_set_config_mpeg_defaults(&config.mpeg); + + config.mpeg.bitr = 320; + + uint32_t sampleRate = 0; + uint64_t totalSampleCount = 0; + uint32_t channels = 0; + int16_t *data_in = drwav_open_memory_and_read_pcm_frames_s16(ptr, size, &channels, &sampleRate, &totalSampleCount, NULL); + if (data_in == NULL) + return; + totalSampleCount *= channels; + double startTime = now(); + config.wave.samplerate = sampleRate; + config.wave.channels = (decltype(config.wave.channels))channels; + + /* See if samplerate and bitrate are valid */ + if (shine_check_config(config.wave.samplerate, config.mpeg.bitr) < 0) + return; + // printf("Unsupported samplerate/bitrate configuration."); + + /* Set to stereo mode if wave data is stereo, mono otherwise. */ + if (config.wave.channels > 1) + config.mpeg.mode = (decltype(config.mpeg.mode))stereo; + else + config.mpeg.mode = MONO; + + /* Initiate encoder */ + s = shine_initialise(&config); + + int samples_per_pass = shine_samples_per_pass(s) * channels; + std::string sdata; + /* All the magic happens here */ + size_t count = totalSampleCount / samples_per_pass; + int16_t *buffer = data_in; + for (int i = 0; i < count; i++) + { + data = shine_encode_buffer_interleaved(s, buffer, &written); + sdata += std::string((char *)data, written); + buffer += samples_per_pass; + } + size_t last = totalSampleCount % samples_per_pass; + if (last != 0) + { + int16_t *cache = (int16_t *)calloc(samples_per_pass, sizeof(int16_t)); + if (cache != NULL) + { + memcpy(cache, buffer, last * sizeof(int16_t)); + data = shine_encode_buffer_interleaved(s, cache, &written); + free(cache); + sdata += std::string((char *)data, written); + } + } + /* Flush and write remaining data. */ + data = shine_flush(s, &written); + sdata += std::string((char *)data, written); + /* Close encoder. */ + shine_close(s); + free(data_in); + double time_interval = calcElapsed(startTime, now()); + cb(sdata.data(), sdata.size()); +}