-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cpp
193 lines (169 loc) · 4.38 KB
/
Sound.cpp
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
185
186
187
188
189
190
191
192
193
#include <apollo/Sound/Sound.h>
#include <AL/alure.h>
#include <map>
#include <apollo/Utilities/ResourceManager.h>
#include <assert.h>
#include <apollo/Logging.h>
namespace Sound
{
const int CHUNK_LENGTH = 16386;
typedef std::map<std::string, ALuint> BufferMap;
BufferMap sounds;
int soundSourceCount;
ALuint* soundSources;
ALuint musicSource;
ALuint musicBufs[2];
alureStream* currentMusicStream = NULL;
std::string currentMusicName = "";
int soundSourceIndex = 0;
void Quit()
{
alureUpdateInterval(0);
StopMusic();
alureSleep(0.1f);
alureShutdownDevice();
}
ALubyte* GetSoundData(const std::string& path, size_t& length)
{
SDL_RWops* rwops = ResourceManager::OpenFile(path + ".aiff");
if (!rwops)
rwops = ResourceManager::OpenFile(path + ".ogg");
if (!rwops)
{
length = 0;
return NULL;
}
return (ALubyte*)ResourceManager::ReadFull(&length, rwops, 1);
}
static ALuint GetSound(const std::string& name)
{
BufferMap::iterator iter = sounds.find(name);
if (iter != sounds.end())
return iter->second;
size_t length;
ALubyte* data = GetSoundData("Sounds/" + name, length);
ALuint buf = alureCreateBufferFromMemory(data, length);
free(data);
sounds.insert(std::make_pair(name, buf));
return buf;
}
void Init(int frequency, int resolution, int sources)
{
(void)resolution;
--sources; // one spare source for music
ALCint attribs[] = {
ALC_FREQUENCY, frequency,
/*ALC_MONO_SOURCES, sources,
ALC_STEREO_SOURCES, 1,*/
0
};
alureInitDevice(NULL, attribs);
soundSources = new ALuint[sources];
alGenSources(sources, soundSources);
alGenSources(1, &musicSource);
//alGenBuffers(2, musicBufs);
soundSourceCount = sources;
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
alSpeedOfSound(1400.0);
alDopplerFactor(0.7);
alureUpdateInterval(0.03333f);
}
static ALuint GetFreeSource()
{
int idx = soundSourceIndex++;
soundSourceIndex = soundSourceIndex % soundSourceCount;
return soundSources[idx];
}
void Preload(const std::string& name)
{
GetSound(name);
}
void PlaySound(const std::string& name, float gain)
{
const static ALfloat fz[] = { 0.0f, 0.0f, 0.0f };
ALuint buf = GetSound(name);
ALuint source = GetFreeSource();
LOG("Sound", LOG_NOTICE, "playing sound %s on channel %d", name.c_str(), source);
alSourcei(source, AL_BUFFER, buf);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef(source, AL_GAIN, gain);
alSourcefv(source, AL_POSITION, fz);
alSourcefv(source, AL_VELOCITY, fz);
alSourcePlay(source);
}
void PlaySoundPositional(const std::string& name, vec2 pos, vec2 vel, float gain)
{
ALfloat fpos[] = {pos.X(), pos.Y(), 0.0f};
ALfloat fvel[] = {vel.X(), vel.Y(), 0.0f};
ALuint buf = GetSound(name);
ALuint source = GetFreeSource();
alSourcei(source, AL_BUFFER, buf);
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcefv(source, AL_POSITION, fpos);
alSourcefv(source, AL_VELOCITY, fvel);
alSourcef(source, AL_REFERENCE_DISTANCE, 50.0);
alSourcef(source, AL_GAIN, gain);
alSourcePlay(source);
}
static ALfloat forientation[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
void SetListener(vec2 pos, vec2 vel)
{
ALfloat fpos[] = {pos.X(), pos.Y(), 0.0f};
ALfloat fvel[] = {vel.X(), vel.Y(), 0.0f};
alListenerfv(AL_POSITION, fpos);
alListenerfv(AL_VELOCITY, fvel);
if (vel.ModulusSquared() > 0.2f)
{
forientation[0] = fvel[0];
forientation[1] = fvel[1];
}
alListenerfv(AL_ORIENTATION, forientation);
}
static void MusicEndCallback(void* ud, ALuint source)
{
alureDestroyStream(currentMusicStream, 2, musicBufs);
currentMusicStream = NULL;
currentMusicName = "";
free(ud);
}
void PlayMusic(const std::string& name)
{
if (currentMusicStream)
alureStopSource(musicSource, AL_TRUE);
size_t length;
ALubyte* data = GetSoundData("Music/" + name, length);
currentMusicName = name;
currentMusicStream = alureCreateStreamFromStaticMemory(data, length, CHUNK_LENGTH, 2, musicBufs);
alurePlaySourceStream(musicSource, currentMusicStream, 2, 1, MusicEndCallback, data);
LOG("Sound", LOG_MESSAGE, "Music: %s", name.c_str());
if (!data)
LOG("Sound", LOG_WARNING, "failed to find music");
}
void StopMusic()
{
if (!currentMusicStream)
return;
alureStopSource(musicSource, AL_TRUE);
}
std::string MusicName()
{
return "";
}
float MusicVolume()
{
ALfloat rv;
alGetSourcefv(musicSource, AL_GAIN, &rv);
return rv;
}
void SetMusicVolume(float mvol)
{
alSourcef(musicSource, AL_GAIN, mvol);
}
float SoundVolume()
{
return 1.0f;
}
void SetSoundVolume(float mvol)
{
}
}