X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fsound_openal.cpp;h=0b53572c428b645a8d87e7c3798db8f1959a88d7;hb=ff73c7a5da6ab8ac0bb678ebf25b83e805397029;hp=4f056888b0dd56035843cdbbafabf89e1a82d500;hpb=8c2f3bb378640c921a0ad40c4577687b0c7c37f3;p=dragonfireclient.git diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index 4f056888b..0b53572c4 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -1,46 +1,49 @@ /* -Minetest-c55 -Copyright (C) 2012 celeron55, Perttu Ahola +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola OpenAL support based on work by: Copyright (C) 2011 Sebastian 'Bahamada' Rühl Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits Copyright (C) 2011 Giuseppe Bilotta This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +GNU Lesser General Public License for more details. -You should have received a copy of the GNU General Public License along +You should have received a copy of the GNU Lesser General Public License along with this program; ifnot, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "sound_openal.h" -#if defined(_MSC_VER) +#if defined(_WIN32) #include #include - #include + //#include #elif defined(__APPLE__) #include #include - #include + //#include #else #include #include #include #endif #include +#include #include "log.h" -#include +#include "util/numeric.h" // myrand() +#include "porting.h" #include -#include "utility.h" // myrand() +#include +#include #define BUFFER_SIZE 30000 @@ -88,7 +91,7 @@ static ALenum warn_if_error(ALenum err, const char *desc) { if(err == AL_NO_ERROR) return err; - errorstream<<"WARNING: "< buffer; }; -SoundBuffer* loadOggFile(const std::string &filepath) +SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile, + const std::string &filename_for_logging) { int endian = 0; // 0 for Little-Endian, 1 for Big-Endian int bitStream; long bytes; char array[BUFFER_SIZE]; // Local fixed size array vorbis_info *pInfo; - OggVorbis_File oggFile; - - // Try opening the given file - if(ov_fopen(filepath.c_str(), &oggFile) != 0) - { - infostream<<"Audio: Error opening "<channels == 1) @@ -141,12 +137,14 @@ SoundBuffer* loadOggFile(const std::string &filepath) do { // Read up to a buffer's worth of decoded sound data - bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); + bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if(bytes < 0) { - ov_clear(&oggFile); - infostream<<"Audio: Error decoding "<= 1.3.2, as + // previous versions expect a non-const char * + if (ov_fopen(path.c_str(), &oggFile) != 0) { + infostream << "Audio: Error opening " << path + << " for decoding" << std::endl; + return NULL; + } + + return load_opened_ogg_file(&oggFile, path); +} + +struct BufferSource { + const char *buf; + size_t cur_offset; + size_t len; +}; + +size_t buffer_sound_read_func(void *ptr, size_t size, size_t nmemb, void *datasource) +{ + BufferSource *s = (BufferSource *)datasource; + size_t copied_size = MYMIN(s->len - s->cur_offset, size); + memcpy(ptr, s->buf + s->cur_offset, copied_size); + s->cur_offset += copied_size; + return copied_size; +} + +int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence) +{ + BufferSource *s = (BufferSource *)datasource; + if (whence == SEEK_SET) { + if (offset < 0 || (size_t)MYMAX(offset, 0) >= s->len) { + // offset out of bounds + return -1; + } + s->cur_offset = offset; + return 0; + } else if (whence == SEEK_CUR) { + if ((size_t)MYMIN(-offset, 0) > s->cur_offset + || s->cur_offset + offset > s->len) { + // offset out of bounds + return -1; + } + s->cur_offset += offset; + return 0; + } + // invalid whence param (SEEK_END doesn't have to be supported) + return -1; +} + +long BufferSourceell_func(void *datasource) +{ + BufferSource *s = (BufferSource *)datasource; + return s->cur_offset; +} + +static ov_callbacks g_buffer_ov_callbacks = { + &buffer_sound_read_func, + &buffer_sound_seek_func, + NULL, + &BufferSourceell_func +}; + +SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log) +{ + OggVorbis_File oggFile; + + BufferSource s; + s.buf = buf.c_str(); + s.cur_offset = 0; + s.len = buf.size(); + + if (ov_open_callbacks(&s, &oggFile, NULL, 0, g_buffer_ov_callbacks) != 0) { + infostream << "Audio: Error opening " << id_for_log + << " for decoding" << std::endl; + return NULL; + } + + return load_opened_ogg_file(&oggFile, id_for_log); +} + struct PlayingSound { ALuint source_id; @@ -186,21 +270,35 @@ class OpenALSoundManager: public ISoundManager OnDemandSoundFetcher *m_fetcher; ALCdevice *m_device; ALCcontext *m_context; - bool m_can_vorbis; int m_next_id; - std::map > m_buffers; - std::map m_sounds_playing; + std::unordered_map> m_buffers; + std::unordered_map m_sounds_playing; v3f m_listener_pos; + struct FadeState { + FadeState() {} + FadeState(float step, float current_gain, float target_gain): + step(step), + current_gain(current_gain), + target_gain(target_gain) {} + float step; + float current_gain; + float target_gain; + }; + + std::unordered_map m_sounds_fading; + float m_fade_delay; public: + bool m_is_initialized; OpenALSoundManager(OnDemandSoundFetcher *fetcher): m_fetcher(fetcher), m_device(NULL), m_context(NULL), - m_can_vorbis(false), - m_next_id(1) + m_next_id(1), + m_fade_delay(0), + m_is_initialized(false) { ALCenum error = ALC_NO_ERROR; - + infostream<<"Audio: Initializing..."<>::iterator i = + m_buffers.begin(); i != m_buffers.end(); ++i) { + for (std::vector::iterator iter = (*i).second.begin(); + iter != (*i).second.end(); ++iter) { + delete *iter; + } + (*i).second.clear(); + } + m_buffers.clear(); infostream<<"Audio: Deinitialized."< >::iterator i = + std::unordered_map>::iterator i = m_buffers.find(name); if(i != m_buffers.end()){ i->second.push_back(buf); @@ -277,7 +384,7 @@ class OpenALSoundManager: public ISoundManager SoundBuffer* getBuffer(const std::string &name) { - std::map >::iterator i = + std::unordered_map>::iterator i = m_buffers.find(name); if(i == m_buffers.end()) return NULL; @@ -286,24 +393,8 @@ class OpenALSoundManager: public ISoundManager return bufs[j]; } - bool loadSound(const std::string &name, - const std::string &filepath) - { - SoundBuffer *buf = loadOggFile(filepath); - if(buf) - addBuffer(name, buf); - return false; - } - bool loadSound(const std::string &name, - const std::vector &filedata) - { - errorstream<<"OpenALSoundManager: Loading from filedata not" - " implemented"<source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE); volume = MYMAX(0.0, volume); alSourcef(sound->source_id, AL_GAIN, volume); + alSourcef(sound->source_id, AL_PITCH, pitch); alSourcePlay(sound->source_id); warn_if_error(alGetError(), "createPlayingSound"); return sound; } PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop, - float volume, v3f pos) + float volume, v3f pos, float pitch) { infostream<<"OpenALSoundManager: Creating positional playing sound" <source_id, AL_SOURCE_RELATIVE, false); alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z); alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0); - //alSourcef(sound->source_id, AL_ROLLOFF_FACTOR, 0.7); alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0); alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE); volume = MYMAX(0.0, volume); alSourcef(sound->source_id, AL_GAIN, volume); + alSourcef(sound->source_id, AL_PITCH, pitch); alSourcePlay(sound->source_id); warn_if_error(alGetError(), "createPlayingSoundAt"); return sound; } - int playSoundRaw(SoundBuffer *buf, bool loop, float volume) + int playSoundRaw(SoundBuffer *buf, bool loop, float volume, float pitch) { assert(buf); - PlayingSound *sound = createPlayingSound(buf, loop, volume); + PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch); if(!sound) return -1; int id = m_next_id++; @@ -358,25 +450,24 @@ class OpenALSoundManager: public ISoundManager return id; } - int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos) + int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch) { assert(buf); - PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos); + PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch); if(!sound) return -1; int id = m_next_id++; m_sounds_playing[id] = sound; return id; } - + void deleteSound(int id) { - std::map::iterator i = - m_sounds_playing.find(id); + std::unordered_map::iterator i = m_sounds_playing.find(id); if(i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; - + alDeleteSources(1, &sound->source_id); delete sound; @@ -384,7 +475,7 @@ class OpenALSoundManager: public ISoundManager } /* If buffer does not exist, consult the fetcher */ - SoundBuffer* getFetchBuffer(const std::string name) + SoundBuffer* getFetchBuffer(const std::string &name) { SoundBuffer *buf = getBuffer(name); if(buf) @@ -392,19 +483,19 @@ class OpenALSoundManager: public ISoundManager if(!m_fetcher) return NULL; std::set paths; - std::set > datas; + std::set datas; m_fetcher->fetchSounds(name, paths, datas); for(std::set::iterator i = paths.begin(); - i != paths.end(); i++){ - loadSound(name, *i); + i != paths.end(); ++i){ + loadSoundFile(name, *i); } - for(std::set >::iterator i = datas.begin(); - i != datas.end(); i++){ - loadSound(name, *i); + for(std::set::iterator i = datas.begin(); + i != datas.end(); ++i){ + loadSoundData(name, *i); } return getBuffer(name); } - + // Remove stopped sounds void maintain() { @@ -412,10 +503,8 @@ class OpenALSoundManager: public ISoundManager < del_list; - for(std::map::iterator - i = m_sounds_playing.begin(); - i != m_sounds_playing.end(); i++) - { + for(std::unordered_map::iterator i = m_sounds_playing.begin(); + i != m_sounds_playing.end(); ++i) { int id = i->first; PlayingSound *sound = i->second; // If not playing, remove it @@ -427,11 +516,11 @@ class OpenALSoundManager: public ISoundManager } } } - if(del_list.size() != 0) + if(!del_list.empty()) verbosestream<<"OpenALSoundManager::maintain(): deleting " <::iterator i = del_list.begin(); - i != del_list.end(); i++) + i != del_list.end(); ++i) { deleteSound(*i); } @@ -439,6 +528,24 @@ class OpenALSoundManager: public ISoundManager /* Interface */ + bool loadSoundFile(const std::string &name, + const std::string &filepath) + { + SoundBuffer *buf = load_ogg_from_file(filepath); + if (buf) + addBuffer(name, buf); + return false; + } + + bool loadSoundData(const std::string &name, + const std::string &filedata) + { + SoundBuffer *buf = load_ogg_from_buffer(filedata, name); + if (buf) + addBuffer(name, buf); + return false; + } + void updateListener(v3f pos, v3f vel, v3f at, v3f up) { m_listener_pos = pos; @@ -446,42 +553,142 @@ class OpenALSoundManager: public ISoundManager alListener3f(AL_VELOCITY, vel.X, vel.Y, vel.Z); ALfloat f[6]; f3_set(f, at); - f3_set(f+3, up); + f3_set(f+3, -up); alListenerfv(AL_ORIENTATION, f); warn_if_error(alGetError(), "updateListener"); } - int playSound(const std::string &name, bool loop, float volume) + void setListenerGain(float gain) + { + alListenerf(AL_GAIN, gain); + } + + int playSound(const std::string &name, bool loop, float volume, float fade, float pitch) { maintain(); + if(name == "") + return 0; SoundBuffer *buf = getFetchBuffer(name); if(!buf){ infostream<<"OpenALSoundManager: \""< 0) { + handle = playSoundRaw(buf, loop, 0.0f, 0.0f); + fadeSound(handle, fade, volume); + } else { + handle = playSoundRaw(buf, loop, volume, pitch); + } + return handle; } - int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) + + int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch) { maintain(); + if(name == "") + return 0; SoundBuffer *buf = getFetchBuffer(name); if(!buf){ infostream<<"OpenALSoundManager: \""<::iterator i = m_sounds_fading.begin(); + i != m_sounds_fading.end();) { + if (i->second.step < 0.f) + chkGain = -(i->second.current_gain); + else + chkGain = i->second.current_gain; + + if (chkGain < i->second.target_gain) { + i->second.current_gain += (i->second.step * m_fade_delay); + i->second.current_gain = rangelim(i->second.current_gain, 0, 1); + + updateSoundGain(i->first, i->second.current_gain); + ++i; + } else { + if (i->second.target_gain <= 0.f) + stopSound(i->first); + + m_sounds_fading.erase(i++); + } + } + m_fade_delay = 0; + } + + bool soundExists(int sound) + { + maintain(); + return (m_sounds_playing.count(sound) != 0); + } + + void updateSoundPosition(int id, v3f pos) + { + std::unordered_map::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) + return; + PlayingSound *sound = i->second; + + alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false); + alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z); + alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0); + alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0); + } + + bool updateSoundGain(int id, float gain) + { + std::unordered_map::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) + return false; + + PlayingSound *sound = i->second; + alSourcef(sound->source_id, AL_GAIN, gain); + return true; + } + + float getSoundGain(int id) + { + std::unordered_map::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) + return 0; + + PlayingSound *sound = i->second; + ALfloat gain; + alGetSourcef(sound->source_id, AL_GAIN, &gain); + return gain; + } }; ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher) { - return new OpenALSoundManager(fetcher); + OpenALSoundManager *m = new OpenALSoundManager(fetcher); + if(m->m_is_initialized) + return m; + delete m; + return NULL; };