OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4SoundLoaders::VorbisLoader Class Reference

#include <C4SoundLoaders.h>

Inheritance diagram for C4SoundLoaders::VorbisLoader:
[legend]
Collaboration diagram for C4SoundLoaders::VorbisLoader:
[legend]

Classes

struct  CompressedData
 

Public Member Functions

virtual bool ReadInfo (SoundInfo *result, BYTE *data, size_t data_length, uint32_t)
 

Static Public Member Functions

static size_t mem_read_func (void *ptr, size_t byte_size, size_t size_to_read, void *datasource)
 
static int mem_seek_func (void *datasource, ogg_int64_t offset, int whence)
 
static int mem_close_func (void *datasource)
 
static long mem_tell_func (void *datasource)
 
static size_t file_read_func (void *ptr, size_t byte_size, size_t size_to_read, void *datasource)
 
static int file_seek_func (void *datasource, ogg_int64_t offset, int whence)
 
static int file_close_func (void *datasource)
 
static long file_tell_func (void *datasource)
 

Public Attributes

SoundLoadernext
 

Static Public Attributes

static const int OPTION_Raw = 1
 
static SoundLoaderfirst_loader
 

Static Protected Attributes

static VorbisLoader singleton
 

Detailed Description

Definition at line 67 of file C4SoundLoaders.h.

Member Function Documentation

int VorbisLoader::file_close_func ( void *  datasource)
static

Definition at line 172 of file C4SoundLoaders.cpp.

173 {
174  C4MusicFileOgg *ogg = static_cast<C4MusicFileOgg *>(datasource);
175  ogg->source_file.Close();
176  return 1;
177 }
size_t VorbisLoader::file_read_func ( void *  ptr,
size_t  byte_size,
size_t  size_to_read,
void *  datasource 
)
static

Definition at line 157 of file C4SoundLoaders.cpp.

158 {
159  C4MusicFileOgg *ogg = static_cast<C4MusicFileOgg *>(datasource);
160  size_t bytes_to_read = size_to_read*byte_size;
161  size_t bytes_read = 0u;
162  ogg->source_file.Read(ptr, bytes_to_read, &bytes_read);
163  return bytes_read / byte_size;
164 }
int VorbisLoader::file_seek_func ( void *  datasource,
ogg_int64_t  offset,
int  whence 
)
static

Definition at line 166 of file C4SoundLoaders.cpp.

167 {
168  C4MusicFileOgg *ogg = static_cast<C4MusicFileOgg *>(datasource);
169  return ogg->source_file.Seek(static_cast<long int>(offset), whence);
170 }
long VorbisLoader::file_tell_func ( void *  datasource)
static

Definition at line 179 of file C4SoundLoaders.cpp.

180 {
181  C4MusicFileOgg *ogg = static_cast<C4MusicFileOgg *>(datasource);
182  return ogg->source_file.Tell();
183 }
int VorbisLoader::mem_close_func ( void *  datasource)
static

Definition at line 147 of file C4SoundLoaders.cpp.

Referenced by ReadInfo().

148 {
149  return 1;
150 }

Here is the caller graph for this function:

size_t VorbisLoader::mem_read_func ( void *  ptr,
size_t  byte_size,
size_t  size_to_read,
void *  datasource 
)
static

Definition at line 107 of file C4SoundLoaders.cpp.

References C4SoundLoaders::VorbisLoader::CompressedData::data, C4SoundLoaders::VorbisLoader::CompressedData::data_length, and C4SoundLoaders::VorbisLoader::CompressedData::data_pos.

Referenced by ReadInfo().

108 {
109  size_t spaceToEOF;
110  size_t actualSizeToRead;
111  CompressedData* data = (CompressedData*)datasource;
112 
113  spaceToEOF = data->data_length - data->data_pos;
114  if (size_to_read*byte_size < spaceToEOF)
115  actualSizeToRead = size_to_read*byte_size;
116  else
117  actualSizeToRead = spaceToEOF;
118 
119  if (actualSizeToRead)
120  {
121  memcpy(ptr, (char*)data->data + data->data_pos, actualSizeToRead);
122  data->data_pos += actualSizeToRead;
123  }
124 
125  return actualSizeToRead;
126 }

Here is the caller graph for this function:

int VorbisLoader::mem_seek_func ( void *  datasource,
ogg_int64_t  offset,
int  whence 
)
static

Definition at line 128 of file C4SoundLoaders.cpp.

References C4SoundLoaders::VorbisLoader::CompressedData::data_length, and C4SoundLoaders::VorbisLoader::CompressedData::data_pos.

Referenced by ReadInfo().

