00001 /* 00002 $Id: soundbuffer_static.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 "API/Core/System/error.h" 00020 #include "API/Core/System/cl_assert.h" 00021 #include <API/Sound/sound.h> 00022 #include <API/Sound/soundbuffer.h> 00023 #include <API/Sound/soundbuffer_session.h> 00024 #include <API/Sound/static_soundprovider.h> 00025 #include <Sound/Sound/Generic/cardsession_manager.h> 00026 #include <Sound/Sound/Generic/cardsoundbuffer_static.h> 00027 #include <Sound/Sound/Generic/soundbuffer_static.h> 00028 #include <Sound/Sound/Generic/soundcard_generic.h> 00029 #include <Sound/Sound/Generic/cardsoundbuffer_playback.h> 00030 00031 CL_SoundBuffer_Generic_Static::CL_SoundBuffer_Generic_Static( 00032 CL_StaticSoundProvider *_provider, 00033 bool _delete_provider, 00034 CL_Resource *resource) 00035 : 00036 CL_SoundBuffer_Generic(resource) 00037 { 00038 provider = _provider; 00039 delete_provider = _delete_provider; 00040 00041 int num_cards = CL_Sound::cards.size(); 00042 card_buffers = new CL_CardSoundBuffer_Static*[num_cards]; 00043 for (int i=0; i<num_cards; i++) card_buffers[i] = NULL; 00044 00045 volume = -1; 00046 pan = -1; 00047 frequency = -1; 00048 } 00049 00050 CL_SoundBuffer_Generic_Static::~CL_SoundBuffer_Generic_Static() 00051 { 00052 int num = CL_Sound::cards.size(); 00053 for (int i=0; i<num; i++) 00054 { 00055 CL_SoundCard_Generic *gen = (CL_SoundCard_Generic *) CL_Sound::cards[i]; 00056 gen->manager->remove_soundbuffer_playbacks(this); 00057 delete card_buffers[i]; 00058 } 00059 00060 if (delete_provider) delete provider; 00061 } 00062 00063 CL_StaticSoundProvider *CL_SoundBuffer_Generic_Static::get_static_provider() const 00064 { 00065 return provider; 00066 } 00067 00068 int CL_SoundBuffer_Generic_Static::get_length() const 00069 { 00070 return provider->data_size(); 00071 } 00072 00073 int CL_SoundBuffer_Generic_Static::get_num_samples() const 00074 { 00075 switch (provider->get_format()) 00076 { 00077 case sf_8bit_signed: 00078 return provider->data_size(); 00079 case sf_8bit_signed_stereo: 00080 case sf_16bit_signed: 00081 return provider->data_size()/2; 00082 00083 case sf_16bit_signed_stereo: 00084 return provider->data_size()/4; 00085 } 00086 00087 cl_assert(false); // invalid sample format. 00088 00089 return -1; 00090 } 00091 00092 int CL_SoundBuffer_Generic_Static::get_frequency() const 00093 { 00094 return frequency; 00095 } 00096 00097 bool CL_SoundBuffer_Generic_Static::set_frequency(int new_freq) 00098 { 00099 frequency = new_freq; 00100 return true; 00101 } 00102 00103 float CL_SoundBuffer_Generic_Static::get_volume() const 00104 { 00105 return volume; 00106 } 00107 00108 bool CL_SoundBuffer_Generic_Static::set_volume(float new_volume) 00109 { 00110 volume = new_volume; 00111 return true; 00112 } 00113 00114 float CL_SoundBuffer_Generic_Static::get_pan() const 00115 { 00116 return pan; 00117 } 00118 00119 bool CL_SoundBuffer_Generic_Static::set_pan(float new_pan) 00120 { 00121 pan = new_pan; 00122 return true; 00123 } 00124 00125 bool CL_SoundBuffer_Generic_Static::is_playing(CL_SoundBuffer_Session **session, CL_SoundCard *card) const 00126 { 00127 if (session != NULL) 00128 { 00129 return (*session)->is_playing(); 00130 } 00131 else 00132 { 00133 if (card != NULL) 00134 { 00135 return ((CL_SoundCard_Generic *) card)->manager->is_soundbuffer_playing((CL_SoundBuffer *) this); 00136 } 00137 else 00138 { 00139 if (CL_Sound::get_current_card() != NULL) 00140 { 00141 return ((CL_SoundCard_Generic *) CL_Sound::get_current_card())->manager->is_soundbuffer_playing((CL_SoundBuffer *) this); 00142 } 00143 else 00144 { 00145 return false; 00146 } 00147 } 00148 } 00149 return true; 00150 } 00151 00152 void CL_SoundBuffer_Generic_Static::stop(CL_SoundCard *card) 00153 { 00154 if (card != NULL) 00155 { 00156 ((CL_SoundCard_Generic *) card)->manager->remove_soundbuffer_playbacks(this); 00157 } 00158 else 00159 { 00160 if (CL_Sound::get_current_card() != NULL) 00161 { 00162 ((CL_SoundCard_Generic *) CL_Sound::get_current_card())->manager->remove_soundbuffer_playbacks(this); 00163 } 00164 } 00165 } 00166 00167 CL_SoundBuffer_Session CL_SoundBuffer_Generic_Static::play(bool looping, CL_SoundCard *card) 00168 { 00169 if (card == NULL) card = CL_Sound::get_current_card(); 00170 00171 CL_SoundBuffer_Session ret = prepare(looping, card); 00172 ret.set_looping(looping); 00173 ret.play(); 00174 00175 return ret; 00176 } 00177 00178 CL_SoundBuffer_Session CL_SoundBuffer_Generic_Static::prepare(bool looping, CL_SoundCard *card) 00179 { 00180 if (card == NULL) card = CL_Sound::get_current_card(); 00181 if (card == NULL) throw CL_Error("Cannot access sound card. Please make sure it's not in use"); 00182 00183 if (card_buffers[card->card_no] == NULL) 00184 { 00185 provider->lock(); 00186 00187 volume = 1; 00188 pan = 0; 00189 frequency = provider->get_frequency(); 00190 00191 card_buffers[card->card_no] = 00192 ((CL_SoundCard_Generic *) card)->create_soundbuffer_static(this, provider); 00193 00194 provider->unlock(); 00195 } 00196 00197 CL_SoundBuffer_Session ret( 00198 CL_SoundBuffer_Session( 00199 ((CL_SoundCard_Generic *) card)->manager->add_playback( 00200 card_buffers[card->card_no]->prepare()), 00201 card)); 00202 00203 ret.set_volume(volume); 00204 ret.set_frequency(frequency); 00205 ret.set_pan(pan); 00206 00207 return ret; 00208 }
1.2.6 written by Dimitri van Heesch,
© 1997-2001