1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
$Id: stream_provider_raw.cpp,v 1.3 2001/12/11 20:44:24 mbn Exp $
------------------------------------------------------------------------
ClanLib, the platform independent game SDK.
This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
version 2. See COPYING for details.
For a total list of contributers see CREDITS.
------------------------------------------------------------------------
File purpose:
Streamed sample
*/
#ifdef WIN32
#pragma warning (disable:4786)
#endif
#include "API/Core/System/cl_assert.h"
#include "API/Core/IOData/inputsource.h"
#include "API/Core/IOData/inputsource_provider.h"
#include "API/Sound/SoundProviders/stream_provider_raw.h"
/*****************************
CL_Streamed_RawSample
*****************************/
CL_SoundBuffer *CL_Streamed_RawSample::create(
const std::string &filename,
SoundFormat format,
int frequency,
CL_InputSourceProvider *inputprovider,
bool looped)
{
return CL_SoundBuffer::create(
new CL_Streamed_RawSample(
filename,
format,
frequency,
inputprovider,
looped),
true);
}
CL_Streamed_RawSample::CL_Streamed_RawSample(
const std::string &_filename,
SoundFormat _format,
int _frequency,
CL_InputSourceProvider *_inputprovider,
bool _looped)
{
filename = _filename;
looped = _looped;
format = _format;
frequency = _frequency;
if (_inputprovider == NULL)
{
inputprovider = CL_InputSourceProvider::create_file_provider(".");
}
else
{
inputprovider = _inputprovider->clone();
}
}
CL_Streamed_RawSample::~CL_Streamed_RawSample()
{
delete inputprovider;
}
CL_StreamSoundProvider_Session *CL_Streamed_RawSample::begin_session()
{
return new CL_Streamed_RawSample_Session(
inputprovider->open_source(filename.c_str()),
format,
frequency,
looped);
}
void CL_Streamed_RawSample::end_session(CL_StreamSoundProvider_Session *session)
{
delete session;
}
/********************************
CL_Streamed_RawSample_Session
********************************/
CL_Streamed_RawSample_Session::CL_Streamed_RawSample_Session(
CL_InputSource *_input,
SoundFormat _format,
int _frequency,
bool _looped)
{
input = _input;
looped = _looped;
cl_assert(input != NULL);
sample_size = input->size();
sample_freq = _frequency;
sample_format = _format;
sample_left = sample_size;
}
CL_Streamed_RawSample_Session::~CL_Streamed_RawSample_Session()
{
delete input;
}
bool CL_Streamed_RawSample_Session::eof() const
{
if (sample_left <= 0 && !looped) return true;
return false;
}
int CL_Streamed_RawSample_Session::get_data(void *data_ptr, int data_requested)
{
if (sample_left <= 0)
{
if (looped)
{
sample_left = sample_size;
input->seek(0, CL_InputSource::seek_set);
}
else
{
return 0;
}
}
if (looped)
{
if (data_requested > sample_left)
{
int pos = 0;
while (data_requested > 0)
{
input->read(&((unsigned char *) data_ptr)[pos], sample_left);
if (get_format() == sf_8bit_signed)
{
for (int i=0;i<sample_left;i++)
{
((unsigned char *) data_ptr)[pos+i] = char(short(((unsigned char *) data_ptr)[pos+i])+128);
}
}
data_requested -= sample_left;
pos += sample_left;
input->seek(0, CL_InputSource::seek_set);
int bytes_read = input->read(&((unsigned char *)data_ptr)[pos], data_requested);
data_requested -= bytes_read;
if (get_format() == sf_8bit_signed)
{
for (int i=0;i<bytes_read;i++)
{
((unsigned char *) data_ptr)[pos+i] = char(short(((unsigned char *) data_ptr)[pos+i])+128);
}
}
pos += bytes_read;
sample_left = sample_size - bytes_read;
}
return data_requested;
}
else
{
sample_left -= data_requested;
int read = input->read(data_ptr, data_requested);
if (get_format() == sf_8bit_signed)
{
for (int i=0;i<read;i++)
{
((unsigned char *) data_ptr)[i] = char(short(((unsigned char *) data_ptr)[i])+128);
}
}
return read;
}
}
else
{
sample_left -= data_requested;
if (sample_left < 0)
{
data_requested += sample_left;
}
int read = input->read(data_ptr, data_requested);
if (get_format() == sf_8bit_signed)
{
for (int i=0;i<read;i++)
{
((unsigned char *) data_ptr)[i] = char(short(((unsigned char *) data_ptr)[i])+128);
}
}
return read;
}
}
int CL_Streamed_RawSample_Session::get_frequency() const
{
return sample_freq;
}
SoundFormat CL_Streamed_RawSample_Session::get_format() const
{
return sample_format;
}
|