/
soundmanager.cpp
156 lines (134 loc) · 3.25 KB
/
soundmanager.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
#include "soundmanager.h"
ALboolean SoundManager::loadALData()
{
// Variables to load into.
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;
// Load wav data into a buffer.
alGenBuffers(1, &Buffer);
if(alGetError() != AL_NO_ERROR)
return AL_FALSE;
alutLoadWAVFile((ALbyte*)file.c_str(), &format, &data, &size, &freq, &loop);
alBufferData(Buffer, format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
// Bind the buffer with the source.
alGenSources(1, &Source);
if(alGetError() != AL_NO_ERROR)
{
std::cerr<<"Error loading data."<<std::endl;
return AL_FALSE;
}
alSourcei (Source, AL_BUFFER, Buffer );
alSourcef (Source, AL_PITCH, 1.0 );
alSourcef (Source, AL_GAIN, 2.0 );
alSourcefv(Source, AL_POSITION, SourcePos);
alSourcefv(Source, AL_VELOCITY, SourceVel);
alSourcei (Source, AL_LOOPING, loop );
// Do another error check and return.
if(alGetError() == AL_NO_ERROR)
{
std::cerr<<"Successfully loaded data."<<std::endl;
return AL_TRUE;
}
std::cerr<<"Error loading data."<<std::endl;
return AL_FALSE;
}
/*
* void SetListenerValues()
*
* We already defined certain values for the Listener, but we need
* to tell OpenAL to use that data. This function does just that.
*/
void SoundManager::setListenerValues()
{
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
}
/*
* void KillALData()
*
* We have allocated memory for our buffers and sources which needs
* to be returned to the system. This function frees that memory.
*/
void SoundManager::killALData()
{
initialized = false;
alDeleteBuffers(1, &Buffer);
alDeleteSources(1, &Source);
}
SoundManager::SoundManager():file(""),playing(false),initialized(0)
{
SourcePos[0] = 0.0;
SourcePos[1] = 0.0;
SourcePos[2] = 0.0;
// Velocity of the source sound.
SourceVel[0] = 0.0;
SourceVel[1] = 0.0;
SourceVel[2] = 0.0;
// Position of the Listener.
ListenerPos[0] = 0.0;
ListenerPos[1] = 0.0;
ListenerPos[2] = 0.0;
// Velocity of the Listener.
ListenerVel[0] = 0.0;
ListenerVel[1] = 0.0;
ListenerVel[2] = 0.0;
// Orientation of the Listener. (first 3 elements are "at", second 3 are "up")
// Also note that these should be units of '1'.
ListenerOri[0] = 0.0;
ListenerOri[1] = 0.0;
ListenerOri[2] = -1.0;
ListenerOri[3] = 0.0;
ListenerOri[4] = 1.0;
ListenerOri[5] = 0.0;
}
bool SoundManager::initialize(std::string fn)
{
if(file == fn)
{
std::cerr<<"Error already initialized this file"<<std::endl;
return false;
}
if(initialized)
{
killALData();
}
file = fn;
// Initialize OpenAL and clear the error bit.
alutInit(NULL, 0);
alGetError();
// Load the wav data.
if(loadALData() == AL_FALSE)
{
return false;
}
setListenerValues();
initialized = true;
return true;
}
bool SoundManager::play()
{
if(!playing)
{
alSourcePlay(Source);
playing = true;
}
}
bool SoundManager::pause()
{
alSourcePause(Source);
playing = false;
}
bool SoundManager::stop()
{
alSourceStop(Source);
playing = false;
}
void SoundManager::setLooping(bool loop)
{
alSourcei (Source, AL_LOOPING, loop);
}