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

playback_static.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: playback_static.cpp,v 1.1 2001/03/06 15:09:25 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 #ifdef USE_CLANSOUND
00020 
00021 #include <Sound/Sound/ClanSound/soundbuffer_static_clan.h>
00022 #include <Sound/Sound/ClanSound/cardplayback_clan.h>
00023 #include <Sound/Sound/ClanSound/playback_static.h>
00024 #include <Sound/Sound/ClanSound/soundcard_clan.h>
00025 
00026 #include <math.h>
00027 
00028 CL_Playback_Static::CL_Playback_Static(
00029         CL_SoundCard_ClanSound *card,
00030         CL_CardBuffer_Static_ClanSound *static_buffer)
00031 : CL_CardPlayback_ClanSound(card)
00032 {
00033         volume = 1.0f;
00034         pan = 0.0f;
00035         frequency = 0;
00036         pos = 0;
00037         playing = false;
00038         
00039         this->static_buffer = static_buffer;
00040         card->add(this);
00041 }
00042 
00043 CL_Playback_Static::~CL_Playback_Static()
00044 {
00045         card->remove(this);
00046 }
00047 
00048 void CL_Playback_Static::mix_to(int *data, int num_samples)
00049 {
00050         static int temp[16*1024*2];
00051 
00052         for (int i=0; i<num_samples; i+=16*1024)
00053         {
00054                 int size = num_samples-i;
00055                 if (size > 16*1024) size = 16*1024;
00056                 
00057                 get_playback_data(temp, size);
00058                 filter(temp, size);
00059                 for (int j=0; j<size*2; j++) data[i+j] += temp[j];
00060         }
00061 }
00062 
00063 // Functions inherited from Generic:
00064 // ---------------------------------
00065 
00066 int CL_Playback_Static::get_position()
00067 {
00068         return (int) pos;
00069 }
00070 
00071 float CL_Playback_Static::get_position_relative()
00072 {
00073         return pos / (float) static_buffer->get_size();
00074 }
00075 
00076 bool CL_Playback_Static::set_position(int new_pos)
00077 {
00078         pos = new_pos;
00079 
00080         return true;
00081 }
00082 
00083 bool CL_Playback_Static::set_position_relative(float new_pos)
00084 {
00085         pos = new_pos * static_buffer->get_size();
00086         if (pos < 0) pos = 0;
00087         if (pos > static_buffer->get_size()) pos = static_buffer->get_size();
00088 
00089         return true;
00090 }
00091 
00092 int CL_Playback_Static::get_length()
00093 {
00094         return static_buffer->get_size();
00095 }
00096 
00097 int CL_Playback_Static::get_frequency()
00098 {
00099         return frequency;
00100 }
00101 
00102 bool CL_Playback_Static::set_frequency(int new_freq)
00103 {
00104         frequency = new_freq;
00105         return true;
00106 }
00107 
00108 float CL_Playback_Static::get_volume()
00109 {
00110         return volume;
00111 }
00112 
00113 bool CL_Playback_Static::set_volume(float new_volume)
00114 {
00115         volume = new_volume;
00116 //      cs_set_volume(session_id, (int) (new_volume*128));
00117         return true;
00118 }
00119 
00120 float CL_Playback_Static::get_pan()
00121 {
00122         return pan;
00123 }
00124 
00125 bool CL_Playback_Static::set_pan(float new_pan)
00126 {
00127         pan = new_pan;
00128         return true;
00129 }
00130 
00131 void CL_Playback_Static::set_looping(bool looping)
00132 {
00133         static bool warning = true;
00134         if (warning)
00135         {
00136                 cl_info(info_sound, "Not implemented.");
00137                 warning = false;
00138         }
00139 }
00140 
00141 bool CL_Playback_Static::get_looping()
00142 {
00143         return false;
00144 }
00145 
00146 void CL_Playback_Static::play()
00147 {
00148         playing = true;
00149 }
00150 
00151 void CL_Playback_Static::stop()
00152 {
00153         playing = false;
00154 }
00155 
00156 bool CL_Playback_Static::is_playing()
00157 {
00158         return playing;
00159 }
00160 
00161 // Mixer interface:
00162 // ----------------
00163 
00164 void CL_Playback_Static::get_playback_data(int *data, int num_samples)
00165 {
00166         int i;
00167         int freq = frequency;
00168         int vol = (int) (volume*128);
00169 
00170         if (freq == 0 || vol==0 || playing == false)
00171         {
00172                 memset(data, 0, num_samples*sizeof(int)*2);
00173                 return;
00174         }
00175 
00176         double speed = freq/(double) 22050;
00177         double fill_samples = (static_buffer->get_size()-pos) / speed;
00178 
00179         if (fill_samples >= num_samples) fill_samples = num_samples;
00180         else // reached end of buffer - pad the rest with silence
00181         {
00182                 memset(
00183                         data,
00184                         0,
00185                         sizeof(int)*num_samples*2);
00186                 
00187                 playing = false;
00188         }
00189 
00190         int *ptr = data;
00191         short *src_data = static_buffer->get_data();
00192         for (i=0; i<fill_samples; i++)
00193         {
00194                 int cur_pos = (int) pos;
00195 
00196                 // debug: (remove this one if it hasn't occoured in a long
00197                 // time.
00198                 // cs_assert(cur_pos < static_buffer->get_size());
00199 
00200                 *(ptr++) = src_data[cur_pos*2+0];
00201                 *(ptr++) = src_data[cur_pos*2+1];
00202                 
00203                 pos += speed;
00204         }
00205 
00206         // apply volume and panning:
00207         ptr = data;
00208         int left_pan = (int) (128-pan*128);
00209         int right_pan = (int) (128+pan*128);
00210         if (left_pan < 0) left_pan = 0;
00211         if (left_pan > 128) left_pan = 128;
00212         if (right_pan < 0) right_pan = 0;
00213         if (right_pan > 128) right_pan = 128;
00214 
00215         for (i=0; i<fill_samples; i++)
00216         {
00217                 // left channel:
00218                 (*ptr) = (*ptr)*vol*left_pan/16384; ptr++;
00219                 
00220                 // right channel:
00221                 (*ptr) = (*ptr)*vol*right_pan/16384; ptr++;
00222         }
00223 
00224         if (!playing) // stopped playing because buffer ended
00225         {
00226                 pos = 0; // is this the behaviour of a directsound buffer? -- dok
00227         }
00228 }
00229 
00230 #endif

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