From: Bastiaan Zapf Date: Fri, 6 May 2005 17:15:54 +0000 (+0000) Subject: SVN-Revision: 2418 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=20ca5e7c49d7d98aee81921ea706829e5ff231ed;p=supertux.git SVN-Revision: 2418 --- diff --git a/data/levels/test/ambient_sound.stl b/data/levels/test/ambient_sound.stl new file mode 100644 index 000000000..63ab6764d --- /dev/null +++ b/data/levels/test/ambient_sound.stl @@ -0,0 +1,149 @@ +;; Generated by Flexlay Editor +(supertux-level + (version 2) + (name (_ "Ambient Sound Test")) + (author "basti_") + (sector + (name "main") + (gravity 10.000000) + (background (image "arctis.jpg") + (speed 0.5)) + (tilemap + (layer "background") + (solid #f) + (speed 1.000000) + (width 30) + (height 30) + (tiles + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + )) + (tilemap + (layer "interactive") + (solid #t) + (speed 1.000000) + (width 30) + (height 30) + (tiles + 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 +61 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 0 0 0 61 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 61 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 + 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 61 + 61 0 0 0 0 61 61 61 61 61 61 61 61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 160)) + (ambient_sound (x 128) (y 800) (distance_factor 0.1) + (distance_bias 200) (sample "waterfall")) + (infoblock (x 128) (y 800) + (message (_ "-Info +#An ambient sound source +#should be audible here")) + ) + ) + ) + +;; EOF ;; diff --git a/data/sounds/waterfall.wav b/data/sounds/waterfall.wav new file mode 100644 index 000000000..52eb8e795 Binary files /dev/null and b/data/sounds/waterfall.wav differ diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index 9d2883e3d..8e0d558e7 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -57,6 +57,20 @@ SoundManager::play_sound(const std::string& name) Mix_PlayChannel(-1, chunk, 0); } +int +SoundManager::play_sound(const std::string& name,int loops) +{ + if(!audio_device || !m_sound_enabled) + return -1; + + Mix_Chunk* chunk = preload_sound(name); + if(chunk == 0) { + std::cerr << "Sound '" << name << "' not found.\n"; + return -1; + } + return Mix_PlayChannel(-1, chunk, loops); +} + void SoundManager::play_sound(const std::string& sound, const MovingObject* object, const Vector& pos) @@ -98,6 +112,38 @@ SoundManager::play_sound(const std::string& sound, const Vector& pos, Mix_SetPanning(chan, 24, 230); } +// Register a sound effect function - basti_ + +void +SoundManager::register_effect(int channel,Mix_EffectFunc_t f, + Mix_EffectDone_t d,void * arg) { + + if(!audio_device || !m_sound_enabled) + return; + Mix_RegisterEffect(channel,f,d,arg); +} + +// 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) { + ((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;i + +#include "ambient_sound.h" +#include "object_factory.h" +#include "lisp/lisp.h" +#include "sector.h" + +/*** + * 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, clicks sometimes and still leaks mem + * + * basti_ + */ + +AmbientSound::AmbientSound(const lisp::Lisp& lisp) +{ + + // position=pos; + lisp.get("x", position.x); + lisp.get("y", position.y); + lisp.get("distance_factor",distance_factor); + lisp.get("distance_bias" ,distance_bias ); + lisp.get("sample" ,sample ); + + if (distance_factor == 0) + silence_distance = 10e99; + else + silence_distance = distance_factor*10e2; + + volume_ptr=NULL; + playing=0; + startPlaying(); +} + + +void +AmbientSound::hit(Player& ) +{ +} + +void +AmbientSound::startPlaying() +{ + playing=sound_manager->play_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); +} + +void +AmbientSound::action(float) +{ + float dx=Sector::current()->player->get_pos().x-position.x; + float dy=Sector::current()->player->get_pos().y-position.y; + float distance=sqrt(dx*dx+dy*dy); + + distance-=distance_bias; + + if (distance<0) + distance=0; + + volume_ptr[0]=1/(1+distance*distance_factor); // inverse square of distance + +} + +void +AmbientSound::draw(DrawingContext &dc) +{ + 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 new file mode 100644 index 000000000..7963c40a8 --- /dev/null +++ b/src/object/ambient_sound.h @@ -0,0 +1,53 @@ +// ambient_sound.h basti_ +// +// SuperTux +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. + +#ifndef __AMBIENT_SOUND_H__ +#define __AMBIENT_SOUND_H__ + + +#include "game_object.h" +#include "resources.h" +#include "player.h" + +class AmbientSound : public GameObject +{ +public: + AmbientSound(const lisp::Lisp& lisp); + +protected: + virtual void hit(Player& player); + virtual void action(float time); + virtual void draw(DrawingContext&); + virtual void startPlaying(); +private: + Vector position; + Sprite *sprite; + + std::string sample; + int playing; + float distance_factor; + + float distance_bias; + float silence_distance; + + float * volume_ptr; // this will be used by the volume adjustment effect. +}; + +#endif +