Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OggData.cpp
Go to the documentation of this file.
1 //
2 // C++ implementation: Audio::__impl::OggData
3 //
4 
5 #include "config.h"
6 
7 #ifdef HAVE_OGG
8 #include <sys/types.h>
9 #ifdef HAVE_STDINT_H
10 #include <stdint.h>
11 #endif
12 #include "OggData.h"
13 #include "config.h"
14 #include "../Format.h"
15 #include "../Exceptions.h"
16 
17 #include <utility>
18 #include <limits>
19 
20 #include <vorbis/vorbisfile.h>
21 #include "vsfilesystem.h"
22 
23 
24 namespace Audio {
25 
26  namespace __impl {
27 
28  using std::numeric_limits;
29 
30  size_t OggData::read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
31  {
32  return ((VSFileSystem::VSFile*)datasource)->Read(ptr, size*nmemb);
33  }
34 
35  int OggData::seek_func(void *datasource, ogg_int64_t offset, int whence)
36  {
37  if (offset > numeric_limits<long>::max())
38  return -1;
39 
41  switch(whence) {
42  case SEEK_SET:
43  file->GoTo((long)offset);
44  break;
45  case SEEK_END:
46  file->GoTo(file->Size() + (long)offset);
47  break;
48  case SEEK_CUR:
49  file->GoTo(file->GetPosition() + (long)offset);
50  break;
51  default:
52  return -1;
53  }
54 
55  return 0;
56  }
57 
58  int OggData::close_func(void *datasource)
59  {
60  ((VSFileSystem::VSFile*)datasource)->Close();
61  return 0;
62  }
63 
64  long OggData::tell_func(void *datasource)
65  {
66  return ((VSFileSystem::VSFile*)datasource)->GetPosition();
67  }
68 
69  int OggData::nativeIsLsb()
70  {
71  union {
72  short s;
73  char c[sizeof(short)];
74  };
75  s = 1;
76  return c[0]?1:0;
77  }
78 
79  OggData::OggData(VSFileSystem::VSFile &file, Format &fmt, int streamIdx, bool test)
80  {
81  callbacks.read_func = &read_func;
82  callbacks.seek_func = &seek_func;
83  callbacks.close_func = &close_func;
84  callbacks.tell_func = &tell_func;
85 
86  streamIndex = streamIdx;
87 
88  if (test) {
89  if (ov_test_callbacks(&file, &vorbisFile, NULL, 0, callbacks))
90  throw FileFormatException("File \"" + file.GetFilename() + "\"is not ogg vorbis");
91  } else {
92  if (ov_open_callbacks(&file, &vorbisFile, NULL, 0, callbacks))
93  throw FileFormatException("File \"" + file.GetFilename() + "\"is not ogg vorbis");
94 
95  vorbis_info *info = ov_info(&vorbisFile, streamIndex);
96  fmt.sampleFrequency = info->rate;
97  fmt.channels = info->channels;
98  fmt.bitsPerSample = 16;
99  fmt.nativeOrder = nativeIsLsb();
100  fmt.signedSamples = 1;
101  }
102  }
103 
104  OggData::~OggData()
105  {
106  ov_clear(&vorbisFile);
107  }
108  }
109 
110 };
111 
112 #endif // HAVE_OGG
113