Files
dl_old/dl/system/dl_audio.h
T
2024-01-03 15:16:54 +08:00

143 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file dl_audio.h
* @brief 音频系统
*
*
* @version 1.0
* @author lveyou
* @date 22-08-26
*
* @note 基于OpenAL
* @note 可播放wav格式文件
* @note 可播放ogg格式文件,使用libogg、libvorbis(libvorbisfile)两个库
* @note 此处实现播放的基本操作,渐变切换音乐功能在 dl_music实现
*
* @note 命名规范:Audio代表整个音频系统、Sound代表一个音效文件
* @note 独立与factory
*/
#pragma once
#include <unordered_map>
#include "memory/dl_memory_pool.h"
namespace dl
{
//! 可加载音频文件上限
constexpr size_t NUM_MAX_FILE = 256;
class SoundImp;
//! 音频实例
class Sound
{
friend class Audio;
friend class PoolList<Sound>;
public:
/**
* @brief 播放
* @note 调用后马上从开头播放
*/
void Play();
/**
* @brief 停止
* @note 调用后马上停止
*/
void Stop();
/**
* @brief 设置循环
* @note 调用后马上从开头播放
*/
void SetLoop(bool loop);
/**
* @brief 设置音量
* @param[in] volume 音量 1.0为默认
* @note 每变化2倍数值,相当于6dB
*/
void SetVolume(float volume);
/**
* @brief 查询是否正在播放
* @note 调用后检测是否播放
*/
bool IsPlaying();
~Sound();
private:
Sound();
SoundImp* _imp;
};
using gcSound = GC<Sound>;
class AudioImp;
//! 音频主控
class Audio
{
friend class System;
public:
Audio();
/**
* @brief 设置资源路径(不设置则不会加载)
* @param[in] path 路径
*/
void SetPath(std::string_view path)
{
_bSetPath = true;
_strPath = path;
}
/**
* @brief 加载一个音效
* @param[in] path_name 路径
* @param[in] name 动命名
*/
void Load(std::string_view name, std::string_view path_name);
/**
* @brief 创建一个声音
* @param[in] name 资源名
* @note 不会返回空指针
*/
gcSound Create(std::string_view name);
gcSound CreateDefault()
{
return get_gc<Sound>();
}
//! 由system调用
void _load();
~Audio();
private:
AudioImp* _imp;
bool _bSetPath;
std::string _strPath;
//name -> buffer下标
std::unordered_map<std::string, size_t> _mapName2Buffer;
};
extern Audio* g_audio;
/**
* @brief 播放一个音效
* @param[in] name 名字
* @param[in] group_id 分组id
* @param[in] volume 音量
*/
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 g_audio->CreateDefault();
}
voc->SetVolume(volume);
voc->Play();
return voc;
}
}