2 Star 0 Fork 0

SadLava/Arrow3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
soundSystem.cpp 8.55 KB
一键复制 编辑 原始数据 按行查看 历史
SadLava 提交于 2022-05-08 17:08 . first update
#include <bass.h>
#include <basscd.h>
#include <bassmidi.h>
#include <bassflac.h>
#include <basswma.h>
/* 音频引擎 */
/* 弃用了旧的CL_Music,直接操作Bass提高效率 */
class soundSystem
{
private:
/* 先是常量 */
#define GBGMNUM 13 + 1
#define MBGMNUM 7 + 1
enum SoundEffects {
shootSE,
blastSE,
pblastSE,
saveSE,
hitSE,
changeSE,
seEnd
};
enum VolumeModes {
SilenceVM, /* 静音 */
NormalVM, /* 正常音量比例(无子弹发射) */
FightVM, /* bgm声音略大se不变 */
StressVM /* 强调模式,bgm与大部分se小声,个别se大声 */
};
/* 加载音乐 */
DWORD OpenFile( string fn ) {
return(BASS_StreamCreateFile( FALSE, fn.c_str(), 0, 0, 0 | BASS_SAMPLE_FX ) );
}
/* 播放 */
inline void Play( DWORD id ) {
BASS_ChannelPlay( id, 1 );
}
/* 设置音量 */
inline void setVolume( DWORD id, float vol ) {
BASS_ChannelSetAttribute( id, BASS_ATTRIB_VOL, vol );
}
/* 停止 */
inline void Stop( DWORD id ) {
BASS_ChannelStop( id );
}
private:
DWORD gbgmList[GBGMNUM]; /* 游戏背景音乐列表 */
DWORD agbgmList[GBGMNUM]; /* 游戏结束背景音乐列表 */
DWORD mbgmList[MBGMNUM]; /* 菜单背景音乐列表 */
DWORD seList[seEnd]; /* 音效列表 */
int gbgmId; /* 当前正在播放的bgm代号 */
int mbgmId; /* 菜单正在播放的bgm代号 */
//音量
float bgmVolume;
float seVolume;
//用于高速执行的缓存
float abgmVolume;
float aseVolume;
int vMode; /* 音量模式 */
/*
* 自动音量调节
* 为了方便,一次仅处理se、mbgm、gbgm 、 agbgm
*/
inline void autoSVolume() {
/* 所有效果的权值,sseW是特殊se(其实就是pBlast) */
float sseW, seW, mbgmW, gbgmW, agbgmW;
/* 初始化 */
sseW = seW = mbgmW = gbgmW = agbgmW = 1;
/* 设置权值 */
switch ( vMode ) {
case SilenceVM: {
seW = mbgmW = gbgmW = agbgmW = 0;
break;
}
case NormalVM: {
/* 直接用初始值 */
break;
}
case FightVM: {
mbgmW = gbgmW = agbgmW = 1.5;
break;
}
case StressVM: {
sseW = 2;
mbgmW = gbgmW = agbgmW = 0.5;
break;
}
}
/* 调节音量 */
setVolume( gbgmList[gbgmId], bgmVolume * gbgmW );
setVolume( agbgmList[gbgmId], bgmVolume * gbgmW );
setVolume( mbgmList[mbgmId], bgmVolume * gbgmW );
for ( int i = 0; i < seEnd; i++ ) {
setVolume( seList[i], seVolume * seW );
}
setVolume( seList[pblastSE], seVolume * sseW );
//智能检测
if(abgmVolume!=bgmVolume || aseVolume!=seVolume) {
api_sleep(300);
}
abgmVolume=bgmVolume;
aseVolume=seVolume;
return;
}
/* 自动设置音量模式 */
inline void autoSVM() {
if ( vMode == SilenceVM )
return;
/* 此处有一定的优先级关系 */
if ( BASS_ChannelIsActive( seList[pblastSE] ) == BASS_ACTIVE_PLAYING ) {
vMode = StressVM;
} else if ( BASS_ChannelIsActive( seList[shootSE] ) == BASS_ACTIVE_PLAYING || BASS_ChannelIsActive( seList[blastSE] ) == BASS_ACTIVE_PLAYING ) {
vMode = FightVM;
} else {
vMode = NormalVM;
}
return;
}
/*
* 自动处理音量
* 即上两个的联动
*/
inline void autoHVolume() {
autoSVM();
autoSVolume();
}
public:
soundSystem() {
/* 初始化Bass */
BASS_Init( -1, 44100, 0, 0, 0 );
BASS_PluginLoad( "bass_ape.dll", 0 );
/* 初始化音量 */
abgmVolume=bgmVolume = 0.4;
aseVolume=seVolume = 0.6;
/* 初始化VM */
vMode = NormalVM;
/* 偷懒 */
string gbgmpath = "sound\\music\\run\\";
string agbgmpath = "sound\\music\\over\\Outro "; /* 严重偷懒行为 */
string mbgmpath = "sound\\music\\menu\\";
string sepath = "sound\\se\\";
/* 加载游戏背景音乐列表 */
gbgmList[0] = OpenFile( gbgmpath + "Deadmau5 Professional Griefers.mp3" );
gbgmList[1] = OpenFile( gbgmpath + "Gemini Fire Inside.mp3" );
gbgmList[2] = OpenFile( gbgmpath + "Krubb Wenkroist Bad Weekend.mp3" );
gbgmList[3] = OpenFile( gbgmpath + "Martin Garrix Animals.mp3" );
gbgmList[4] = OpenFile( gbgmpath + "Monsta Holdin On.mp3" );
gbgmList[5] = OpenFile( gbgmpath + "Mord Fustang Windwaker.mp3" );
gbgmList[6] = OpenFile( gbgmpath + "Mutemath Blood Pressure.mp3" );
gbgmList[7] = OpenFile( gbgmpath + "Queen Of The Stone Age Go With The Flow.mp3" );
gbgmList[8] = OpenFile( gbgmpath + "The Bloody Beetroots Rocksteady.mp3" );
gbgmList[9] = OpenFile( gbgmpath + "The Crystal Method Ove It.mp3" );
gbgmList[10] = OpenFile( gbgmpath + "The Crystal Method Play For Real Edit.mp3" );
gbgmList[11] = OpenFile( gbgmpath + "The Qemists Be Electric.mp3" );
gbgmList[12] = OpenFile( gbgmpath + "Trentemoller Silver Surfer Ghost Rider Go.mp3" );
gbgmList[13] = OpenFile( gbgmpath + "Vitalic Stamina.mp3" );
/* 加载游戏结束背景音乐列表 */
agbgmList[0] = OpenFile( agbgmpath + "Deadmau5 Professional Griefers.mp3" );
agbgmList[1] = OpenFile( agbgmpath + "Gemini Fire Inside.mp3" );
agbgmList[2] = OpenFile( agbgmpath + "Krubb Wenkroist Bad Weekend.mp3" );
agbgmList[3] = OpenFile( agbgmpath + "Martin Garrix Animals.mp3" );
agbgmList[4] = OpenFile( agbgmpath + "Monsta Holdin On.mp3" );
agbgmList[5] = OpenFile( agbgmpath + "Mord Fustang Windwaker.mp3" );
agbgmList[6] = OpenFile( agbgmpath + "Mutemath Blood Pressure.mp3" );
agbgmList[7] = OpenFile( agbgmpath + "Queen Of The Stone Age Go With The Flow.mp3" );
agbgmList[8] = OpenFile( agbgmpath + "The Bloody Beetroots Rocksteady.mp3" );
agbgmList[9] = OpenFile( agbgmpath + "The Crystal Method Ove It.mp3" );
agbgmList[10] = OpenFile( agbgmpath + "The Crystal Method Play For Real Edit.mp3" );
agbgmList[11] = OpenFile( agbgmpath + "The Qemists Be Electric.mp3" );
agbgmList[12] = OpenFile( agbgmpath + "Trentemoller Silver Surfer Ghost Rider Go.mp3" );
agbgmList[13] = OpenFile( agbgmpath + "Vitalic Stamina.mp3" );
/* 加载菜单背景音乐 */
mbgmList[0] = OpenFile( mbgmpath + "Celldweller Pulsar.mp3" );
mbgmList[1] = OpenFile( mbgmpath + "Celldweller Through The Gates.mp3" );
mbgmList[2] = OpenFile( mbgmpath + "Dj Dubai Vodka Aspirin.mp3" );
mbgmList[3] = OpenFile( mbgmpath + "Dj Gontran Burning.mp3" );
mbgmList[4] = OpenFile( mbgmpath + "Dj Gontran Chemistry.mp3" );
mbgmList[5] = OpenFile( mbgmpath + "Dj Gontran Moby Glitch.mp3" );
mbgmList[6] = OpenFile( mbgmpath + "Dj Gontran Outrun.mp3" );
mbgmList[7] = OpenFile( mbgmpath + "Dj Gontran Phantasmagorical.mp3" );
/* 加载音效列表 */
seList[shootSE] = OpenFile( sepath + "bomb.wav" );
seList[blastSE] = OpenFile( sepath + "bomb.wav" );
seList[pblastSE] = OpenFile( sepath + "bomb.wav" );
seList[saveSE] = OpenFile( sepath + "save.mp3" );
seList[hitSE] = OpenFile( sepath + "hit.wav" );
seList[changeSE] = OpenFile( sepath + "change.mp3" );
autoHVolume();
}
~soundSystem() {
BASS_Free();
}
inline void change() {
autoHVolume();
Play( seList[changeSE] );
}
inline void blast() {
autoHVolume();
Play( seList[blastSE] );
}
inline void save() {
autoHVolume();
Play( seList[saveSE] );
}
inline void pBlast() {
autoHVolume();
Play( seList[pblastSE] );
}
inline void shoot() {
autoHVolume();
Play( seList[shootSE] );
}
inline void hit() {
autoHVolume();
Play( seList[hitSE] );
}
inline void playMBgm() {
if ( BASS_ChannelIsActive( agbgmList[gbgmId] ) == BASS_ACTIVE_PLAYING ) {
Stop( agbgmList[gbgmId] );
}
if ( BASS_ChannelIsActive( mbgmList[mbgmId] ) == BASS_ACTIVE_STOPPED ) {
mbgmId = random( MBGMNUM );
autoHVolume();
Play( mbgmList[mbgmId] );
}
}
inline void playGBgm() {
if ( BASS_ChannelIsActive( mbgmList[mbgmId] ) == BASS_ACTIVE_PLAYING ) {
Stop( mbgmList[mbgmId] );
}
if ( BASS_ChannelIsActive( gbgmList[gbgmId] ) == BASS_ACTIVE_STOPPED ) {
gbgmId = random( GBGMNUM );
autoHVolume();
Play( gbgmList[gbgmId] );
}
}
inline void playAGBgm() {
autoHVolume();
if ( BASS_ChannelIsActive( gbgmList[gbgmId] ) == BASS_ACTIVE_PLAYING ) {
Stop( gbgmList[gbgmId] );
}
if ( BASS_ChannelIsActive( agbgmList[gbgmId] ) == BASS_ACTIVE_STOPPED ) {
Play( agbgmList[gbgmId] );
}
}
inline float getBV() {
return bgmVolume;
}
inline float getSV() {
return seVolume;
}
inline void setBV(float bv) {
bgmVolume=bv;
autoHVolume();
}
inline void setSV(float sv) {
seVolume=sv;
autoHVolume();
}
#undef GBGMNUM
#undef MBGMNUM
} MSYS;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sadlava/arrow3.git
git@gitee.com:sadlava/arrow3.git
sadlava
arrow3
Arrow3
master

搜索帮助