Skip to content

Commit 97c4466

Browse files
committed
Get rid of some stupid javisms
1 parent 66b2f11 commit 97c4466

File tree

4 files changed

+45
-106
lines changed

4 files changed

+45
-106
lines changed

src/engine/audio/Audio.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ namespace Audio {
194194
}
195195

196196
for (int i = 0; i < MAX_GENTITIES; i++) {
197-
auto& loop = entityLoops[i];
197+
entityLoop_t& loop = entityLoops[i];
198198
if (loop.sound and not loop.addedThisFrame) {
199199
// The loop wasn't added this frame, that means it has to be removed.
200-
loop.sound->SetSoundGain( 0 );
200+
loop.sound->soundGain = 0;
201201
} else if (loop.oldSfx != loop.newSfx) {
202202
// The last sfx added in the frame is not the current one being played
203203
// To mimic the previous sound system's behavior we sart playing the new one.
@@ -341,7 +341,7 @@ namespace Audio {
341341

342342
StopMusic();
343343
music = std::make_shared<LoopingSound>(loopingSample, leadingSample);
344-
music->SetVolumeModifier(musicVolume);
344+
music->volumeModifier = &musicVolume;
345345
AddSound(GetLocalEmitter(), music, 1);
346346
}
347347

@@ -379,7 +379,7 @@ namespace Audio {
379379
}
380380
}
381381

382-
streams[streamNum]->SetGain(volume);
382+
streams[streamNum]->soundGain = volume;
383383

384384
AudioData audioData { rate, width, channels };
385385
audioData.rawSamples.resize( width * numSamples * channels );

src/engine/audio/Emitter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ namespace Audio {
261261
Emitter::~Emitter() = default;
262262

263263
void Emitter::SetupSound(Sound& sound) {
264-
sound.GetSource().SetReferenceDistance(120.0f);
264+
sound.source->SetReferenceDistance(120.0f);
265265
InternalSetupSound(sound);
266266
UpdateSound(sound);
267267
}
@@ -278,7 +278,7 @@ namespace Audio {
278278
}
279279

280280
void EntityEmitter::UpdateSound(Sound& sound) {
281-
AL::Source& source = sound.GetSource();
281+
AL::Source& source = *sound.source;
282282

283283
if (entityNum == listenerEntity) {
284284
MakeLocal(source);
@@ -288,7 +288,7 @@ namespace Audio {
288288
}
289289

290290
void EntityEmitter::InternalSetupSound(Sound& sound) {
291-
AL::Source& source = sound.GetSource();
291+
AL::Source& source = *sound.source;
292292

293293
Make3D(source, entities[entityNum].position, entities[entityNum].velocity);
294294
}
@@ -306,13 +306,13 @@ namespace Audio {
306306
}
307307

308308
void PositionEmitter::UpdateSound(Sound& sound) {
309-
AL::Source& source = sound.GetSource();
309+
AL::Source& source = *sound.source;
310310

311311
Make3D(source, position, origin);
312312
}
313313

314314
void PositionEmitter::InternalSetupSound(Sound& sound) {
315-
AL::Source& source = sound.GetSource();
315+
AL::Source& source = *sound.source;
316316

317317
Make3D(source, position, origin);
318318
}
@@ -334,7 +334,7 @@ namespace Audio {
334334
}
335335

336336
void LocalEmitter::InternalSetupSound(Sound& sound) {
337-
AL::Source& source = sound.GetSource();
337+
AL::Source& source = *sound.source;
338338

339339
MakeLocal(source);
340340
}

src/engine/audio/Sound.cpp

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ namespace Audio {
8585

8686
for (int i = 0; i < nSources; i++) {
8787
if (sources[i].active) {
88-
auto sound = sources[i].usingSound;
88+
std::shared_ptr<Sound> sound = sources[i].usingSound;
8989

9090
// Update and Emitter::UpdateSound can call Sound::Stop
91-
if (not sound->IsStopped()) {
91+
if ( sound->playing ) {
9292
sound->Update();
9393
}
9494

95-
if (not sound->IsStopped()) {
96-
sound->GetEmitter()->UpdateSound(*sound);
95+
if ( sound->playing ) {
96+
sound->emitter->UpdateSound(*sound);
9797
}
9898

99-
if (sound->IsStopped()) {
99+
if ( !sound->playing ) {
100100
sources[i].active = false;
101101
sources[i].usingSound = nullptr;
102102
}
@@ -126,7 +126,7 @@ namespace Audio {
126126
if (source) {
127127
// Make the source forget if it was a "static" or a "streaming" source.
128128
source->source.ResetBuffer();
129-
sound->SetEmitter(emitter);
129+
sound->emitter = emitter;
130130
sound->AcquireSource(source->source);
131131
source->usingSound = sound;
132132
source->priority = priority;
@@ -188,40 +188,6 @@ namespace Audio {
188188
playing = false;
189189
}
190190

191-
bool Sound::IsStopped() {
192-
return not playing;
193-
}
194-
195-
void Sound::SetPositionalGain(float gain) {
196-
positionalGain = gain;
197-
}
198-
199-
void Sound::SetSoundGain(float gain) {
200-
soundGain = gain;
201-
}
202-
203-
float Sound::GetCurrentGain() {
204-
return currentGain;
205-
}
206-
207-
void Sound::SetVolumeModifier(const Cvar::Range<Cvar::Cvar<float>>& volumeMod)
208-
{
209-
this->volumeModifier = &volumeMod;
210-
}
211-
212-
float Sound::GetVolumeModifier() const
213-
{
214-
return volumeModifier->Get();
215-
}
216-
217-
void Sound::SetEmitter(std::shared_ptr<Emitter> emitter) {
218-
this->emitter = emitter;
219-
}
220-
221-
std::shared_ptr<Emitter> Sound::GetEmitter() {
222-
return emitter;
223-
}
224-
225191
void Sound::AcquireSource(AL::Source& source) {
226192
this->source = &source;
227193

@@ -231,19 +197,15 @@ namespace Audio {
231197
emitter->SetupSound(*this);
232198
}
233199

234-
AL::Source& Sound::GetSource() {
235-
return *source;
236-
}
237-
238200
// Set the gain before the source is started to avoid having a few milliseconds of very loud sound
239201
void Sound::FinishSetup() {
240-
currentGain = positionalGain * soundGain * SliderToAmplitude(GetVolumeModifier());
202+
currentGain = positionalGain * soundGain * SliderToAmplitude(volumeModifier->Get());
241203
source->SetGain(currentGain);
242204
}
243205

244206
void Sound::Update() {
245207
// Fade the Gain update to avoid "ticking" sounds when there is a gain discontinuity
246-
float targetGain = positionalGain * soundGain * SliderToAmplitude(GetVolumeModifier());
208+
float targetGain = positionalGain * soundGain * SliderToAmplitude(volumeModifier->Get());
247209

248210
//TODO make it framerate independent and fade out in about 1/8 seconds ?
249211
if (currentGain > targetGain) {
@@ -267,15 +229,15 @@ namespace Audio {
267229

268230
void OneShotSound::SetupSource(AL::Source& source) {
269231
source.SetBuffer(sample->GetBuffer());
270-
SetSoundGain(GetVolumeModifier());
232+
soundGain = volumeModifier->Get();
271233
}
272234

273235
void OneShotSound::InternalUpdate() {
274-
if (GetSource().IsStopped()) {
236+
if ( source->IsStopped() ) {
275237
Stop();
276238
return;
277239
}
278-
SetSoundGain(GetVolumeModifier());
240+
soundGain = volumeModifier->Get();
279241
}
280242

281243
// Implementation of LoopingSound
@@ -289,7 +251,7 @@ namespace Audio {
289251

290252
void LoopingSound::FadeOutAndDie() {
291253
fadingOut = true;
292-
SetSoundGain(0.0f);
254+
soundGain = 0.0f;
293255
}
294256

295257
void LoopingSound::SetupSource(AL::Source& source) {
@@ -298,23 +260,23 @@ namespace Audio {
298260
} else {
299261
SetupLoopingSound(source);
300262
}
301-
SetSoundGain(GetVolumeModifier());
263+
soundGain = volumeModifier->Get();
302264
}
303265

304266
void LoopingSound::InternalUpdate() {
305-
if (fadingOut and GetCurrentGain() == 0.0f) {
267+
if (fadingOut and currentGain == 0.0f) {
306268
Stop();
307269
}
308270

309271
if (not fadingOut) {
310272
if (leadingSample) {
311-
if (GetSource().IsStopped()) {
312-
SetupLoopingSound(GetSource());
313-
GetSource().Play();
273+
if ( source->IsStopped() ) {
274+
SetupLoopingSound( *source );
275+
source->Play();
314276
leadingSample = nullptr;
315277
}
316278
}
317-
SetSoundGain(GetVolumeModifier());
279+
soundGain = volumeModifier->Get();
318280
}
319281
}
320282

@@ -335,32 +297,25 @@ namespace Audio {
335297
}
336298

337299
void StreamingSound::InternalUpdate() {
338-
AL::Source& source = GetSource();
339-
340-
while (source.GetNumProcessedBuffers() > 0) {
341-
source.PopBuffer();
300+
while ( source->GetNumProcessedBuffers() > 0 ) {
301+
source->PopBuffer();
342302
}
343303

344-
if (source.GetNumQueuedBuffers() == 0) {
304+
if ( source->GetNumQueuedBuffers() == 0 ) {
345305
Stop();
346306
}
347307
}
348308

349309
//TODO somehow try to catch back when data is coming faster than we consume (e.g. capture data)
350310
void StreamingSound::AppendBuffer(AL::Buffer buffer) {
351-
if (IsStopped()) {
311+
if ( !playing ) {
352312
return;
353313
}
354314

355-
AL::Source& source = GetSource();
356-
source.QueueBuffer(std::move(buffer));
315+
source->QueueBuffer(std::move(buffer));
357316

358-
if (source.IsStopped()) {
359-
source.Play();
317+
if ( source->IsStopped() ) {
318+
source->Play();
360319
}
361320
}
362-
363-
void StreamingSound::SetGain(float gain) {
364-
SetSoundGain(gain);
365-
}
366321
}

src/engine/audio/Sound.h

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,24 @@ namespace Audio {
5454
//TODO sound.mute
5555
class Sound {
5656
public:
57+
float positionalGain;
58+
float soundGain;
59+
float currentGain;
60+
61+
bool playing;
62+
const Cvar::Range<Cvar::Cvar<float>>* volumeModifier;
63+
64+
AL::Source* source;
65+
std::shared_ptr<Emitter> emitter;
66+
5767
Sound();
5868
virtual ~Sound();
5969

6070
void Play();
6171
// Stop the source and marks the sound for deletion.
6272
void Stop();
63-
bool IsStopped();
64-
65-
// The is attenuated because of its inherent porperties and because of its position.
66-
// Each attenuation can be set separately.
67-
void SetPositionalGain(float gain);
68-
void SetSoundGain(float gain);
69-
float GetCurrentGain();
70-
71-
// sfx vs. music
72-
void SetVolumeModifier(const Cvar::Range<Cvar::Cvar<float>>& volumeMod);
73-
float GetVolumeModifier() const;
74-
75-
void SetEmitter(std::shared_ptr<Emitter> emitter);
76-
std::shared_ptr<Emitter> GetEmitter();
7773

7874
void AcquireSource(AL::Source& source);
79-
AL::Source& GetSource();
8075

8176
// Used to setup a source for a specific kind of sound and to start the sound.
8277
virtual void SetupSource(AL::Source& source) = 0;
@@ -85,16 +80,6 @@ namespace Audio {
8580
void Update();
8681
// Called each frame, after emitters have been updated.
8782
virtual void InternalUpdate() = 0;
88-
89-
private:
90-
float positionalGain;
91-
float soundGain;
92-
float currentGain;
93-
94-
bool playing;
95-
std::shared_ptr<Emitter> emitter;
96-
const Cvar::Range<Cvar::Cvar<float>>* volumeModifier;
97-
AL::Source* source;
9883
};
9984

10085
// A sound that is played once.
@@ -139,7 +124,6 @@ namespace Audio {
139124
virtual void InternalUpdate() override;
140125

141126
void AppendBuffer(AL::Buffer buffer);
142-
void SetGain(float gain);
143127
};
144128

145129
}

0 commit comments

Comments
 (0)