From 4f3155fba6013539b37576aed4e17036acc2b57f Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Tue, 11 Feb 2025 19:00:12 +0800 Subject: [PATCH] =?UTF-8?q?*=20ogg=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/base/dl_range_erase.h | 15 ++++++++++ dl/base/dl_string.cpp | 4 +-- dl/base/dl_string.h | 12 ++++++++ dl/io/dl_log.cpp | 12 +++++++- dl/io/dl_log.h | 1 + dl/io/dl_net.cpp | 7 +++-- dl/system/dl_audio_loader.cpp | 52 ++++++++++++++++++++++------------- project/dl_chess | 2 +- 8 files changed, 79 insertions(+), 26 deletions(-) diff --git a/dl/base/dl_range_erase.h b/dl/base/dl_range_erase.h index ccb75241..ebe8339f 100644 --- a/dl/base/dl_range_erase.h +++ b/dl/base/dl_range_erase.h @@ -19,6 +19,21 @@ namespace dl::Erase { /** +* @brief 从容器container中移除 最后 匹配到的a元素 之前(包含a)的所有元素,如果没有匹配到,则什么都不做。 +* @param container 操作的容器 +* @param[in] a 要匹配的元素 +* @remarks {"data/image/cat.png", '/'} -> "cat.png" +*/ +template +bool HeadFromLast(T& container, const V& a) +{ + auto iter = find(container.rbegin(), container.rend(), a); + if (iter == container.rend()) + return false; + container.erase(container.begin(), iter.base()); + return true; +} +/** * @brief 从容器container中移除 最后 匹配到的a元素 之后(不包含a)的所有元素,如果没有匹配到,则什么都不做。 * @param container 操作的容器 * @param[in] a 要匹配的元素 diff --git a/dl/base/dl_string.cpp b/dl/base/dl_string.cpp index 3a5f8b35..3ef971c7 100644 --- a/dl/base/dl_string.cpp +++ b/dl/base/dl_string.cpp @@ -163,7 +163,7 @@ std::vector ParseCommand(std::string_view cmd) return ret; size_t i = 0; // 跳过起始空白 - while (cmd[i] == L' ' || cmd[i] == L'\t') + while (cmd[i] == ' ' || cmd[i] == '\t') { ++i; } @@ -172,7 +172,7 @@ std::vector ParseCommand(std::string_view cmd) std::string temp; while (cmd[i]) { - const wchar_t& ch = cmd[i]; + const char& ch = cmd[i]; if (mode == 0) { if (ch == '\"') diff --git a/dl/base/dl_string.h b/dl/base/dl_string.h index c487c819..f295fe6c 100644 --- a/dl/base/dl_string.h +++ b/dl/base/dl_string.h @@ -694,6 +694,18 @@ inline std::string EraseFilename(std::string_view path_name) return ret; } +/** +* @brief 获取文件名不含后缀 +*/ +inline std::string GetFileStem(std::string_view path_name) +{ + std::string name{ path_name }; + Erase::HeadFromLast(name, '/'); + if (Erase::TailFromLast(name, '.')) + name.pop_back(); + return name; +} + /** * @brief 相对路径返回真 * @param[in] path_name 路径名字 diff --git a/dl/io/dl_log.cpp b/dl/io/dl_log.cpp index e25b2d34..5d018acc 100644 --- a/dl/io/dl_log.cpp +++ b/dl/io/dl_log.cpp @@ -279,10 +279,20 @@ void log_err0(const std::exception& e, const std::source_location location /*= s if (g_log->IsLevelLimit(LogSystem::Level::ERR)) return; g_log->PushLevel(LogSystem::Level::ERR); - log_imp("捕获到C++异常:" + CodeCvt::MultiByteToUtf8(e.what()), location); + log_imp(fmt::format("捕获到C++异常:{}", CodeCvt::MultiByteToUtf8(e.what())), location); g_log->Pop(); } +void log_err0(const std::system_error& e, const std::source_location location /*= std::source_location::current()*/) +{ + if (g_log->IsLevelLimit(LogSystem::Level::ERR)) + return; + g_log->PushLevel(LogSystem::Level::ERR); + log_imp(fmt::format("捕获到系统异常[{}]:{}", e.code().value(), CodeCvt::MultiByteToUtf8(e.what())), location); + g_log->Pop(); +} + + void log(std::string_view str, const std::source_location location /*= std::source_location::current()*/) { if (g_log->IsLevelLimit(g_log->GetSetting()._level)) diff --git a/dl/io/dl_log.h b/dl/io/dl_log.h index f4d9872b..db574c1d 100644 --- a/dl/io/dl_log.h +++ b/dl/io/dl_log.h @@ -204,6 +204,7 @@ void log_damage0(std::string_view str, const std::source_location location = std void log(std::string_view str, const std::source_location location = std::source_location::current()); void log_err0(const std::exception& e, const std::source_location location = std::source_location::current()); +void log_err0(const std::system_error& e, const std::source_location location = std::source_location::current()); #define log_(...) log(fmt::format(__VA_ARGS__), std::source_location::current()) #define log_pause(...) log_pause0(fmt::format(__VA_ARGS__), std::source_location::current()) diff --git a/dl/io/dl_net.cpp b/dl/io/dl_net.cpp index cdf26a12..08c1afb2 100644 --- a/dl/io/dl_net.cpp +++ b/dl/io/dl_net.cpp @@ -131,7 +131,6 @@ public: ClientUdpImp() : _socket{ _ioContext } {} - void _init(const asio::ip::address& addr, asio::ip::port_type port, bool broadcast) { _endpoint = asio::ip::udp::endpoint{ addr, port }; @@ -279,8 +278,8 @@ Client::Client() : Client::~Client() { _bEnd = true; - delete _t; delete _imp; + delete _t; } void Client::_thread_run(void* p) @@ -374,9 +373,9 @@ ClientUdp::ClientUdp() : ClientUdp::~ClientUdp() { _bEnd = true; + delete _imp; delete _tSend; delete _tRecv; - delete _imp; } void ClientUdp::SetAddress(std::string_view ip, uint16_t port, bool broadcast /*= false*/) @@ -447,6 +446,8 @@ void ClientUdp::_thread_recv(void* p) } catch (std::system_error& e) { + if(e.code().value() == 10004) + break; log_err0(e); } assert(size == buf.size()); diff --git a/dl/system/dl_audio_loader.cpp b/dl/system/dl_audio_loader.cpp index a226006c..dd43f558 100644 --- a/dl/system/dl_audio_loader.cpp +++ b/dl/system/dl_audio_loader.cpp @@ -343,12 +343,12 @@ bool AudioLoader::ReadMp3(std::string_view path_name, PCM& pcm) bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) { - std::ofstream ofs; - ofs.open(std::string{ path_name }, - std::ofstream::binary | std::ofstream::out | std::ofstream::trunc); - + std::ofstream ofs = File::Set(path_name); if (!ofs.is_open()) + { + log_err("文件创建失败:{}", path_name); return false; + } #define READ 1024 @@ -368,7 +368,6 @@ bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) vorbis_block vb; /* local working space for packet->PCM decode */ int eos = 0, ret; - int i, founddata; /********** Encode setup ************/ @@ -403,14 +402,17 @@ bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) *********************************************************************/ - ret = vorbis_encode_init_vbr(&vi, pcm._numChannel, pcm._freq, 0.1); + ret = vorbis_encode_init_vbr(&vi, pcm._numChannel, (long)pcm._freq, 0.1f); /* do not continue if setup failed; this can happen if we ask for a mode that libVorbis does not support (eg, too low a bitrate, etc, will return 'OV_EIMPL') */ if (ret) + { + log_err("vorbis_encode_init_vbr失败:", path_name); return false; + } /* add a comment */ vorbis_comment_init(&vc); @@ -423,8 +425,8 @@ bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) /* set up our packet->stream encoder */ /* pick a random serial number; that way we can more likely build chained streams just by concatenation */ - srand(time(NULL)); - ogg_stream_init(&os, rand()); + //srand(time(NULL)); + ogg_stream_init(&os, 0);//rand()); /* Vorbis streams begin with three headers; the initial header (with most of the codec setup parameters) which is mandated by the Ogg @@ -459,17 +461,19 @@ bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) } - + //readbuffer[0] = 0; long buf_cur = 0; + long step = pcm._numChannel * 2; while (!eos) { long i; long bytes = 0; - if (buf_cur + READ * 4 < pcm._buf.size()) - bytes = READ * 4; + if (buf_cur + READ * step < (long)pcm._buf.size()) + bytes = READ * step; else - bytes = pcm._buf.size() - buf_cur; + bytes = (long)pcm._buf.size() - buf_cur; - char* p = (char*)pcm._buf.data() + buf_cur; + char* readbuffer = (char*)pcm._buf.data() + buf_cur; + //memcpy(readbuffer, pcm._buf.data() + buf_cur, READ * 4); buf_cur += bytes; //long bytes = fread(readbuffer, 1, READ * 4, stdin); /* stereo hardwired here */ @@ -488,13 +492,23 @@ bool AudioLoader::WriteOgg(std::string_view path_name, const PCM& pcm) float** buffer = vorbis_analysis_buffer(&vd, READ); /* uninterleave samples */ - for (i = 0; i < bytes / 4; i++) { - buffer[0][i] = ((p[i * 4 + 1] << 8) | - (0x00ff & (int)p[i * 4])) / 32768.f; - buffer[1][i] = ((p[i * 4 + 3] << 8) | - (0x00ff & (int)p[i * 4 + 2])) / 32768.f; + if (step == 4) + { + for (i = 0; i < bytes / 4; i++) { + buffer[0][i] = ((readbuffer[i * 4 + 1] << 8) | + (0x00ff & (int)readbuffer[i * 4])) / 32768.f; + buffer[1][i] = ((readbuffer[i * 4 + 3] << 8) | + (0x00ff & (int)readbuffer[i * 4 + 2])) / 32768.f; + } } - + else if(step == 2) + { + for (i = 0; i < bytes / 2; i++) { + buffer[0][i] = ((readbuffer[i * 2 + 1] << 8) | (0x00ff & (int)readbuffer[i * 2])) / 32768.f; + //buffer[1][i] = buffer[0][i]; + } + } + /* tell the library how much we actually submitted */ vorbis_analysis_wrote(&vd, i); } diff --git a/project/dl_chess b/project/dl_chess index 455b70f3..003559c1 160000 --- a/project/dl_chess +++ b/project/dl_chess @@ -1 +1 @@ -Subproject commit 455b70f3c70c68365ccf8ebbe302734ec62db070 +Subproject commit 003559c165c72b60e230914dd692a8b808c2a64a