Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: d7f85d92f11b092b6d93fcd56430160e215a1abd (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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
   audio/audio.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "audio/audio.h"
#include "audio/buffers.h"
#include "audio/pcm.h"
#include "audio/sources.h"
#include "sys/sys.h"

namespace audio
{

ALCdevice 			*audio_device = 0;
ALCcontext			*audio_context = 0;
bool				audio_initialized = false;

bool initialized()
{
	return audio_initialized;
}

void init()
{
	con_print << "^BInitializing audio..." << std::endl;

	audio_initialized = false;
	
	// open the default audio device
	audio_device = alcOpenDevice(0);

	if (!audio_device) {
		con_warn << "Could not initialize audio!" << std::endl;
		return;
	}

	// create the audio context
	audio_context = alcCreateContext(audio_device , 0);

	if (!audio_context) {
		alcCloseDevice(audio_device);
		audio_device = 0;
		con_warn << "Could not create audio context!" << std::endl;
		return;
	}

	alcMakeContextCurrent(audio_context);

	// clear errors
	alGetError();

	Buffers::init();

	Sources::init();

	//con_debug << "  audio device  ^B" << alcGetString(audio_device, ALC_DEFAULT_DEVICE_SPECIFIER) << std::endl;

	// the "no sound" sound
	load("ui/nosnd");

	// console sound
	load("ui/console");
	
	audio_initialized = true;
}

void reset()
{
	if (audio_initialized) {
		con_print << "^BResetting audio..." << std::endl;

		Sources::shutdown();
		Buffers::shutdown();
	}
	
	if (!audio_initialized) {
		init();
	} else {
		Buffers::init();
		Sources::init();

		load("ui/nosnd");
		load("ui/console");
	}
}

void load(const char *name)
{
	Buffers::load(std::string(name));
}

void shutdown()
{
	con_print << "^BShutting down audio..." << std::endl;

	if (audio_initialized) {
		Sources::shutdown();
		Buffers::shutdown();
	}

	if (audio_context) {
		alcMakeContextCurrent(0);
		alcDestroyContext(audio_context);
		audio_context = 0;
	}

	if (audio_device) {
		alcCloseDevice(audio_device);
		audio_device = 0;
	}
	
	audio_initialized = false;
}

size_t play(const char *name)
{
	if (!audio_context)
		return 0;

	size_t buffer_index = Buffers::load(std::string(name));

	for (size_t i = 0; i < MAXUISOURCES; i++) {
		ALint srcstate = 0;
		ALuint source = Sources::source(i);
		alGetSourcei(source , AL_SOURCE_STATE , &srcstate);
		if (srcstate != AL_PLAYING) {
			Buffers::bind(source, buffer_index);
			alSourcef(source, AL_PITCH, 1.0);
			alSourcef(source, AL_GAIN, 1.0);
			alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
			ALfloat location[] = {0.0f, 0.0f, 0.0f};
			alSourcefv(source, AL_POSITION, location);
			alSourceRewind(source);
			alSourcePlay(source);
			return buffer_index;
		}
	}

	return 0;
}

size_t play(size_t source_index, size_t buffer_index, float pitch, float gain)
{
	if (!audio_context || !buffer_index)
		return 0;

	ALuint source = Sources::source(source_index);
	ALint srcstate = 0;
	alGetSourcei(source , AL_SOURCE_STATE , &srcstate);
	if (srcstate == AL_PLAYING) {
		alSourceStop(source);
	}
	Buffers::bind(source, buffer_index);
	alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
	alSourcef(source, AL_PITCH, pitch);
	alSourcef(source, AL_GAIN, gain);

	alSourcei(source, AL_LOOPING,  AL_FALSE);
	alSourceRewind(source);
	alSourcePlay(source);

	return buffer_index;
}

size_t loop(size_t source_index, size_t buffer_index, float pitch, float gain)
{
	if (!audio_context || !buffer_index)
		return 0;

	ALuint source = Sources::source(source_index);
	ALint srcstate = 0;
	alGetSourcei(source , AL_SOURCE_STATE , &srcstate);
	if (srcstate == AL_PLAYING) {
		alSourceStop(source);
	}

	Buffers::bind(source, buffer_index);

	//alSourcef(source, AL_REFERENCE_DISTANCE, 2.0f); // might be the cause of cracks in the sound
	alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
	alSourcef(source, AL_PITCH, pitch);
	alSourcef(source, AL_GAIN, gain);

	alSourcei(source, AL_LOOPING,  AL_TRUE);
	alSourceRewind(source);
	alSourcePlay(source);

	return buffer_index;
}

size_t loop(size_t source_index, const char *name, float pitch, float gain)
{
	if (!audio_context)
		return 0;

	size_t buffer_index = Buffers::load(std::string(name));
	if (buffer_index)
		loop(source_index, buffer_index, pitch, gain);

	return buffer_index;
}

void update_listener(math::Vector3f const &location, math::Axis const &axis, math::Vector3f const & velocity, float gain)
{
	if (!audio_context)
		return;

	alListenerfv(AL_POSITION, location.ptr());

	ALfloat orientation[6];
	for (size_t i = 0; i < 3; i++) {
		orientation[i] = axis.forward()[i];
		orientation[i+3] = axis.up()[i];
	}
	alListenerfv(AL_ORIENTATION, orientation);
	alListenerf(AL_GAIN, gain);
	alListenerfv(AL_VELOCITY, velocity.ptr());
}


void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch, float gain)
{
	if (!audio_context)
		return;

	ALuint source = Sources::source(source_index);
	alSourcefv(source, AL_POSITION, location.ptr());
	alSourcefv(source, AL_VELOCITY, velocity.ptr());
	alSourcef(source, AL_PITCH, pitch);
	alSourcef(source, AL_GAIN, gain);
}

bool is_playing(const size_t source_index)
{
	if (!audio_context)
		return false;

	ALuint source = Sources::source(source_index);
	ALint srcstate = 0;
	alGetSourcei(source , AL_SOURCE_STATE , &srcstate);
	
	return (srcstate == AL_PLAYING);		
}

}