This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sound.d
182 lines (154 loc) · 4.1 KB
/
sound.d
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
module sound;
import sdl.all;
import misc.singleton;
import math.common;
import misc.logger;
import bass.all;
import res.settings;
enum FX
{
AVALE1, AVALE2, AVALE3, EXPLODE, MENU, MENU2, PLOP, DIE
}
final class SoundManager
{
private
{
const int MUSIC_NUM = 3;
BASSSystem m_system;
BASSStream[MUSIC_NUM] m_music;
BASSSample[FX.max + 1] m_fxs;
int m_index = -1;
static const char[][MUSIC_NUM] musicFiles =
[
"data/music/maf_-_inspace.mp3",
"data/music/maf_-_lego_gland.mp3",
"data/music/maf_-_81.mp3"
];
static const char[][FX.max + 1] fxFiles =
[
"data/fx/avale1.wav",
"data/fx/avale2.wav",
"data/fx/avale3.wav",
"data/fx/explode.wav",
"data/fx/menu.wav",
"data/fx/menu2.wav",
"data/fx/plop.wav",
"data/fx/die.wav"
];
static const char[][MUSIC_NUM] musicNames =
[
"maF - inspace",
"maF - lego gland",
"maF - 81"
];
static const float[MUSIC_NUM] musicVolumes =
[
//0.3f,
0.5f,
0.5f,
0.5f,
];
}
public
{
this(HWND hwnd)
{
info(">SoundManager.this");
try
{
m_system = new BASSSystem(hwnd);
} catch(BASSError e)
{
warn(e.msg);
info("Sound is now disabled.");
m_system = null; // no sound
info("<SoundManager.this");
return;
}
info("*loading musics");
for (int i = 0; i < MUSIC_NUM; ++i)
{
try
{
m_music[i] = new BASSStream(m_system, musicFiles[i]);
}
catch(BASSError e)
{
warn(e.msg);
info("Sound will be disabled.");
m_system = null; // no sound for now
info("<SoundManager.this");
return;
}
}
info("*loading fx sounds");
for (FX f = FX.min; f <= FX.max; ++f)
{
try
{
m_fxs[f] = new BASSSample(m_system, fxFiles[f]);
} catch(BASSError e)
{
warn(e.msg);
info("Sound will be disabled.");
m_system = null; // no sound for now
info("<SoundManager.this");
return;
}
}
setMusicVolume(musicVolume);
info("<SoundManager.this");
}
~this()
{
}
void playMusic(int index)
{
try
{
m_music[index].play(true);
info(format("Now playing: %s", musicNames[index]));
}
catch(BASSError e)
{
warn(e.msg);
}
}
}
public
{
void nextMusicIfNeeded()
{
bool needNewMusic = (m_index == -1) || (!m_music[m_index].isPlaying);
if (needNewMusic)
{
nextMusic();
}
}
void nextMusic()
{
m_index++;
if (m_index >= MUSIC_NUM) m_index = 0;
playMusic(m_index);
}
void previousMusic()
{
m_index--;
if (m_index < 0) m_index = MUSIC_NUM - 1;
playMusic(m_index);
}
void setMusicVolume(int volume) // between 0 and 1
{
float volumeCorrected = sqr(volume * 0.1f);
for (int i = 0; i < MUSIC_NUM; ++i)
{
m_music[i].setVolume(volumeCorrected);
}
}
void playSound(FX fx, float volume = 1.f)
{
float correctedVolume = sqr(volume * soundFXVolume * 0.09f);
m_fxs[fx].play(correctedVolume);
}
}
}