dl
dl_audio.h
浏览该文件的文档.
1
18#pragma once
19
20#ifndef DL_DISABLE_AUDIO
21#include <unordered_map>
22
24#include "io/dl_json.h"
25
26namespace dl
27{
28
30constexpr size_t NUM_MAX_FILE = 256;
31
32class SoundImp;
34class Sound
35{
36 friend class Audio;
37 friend class PoolList<Sound>;
38 using ID = unsigned;
39public:
44 void Play();
49 void Stop();
54 void SetLoop(bool loop);
55
61 void SetVolume(float volume);
66 bool IsPlaying();
67
69
70private:
71 Sound();
72 ID _source;
73 bool _deleteWaitStop;// 删除时,等待播完
74};
76
77class AudioImp;
79class Audio
80{
81 friend class System;
82public:
83 Audio(bool open);
88 void Init(std::string_view path_name)
89 {
90 json j = Json::ParseFile(path_name);
91 if (j.is_null())
92 return;
93
94 std::string path;
95 if (Json::Get(j, "path", path))
96 SetPath(path);
97
98 Json::Get(j, "whitelist", _vecWhitelist);
99 }
100
104 bool IsOpen() { return _bOpen; }
109 void SetPath(std::string_view path)
110 {
111 _bSetPath = true;
112 _strPath = path;
113 }
114
119 void SetWhitelist(const std::vector<std::string>& vec_load)
120 {
121 _vecWhitelist = vec_load;
122 }
123
128 void Load(std::string_view name, std::string_view path_name);
129
133 std::vector<std::string> GetAllSound();
134
140 gcSound Create(std::string_view name);
141
143 {
144 return get_gc<Sound>();
145 }
146
148 void _load();
149
151
153private:
154 AudioImp* _imp;
155 bool _bSetPath;
156 bool _bOpen; // 初始化前手动关闭,或声音设备不存在时设置
157 std::string _strPath;
158 std::vector<std::string> _vecWhitelist;
159 // name -> buffer下标
160 std::unordered_map<std::string, size_t> _mapName2Buffer;
161 // 删除列表(延迟删除)
162 std::list<Sound::ID> _listDelete;
163};
164
165extern Audio* g_audio;
166
173inline gcSound snd_play(std::string_view name, size_t group_id = 0, float volume = 1.0f)
174{
175 gcSound voc = g_audio->Create(name);
176 if (!voc)
177 {
178 log_err("音效创建失败:{}", name);
179 return g_audio->CreateDefault();
180 }
181
182 voc->SetVolume(volume);
183 voc->Play();
184 return voc;
185}
186
187}
188#else
189#include <string_view>
190namespace dl
191{
192 inline void snd_play(std::string_view name, size_t group_id = 0, float volume = 1.0f)
193 {
194 }
195}
196
197#endif
音频主控
void SetWhitelist(const std::vector< std::string > &vec_load)
设置加载白名单
void Init(std::string_view path_name)
初始化配置
std::vector< std::string > GetAllSound()
返回所有加载的音效名
void _load()
由system调用
gcSound CreateDefault()
void SetPath(std::string_view path)
设置资源路径(不设置则不会加载)
gcSound Create(std::string_view name)
创建一个声音
Audio(bool open)
bool IsOpen()
是否使用
void _delete_sound(Sound *snd)
void Load(std::string_view name, std::string_view path_name)
加载一个音效
friend class System
引用计数对象,通过get_gc函数创建
内存池数据
音频实例
friend class Audio
void SetLoop(bool loop)
设置循环
void Play()
播放
bool IsPlaying()
查询是否正在播放
void SetVolume(float volume)
设置音量
void Stop()
停止
json格式相关
nlohmann::json json
#define log_err(...)
基于引用计数的内存池
json ParseFile(std::string_view path_name)
从 文件 构建json对象
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
Audio * g_audio
auto get_gc(Args &&...args)
定义一个GC变量(引用计数内存池)
constexpr size_t NUM_MAX_FILE
可加载音频文件上限
gcSound snd_play(std::string_view name, size_t group_id=0, float volume=1.0f)
播放一个音效
GC< Sound > gcSound