129 {
130  CompressedData* data = (CompressedData*)datasource;
131 
132  switch (whence)
133  {
134  case SEEK_SET:
135  data->data_pos = static_cast<size_t>(offset) < data->data_length ? static_cast<size_t>(offset) : data->data_length;
136  break;
137  case SEEK_CUR:
138  data->data_pos += static_cast<size_t>(offset) < data->data_length - data->data_pos ? static_cast<size_t>(offset) : data->data_length - data->data_pos;
139  break;
140  case SEEK_END:
141  data->data_pos = data->data_length+1;
142  break;
143  }
144  return 0;
145 }

Here is the caller graph for this function:

long VorbisLoader::mem_tell_func ( void *  datasource)
static

Definition at line 152 of file C4SoundLoaders.cpp.

Referenced by ReadInfo().

153 {
154  return ((CompressedData*)datasource)->data_pos;
155 }

Here is the caller graph for this function:

bool VorbisLoader::ReadInfo ( SoundInfo result,
BYTE data,
size_t  data_length,
uint32_t   
)
virtual

Implements C4SoundLoaders::SoundLoader.

Definition at line 185 of file C4SoundLoaders.cpp.

References C4SoundLoaders::SoundInfo::format, mem_close_func(), mem_read_func(), mem_seek_func(), mem_tell_func(), C4SoundLoaders::SoundInfo::sample_length, C4SoundLoaders::SoundInfo::sample_rate, and C4SoundLoaders::SoundInfo::sound_data.

186 {
187  CompressedData compressed(data, data_length);
188 
189  int endian = 0;
190 
191  vorbis_info* info;
192  OggVorbis_File ogg_file;
193  memset(&ogg_file, 0, sizeof(ogg_file));
194  ov_callbacks callbacks;
195  callbacks.read_func = &mem_read_func;
196  callbacks.seek_func = &mem_seek_func;
197  callbacks.close_func = &mem_close_func;
198  callbacks.tell_func = &mem_tell_func;
199 
200  // open using callbacks
201  if (ov_open_callbacks(&compressed, &ogg_file, nullptr, 0, callbacks) != 0)
202  {
203  ov_clear(&ogg_file);
204  return false;
205  }
206 
207  // get information about sound
208  info = ov_info(&ogg_file, -1);
209  if (info->channels == 1)
210  result->format = AL_FORMAT_MONO16;
211  else
212  result->format = AL_FORMAT_STEREO16;
213  result->sample_rate = info->rate;
214  result->sample_length = ov_time_total(&ogg_file, -1)/1000.0;
215 
216  // Compute the total buffer size
217  const unsigned long total_size = static_cast<unsigned int>(result->sample_rate * result->sample_length * 1000.0 * info->channels * 2 + 0.5);
218  const unsigned long extra_size = 1024 * 8;
219 
220  // read: try to read the whole track in one go, and if there is more data
221  // than we predicted in total_size, read the additional data in 8K blocks.
222  unsigned long buffer_size = 0;
223  long bytes_read;
224  do {
225  const int chunk_size = total_size + extra_size - std::min(total_size, buffer_size);
226  int bitStream;
227  if (buffer_size+chunk_size > result->sound_data.size())
228  result->sound_data.resize(buffer_size+chunk_size);
229  bytes_read = ov_read(&ogg_file, (char*)&result->sound_data[buffer_size], chunk_size*sizeof(BYTE), endian, 2, 1, &bitStream);
230  buffer_size += bytes_read;
231  } while (bytes_read > 0);
232  result->sound_data.resize(buffer_size);
233 
234  // clear ogg file
235  ov_clear(&ogg_file);
236  return true;
237 }
static int mem_seek_func(void *datasource, ogg_int64_t offset, int whence)
static long mem_tell_func(void *datasource)
static int mem_close_func(void *datasource)
uint8_t BYTE
std::vector< BYTE > sound_data
static size_t mem_read_func(void *ptr, size_t byte_size, size_t size_to_read, void *datasource)

Here is the call graph for this function:

Member Data Documentation

SoundLoader * SoundLoader::first_loader
staticinherited

Definition at line 44 of file C4SoundLoaders.h.

Referenced by C4SoundLoaders::SoundLoader::SoundLoader().

SoundLoader* C4SoundLoaders::SoundLoader::next
inherited

Definition at line 45 of file C4SoundLoaders.h.

Referenced by C4SoundEffect::Load().

const int C4SoundLoaders::SoundLoader::OPTION_Raw = 1
staticinherited

Definition at line 42 of file C4SoundLoaders.h.

VorbisLoader VorbisLoader::singleton
staticprotected

Definition at line 98 of file C4SoundLoaders.h.


The documentation for this class was generated from the following files: