OpenClonk
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

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

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 66 of file C4SoundLoaders.h.

Member Function Documentation

◆ file_close_func()

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 }

◆ file_read_func()

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 }

◆ file_seek_func()

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 }

◆ file_tell_func()

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 }

◆ mem_close_func()

int VorbisLoader::mem_close_func ( void *  datasource)
static

Definition at line 147 of file C4SoundLoaders.cpp.

148 {
149  return 1;
150 }

Referenced by ReadInfo().

Here is the caller graph for this function:

◆ mem_read_func()

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.

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 }

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

Referenced by ReadInfo().

Here is the caller graph for this function:

◆ mem_seek_func()

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

Definition at line 128 of file C4SoundLoaders.cpp.

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 }

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

Referenced by ReadInfo().

Here is the caller graph for this function:

◆ mem_tell_func()

long VorbisLoader::mem_tell_func ( void *  datasource)
static

Definition at line 152 of file C4SoundLoaders.cpp.

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

Referenced by ReadInfo().

Here is the caller graph for this function:

◆ ReadInfo()

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

Implements C4SoundLoaders::SoundLoader.

Definition at line 185 of file C4SoundLoaders.cpp.

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 }
uint8_t BYTE
static long mem_tell_func(void *datasource)
static int mem_close_func(void *datasource)
static int mem_seek_func(void *datasource, ogg_int64_t offset, int whence)
static size_t mem_read_func(void *ptr, size_t byte_size, size_t size_to_read, void *datasource)
std::vector< BYTE > sound_data

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.

Here is the call graph for this function:

Member Data Documentation

◆ first_loader

SoundLoader * SoundLoader::first_loader
staticinherited

◆ next

SoundLoader* C4SoundLoaders::SoundLoader::next
inherited

◆ OPTION_Raw

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

Definition at line 41 of file C4SoundLoaders.h.

Referenced by C4SoundEffect::Load().

◆ singleton

VorbisLoader VorbisLoader::singleton
staticprotected

Definition at line 97 of file C4SoundLoaders.h.


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