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

resourcetype_sample.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: resourcetype_sample.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         File purpose:
00015                 Sample resource type
00016 
00017 */
00018 
00019 #ifdef WIN32
00020 #pragma warning (disable:4786)
00021 #endif
00022 
00023 #include <API/Core/IOData/inputsource.h>
00024 #include <API/Core/IOData/inputsource_provider.h>
00025 #include <API/Core/IOData/outputsource.h>
00026 #include <API/Core/Resources/resource.h>
00027 #include <API/Core/Resources/resource_manager.h>
00028 #include <API/Core/Resources/resourceoptions.h>
00029 #include <API/Core/Resources/resourcetype.h>
00030 #include <API/Core/System/clanstring.h>
00031 #include <API/Core/System/cl_assert.h>
00032 #include <API/Sound/soundbuffer.h>
00033 #include <API/Sound/static_soundprovider.h>
00034 #include <API/Sound/stream_soundprovider.h>
00035 #include <API/Sound/SoundProviders/static_provider_wave.h>
00036 #include <API/Sound/SoundProviders/stream_provider_wave.h>
00037 #include <Sound/Sound/Generic/resourcetype_sample.h>
00038 
00039 /******************************************************************************
00040                                                 class CL_Res_Sample
00041 ******************************************************************************/
00042 
00043 CL_Res_Sample::CL_Res_Sample() : CL_ResourceType("sample")
00044 {
00045 }
00046 
00047 CL_Resource *CL_Res_Sample::create_from_location(
00048         std::string name,
00049         std::string location,
00050         CL_ResourceOptions *options,
00051         CL_ResourceManager *parent)
00052 {
00053         CL_String ext = CL_String(location).right(4);
00054         ext.to_lower();
00055 
00056         if ((options->exists("type") && options->get_value("type") == "sample") ||
00057                 (options->exists("wav") || ext == ".wav"))
00058         {
00059                 return new CL_SampleFileResource(
00060                         name,
00061                         location,
00062                         options,
00063                         parent);
00064         }
00065 
00066         return NULL;
00067 }
00068 
00069 CL_Resource *CL_Res_Sample::create_from_serialization(
00070         std::string name,
00071         CL_ResourceManager *parent)
00072 {
00073         return new CL_SampleDatafileResource(
00074                 name,
00075                 parent);
00076 }
00077 
00078 /******************************************************************************
00079                                                 class CL_WritableSampleResource
00080 ******************************************************************************/
00081 
00082 CL_WritableSampleResource::CL_WritableSampleResource(std::string name)
00083 : CL_Resource("sample", name)
00084 { 
00085         loaded = false;
00086         loaded_sample = NULL;
00087         load_count = 0;
00088 }
00089 
00090 void CL_WritableSampleResource::serialize_save(CL_OutputSource *output)
00091 {
00092         CL_InputSourceProvider *provider = CL_InputSourceProvider::create_file_provider(".");
00093         cl_assert(provider != NULL);
00094 
00095         CL_InputSource *wavefile = provider->open_source(sample_location);
00096         cl_assert(wavefile != NULL);
00097 
00098         int size_left = wavefile->size();
00099         while (size_left > 0)
00100         {
00101                 unsigned char buffer[32768];
00102                 int read = wavefile->read(buffer, 32768);
00103                 size_left -= read;
00104                 output->write(buffer, read);
00105         }
00106 
00107         delete wavefile;
00108         delete provider;
00109 }
00110 
00111 void CL_WritableSampleResource::load()
00112 {
00113         load_count++;
00114         if (loaded) return;
00115 
00116         create_sample();
00117 }
00118 
00119 void CL_WritableSampleResource::unload()
00120 {
00121         load_count--;
00122         if (load_count == 0)
00123         {
00124                 delete loaded_sample;
00125                 loaded = false;
00126         }
00127 }
00128 
00129 /******************************************************************************
00130                                                 class CL_SampleFileResource
00131 ******************************************************************************/
00132 
00133 CL_SampleFileResource::CL_SampleFileResource(
00134         std::string name,
00135         std::string location, 
00136         CL_ResourceOptions *_options, 
00137         CL_ResourceManager *_parent)
00138 : CL_WritableSampleResource(name)
00139 {
00140         sample_location = location;
00141         options = _options;
00142         parent = _parent;
00143 }
00144 
00145 CL_SampleFileResource::~CL_SampleFileResource()
00146 {
00147         delete options;
00148 }
00149 
00150 CL_SoundBuffer *CL_SampleFileResource::create_sample()
00151 {
00152         if (loaded) return loaded_sample;
00153 
00154         if (options->exists("stream"))
00155         {
00156                 loaded_sample = CL_SoundBuffer::create(
00157                         new CL_Streamed_WaveSample(
00158                                 sample_location,
00159                                 parent->get_resource_provider(),
00160                                 options->exists("loop")),
00161                         true);
00162         }
00163         else
00164         {
00165                 loaded_sample = CL_SoundBuffer::create(
00166                         new CL_Sample(
00167                                 sample_location,
00168                                 parent->get_resource_provider()),
00169                         true);
00170         }
00171 
00172         loaded = true;
00173         return loaded_sample;
00174 }
00175 
00176 
00177 /******************************************************************************
00178                                                 class CL_SampleDatafileResource
00179 ******************************************************************************/
00180 
00181 CL_SampleDatafileResource::CL_SampleDatafileResource(
00182         std::string name,
00183         CL_ResourceManager *_parent)
00184 : CL_WritableSampleResource(name)
00185 {
00186         sample_location = "";
00187         options = NULL;
00188         parent = _parent;
00189 }
00190 
00191 CL_SampleDatafileResource::~CL_SampleDatafileResource()
00192 {
00193         delete options;
00194 }
00195 
00196 CL_SoundBuffer *CL_SampleDatafileResource::create_sample()
00197 {
00198         if (loaded) return loaded_sample;
00199 
00200         int streamed = false; // todo: somehow write this into the stream.
00201 
00202         if (streamed)
00203         {
00204                 loaded_sample = CL_SoundBuffer::create(
00205                         new CL_Streamed_WaveSample(
00206                                 get_name().c_str(),
00207                                 parent->get_resource_provider(),
00208                                 false /*options->exists("loop")*/ ),
00209                         true);
00210         }
00211         else
00212         {
00213                 loaded_sample = CL_SoundBuffer::create(
00214                         new CL_Sample(
00215                                 get_name().c_str(),
00216                                 parent->get_resource_provider()),
00217                         true);
00218         }
00219         
00220         loaded = true;
00221         return loaded_sample;
00222 }

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