* 支持ogg音频格式
This commit is contained in:
@@ -26,6 +26,11 @@ add_subdirectory ("third/glfw")
|
||||
endif()
|
||||
message("------------------freetype 字体解析库-----------")
|
||||
add_subdirectory ("third/freetype")
|
||||
message("------------------ogg vorbis 音频格式库-----------")
|
||||
add_subdirectory ("third/ogg")
|
||||
set (OGG_LIBRARY "third/ogg")
|
||||
set (OGG_INCLUDE_DIR "third/ogg")
|
||||
add_subdirectory ("third/vorbis")
|
||||
message("------------------openal-soft 音频库-----------")
|
||||
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||
add_subdirectory ("third/openal-soft")
|
||||
|
||||
@@ -71,6 +71,10 @@ endif()
|
||||
|
||||
# freetype
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE freetype)
|
||||
# ogg
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ogg)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE vorbis)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE vorbisfile)
|
||||
# openal
|
||||
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||
add_definitions(-DLIBTYPE=STATIC)
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
|
||||
//---------------------------------------------特殊内容写入---------------------------
|
||||
/**
|
||||
* @brief 写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
|
||||
* @brief 写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
|
||||
*/
|
||||
template <typename T>
|
||||
void Write64(const T* data)
|
||||
@@ -107,39 +107,39 @@ public:
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief 写入一段数据,最基础操作
|
||||
* @brief 写入一段数据,最基础操作
|
||||
*/
|
||||
void Write(const void* data, size_t byte_count);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 填充一段数据,本质调用memset
|
||||
* @brief 填充一段数据,本质调用memset
|
||||
*/
|
||||
void Fill(int val, size_t byte_count);
|
||||
|
||||
/**
|
||||
* @brief 保存到文件
|
||||
* @brief 保存到文件
|
||||
*/
|
||||
bool Save(std::string_view path_name, bool compress = false);
|
||||
|
||||
/**
|
||||
* @brief 清除数据(移动head标记到内存最开始)
|
||||
* @brief 清除数据(移动head标记到内存最开始)
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
/**
|
||||
* @brief 返回 流位置
|
||||
* @brief 返回 流位置
|
||||
*/
|
||||
size_t GetPos() { return _head; }
|
||||
|
||||
/**
|
||||
* @brief 设置 流位置,如果内存不够会扩大
|
||||
* @brief 设置 流位置,如果内存不够会扩大
|
||||
*/
|
||||
void SetPos(size_t pos);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 标记内存已经被移走所有权,析构函数不再释放
|
||||
* @brief 标记内存已经被移走所有权,析构函数不再释放
|
||||
*/
|
||||
void SetDelete(bool d)
|
||||
{
|
||||
|
||||
@@ -240,11 +240,11 @@ bool Factory::RegisterSprite(std::string_view name, Image* img)
|
||||
std::vector<SpriteID>& vec_spr_id = _imp->_mapName2SpriteID[std::string{ name }];
|
||||
SpriteID& spr_id = Select::CreateIfEmpty(vec_spr_id, 0);
|
||||
spr_id._tex = id_tex;
|
||||
spr_id._rect = -1;
|
||||
spr_id._rect = id_rect;
|
||||
|
||||
//{纹理id,区域id} -> { SpriteInfo }
|
||||
FactoryImp::SpriteInfo& info = Select::CreateIfEmpty(
|
||||
_imp->_allSpriteInfo, id_tex, -1);
|
||||
_imp->_allSpriteInfo, id_tex, id_rect);
|
||||
|
||||
info._rect = createRectU({}, Align::GetSize(_imp->_get_rect(id_tex, id_rect)));
|
||||
_imp->_rect_to_uv(info._rect, info._uv);
|
||||
|
||||
+11
-7
@@ -126,22 +126,25 @@ void Audio::Load(std::string_view name, std::string_view path_name)
|
||||
AudioLoader::PCM pcm;
|
||||
if (str_extension == "wav")
|
||||
{
|
||||
if (!AudioLoader::ReadWAV(path_name, pcm))
|
||||
if (!AudioLoader::ReadWav(path_name, pcm))
|
||||
{
|
||||
log_err("解析wav文件失败:{}", path_name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (str_extension == "ogg")
|
||||
{
|
||||
if (!AudioLoader::ReadOgg(path_name, pcm))
|
||||
{
|
||||
log_err("解析ogg文件失败:{}", path_name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_err("解析格式暂未实现:{}", path_name);
|
||||
return;
|
||||
}
|
||||
//资源释放
|
||||
finally f = [&]()
|
||||
{
|
||||
pcm.Delete();
|
||||
};
|
||||
//寻找空位
|
||||
size_t index = Find::FirstIndex(_imp->_allFlag, false);
|
||||
if (index == -1)
|
||||
@@ -170,7 +173,8 @@ void Audio::Load(std::string_view name, std::string_view path_name)
|
||||
}
|
||||
assert(format);
|
||||
//填充buff
|
||||
alBufferData(_imp->_allBuffer[index], format, pcm._data, (ALsizei)pcm._size, (ALsizei)pcm._freq);
|
||||
alBufferData(_imp->_allBuffer[index], format,
|
||||
pcm._buf.data(), (ALsizei)pcm._buf.size(), (ALsizei)pcm._freq);
|
||||
if (alGetError() != AL_NO_ERROR)
|
||||
{
|
||||
log_err0("填充缓冲区失败!");
|
||||
|
||||
@@ -115,17 +115,18 @@ extern Audio* g_audio;
|
||||
* @param[in] group_id 分组id
|
||||
* @param[in] volume 音量
|
||||
*/
|
||||
inline void snd_play(std::string_view name, size_t group_id = 0, float volume = 1.0f)
|
||||
inline gcSound snd_play(std::string_view name, size_t group_id = 0, float volume = 1.0f)
|
||||
{
|
||||
gcSound voc = g_audio->Create(name);
|
||||
if (!voc)
|
||||
{
|
||||
log_err("音效创建失败:{}", name);
|
||||
return;
|
||||
return g_audio->CreateDefault();
|
||||
}
|
||||
|
||||
voc->SetVolume(volume);
|
||||
voc->Play();
|
||||
return voc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <vorbis\codec.h>
|
||||
#include <vorbis\vorbisfile.h>
|
||||
|
||||
#include "system/dl_platform.h"
|
||||
#include "../io/dl_file.h"
|
||||
#include "io/dl_file.h"
|
||||
#include "memory/dl_stream_output.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
@@ -83,7 +87,7 @@ bool ReadChunkData(std::ifstream& ifs, void* buffer, uint32_t size, uint32_t pos
|
||||
ifs.read((char*)buffer, size);
|
||||
return true;
|
||||
}
|
||||
bool AudioLoader::ReadWAV(std::string_view path_name, PCM& pcm)
|
||||
bool AudioLoader::ReadWav(std::string_view path_name, PCM& pcm)
|
||||
{
|
||||
std::ifstream ifs = File::Get(path_name);
|
||||
if (!ifs.is_open())
|
||||
@@ -124,27 +128,82 @@ bool AudioLoader::ReadWAV(std::string_view path_name, PCM& pcm)
|
||||
return false;
|
||||
};
|
||||
|
||||
pcm._data = new std::byte[dwChunkSize];
|
||||
pcm._buf.resize(dwChunkSize);
|
||||
|
||||
if (!ReadChunkData(ifs, pcm._data, dwChunkSize, dwChunkPosition))
|
||||
if (!ReadChunkData(ifs, pcm._buf.data(), dwChunkSize, dwChunkPosition))
|
||||
{
|
||||
log_err("读取块失败(pcm数据):{}", path_name);
|
||||
pcm.Delete();
|
||||
return false;
|
||||
};
|
||||
|
||||
pcm._size = dwChunkSize;
|
||||
pcm._numChannel = wave_format.numChannels;
|
||||
pcm._bitPerSample = wave_format.bitsPerSample;
|
||||
pcm._freq = wave_format.sampleRate;
|
||||
if (pcm._bitPerSample == 32)
|
||||
{
|
||||
log_err("不支持32位:{}", path_name);
|
||||
pcm.Delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioLoader::ReadOgg(std::string_view path_name, PCM& pcm)
|
||||
{
|
||||
FILE* file = File::GetFile(path_name);
|
||||
if (!file)
|
||||
{
|
||||
log_err("打开文件失败:{}", path_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
OggVorbis_File vf;
|
||||
int eof = 0;
|
||||
int current_section;
|
||||
|
||||
int ov_ret = ov_open_callbacks(file, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
|
||||
if (ov_ret < 0)
|
||||
{
|
||||
log_err("不是ogg文件:{}", path_name );
|
||||
fclose(file);
|
||||
return false;
|
||||
}
|
||||
|
||||
//写入wfx
|
||||
{
|
||||
vorbis_info* vi = ov_info(&vf, -1);
|
||||
|
||||
//只需要填充Foramt这一个结构,其他被截断了
|
||||
pcm._numChannel = vi->channels;
|
||||
pcm._freq = vi->rate;
|
||||
pcm._bitPerSample = 16;
|
||||
}
|
||||
|
||||
|
||||
//写入buffer
|
||||
std::byte pcmout[4096];
|
||||
Buffer& buf = pcm._buf;
|
||||
|
||||
while (!eof) {
|
||||
long ret = ov_read(&vf, (char*)pcmout, sizeof(pcmout), 0, 2, 1, ¤t_section);
|
||||
if (ret == 0) {
|
||||
/* EOF */
|
||||
eof = 1;
|
||||
}
|
||||
else if (ret < 0) {
|
||||
/* error in the stream. Not a problem, just reporting it in
|
||||
case we (the app) cares. In this case, we don't. */
|
||||
}
|
||||
else {
|
||||
/* we don't bother dealing with sample rate changes, etc, but
|
||||
you'll have to*/
|
||||
buf.insert(buf.end(), pcmout, pcmout + ret);
|
||||
}
|
||||
}
|
||||
|
||||
ov_clear(&vf);
|
||||
fclose(file);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "memory/dl_buffer.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
@@ -23,12 +24,11 @@ public:
|
||||
{
|
||||
int _numChannel;//通道数 1,2 AL_FORMAT_MONO8,AL_FORMAT_STEREO8
|
||||
int _bitPerSample;//采样数 8,16
|
||||
std::byte* _data;
|
||||
size_t _size;
|
||||
Buffer _buf;
|
||||
size_t _freq;//采样率
|
||||
void Delete() { delete[] _data; }
|
||||
};
|
||||
static bool ReadWAV(std::string_view path_name, PCM& pcm);
|
||||
static bool ReadWav(std::string_view path_name, PCM& pcm);
|
||||
static bool ReadOgg(std::string_view path_name, PCM& pcm);
|
||||
};
|
||||
|
||||
//16字节
|
||||
|
||||
+9
-7
@@ -24,10 +24,17 @@ CodeWriter::Segment* s;
|
||||
|
||||
void Init()
|
||||
{
|
||||
//
|
||||
/*File::CreateFolder(File::PathStorage() + "image/");
|
||||
Image* img = createImageSvg("vulkan-logo.svg", { 400, 200 });
|
||||
saveImage(img, File::PathStorage() + "image/vk_logo.png");
|
||||
g_factory->RegisterSprite("vk_logo", img);
|
||||
delete img;*/
|
||||
|
||||
g_ui->SetStandardSize({ 800, 600 });
|
||||
g_ui->RegisterAll("ui/");
|
||||
|
||||
base = new Base;
|
||||
/*base = new Base;
|
||||
base->spr = g_factory->CreateSpriteDefault();
|
||||
|
||||
base->txt = g_factory->CreateTextDefault();
|
||||
@@ -38,7 +45,7 @@ void Init()
|
||||
base->spr_logo = g_factory->CreateSprite("vk_logo");
|
||||
base->spr_logo->SetOrder(0.5f);
|
||||
|
||||
base->spr9 = Sprite9::Create("bg0");
|
||||
base->spr9 = Sprite9::Create("bg0");*/
|
||||
|
||||
// xlsx
|
||||
Vector2<std::string> vec2;
|
||||
@@ -112,11 +119,6 @@ int main()
|
||||
|
||||
//log(to_string(d));
|
||||
|
||||
//
|
||||
// File::CreateFolder(File::PathStorage() + "image/");
|
||||
// Image* img = createImageSvg("vulkan-logo.svg", { 800, 600 });
|
||||
// saveImage(img, File::PathStorage() + "image/vk_logo.png");
|
||||
// delete img;
|
||||
|
||||
//Image* img1 = createImageWebp("man.webp");
|
||||
//saveImage(img1, "image/man.png");
|
||||
|
||||
+6
-2
@@ -112,8 +112,12 @@ public:
|
||||
_uiInfo._txtInfo->SetString("音频系统 点左键播放");
|
||||
_uiInfo._panelInfo->Update();
|
||||
_uiInfo._panelInfo->Render();
|
||||
if(g_input->KeyDown(MouseCode::LEFT))
|
||||
snd_play("swt_over");
|
||||
if (g_input->KeyDown(MouseCode::LEFT))
|
||||
{
|
||||
snd_play("music_0");
|
||||
snd_play("swt_click");
|
||||
//snd_play("swt_over");
|
||||
}
|
||||
}
|
||||
|
||||
if (!name.empty())
|
||||
|
||||
Reference in New Issue
Block a user