OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
soundfx.cpp
Go to the documentation of this file.
1#include "soundfx.h"
2
3#include <Tempest/Log>
4#include <cctype>
5
7#include "gothic.h"
8
9#include "resources.h"
10#include "utils/string_frm.h"
11
12SoundFx::SoundVar::SoundVar(const zenkit::ISoundEffect& sfx, Tempest::Sound &&snd)
13 :snd(std::move(snd)),vol(float(sfx.vol)/127.f),loop(sfx.loop){
14 }
15
16SoundFx::SoundVar::SoundVar(const float vol, Tempest::Sound &&snd)
17 :snd(std::move(snd)),vol(vol/127.f){
18 }
19
20SoundFx::SoundFx(std::string_view s) {
21 implLoad(s);
22 if(inst.size()!=0)
23 return;
24 // lowcase?
25 std::string name = std::string(s);
26 for(auto& i:name)
27 i = char(std::toupper(i));
28
29 implLoad(name);
30 if(inst.size()!=0)
31 return;
32
33 if(name.rfind(".WAV")==name.size()-4) {
34 auto snd = Resources::loadSoundBuffer(name);
35 if(snd.isEmpty())
36 Tempest::Log::d("unable to load sound fx: ",name); else
37 inst.emplace_back(1.f,std::move(snd));
38 }
39
40 if(inst.size()==0)
41 Tempest::Log::d("unable to load sound fx: ",name);
42 }
43
44SoundFx::SoundFx(Tempest::Sound &&snd) {
45 if(!snd.isEmpty())
46 inst.emplace_back(127.f,std::move(snd));
47 }
48
49Tempest::SoundEffect SoundFx::load(Tempest::SoundDevice &dev, bool& loop) const {
50 if(inst.size()==0)
51 return Tempest::SoundEffect();
52 auto& var = inst[size_t(std::rand())%inst.size()];
53 Tempest::SoundEffect effect = dev.load(var.snd);
54 effect.setVolume(var.vol);
55 loop = var.loop;
56 return effect;
57 }
58
59void SoundFx::implLoad(std::string_view s) {
60 auto& sfx = Gothic::sfx()[s];
61 auto snd = Resources::loadSoundBuffer(sfx.file);
62
63 if(!snd.isEmpty())
64 inst.emplace_back(sfx,std::move(snd));
65 loadVariants(s);
66 }
67
68void SoundFx::loadVariants(std::string_view s) {
69 for(int i=1;i<100;++i) {
70 string_frm name(s,"_A",i);
71 auto& sfx = Gothic::sfx()[name];
72 auto snd = Resources::loadSoundBuffer(sfx.file);
73 if(snd.isEmpty())
74 break;
75 inst.emplace_back(sfx,std::move(snd));
76 }
77 }
static const SoundDefinitions & sfx()
Definition gothic.cpp:655
static Tempest::Sound loadSoundBuffer(std::string_view name)
Tempest::SoundEffect load(Tempest::SoundDevice &dev, bool &loop) const
Definition soundfx.cpp:49
SoundFx(std::string_view tagname)
Definition soundfx.cpp:20