forked from scarsty/kys-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudio.cpp
More file actions
184 lines (173 loc) · 4.38 KB
/
Audio.cpp
File metadata and controls
184 lines (173 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Audio.h"
#include "File.h"
#include "convert.h"
Audio::Audio()
{
#ifndef USE_SDL_MIXER_AUDIO
if (!BASS_Init(-1, 22050, BASS_DEVICE_3D, 0, nullptr))
{
printf("Init Bass fault!\n");
}
#else
Mix_Init(MIX_INIT_MP3);
if (Mix_OpenAudio(22500, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
printf("Mix_OpenAudio: %s\n", Mix_GetError());
}
#endif
init();
}
Audio::~Audio()
{
#ifndef USE_SDL_MIXER_AUDIO
if (mid_sound_font_.font)
{
BASS_MIDI_FontFree(mid_sound_font_.font);
}
BASS_Free();
#else
//如使用SDL_Mixer播放音频,则销毁应该在SDL_Quit之前,此处的单例设计无法保证,故暂时不处理
/*
for (auto m : music_)
{
Mix_FreeMusic(m);
}
for (auto a : asound_)
{
Mix_FreeChunk(a);
}
for (auto e : esound_)
{
Mix_FreeChunk(e);
}
Mix_Quit();
*/
#endif
}
void Audio::init()
{
std::string music_path;
std::string asound_path;
std::string esound_path;
#ifndef USE_SDL_MIXER_AUDIO
auto flag = BASS_SAMPLE_3D | BASS_SAMPLE_MONO;
mid_sound_font_.font = BASS_MIDI_FontInit("../game/music/mid.sf2", 0);
mid_sound_font_.preset = -1;
mid_sound_font_.bank = 0;
BASS_MIDI_StreamSetFonts(0, &mid_sound_font_, 1);
#endif
for (int i = 0; i < 100; i++)
{
music_path = convert::formatString("../game/music/%d.mid", i);
if (File::fileExist(music_path))
{
#ifndef USE_SDL_MIXER_AUDIO
auto m = BASS_MIDI_StreamCreateFile(false, music_path.c_str(), 0, 0, 0, 0);
BASS_MIDI_StreamSetFonts(m, &mid_sound_font_, 1);
#else
auto m = Mix_LoadMUS(music_path.c_str());
#endif
music_.push_back(m);
}
else
{
music_path = convert::formatString("../game/music/%d.mp3", i);
#ifndef USE_SDL_MIXER_AUDIO
auto m = BASS_StreamCreateFile(false, music_path.c_str(), 0, 0, flag);
#else
auto m = Mix_LoadMUS(music_path.c_str());
#endif
music_.push_back(m);
}
//int error_t = BASS_ErrorGetCode();
asound_path = convert::formatString("../game/sound/atk%02d.wav", i);
#ifndef USE_SDL_MIXER_AUDIO
auto a = BASS_StreamCreateFile(false, asound_path.c_str(), 0, 0, flag);
#else
auto a = Mix_LoadWAV(asound_path.c_str());
#endif
asound_.push_back(a);
esound_path = convert::formatString("../game/sound/e%02d.wav", i);
#ifndef USE_SDL_MIXER_AUDIO
auto e = BASS_StreamCreateFile(false, esound_path.c_str(), 0, 0, flag);
#else
auto e = Mix_LoadWAV(esound_path.c_str());
#endif
esound_.push_back(e);
}
}
void Audio::playMusic(int num)
{
if (num < 0 || num >= music_.size() || music_[num] == 0)
{
return;
}
stopMusic();
#ifndef USE_SDL_MIXER_AUDIO
BASS_ChannelSetAttribute(music_[num], BASS_ATTRIB_VOL, volume_ / 100.0);
BASS_Apply3D();
BASS_ChannelFlags(music_[num], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP);
BASS_ChannelPlay(music_[num], false);
#else
Mix_VolumeMusic(volume_);
Mix_FadeInMusic(music_[num], -1, 500);
#endif
current_music_ = music_[num];
}
void Audio::playASound(int num)
{
if (num < 0 || num >= asound_.size() || asound_[num] == 0)
{
return;
}
//BASS_ChannelStop(current_sound_);
#ifndef USE_SDL_MIXER_AUDIO
auto ch = BASS_SampleGetChannel(asound_[num], false);
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, volume_ / 100.0);
BASS_ChannelPlay(asound_[num], false);
#else
Mix_Volume(-1, volume_);
Mix_PlayChannel(-1, asound_[num], 0);
#endif
current_sound_ = asound_[num];
}
void Audio::playESound(int num)
{
if (num < 0 || num >= esound_.size() || esound_[num] == 0)
{
return;
}
#ifndef USE_SDL_MIXER_AUDIO
auto ch = BASS_SampleGetChannel(esound_[num], false);
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, volume_ / 100.0);
BASS_ChannelPlay(esound_[num], false);
#else
Mix_Volume(-1, volume_);
Mix_PlayChannel(-1, esound_[num], 0);
#endif
current_sound_ = esound_[num];
}
void Audio::pauseMusic()
{
#ifndef USE_SDL_MIXER_AUDIO
BASS_ChannelPause(current_music_);
#else
Mix_PausedMusic();
#endif
}
void Audio::continueMusic()
{
#ifndef USE_SDL_MIXER_AUDIO
BASS_ChannelPlay(current_music_, false);
#else
Mix_ResumeMusic();
#endif
}
void Audio::stopMusic()
{
#ifndef USE_SDL_MIXER_AUDIO
BASS_ChannelStop(current_music_);
#else
Mix_HaltMusic();
#endif
}