* ogg编码

This commit is contained in:
2025-02-11 19:00:12 +08:00
parent 3f6c6d58cf
commit 4f3155fba6
8 changed files with 79 additions and 26 deletions
+15
View File
@@ -19,6 +19,21 @@
namespace dl::Erase
{
/**
* @brief 从容器container中移除 最后 匹配到的a元素 之前(包含a)的所有元素,如果没有匹配到,则什么都不做。
* @param container 操作的容器
* @param[in] a 要匹配的元素
* @remarks {"data/image/cat.png", '/'} -> "cat.png"
*/
template<typename T, typename V>
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 要匹配的元素
+2 -2
View File
@@ -163,7 +163,7 @@ std::vector<std::string> 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<std::string> 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 == '\"')
+12
View File
@@ -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 路径名字
+11 -1
View File
@@ -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))
+1
View File
@@ -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())
+4 -3
View File
@@ -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());
+33 -19
View File
@@ -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);
}