Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: b5dc8e3a9efb5b0828b799b1a6ae68fea17a7848 (plain)
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
/*
   audio/buffers.cc
   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/audio.h"
#include "audio/buffers.h"
#include "audio/pcm.h"
#include "audio/vorbisfile.h"
#include "audio/wavfile.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()
{
	clear();

	alGenBuffers(MAXBUFFERS, buffers);

	int error;
	if ((error = alGetError()) != AL_NO_ERROR) {
		con_warn << "Error " << error << " initializing OpenAL buffers!" << std::endl;
		return;
	}
}

void Buffers::shutdown()
{
	alDeleteBuffers(MAXBUFFERS, buffers);

	int error;
	if ((error = alGetError()) != AL_NO_ERROR) {
		con_warn << "Error " << error << " clearing OpenAL buffers!" << std::endl;
		return;
	}

	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;

	// check if the audio subsystem has been initialized
	if (!initialized()) {
		registry[name] = 0;
		return 0;
	}
	
	std::string filename;
	
	PCM *pcm = 0;
	
	// try the .oga version
	if (!pcm) {
		filename.assign("sounds/");
		filename.append(name);
		filename.append(".oga");
		pcm = Vorbis::load(filename);
	}

	// try the .ogg version
	if (!pcm) {
		filename.assign("sounds/");
		filename.append(name);
		filename.append(".ogg");
		pcm = Vorbis::load(filename);
	}

	// try the .wav version
	if (!pcm) {
		filename.assign("sounds/");
		filename.append(name);
		filename.append(".wav");
		pcm = Wav::load(filename);
	}

	if (!pcm) {
		con_warn << "Could not open sound " << name << std::endl;
		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 " << error << " binding  buffer " << buffers[id] << " to source " << source << std::endl;
	}
}

void Buffers::bind(ALuint source, std::string name)
{
	bind(source, find(name));
}

}