From a21fb5079827ea71ff263a6ad3311b89976b445e Mon Sep 17 00:00:00 2001 From: Bastiaan Zapf Date: Fri, 6 May 2005 22:12:38 +0000 Subject: [PATCH] - SoundManager doesn't need Effects for now - ambient sound volume can be adjusted via mix_volume - ambient_sound is doing so now. Still clicks, because volume is done frame wise, not sample wise as it ought to be. SVN-Revision: 2422 --- data/levels/test/ambient_sound.stl | 2 +- src/audio/sound_manager.cpp | 31 +++++++++++---------------- src/audio/sound_manager.h | 7 ++---- src/object/ambient_sound.cpp | 44 ++++++++++++++++---------------------- src/object/ambient_sound.h | 26 +++++++++++++++++----- 5 files changed, 54 insertions(+), 56 deletions(-) diff --git a/data/levels/test/ambient_sound.stl b/data/levels/test/ambient_sound.stl index 63ab6764d..de699bc54 100644 --- a/data/levels/test/ambient_sound.stl +++ b/data/levels/test/ambient_sound.stl @@ -136,7 +136,7 @@ (speed 0.500000) ) (spawnpoint (name "main") (x 96) (y 160)) - (ambient_sound (x 128) (y 800) (distance_factor 0.1) + (ambient_sound (x 128) (y 800) (distance_factor 1) (distance_bias 200) (sample "waterfall")) (infoblock (x 128) (y 800) (message (_ "-Info diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index 8e0d558e7..d0c547e0f 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -25,6 +25,7 @@ #include #include "audio/sound_manager.h" + #include "audio/musicref.h" #include "moving_object.h" #include "resources.h" @@ -43,20 +44,6 @@ SoundManager::~SoundManager() sounds.clear(); } -void -SoundManager::play_sound(const std::string& name) -{ - if(!audio_device || !m_sound_enabled) - return; - - Mix_Chunk* chunk = preload_sound(name); - if(chunk == 0) { - std::cerr << "Sound '" << name << "' not found.\n"; - return; - } - Mix_PlayChannel(-1, chunk, 0); -} - int SoundManager::play_sound(const std::string& name,int loops) { @@ -71,6 +58,7 @@ SoundManager::play_sound(const std::string& name,int loops) return Mix_PlayChannel(-1, chunk, loops); } + void SoundManager::play_sound(const std::string& sound, const MovingObject* object, const Vector& pos) @@ -112,6 +100,10 @@ SoundManager::play_sound(const std::string& sound, const Vector& pos, Mix_SetPanning(chan, 24, 230); } +/* + +ok, i was stupid. We won't need this now. + // Register a sound effect function - basti_ void @@ -125,23 +117,24 @@ SoundManager::register_effect(int channel,Mix_EffectFunc_t f, // Adjust the Volume of a channel "on line". Needs sizeof(float) static data. -#define __ATYPE__ signed short int - void SoundManager::volume_adjust(int chan, void *stream, int len, void *udata) { + + typedef signed short int atype; + ((float *)udata)[1]=((float *)udata)[1]*0.95+ (((float *)udata)[0]- ((float *)udata)[1])*0.05; // decay towards [0] - declick float vol=((float*)udata)[1]; for (int i=0;iplay_sound(sample,-1); - volume_ptr=new float[2]; - volume_ptr[0]=0; - volume_ptr[1]=0; - sound_manager->register_effect(playing,&SoundManager::volume_adjust, - NULL,(void *)volume_ptr); + Mix_Volume(playing,0); + currentvolume=targetvolume=1e-20; } void @@ -86,16 +76,18 @@ AmbientSound::action(float) if (distance<0) distance=0; - volume_ptr[0]=1/(1+distance*distance_factor); // inverse square of distance + targetvolume=1/(1+distance*distance_factor); + float rise=targetvolume/currentvolume; + currentvolume*=pow(rise,.05); + currentvolume += 1e-30; + // std::cout << currentvolume << " " << targetvolume << std::endl; + Mix_Volume(playing,(int)(currentvolume*MIX_MAX_VOLUME)); } void -AmbientSound::draw(DrawingContext &dc) +AmbientSound::draw(DrawingContext &) { - return; - sprite->draw(dc,position,1); - } IMPLEMENT_FACTORY(AmbientSound, "ambient_sound"); diff --git a/src/object/ambient_sound.h b/src/object/ambient_sound.h index 7963c40a8..f6fd28508 100644 --- a/src/object/ambient_sound.h +++ b/src/object/ambient_sound.h @@ -17,6 +17,19 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. +/** + * Ambient Sound Source, beta version. Features: + * + * - "disc" like structure. Full volume up to some distance + * (distance_bias) to the source, then fading proportional to + * inverse square distance + * + * This is experimental and clicks a little. It is not possible to + * get the clicks out without sample-granular volume adjustment. + * + * basti_ + */ + #ifndef __AMBIENT_SOUND_H__ #define __AMBIENT_SOUND_H__ @@ -24,6 +37,7 @@ #include "game_object.h" #include "resources.h" #include "player.h" +#include "SDL_mixer.h" class AmbientSound : public GameObject { @@ -37,16 +51,18 @@ protected: virtual void startPlaying(); private: Vector position; - Sprite *sprite; std::string sample; int playing; - float distance_factor; - float distance_bias; - float silence_distance; + float distance_factor; /// distance scaling + float distance_bias; /// 100% volume disc radius + float silence_distance; /// not implemented yet + + float targetvolume; /// how loud we want to be + float currentvolume; /// how loud we are - float * volume_ptr; // this will be used by the volume adjustment effect. + float * volume_ptr; /// this will be used by the volume adjustment effect. }; #endif -- 2.11.0