From 6ccb5835ff55d85156be91473c598eca9d6cb9a6 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Wed, 4 Nov 2020 16:57:41 +0100 Subject: Revert "Make Lint Happy" This reverts commit ad148587dcf5244c2d2011dba339786c765c54c4. --- src/client/sound_openal.cpp | 229 ++++++++++++++++++++++---------------------- 1 file changed, 113 insertions(+), 116 deletions(-) (limited to 'src/client/sound_openal.cpp') diff --git a/src/client/sound_openal.cpp b/src/client/sound_openal.cpp index 7cc16d8d7..20a651c1d 100644 --- a/src/client/sound_openal.cpp +++ b/src/client/sound_openal.cpp @@ -24,17 +24,17 @@ with this program; ifnot, write to the Free Software Foundation, Inc., #include "sound_openal.h" #if defined(_WIN32) -#include -#include -//#include + #include + #include + //#include #elif defined(__APPLE__) -#include -#include -//#include + #include + #include + //#include #else -#include -#include -#include + #include + #include + #include #endif #include #include @@ -52,7 +52,7 @@ with this program; ifnot, write to the Free Software Foundation, Inc., std::shared_ptr g_sound_manager_singleton; typedef std::unique_ptr unique_ptr_alcdevice; -typedef std::unique_ptr unique_ptr_alccontext; +typedef std::unique_ptr unique_ptr_alccontext; static void delete_alcdevice(ALCdevice *p) { @@ -90,9 +90,9 @@ static const char *alErrorString(ALenum err) static ALenum warn_if_error(ALenum err, const char *desc) { - if (err == AL_NO_ERROR) + if(err == AL_NO_ERROR) return err; - warningstream << desc << ": " << alErrorString(err) << std::endl; + warningstream< buffer; }; -SoundBuffer *load_opened_ogg_file( - OggVorbis_File *oggFile, const std::string &filename_for_logging) +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; @@ -126,7 +126,7 @@ SoundBuffer *load_opened_ogg_file( pInfo = ov_info(oggFile, -1); // Check the number of channels... always use 16-bit samples - if (pInfo->channels == 1) + if(pInfo->channels == 1) snd->format = AL_FORMAT_MONO16; else snd->format = AL_FORMAT_STEREO16; @@ -135,14 +135,16 @@ SoundBuffer *load_opened_ogg_file( snd->freq = pInfo->rate; // Keep reading until all is read - do { + do + { // Read up to a buffer's worth of decoded sound data bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); - if (bytes < 0) { + if(bytes < 0) + { ov_clear(oggFile); - infostream << "Audio: Error decoding " << filename_for_logging - << std::endl; + infostream << "Audio: Error decoding " + << filename_for_logging << std::endl; delete snd; return nullptr; } @@ -152,17 +154,18 @@ SoundBuffer *load_opened_ogg_file( } while (bytes > 0); alGenBuffers(1, &snd->buffer_id); - alBufferData(snd->buffer_id, snd->format, &(snd->buffer[0]), snd->buffer.size(), + alBufferData(snd->buffer_id, snd->format, + &(snd->buffer[0]), snd->buffer.size(), snd->freq); ALenum error = alGetError(); - if (error != AL_NO_ERROR) { + if(error != AL_NO_ERROR){ infostream << "Audio: OpenAL error: " << alErrorString(error) - << "preparing sound buffer" << std::endl; + << "preparing sound buffer" << std::endl; } - // infostream << "Audio file " + //infostream << "Audio file " // << filename_for_logging << " loaded" << std::endl; // Clean up! @@ -179,16 +182,15 @@ SoundBuffer *load_ogg_from_file(const std::string &path) // This requires libvorbis >= 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; + infostream << "Audio: Error opening " << path + << " for decoding" << std::endl; return nullptr; } return load_opened_ogg_file(&oggFile, path); } -struct BufferSource -{ +struct BufferSource { const char *buf; size_t cur_offset; size_t len; @@ -214,8 +216,8 @@ int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence) 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) { + if ((size_t)MYMIN(-offset, 0) > s->cur_offset + || s->cur_offset + offset > s->len) { // offset out of bounds return -1; } @@ -232,8 +234,12 @@ long BufferSourceell_func(void *datasource) return s->cur_offset; } -static ov_callbacks g_buffer_ov_callbacks = {&buffer_sound_read_func, - &buffer_sound_seek_func, nullptr, &BufferSourceell_func}; +static ov_callbacks g_buffer_ov_callbacks = { + &buffer_sound_read_func, + &buffer_sound_seek_func, + nullptr, + &BufferSourceell_func +}; SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log) { @@ -245,8 +251,8 @@ SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_ s.len = buf.size(); if (ov_open_callbacks(&s, &oggFile, nullptr, 0, g_buffer_ov_callbacks) != 0) { - infostream << "Audio: Error opening " << id_for_log << " for decoding" - << std::endl; + infostream << "Audio: Error opening " << id_for_log + << " for decoding" << std::endl; return nullptr; } @@ -262,54 +268,43 @@ struct PlayingSound class SoundManagerSingleton { public: - unique_ptr_alcdevice m_device; + unique_ptr_alcdevice m_device; unique_ptr_alccontext m_context; - public: SoundManagerSingleton() : - m_device(nullptr, delete_alcdevice), - m_context(nullptr, delete_alccontext) + m_device(nullptr, delete_alcdevice), + m_context(nullptr, delete_alccontext) { } bool init() { - if (!(m_device = unique_ptr_alcdevice( - alcOpenDevice(nullptr), delete_alcdevice))) { - errorstream << "Audio: Global Initialization: Failed to open " - "device" - << std::endl; + if (!(m_device = unique_ptr_alcdevice(alcOpenDevice(nullptr), delete_alcdevice))) { + errorstream << "Audio: Global Initialization: Failed to open device" << std::endl; return false; } if (!(m_context = unique_ptr_alccontext( - alcCreateContext(m_device.get(), nullptr), - delete_alccontext))) { - errorstream << "Audio: Global Initialization: Failed to create " - "context" - << std::endl; + alcCreateContext(m_device.get(), nullptr), delete_alccontext))) { + errorstream << "Audio: Global Initialization: Failed to create context" << std::endl; return false; } if (!alcMakeContextCurrent(m_context.get())) { - errorstream << "Audio: Global Initialization: Failed to make " - "current context" - << std::endl; + errorstream << "Audio: Global Initialization: Failed to make current context" << std::endl; return false; } alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); if (alGetError() != AL_NO_ERROR) { - errorstream << "Audio: Global Initialization: OpenAL Error " - << alGetError() << std::endl; + errorstream << "Audio: Global Initialization: OpenAL Error " << alGetError() << std::endl; return false; } - infostream << "Audio: Global Initialized: OpenAL " - << alGetString(AL_VERSION) << ", using " - << alcGetString(m_device.get(), ALC_DEVICE_SPECIFIER) - << std::endl; + infostream << "Audio: Global Initialized: OpenAL " << alGetString(AL_VERSION) + << ", using " << alcGetString(m_device.get(), ALC_DEVICE_SPECIFIER) + << std::endl; return true; } @@ -320,24 +315,22 @@ public: } }; -class OpenALSoundManager : public ISoundManager +class OpenALSoundManager: public ISoundManager { private: OnDemandSoundFetcher *m_fetcher; ALCdevice *m_device; ALCcontext *m_context; int m_next_id; - std::unordered_map> m_buffers; - std::unordered_map m_sounds_playing; - struct FadeState - { + std::unordered_map> m_buffers; + std::unordered_map m_sounds_playing; + struct FadeState { FadeState() = default; - FadeState(float step, float current_gain, float target_gain) : - step(step), current_gain(current_gain), - target_gain(target_gain) - { - } + 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; @@ -345,11 +338,13 @@ private: std::unordered_map m_sounds_fading; float m_fade_delay; - public: - OpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher) : - m_fetcher(fetcher), m_device(smg->m_device.get()), - m_context(smg->m_context.get()), m_next_id(1), m_fade_delay(0) + OpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher): + m_fetcher(fetcher), + m_device(smg->m_device.get()), + m_context(smg->m_context.get()), + m_next_id(1), + m_fade_delay(0) { infostream << "Audio: Initialized: OpenAL " << std::endl; } @@ -377,34 +372,37 @@ public: infostream << "Audio: Deinitialized." << std::endl; } - void step(float dtime) { doFades(dtime); } + void step(float dtime) + { + doFades(dtime); + } void addBuffer(const std::string &name, SoundBuffer *buf) { - std::unordered_map>::iterator i = + std::unordered_map>::iterator i = m_buffers.find(name); - if (i != m_buffers.end()) { + if(i != m_buffers.end()){ i->second.push_back(buf); return; } - std::vector bufs; + std::vector bufs; bufs.push_back(buf); m_buffers[name] = bufs; } - SoundBuffer *getBuffer(const std::string &name) + SoundBuffer* getBuffer(const std::string &name) { - std::unordered_map>::iterator i = + std::unordered_map>::iterator i = m_buffers.find(name); - if (i == m_buffers.end()) + if(i == m_buffers.end()) return nullptr; - std::vector &bufs = i->second; + std::vector &bufs = i->second; int j = myrand() % bufs.size(); return bufs[j]; } - PlayingSound *createPlayingSound( - SoundBuffer *buf, bool loop, float volume, float pitch) + PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop, + float volume, float pitch) { infostream << "OpenALSoundManager: Creating playing sound" << std::endl; assert(buf); @@ -425,11 +423,11 @@ public: return sound; } - PlayingSound *createPlayingSoundAt( - SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch) + PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop, + float volume, v3f pos, float pitch) { infostream << "OpenALSoundManager: Creating positional playing sound" - << std::endl; + << std::endl; assert(buf); PlayingSound *sound = new PlayingSound; assert(sound); @@ -458,7 +456,7 @@ public: { assert(buf); PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch); - if (!sound) + if(!sound) return -1; int id = m_next_id++; m_sounds_playing[id] = sound; @@ -470,7 +468,7 @@ public: { assert(buf); PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch); - if (!sound) + if(!sound) return -1; int id = m_next_id++; m_sounds_playing[id] = sound; @@ -479,9 +477,8 @@ public: void deleteSound(int id) { - std::unordered_map::iterator i = - m_sounds_playing.find(id); - if (i == m_sounds_playing.end()) + std::unordered_map::iterator i = m_sounds_playing.find(id); + if(i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; @@ -492,12 +489,12 @@ public: } /* 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) + if(buf) return buf; - if (!m_fetcher) + if(!m_fetcher) return nullptr; std::set paths; std::set datas; @@ -516,9 +513,8 @@ public: { if (!m_sounds_playing.empty()) { verbosestream << "OpenALSoundManager::maintain(): " - << m_sounds_playing.size() << " playing sounds, " - << m_buffers.size() << " sound names loaded" - << std::endl; + << m_sounds_playing.size() <<" playing sounds, " + << m_buffers.size() <<" sound names loaded"< del_list; for (const auto &sp : m_sounds_playing) { @@ -528,15 +524,14 @@ public: { ALint state; alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING) { + if(state != AL_PLAYING){ del_list.insert(id); } } } - if (!del_list.empty()) - verbosestream << "OpenALSoundManager::maintain(): deleting " - << del_list.size() << " playing sounds" - << std::endl; + if(!del_list.empty()) + verbosestream<<"OpenALSoundManager::maintain(): deleting " + <second.step < 0.f) chkGain = -(i->second.current_gain); else @@ -637,8 +636,7 @@ public: 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); + i->second.current_gain = rangelim(i->second.current_gain, 0, 1); updateSoundGain(i->first, i->second.current_gain); ++i; @@ -704,8 +702,7 @@ std::shared_ptr createSoundManagerSingleton() return smg; } -ISoundManager *createOpenALSoundManager( - SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher) +ISoundManager *createOpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher) { return new OpenALSoundManager(smg, fetcher); }; -- cgit v1.2.3