Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

fadefilter_generic.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: fadefilter_generic.cpp,v 1.1 2001/03/06 15:09:26 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #ifdef WIN32
00016 #pragma warning (disable:4786)
00017 #endif
00018 
00019 #include "fadefilter_generic.h"
00020 
00021 CL_FadeFilter::CL_FadeFilter(int initial_volume)
00022 {
00023         impl = new CL_FadeFilter_Generic;
00024         impl->cur_volume = initial_volume/100.0f;
00025         impl->new_volume = initial_volume/100.0f;
00026         impl->speed = 0;
00027 }
00028 
00029 CL_FadeFilter::~CL_FadeFilter()
00030 {
00031         delete impl;
00032 }
00033 
00034 void CL_FadeFilter::fade_to_volume(int new_volume, int duration)
00035 {
00036         impl->new_volume = new_volume/100.0f;
00037 
00038         float delta_volume = impl->new_volume - impl->cur_volume;
00039         impl->speed = delta_volume / 22.05f / duration;
00040 }
00041 
00042 void CL_FadeFilter::filter(int *data, int size)
00043 {
00044         float &cur_volume = impl->cur_volume;
00045         float &new_volume = impl->new_volume;
00046         float &speed = impl->speed;
00047 
00048         for (int i=0; i<size*2; i++)
00049         {
00050                 data[i] = (int) (data[i] * cur_volume);
00051 
00052                 if ((i&1) == 1) // change volume for every second sample (because data is in stereo).
00053                 {
00054                         cur_volume += speed;
00055                         if (
00056                                 (speed > 0 && cur_volume > new_volume) ||
00057                                 (speed < 0 && cur_volume < new_volume))
00058                         {
00059                                 cur_volume = new_volume;
00060                                 speed = 0;
00061                         }
00062                 }
00063         }
00064 }

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001