diff options
| author | untodesu <kirill@untode.su> | 2025-03-15 16:22:09 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-03-15 16:22:09 +0500 |
| commit | 3bf42c6ff3805a0d42bbc661794a95ff31bedc26 (patch) | |
| tree | 05049955847504808d6bed2bb7b155f8b03807bb /game/client/sound_effect.cc | |
| parent | 02294547dcde0d4ad76e229106702261e9f10a51 (diff) | |
| download | voxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.tar.bz2 voxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.zip | |
Add whatever I was working on for the last month
Diffstat (limited to 'game/client/sound_effect.cc')
| -rw-r--r-- | game/client/sound_effect.cc | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/game/client/sound_effect.cc b/game/client/sound_effect.cc new file mode 100644 index 0000000..c6cee72 --- /dev/null +++ b/game/client/sound_effect.cc @@ -0,0 +1,104 @@ +#include "client/pch.hh" +#include "client/sound_effect.hh" + +#include "core/resource.hh" + +static emhash8::HashMap<std::string, resource_ptr<SoundEffect>> resource_map; + +static std::size_t drwav_read_physfs(void *file, void *output, std::size_t count) +{ + return static_cast<std::size_t>(PHYSFS_readBytes(reinterpret_cast<PHYSFS_File *>(file), output, count)); +} + +static drwav_bool32 drwav_seek_physfs(void *file, int offset, drwav_seek_origin origin) +{ + if(origin == drwav_seek_origin_current) + return PHYSFS_seek(reinterpret_cast<PHYSFS_File *>(file), PHYSFS_tell(reinterpret_cast<PHYSFS_File *>(file)) + offset); + return PHYSFS_seek(reinterpret_cast<PHYSFS_File *>(file), offset); +} + +template<> +resource_ptr<SoundEffect> resource::load<SoundEffect>(const char *name, unsigned int flags) +{ + auto it = resource_map.find(name); + + if(it != resource_map.cend()) { + // Return an existing resource + return it->second; + } + + auto file = PHYSFS_openRead(name); + + if(file == nullptr) { + spdlog::warn("resource: {} [SoundEffect]: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); + return nullptr; + } + + + drwav wav_info; + + if(!drwav_init(&wav_info, &drwav_read_physfs, &drwav_seek_physfs, file, nullptr)) { + spdlog::warn("resource: {} [SoundEffect]: drwav_init failed", name); + PHYSFS_close(file); + return nullptr; + } + + if(wav_info.channels != 1) { + spdlog::warn("resource: {} [SoundEffect]: only mono sound files are allowed", name); + drwav_uninit(&wav_info); + PHYSFS_close(file); + return nullptr; + } + + auto samples = new ALshort[wav_info.totalPCMFrameCount]; + auto count = drwav_read_pcm_frames_s16(&wav_info, wav_info.totalPCMFrameCount, reinterpret_cast<drwav_int16 *>(samples)); + auto sample_rate = static_cast<ALsizei>(wav_info.sampleRate); + auto length = static_cast<ALsizei>(count * sizeof(ALshort)); + + drwav_uninit(&wav_info); + PHYSFS_close(file); + + auto new_resource = std::make_shared<SoundEffect>(); + new_resource->name = std::string(name); + + alGenBuffers(1, &new_resource->buffer); + alBufferData(new_resource->buffer, AL_FORMAT_MONO16, samples, length, sample_rate); + + delete[] samples; + + return resource_map.insert_or_assign(name, new_resource).first->second; +} + +template<> +void resource::hard_cleanup<SoundEffect>(void) +{ + for(const auto &it : resource_map) { + if(it.second.use_count() > 1L) + spdlog::warn("resource: zombie resource [SoundEffect] {} [use_count={}]", it.first, it.second.use_count()); + else spdlog::debug("resource: releasing [SoundEffect] {}", it.first); + + alDeleteBuffers(1, &it.second->buffer); + } + + resource_map.clear(); +} + +template<> +void resource::soft_cleanup<SoundEffect>(void) +{ + auto iter = resource_map.cbegin(); + + while(iter != resource_map.cend()) { + if(iter->second.use_count() == 1L) { + spdlog::debug("resource: releasing [SoundEffect] {}", iter->first); + + alDeleteBuffers(1, &iter->second->buffer); + + iter = resource_map.erase(iter); + + continue; + } + + iter = std::next(iter); + } +} |
