Rise
The Vieneo Province
Loading...
Searching...
No Matches
EarMuffle.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <vector>
6#include "bass.h"
7#include "bass_fx.h"
8
9// Only owns its two effects, never the channel.
11{
12 HSTREAM stream = 0;
13 HFX lowpass = 0;
14 HFX shelf = 0;
15
16 void Clear()
17 {
18 if (lowpass) BASS_ChannelRemoveFX(stream, lowpass);
19 if (shelf) BASS_ChannelRemoveFX(stream, shelf);
20 lowpass = shelf = 0;
21 }
22
23 bool Apply(float amount)
24 {
25 if (!std::isfinite(amount) || amount < 0.0f)
26 {
27 Clear();
28 return false;
29 }
30 if (amount == 0.0f)
31 {
32 Clear();
33 return true;
34 }
35 amount = (std::min)(amount, 2.5f);
36 BASS_CHANNELINFO info{};
37 if (!BASS_ChannelGetInfo(stream, &info))
38 {
39 Clear();
40 return false;
41 }
42 // Run after the source's atmospheric, glass and communications filters.
43 if (!lowpass) lowpass = BASS_ChannelSetFX(stream, BASS_FX_BFX_BQF, -1);
44 if (!shelf) shelf = BASS_ChannelSetFX(stream, BASS_FX_BFX_BQF, -2);
45 BASS_BFX_BQF lp{};
46 lp.lFilter = BASS_BFX_BQF_LOWPASS;
47 lp.lChannel = BASS_BFX_CHANALL;
48 lp.fCenter = (std::min)(20000.0f * std::exp(-0.76f * amount), info.freq * 0.45f);
49 lp.fQ = 0.8f;
50 BASS_BFX_BQF hs{};
51 hs.lFilter = BASS_BFX_BQF_HIGHSHELF;
52 hs.lChannel = BASS_BFX_CHANALL;
53 hs.fCenter = (std::min)(3500.0f, info.freq * 0.4f);
54 hs.fGain = -9.0f * amount / 2.5f;
55 hs.fS = 0.7f;
56 if (!BASS_FXSetParameters(lowpass, &lp) || !BASS_FXSetParameters(shelf, &hs))
57 {
58 Clear();
59 return false;
60 }
61 return true;
62 }
63};
64
65// Tracks game channels only; interface audio can share the same physical device
66// without inheriting the character's hearing effects. Remove before freeing a
67// tracked stream so a reused BASS handle cannot inherit stale effect handles.
69{
70 std::vector<EarMuffleOutput> channels;
71 float amount = 0.0f;
72
73public:
74 void Clear()
75 {
76 for (auto& channel : channels) channel.Clear();
77 channels.clear();
78 amount = 0.0f;
79 }
80
81 void Remove(HSTREAM stream)
82 {
83 auto found = std::find_if(channels.begin(), channels.end(),
84 [stream](const EarMuffleOutput& channel) { return channel.stream == stream; });
85 if (found != channels.end())
86 {
87 found->Clear();
88 channels.erase(found);
89 }
90 }
91
92 bool SetAmount(float value)
93 {
94 if (!std::isfinite(value) || value < 0.0f)
95 {
96 Clear();
97 return false;
98 }
99 if (value == 0.0f)
100 {
101 Clear();
102 return true;
103 }
104 value = (std::min)(value, 2.5f);
105 if (amount == value) return true;
106 amount = value;
107 for (auto& channel : channels)
108 {
109 if (!channel.Apply(amount))
110 {
111 Clear();
112 return false;
113 }
114 }
115 return true;
116 }
117
118 bool Apply(HSTREAM stream)
119 {
120 if (!stream || amount == 0.0f) return true;
121 auto found = std::find_if(channels.begin(), channels.end(),
122 [stream](const EarMuffleOutput& channel) { return channel.stream == stream; });
123 if (found != channels.end()) return true;
124 channels.push_back({stream});
125 if (channels.back().Apply(amount)) return true;
126 Clear();
127 return false;
128 }
129};
bool Apply(HSTREAM stream)
Definition EarMuffle.h:118
bool SetAmount(float value)
Definition EarMuffle.h:92
void Remove(HSTREAM stream)
Definition EarMuffle.h:81
std::vector< EarMuffleOutput > channels
Definition EarMuffle.h:70
HSTREAM stream
Definition EarMuffle.h:12
bool Apply(float amount)
Definition EarMuffle.h:23