00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef WIN32
00020 #pragma warning (disable:4786)
00021 #endif
00022
00023 #include "API/Core/System/cl_assert.h"
00024 #include "API/Core/IOData/inputsource.h"
00025 #include "API/Sound/SoundProviders/stream_provider_raw.h"
00026
00027
00028
00029
00030
00031 CL_SoundBuffer *CL_Streamed_RawSample::create(
00032 const char *filename,
00033 SoundFormat format,
00034 int frequency,
00035 CL_InputSourceProvider *inputprovider,
00036 bool looped)
00037 {
00038 return CL_SoundBuffer::create(
00039 new CL_Streamed_RawSample(
00040 filename,
00041 format,
00042 frequency,
00043 inputprovider,
00044 looped),
00045 true);
00046 }
00047
00048 CL_Streamed_RawSample::CL_Streamed_RawSample(
00049 const char *_filename,
00050 SoundFormat _format,
00051 int _frequency,
00052 CL_InputSourceProvider *_inputprovider,
00053 bool _looped)
00054 {
00055 filename = _filename;
00056 looped = _looped;
00057 format = _format;
00058 frequency = _frequency;
00059
00060 if (_inputprovider == NULL)
00061 {
00062 inputprovider = CL_InputSourceProvider::create_file_provider(".");
00063 }
00064 else
00065 {
00066 inputprovider = _inputprovider->clone();
00067 }
00068 }
00069
00070 CL_Streamed_RawSample::~CL_Streamed_RawSample()
00071 {
00072 delete inputprovider;
00073 }
00074
00075 CL_StreamSoundProvider_Session *CL_Streamed_RawSample::begin_session()
00076 {
00077 return new CL_Streamed_RawSample_Session(
00078 inputprovider->open_source(filename.c_str()),
00079 format,
00080 frequency,
00081 looped);
00082 }
00083
00084 void CL_Streamed_RawSample::end_session(CL_StreamSoundProvider_Session *session)
00085 {
00086 delete session;
00087 }
00088
00089
00090
00091
00092
00093 CL_Streamed_RawSample_Session::CL_Streamed_RawSample_Session(
00094 CL_InputSource *_input,
00095 SoundFormat _format,
00096 int _frequency,
00097 bool _looped)
00098 {
00099 input = _input;
00100 looped = _looped;
00101 cl_assert(input != NULL);
00102
00103 sample_size = input->size();
00104 sample_freq = _frequency;
00105 sample_format = _format;
00106 sample_left = sample_size;
00107 }
00108
00109 CL_Streamed_RawSample_Session::~CL_Streamed_RawSample_Session()
00110 {
00111 delete input;
00112 }
00113
00114 bool CL_Streamed_RawSample_Session::eof() const
00115 {
00116 if (sample_left <= 0 && !looped) return true;
00117 return false;
00118 }
00119
00120 int CL_Streamed_RawSample_Session::get_data(void *data_ptr, int data_requested)
00121 {
00122 if (sample_left <= 0)
00123 {
00124 if (looped)
00125 {
00126 sample_left = sample_size;
00127 input->seek(0, CL_InputSource::seek_set);
00128 }
00129 else
00130 {
00131 return 0;
00132 }
00133 }
00134
00135 if (looped)
00136 {
00137 if (data_requested > sample_left)
00138 {
00139 int pos = 0;
00140 while (data_requested > 0)
00141 {
00142 input->read(&((unsigned char *) data_ptr)[pos], sample_left);
00143 if (get_format() == sf_8bit_signed)
00144 {
00145 for (int i=0;i<sample_left;i++)
00146 {
00147 ((unsigned char *) data_ptr)[pos+i] = char(short(((unsigned char *) data_ptr)[pos+i])+128);
00148 }
00149 }
00150 data_requested -= sample_left;
00151 pos += sample_left;
00152
00153 input->seek(0, CL_InputSource::seek_set);
00154
00155 int bytes_read = input->read(&((unsigned char *)data_ptr)[pos], data_requested);
00156 data_requested -= bytes_read;
00157 if (get_format() == sf_8bit_signed)
00158 {
00159 for (int i=0;i<bytes_read;i++)
00160 {
00161 ((unsigned char *) data_ptr)[pos+i] = char(short(((unsigned char *) data_ptr)[pos+i])+128);
00162 }
00163 }
00164 pos += bytes_read;
00165
00166 sample_left = sample_size - bytes_read;
00167 }
00168
00169 return data_requested;
00170 }
00171 else
00172 {
00173 sample_left -= data_requested;
00174 int read = input->read(data_ptr, data_requested);
00175 if (get_format() == sf_8bit_signed)
00176 {
00177 for (int i=0;i<read;i++)
00178 {
00179 ((unsigned char *) data_ptr)[i] = char(short(((unsigned char *) data_ptr)[i])+128);
00180 }
00181 }
00182 return read;
00183 }
00184 }
00185 else
00186 {
00187 sample_left -= data_requested;
00188 if (sample_left < 0)
00189 {
00190 data_requested += sample_left;
00191 }
00192 int read = input->read(data_ptr, data_requested);
00193 if (get_format() == sf_8bit_signed)
00194 {
00195 for (int i=0;i<read;i++)
00196 {
00197 ((unsigned char *) data_ptr)[i] = char(short(((unsigned char *) data_ptr)[i])+128);
00198 }
00199 }
00200
00201 return read;
00202 }
00203 }
00204
00205 int CL_Streamed_RawSample_Session::get_frequency() const
00206 {
00207 return sample_freq;
00208 }
00209
00210 SoundFormat CL_Streamed_RawSample_Session::get_format() const
00211 {
00212 return sample_format;
00213 }