From ff4eff8b10b5ad80962689d427555a8b497d95f5 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Mon, 22 Apr 2019 18:43:18 +0200 Subject: [PATCH] Add an optional gain parameter to SoundManager::play (#1078) * Add an optional gain parameter to SoundManager::play It can be used for example to make loud explosions. The default gain is 0.5f and valid values are in [0, 1] because if the Sound Volume is set to 100% in the settings, a gain value of more than 1 is reduced to 1. Since the sounds are now played half as loud with default gain, the default Sound Volume setting is now 100% * Increase the explosion sound gain * Adjust gains where set_gain is explicitly used --- src/audio/sound_manager.cpp | 8 +++++++- src/audio/sound_manager.hpp | 10 ++++++++-- src/badguy/bomb.cpp | 2 +- src/badguy/dart.cpp | 2 +- src/badguy/flame.cpp | 2 +- src/badguy/goldbomb.cpp | 2 +- src/badguy/treewillowisp.cpp | 2 +- src/badguy/willowisp.cpp | 2 +- src/object/explosion.cpp | 5 ++++- src/supertux/gameconfig.cpp | 2 +- 10 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index 579e1ffb640..8557ee6e444 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -184,13 +184,19 @@ SoundManager::preload(const std::string& filename) } void -SoundManager::play(const std::string& filename, const Vector& pos) +SoundManager::play(const std::string& filename, const Vector& pos, + const float gain) { if (!m_sound_enabled) return; + // Test gain for invalid values; it must not exceed 1 because in the end + // the value is set to min(sound_gain * sound_volume, 1) + assert(gain >= 0.0f && gain <= 1.0f); + try { std::unique_ptr source(intern_create_sound_source(filename)); + source->set_gain(gain); if (pos.x < 0 || pos.y < 0) { source->set_relative(true); diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index 7fb6f41a288..81681cfa898 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -57,8 +57,14 @@ class SoundManager final : public Currenton This function never throws exceptions, but might return a DummySoundSource */ std::unique_ptr create_sound_source(const std::string& filename); - /** Convenience function to simply play a sound at a given position. */ - void play(const std::string& name, const Vector& pos = Vector(-1, -1)); + /** Convenience functions to simply play a sound at a given position. */ + void play(const std::string& name, const Vector& pos = Vector(-1, -1), + const float gain = 0.5f); + void play(const std::string& name, const float gain) + { + play(name, Vector(-1, -1), gain); + } + /** Adds the source to the list of managed sources (= the source gets deleted when it finished playing) */ diff --git a/src/badguy/bomb.cpp b/src/badguy/bomb.cpp index 10a189a74df..2095fdd0e7a 100644 --- a/src/badguy/bomb.cpp +++ b/src/badguy/bomb.cpp @@ -34,7 +34,7 @@ Bomb::Bomb(const Vector& pos, Direction dir_, const std::string& custom_sprite / ticking->set_position(get_pos()); ticking->set_looping(true); - ticking->set_gain(2.0); + ticking->set_gain(1.0f); ticking->set_reference_distance(32); ticking->play(); } diff --git a/src/badguy/dart.cpp b/src/badguy/dart.cpp index 5ee9bc33b06..f151ba8d31c 100644 --- a/src/badguy/dart.cpp +++ b/src/badguy/dart.cpp @@ -73,7 +73,7 @@ Dart::activate() sound_source = SoundManager::current()->create_sound_source(DART_SOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); - sound_source->set_gain(1.0); + sound_source->set_gain(0.5f); sound_source->set_reference_distance(32); sound_source->play(); } diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index bb630211fc7..05df6ce6b2f 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -85,7 +85,7 @@ Flame::activate() sound_source = SoundManager::current()->create_sound_source(FLAME_SOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); - sound_source->set_gain(2.0); + sound_source->set_gain(1.0f); sound_source->set_reference_distance(32); sound_source->play(); } diff --git a/src/badguy/goldbomb.cpp b/src/badguy/goldbomb.cpp index f545e94f52a..6587246ecbe 100644 --- a/src/badguy/goldbomb.cpp +++ b/src/badguy/goldbomb.cpp @@ -124,7 +124,7 @@ GoldBomb::collision_squished(GameObject& object) ticking = SoundManager::current()->create_sound_source("sounds/fizz.wav"); ticking->set_position(get_pos()); ticking->set_looping(true); - ticking->set_gain(2.0); + ticking->set_gain(1.0f); ticking->set_reference_distance(32); ticking->play(); } diff --git a/src/badguy/treewillowisp.cpp b/src/badguy/treewillowisp.cpp index f1a32645357..b7e540c2bd8 100644 --- a/src/badguy/treewillowisp.cpp +++ b/src/badguy/treewillowisp.cpp @@ -56,7 +56,7 @@ TreeWillOWisp::activate() sound_source = SoundManager::current()->create_sound_source(TREEWILLOSOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); - sound_source->set_gain(2.0); + sound_source->set_gain(1.0f); sound_source->set_reference_distance(32); sound_source->play(); } diff --git a/src/badguy/willowisp.cpp b/src/badguy/willowisp.cpp index b0e7bbd2c34..8f550990435 100644 --- a/src/badguy/willowisp.cpp +++ b/src/badguy/willowisp.cpp @@ -160,7 +160,7 @@ WillOWisp::activate() m_sound_source = SoundManager::current()->create_sound_source(SOUNDFILE); m_sound_source->set_position(get_pos()); m_sound_source->set_looping(true); - m_sound_source->set_gain(2.0); + m_sound_source->set_gain(1.0f); m_sound_source->set_reference_distance(32); m_sound_source->play(); } diff --git a/src/object/explosion.cpp b/src/object/explosion.cpp index dc9ac31f7ef..dc0bce4ca8a 100644 --- a/src/object/explosion.cpp +++ b/src/object/explosion.cpp @@ -63,7 +63,10 @@ Explosion::explode() set_action(hurt ? "default" : "pop", 1); m_sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action m_sprite->set_angle(graphicsRandom.randf(0, 360)); // a random rotation on the sprite to make explosions appear more random - SoundManager::current()->play(hurt ? "sounds/explosion.wav" : "sounds/firecracker.ogg", get_pos()); + if (hurt) + SoundManager::current()->play("sounds/explosion.wav", get_pos(), 0.98f); + else + SoundManager::current()->play("sounds/firecracker.ogg", get_pos(), 0.7f); // spawn some particles int pnumber = push ? 8 : 100; diff --git a/src/supertux/gameconfig.cpp b/src/supertux/gameconfig.cpp index 5111541a046..fd188d74fbd 100644 --- a/src/supertux/gameconfig.cpp +++ b/src/supertux/gameconfig.cpp @@ -37,7 +37,7 @@ Config::Config() : show_player_pos(false), sound_enabled(true), music_enabled(true), - sound_volume(50), + sound_volume(100), music_volume(50), random_seed(0), // set by time(), by default (unless in config) enable_script_debugger(false),