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
|
/*
audio/buffers.h
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <string.h>
#include "audio/buffers.h"
#include "audio/pcm.h"
#include "audio/wav.h"
#include "sys/sys.h"
namespace audio {
std::map<std::string, size_t> Buffers::registry;
size_t Buffers::index;
ALuint Buffers::buffers[MAXBUFFERS];
void Buffers::init()
{
int error;
clear();
alGenBuffers(MAXBUFFERS, buffers);
if ((error = alGetError()) != AL_NO_ERROR) {
con_warn << "Error " << std::hex << error << " initializing OpenAL buffers!" << std::endl;
return;
}
}
void Buffers::shutdown()
{
alDeleteBuffers(MAXBUFFERS, buffers);
clear();
}
void Buffers::clear()
{
registry.clear();
memset(buffers,0, sizeof(buffers));
index = 0;
}
size_t Buffers::load(std::string name)
{
// check if it is already loaded
iterator it = registry.find(name);
if (it != registry.end())
return (*it).second;
// load wav file
PCM *pcm = Wav::load(name);
if (!pcm) {
registry[name] = 0;
return 0;
}
if (index == MAXBUFFERS) {
con_error << "Buffer limit " << MAXBUFFERS << " exceeded!" << std::endl;
delete pcm;
registry[name] = 0;
return 0;
}
ALenum format = 0;
if (pcm->bitspersample() == 16) {
if (pcm->channels() == 1) {
format = AL_FORMAT_MONO16;
} else if (pcm->channels() == 2) {
format = AL_FORMAT_STEREO16;
};
} else if (pcm->bitspersample() == 8) {
if (pcm->channels() == 1) {
format = AL_FORMAT_MONO8;
} else if (pcm->channels() == 2) {
format = AL_FORMAT_STEREO8;
};
}
size_t id = index;
alBufferData(buffers[id], format, pcm->data(), pcm->size(), pcm->samplerate());
if (alGetError()!= AL_NO_ERROR) {
con_warn << "Error loading PCM data " << name << std::endl;
}
registry[name] = id;
index++;
delete pcm;
return id;
}
size_t Buffers::find(std::string name)
{
size_t id = 0;
iterator it = registry.find(name);
if (it != registry.end())
id = (*it).second;
return id;
}
void Buffers::bind(ALuint source, size_t id)
{
int error;
alSourcei(source, AL_BUFFER, buffers[id]);
if ((error = alGetError()) != AL_NO_ERROR) {
con_warn << "Error " << std::hex << error << " binding buffer " << buffers[id] << " to source " << source << std::endl;
}
}
void Buffers::bind(ALuint source, std::string name)
{
bind(source, find(name));
}
}
|