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

streamed_mikmod_sample.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: streamed_mikmod_sample.cpp,v 1.4 2001/01/30 23:34:05 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         File purpose:
00015                 Streamed sample
00016 
00017 */
00018 
00019 #include "API/MikMod/streamed_mikmod_sample.h"
00020 #include "resourcetype_module.h"
00021 #include "module_reader.h"
00022 
00023 /*****************************
00024       CL_Streamed_MikModSample
00025 *****************************/
00026 
00027 CL_SoundBuffer *CL_Streamed_MikModSample::create(
00028         const char *filename,
00029         CL_InputSourceProvider *inputprovider, 
00030         bool looped)
00031 {
00032         return CL_SoundBuffer::create(new CL_Streamed_MikModSample(filename, inputprovider, looped), true);
00033 }
00034 
00035 CL_SoundBuffer *CL_Streamed_MikModSample::load(
00036         const char *res_id,
00037         CL_ResourceManager *manager, 
00038         bool looped)
00039 {
00040         return CL_SoundBuffer::create(new CL_Streamed_MikModSample(res_id, manager, looped), true);
00041 }
00042 
00043 CL_Streamed_MikModSample::CL_Streamed_MikModSample(
00044         const char *_filename, 
00045         CL_InputSourceProvider *_inputprovider, 
00046         bool _looped)
00047 {
00048         CL_InputSourceProvider *inputprovider;
00049         filename = _filename;
00050         looped = _looped;
00051 
00052         if (_inputprovider == NULL)
00053         {
00054                 inputprovider = CL_InputSourceProvider::create_file_provider(".");
00055         }
00056         else
00057         {
00058                 inputprovider = _inputprovider->clone();
00059         }
00060 
00061         CL_InputSource *input=inputprovider->open_source(filename.c_str());
00062         cl_assert ( input != NULL );
00063 
00064         MREADER *reader=new_clanlib_reader((void *) input);
00065         cl_assert ( reader != NULL );
00066 
00067         module = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);
00068 
00069         if (module == NULL)
00070         {
00071                 throw CL_Error(MikMod_strerror(MikMod_errno));
00072         }
00073 
00074         delete_clanlib_reader(reader);
00075         delete input;
00076         delete inputprovider;
00077 }
00078 
00079 CL_Streamed_MikModSample::CL_Streamed_MikModSample(
00080         const char *_res_id, 
00081         CL_ResourceManager *_manager, 
00082         bool _looped)
00083 {
00084         looped=_looped;
00085         CL_ModuleResource *res;
00086         res=(CL_ModuleResource *) _manager->get_resource(_res_id);
00087         module=res->get_value();
00088 }
00089 
00090 CL_Streamed_MikModSample::~CL_Streamed_MikModSample()
00091 {
00092         Player_Free(module);
00093 }
00094 
00095 CL_StreamSoundProvider_Session *CL_Streamed_MikModSample::begin_session()
00096 {
00097         return new CL_Streamed_MikModSample_Session(module, looped);
00098 }
00099 
00100 void CL_Streamed_MikModSample::end_session(CL_StreamSoundProvider_Session *session)
00101 {
00102         delete session;
00103 }
00104 
00105 
00106 /*****************************
00107   CL_Streamed_MikModSample_Session
00108 *****************************/
00109 
00110 CL_Streamed_MikModSample_Session::CL_Streamed_MikModSample_Session(
00111         MODULE *_module,
00112         bool _looped)
00113 {
00114         module=_module;
00115         looped=_looped;
00116 
00117         cl_assert (module != NULL);
00118 
00119         module->loop=looped;
00120 
00121         Player_Start (module);
00122 
00123         sample_freq = md_mixfreq;
00124 
00125         int bytes_per_sample = ((md_mode&DMODE_STEREO)?2:1) * ((md_mode&DMODE_16BITS)?2:1);
00126 
00127         if (((md_mode&DMODE_STEREO)?2:1) == 2 && bytes_per_sample == 4) sample_format = sf_16bit_signed_stereo;
00128         else if (((md_mode&DMODE_STEREO)?2:1) == 2 && bytes_per_sample == 2) sample_format = sf_8bit_signed_stereo;
00129         else if (((md_mode&DMODE_STEREO)?2:1) == 1 && bytes_per_sample == 2) sample_format = sf_16bit_signed;
00130         else if (((md_mode&DMODE_STEREO)?2:1) == 1 && bytes_per_sample == 1) sample_format = sf_8bit_signed;
00131         else
00132         {
00133                 std::cout << "    Invalid wave file format         " << std::endl;
00134                 std::cout << "---------------------------------" << std::endl;
00135                 std::cout << "Sample size: " << sample_size << std::endl;
00136                 std::cout << "Sample frequency: " << sample_freq << std::endl;
00137                 std::cout << "Number of channels: " << ((md_mode&DMODE_STEREO)?2:1) << std::endl;
00138                 std::cout << "Number of bytes pr. sample: " << bytes_per_sample << std::endl;
00139                 std::cout << "---------------------------------" << std::endl;
00140 
00141                 throw CL_Error("Invalid wave file format");
00142         }
00143 }
00144 
00145 CL_Streamed_MikModSample_Session::~CL_Streamed_MikModSample_Session()
00146 {
00147         Player_Stop();
00148 }
00149 
00150 bool CL_Streamed_MikModSample_Session::eof() const
00151 {
00152         return !Player_Active();
00153 }
00154 
00155 bool CL_Streamed_MikModSample_Session::set_position(int pos)
00156 {
00157         Player_SetPosition(pos);
00158         return true; // (Player_GetPosition() == pos); does not exist, what to do?
00159 }
00160 
00161 bool CL_Streamed_MikModSample_Session::play()
00162 {
00163         if (Player_Active())
00164                 return false;
00165                 
00166         Player_Start(module);
00167         return Player_Active() != 0;
00168 }
00169 
00170 void CL_Streamed_MikModSample_Session::stop()
00171 {
00172         Player_Stop();
00173 }
00174 
00175 int CL_Streamed_MikModSample_Session::get_data(void *data_ptr, int data_requested)
00176 {
00177         MikMod_Update();
00178         int read = VC_WriteBytes((SBYTE*)data_ptr,data_requested);
00179 
00180         return read;
00181 }
00182 
00183 int CL_Streamed_MikModSample_Session::get_frequency() const
00184 {
00185         return sample_freq;
00186 }
00187 
00188 SoundFormat CL_Streamed_MikModSample_Session::get_format() const
00189 {
00190         return sample_format;
00191 }
00192 
00193 // EOF //

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