-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsounds.cpp
132 lines (115 loc) · 3.88 KB
/
sounds.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
#include "sounds.hpp"
SoundDevice::SoundDevice() {
p_ALCDevice = alcOpenDevice(nullptr);
if (!p_ALCDevice) {
throw("Failed to get sound device.");
}
p_ALCConext = alcCreateContext(p_ALCDevice, nullptr);
if (!p_ALCConext) {
throw("Failed to create sound context.");
}
if (!alcMakeContextCurrent(p_ALCConext)) {
throw("Could not make context current.");
}
const ALCchar* name = nullptr;
if (alcIsExtensionPresent(p_ALCDevice, "ALC_ENUMERATE_ALL_EXT"))
name = alcGetString(p_ALCDevice, ALC_ALL_DEVICES_SPECIFIER);
if (!name || alcGetError(p_ALCDevice) != ALC_NO_ERROR)
name = alcGetString(p_ALCDevice, ALC_DEVICE_SPECIFIER);
printf("Opened [%s]\n", name);
}
ALuint SoundBuffer::addSoundEffect(const char* filename) {
ALenum err, format;
ALuint buffer;
SNDFILE* sndfile;
SF_INFO sfinfo;
short* membuf;
sf_count_t num_frames;
ALsizei num_bytes;
/* Open the audio file and check that it's usable. */
sndfile = sf_open(filename, SFM_READ, &sfinfo);
if (!sndfile)
{
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
return 0;
}
if (sfinfo.frames < 1 || sfinfo.frames >(sf_count_t)(INT_MAX / sizeof(short)) / sfinfo.channels)
{
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
sf_close(sndfile);
return 0;
}
/* Get the sound format, and figure out the OpenAL format */
if (sfinfo.channels == 1)
format = AL_FORMAT_MONO16;
else if (sfinfo.channels == 2)
format = AL_FORMAT_STEREO16;
else
{
fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
sf_close(sndfile);
return 0;
}
/* Decode the whole audio file to a buffer. */
membuf = (short*)malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
if (num_frames < 1)
{
free(membuf);
sf_close(sndfile);
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
return 0;
}
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
/* Buffer the audio data into a new buffer object, then free the data and
* close the file.
*/
buffer = 0;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
free(membuf);
sf_close(sndfile);
/* Check if an error occured, and clean up if so. */
err = alGetError();
if (err != AL_NO_ERROR)
{
fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
if (buffer && alIsBuffer(buffer))
alDeleteBuffers(1, &buffer);
return 0;
}
return buffer;
}
SoundSource::SoundSource() {
alGenSources(1, &p_Source);
alSourcef(p_Source, AL_PITCH, p_Pitch);
alSourcef(p_Source, AL_GAIN, p_Gain);
alSource3f(p_Source, AL_POSITION, p_Position[0], p_Position[1], p_Position[2]);
alSource3f(p_Source, AL_VELOCITY, p_Velocity[0], p_Velocity[1], p_Velocity[2]);
alSourcei(p_Source, AL_LOOPING, p_LoopSound);
alSourcei(p_Source, AL_BUFFER, p_Buffer);
}
void SoundSource::LoadSoundEffects() {
ALuint se;
se = SoundBuffer::get()->addSoundEffect("climb/mixkit-game-ball-tap-2073.wav");
sounds[CyclimbSound::Tap] = se;
se = SoundBuffer::get()->addSoundEffect("climb/mixkit-arrow-whoosh-1491.wav");
sounds[CyclimbSound::Whoosh] = se;
se = SoundBuffer::get()->addSoundEffect("climb/mixkit-cartoon-toy-whistle-616.wav");
sounds[CyclimbSound::Whistle] = se;
se = SoundBuffer::get()->addSoundEffect("climb/mixkit-small-hit-in-a-game-2072.wav");
sounds[CyclimbSound::SmallHit] = se;
for (const auto& x : sounds) {
printf("%d = %d\n", int(x.first), int(x.second));
}
}
SoundSource* g_sound_source;
void InitSounds() {
SoundDevice* sd = SoundDevice::get();
printf("Got sound device.\n");
g_sound_source = new SoundSource();
g_sound_source->LoadSoundEffects();
}
void MyPlaySound(CyclimbSound s) {
g_sound_source->Play(s);
}