3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
4 OpenAL support based on work by:
5 Copyright (C) 2011 Sebastian 'Bahamada' Rühl
6 Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
7 Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; ifnot, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "sound_openal.h"
30 #elif defined(__APPLE__)
31 #include <OpenAL/al.h>
32 #include <OpenAL/alc.h>
33 #include <OpenAL/alext.h>
39 #include <vorbis/vorbisfile.h>
43 #include "utility.h" // myrand()
45 #define BUFFER_SIZE 30000
47 static const char *alcErrorString(ALCenum err)
52 case ALC_INVALID_DEVICE:
53 return "invalid device";
54 case ALC_INVALID_CONTEXT:
55 return "invalid context";
56 case ALC_INVALID_ENUM:
57 return "invalid enum";
58 case ALC_INVALID_VALUE:
59 return "invalid value";
60 case ALC_OUT_OF_MEMORY:
61 return "out of memory";
63 return "<unknown OpenAL error>";
67 static const char *alErrorString(ALenum err)
73 return "invalid name";
75 return "invalid enum";
76 case AL_INVALID_VALUE:
77 return "invalid value";
78 case AL_INVALID_OPERATION:
79 return "invalid operation";
80 case AL_OUT_OF_MEMORY:
81 return "out of memory";
83 return "<unknown OpenAL error>";
87 static ALenum warn_if_error(ALenum err, const char *desc)
89 if(err == AL_NO_ERROR)
91 errorstream<<"WARNING: "<<desc<<": "<<alErrorString(err)<<std::endl;
95 void f3_set(ALfloat *f3, v3f v)
107 std::vector<char> buffer;
110 SoundBuffer* loadOggFile(const std::string &filepath)
112 int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
115 char array[BUFFER_SIZE]; // Local fixed size array
117 OggVorbis_File oggFile;
119 // Try opening the given file
120 if(ov_fopen(filepath.c_str(), &oggFile) != 0)
122 infostream<<"Audio: Error opening "<<filepath<<" for decoding"<<std::endl;
126 SoundBuffer *snd = new SoundBuffer;
128 // Get some information about the OGG file
129 pInfo = ov_info(&oggFile, -1);
131 // Check the number of channels... always use 16-bit samples
132 if(pInfo->channels == 1)
133 snd->format = AL_FORMAT_MONO16;
135 snd->format = AL_FORMAT_STEREO16;
137 // The frequency of the sampling rate
138 snd->freq = pInfo->rate;
140 // Keep reading until all is read
143 // Read up to a buffer's worth of decoded sound data
144 bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
149 infostream<<"Audio: Error decoding "<<filepath<<std::endl;
153 // Append to end of buffer
154 snd->buffer.insert(snd->buffer.end(), array, array + bytes);
157 alGenBuffers(1, &snd->buffer_id);
158 alBufferData(snd->buffer_id, snd->format,
159 &(snd->buffer[0]), snd->buffer.size(),
162 ALenum error = alGetError();
164 if(error != AL_NO_ERROR){
165 infostream<<"Audio: OpenAL error: "<<alErrorString(error)
166 <<"preparing sound buffer"<<std::endl;
169 infostream<<"Audio file "<<filepath<<" loaded"<<std::endl;
183 class OpenALSoundManager: public ISoundManager
186 OnDemandSoundFetcher *m_fetcher;
188 ALCcontext *m_context;
191 std::map<std::string, std::vector<SoundBuffer*> > m_buffers;
192 std::map<int, PlayingSound*> m_sounds_playing;
195 OpenALSoundManager(OnDemandSoundFetcher *fetcher):
202 ALCenum error = ALC_NO_ERROR;
204 infostream<<"Audio: Initializing..."<<std::endl;
206 m_device = alcOpenDevice(NULL);
208 infostream<<"Audio: No audio device available, audio system "
209 <<"not initialized"<<std::endl;
213 if(alcIsExtensionPresent(m_device, "EXT_vorbis")){
214 infostream<<"Audio: Vorbis extension present"<<std::endl;
217 infostream<<"Audio: Vorbis extension NOT present"<<std::endl;
218 m_can_vorbis = false;
221 m_context = alcCreateContext(m_device, NULL);
223 error = alcGetError(m_device);
224 infostream<<"Audio: Unable to initialize audio context, "
225 <<"aborting audio initialization ("<<alcErrorString(error)
227 alcCloseDevice(m_device);
232 if(!alcMakeContextCurrent(m_context) ||
233 (error = alcGetError(m_device) != ALC_NO_ERROR))
235 infostream<<"Audio: Error setting audio context, aborting audio "
236 <<"initialization ("<<alcErrorString(error)<<")"<<std::endl;
237 alcDestroyContext(m_context);
239 alcCloseDevice(m_device);
244 alDistanceModel(AL_EXPONENT_DISTANCE);
246 infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
247 <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
251 ~OpenALSoundManager()
253 infostream<<"Audio: Deinitializing..."<<std::endl;
255 // TODO: Clear SoundBuffers
256 alcMakeContextCurrent(NULL);
257 alcDestroyContext(m_context);
259 alcCloseDevice(m_device);
261 infostream<<"Audio: Deinitialized."<<std::endl;
264 void addBuffer(const std::string &name, SoundBuffer *buf)
266 std::map<std::string, std::vector<SoundBuffer*> >::iterator i =
267 m_buffers.find(name);
268 if(i != m_buffers.end()){
269 i->second.push_back(buf);
272 std::vector<SoundBuffer*> bufs;
274 m_buffers[name] = bufs;
278 SoundBuffer* getBuffer(const std::string &name)
280 std::map<std::string, std::vector<SoundBuffer*> >::iterator i =
281 m_buffers.find(name);
282 if(i == m_buffers.end())
284 std::vector<SoundBuffer*> &bufs = i->second;
285 int j = myrand() % bufs.size();
289 bool loadSound(const std::string &name,
290 const std::string &filepath)
292 SoundBuffer *buf = loadOggFile(filepath);
294 addBuffer(name, buf);
297 bool loadSound(const std::string &name,
298 const std::vector<char> &filedata)
300 errorstream<<"OpenALSoundManager: Loading from filedata not"
301 " implemented"<<std::endl;
305 PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
308 infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
310 PlayingSound *sound = new PlayingSound;
312 warn_if_error(alGetError(), "before createPlayingSound");
313 alGenSources(1, &sound->source_id);
314 alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
315 alSourcei(sound->source_id, AL_SOURCE_RELATIVE, true);
316 alSource3f(sound->source_id, AL_POSITION, 0, 0, 0);
317 alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
318 alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
319 volume = MYMAX(0.0, volume);
320 alSourcef(sound->source_id, AL_GAIN, volume);
321 alSourcePlay(sound->source_id);
322 warn_if_error(alGetError(), "createPlayingSound");
326 PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
327 float volume, v3f pos)
329 infostream<<"OpenALSoundManager: Creating positional playing sound"
332 PlayingSound *sound = new PlayingSound;
334 warn_if_error(alGetError(), "before createPlayingSoundAt");
335 alGenSources(1, &sound->source_id);
336 alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
337 alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
338 alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
339 alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
340 //alSourcef(sound->source_id, AL_ROLLOFF_FACTOR, 0.7);
341 alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
342 alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
343 volume = MYMAX(0.0, volume);
344 alSourcef(sound->source_id, AL_GAIN, volume);
345 alSourcePlay(sound->source_id);
346 warn_if_error(alGetError(), "createPlayingSoundAt");
350 int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
353 PlayingSound *sound = createPlayingSound(buf, loop, volume);
356 int id = m_next_id++;
357 m_sounds_playing[id] = sound;
361 int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
364 PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
367 int id = m_next_id++;
368 m_sounds_playing[id] = sound;
372 void deleteSound(int id)
374 std::map<int, PlayingSound*>::iterator i =
375 m_sounds_playing.find(id);
376 if(i == m_sounds_playing.end())
378 PlayingSound *sound = i->second;
380 alDeleteSources(1, &sound->source_id);
383 m_sounds_playing.erase(id);
386 /* If buffer does not exist, consult the fetcher */
387 SoundBuffer* getFetchBuffer(const std::string name)
389 SoundBuffer *buf = getBuffer(name);
394 std::set<std::string> paths;
395 std::set<std::vector<char> > datas;
396 m_fetcher->fetchSounds(name, paths, datas);
397 for(std::set<std::string>::iterator i = paths.begin();
398 i != paths.end(); i++){
401 for(std::set<std::vector<char> >::iterator i = datas.begin();
402 i != datas.end(); i++){
405 return getBuffer(name);
408 // Remove stopped sounds
411 verbosestream<<"OpenALSoundManager::maintain(): "
412 <<m_sounds_playing.size()<<" playing sounds, "
413 <<m_buffers.size()<<" sound names loaded"<<std::endl;
414 std::set<int> del_list;
415 for(std::map<int, PlayingSound*>::iterator
416 i = m_sounds_playing.begin();
417 i != m_sounds_playing.end(); i++)
420 PlayingSound *sound = i->second;
421 // If not playing, remove it
424 alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state);
425 if(state != AL_PLAYING){
430 if(del_list.size() != 0)
431 verbosestream<<"OpenALSoundManager::maintain(): deleting "
432 <<del_list.size()<<" playing sounds"<<std::endl;
433 for(std::set<int>::iterator i = del_list.begin();
434 i != del_list.end(); i++)
442 void updateListener(v3f pos, v3f vel, v3f at, v3f up)
444 m_listener_pos = pos;
445 alListener3f(AL_POSITION, pos.X, pos.Y, pos.Z);
446 alListener3f(AL_VELOCITY, vel.X, vel.Y, vel.Z);
450 alListenerfv(AL_ORIENTATION, f);
451 warn_if_error(alGetError(), "updateListener");
454 int playSound(const std::string &name, bool loop, float volume)
459 SoundBuffer *buf = getFetchBuffer(name);
461 infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
465 return playSoundRaw(buf, loop, volume);
467 int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
472 SoundBuffer *buf = getFetchBuffer(name);
474 infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
478 return playSoundRawAt(buf, loop, volume, pos);
480 void stopSound(int sound)
485 bool soundExists(int sound)
488 return (m_sounds_playing.count(sound) != 0);
490 void updateSoundPosition(int id, v3f pos)
492 std::map<int, PlayingSound*>::iterator i =
493 m_sounds_playing.find(id);
494 if(i == m_sounds_playing.end())
496 PlayingSound *sound = i->second;
498 alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
499 alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
500 alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
501 alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
505 ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)
507 return new OpenALSoundManager(fetcher);