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 PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
292 infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
294 PlayingSound *sound = new PlayingSound;
296 warn_if_error(alGetError(), "before createPlayingSound");
297 alGenSources(1, &sound->source_id);
298 alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
299 alSourcei(sound->source_id, AL_SOURCE_RELATIVE, true);
300 alSource3f(sound->source_id, AL_POSITION, 0, 0, 0);
301 alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
302 alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
303 volume = MYMAX(0.0, volume);
304 alSourcef(sound->source_id, AL_GAIN, volume);
305 alSourcePlay(sound->source_id);
306 warn_if_error(alGetError(), "createPlayingSound");
310 PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
311 float volume, v3f pos)
313 infostream<<"OpenALSoundManager: Creating positional playing sound"
316 PlayingSound *sound = new PlayingSound;
318 warn_if_error(alGetError(), "before createPlayingSoundAt");
319 alGenSources(1, &sound->source_id);
320 alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
321 alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
322 alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
323 alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
324 //alSourcef(sound->source_id, AL_ROLLOFF_FACTOR, 0.7);
325 alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
326 alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
327 volume = MYMAX(0.0, volume);
328 alSourcef(sound->source_id, AL_GAIN, volume);
329 alSourcePlay(sound->source_id);
330 warn_if_error(alGetError(), "createPlayingSoundAt");
334 int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
337 PlayingSound *sound = createPlayingSound(buf, loop, volume);
340 int id = m_next_id++;
341 m_sounds_playing[id] = sound;
345 int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
348 PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
351 int id = m_next_id++;
352 m_sounds_playing[id] = sound;
356 void deleteSound(int id)
358 std::map<int, PlayingSound*>::iterator i =
359 m_sounds_playing.find(id);
360 if(i == m_sounds_playing.end())
362 PlayingSound *sound = i->second;
364 alDeleteSources(1, &sound->source_id);
367 m_sounds_playing.erase(id);
370 /* If buffer does not exist, consult the fetcher */
371 SoundBuffer* getFetchBuffer(const std::string name)
373 SoundBuffer *buf = getBuffer(name);
378 std::set<std::string> paths;
379 std::set<std::string> datas;
380 m_fetcher->fetchSounds(name, paths, datas);
381 for(std::set<std::string>::iterator i = paths.begin();
382 i != paths.end(); i++){
383 loadSoundFile(name, *i);
385 for(std::set<std::string>::iterator i = datas.begin();
386 i != datas.end(); i++){
387 loadSoundData(name, *i);
389 return getBuffer(name);
392 // Remove stopped sounds
395 verbosestream<<"OpenALSoundManager::maintain(): "
396 <<m_sounds_playing.size()<<" playing sounds, "
397 <<m_buffers.size()<<" sound names loaded"<<std::endl;
398 std::set<int> del_list;
399 for(std::map<int, PlayingSound*>::iterator
400 i = m_sounds_playing.begin();
401 i != m_sounds_playing.end(); i++)
404 PlayingSound *sound = i->second;
405 // If not playing, remove it
408 alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state);
409 if(state != AL_PLAYING){
414 if(del_list.size() != 0)
415 verbosestream<<"OpenALSoundManager::maintain(): deleting "
416 <<del_list.size()<<" playing sounds"<<std::endl;
417 for(std::set<int>::iterator i = del_list.begin();
418 i != del_list.end(); i++)
426 bool loadSoundFile(const std::string &name,
427 const std::string &filepath)
429 SoundBuffer *buf = loadOggFile(filepath);
431 addBuffer(name, buf);
434 bool loadSoundData(const std::string &name,
435 const std::string &filedata)
437 errorstream<<"OpenALSoundManager: Loading from filedata not"
438 " implemented"<<std::endl;
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);