zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Sound_Stream_AL.cpp
Go to the documentation of this file.
1 #include <zeni_audio.h>
2 
3 #include <iostream>
4 
5 #ifndef DISABLE_AL
6 
7 #include <Zeni/Define.h>
8 
9 namespace Zeni {
10 
11  Sound_Stream_AL::Sound_Stream_AL(const String &path, const bool &looping_, const float &time_)
12  : buffers_used(0),
13  looping(looping_)
14  {
15  if(!dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer()))
17 
18  const int result = ov_fopen((path + ".ogg").c_str(), &oggStream);
19  if(result < 0)
21 
22  vorbisInfo = ov_info(&oggStream, -1);
23  vorbisComment = ov_comment(&oggStream, -1);
24 
25  if(vorbisInfo->channels == 1)
27  else
29 
30  ov_time_seek(&oggStream, time_);
31 
34 
35  ALfloat pos[3] = {0.0f, 0.0f, 0.0f};
41 
43  if(error != AL_NO_ERROR) {
44  destroy();
45  std::cerr << "OpenAL error: " << Sound_Renderer_AL::errorString(error) << std::endl;
47  }
48  }
49 
51  destroy();
52  }
53 
54  void Sound_Stream_AL::set_pitch(const float &pitch) {
56  }
57 
58  void Sound_Stream_AL::set_gain(const float &gain) {
59  if(dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer()))
61  }
62 
63  void Sound_Stream_AL::set_position(const Point3f &position) {
64  Sound_Renderer_AL::alSourcefv()(source, AL_POSITION, const_cast<ALfloat *>(reinterpret_cast<const ALfloat *>(&position)));
65  }
66 
67  void Sound_Stream_AL::set_velocity(const Vector3f &velocity) {
68  Sound_Renderer_AL::alSourcefv()(source, AL_VELOCITY, const_cast<ALfloat *>(reinterpret_cast<const ALfloat *>(&velocity)));
69  }
70 
71  void Sound_Stream_AL::set_looping(const bool &looping_) {
72  looping = looping_;
73  }
74 
75  void Sound_Stream_AL::set_time(const float &time) {
76  if(dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer()))
77  ov_time_seek(&oggStream, time);
78  }
79 
80  void Sound_Stream_AL::set_reference_distance(const float &reference_distance) {
82  }
83 
84  void Sound_Stream_AL::set_max_distance(const float &max_distance) {
86  }
87 
88  void Sound_Stream_AL::set_rolloff(const float &rolloff) {
90  }
91 
93  return float(ov_time_total(const_cast<OggVorbis_File *>(&oggStream), -1));
94  }
95 
97  float pitch = ZENI_DEFAULT_PITCH;
99  return pitch;
100  }
101 
103  float gain = ZENI_DEFAULT_GAIN;
104  if(dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer())) {
106  }
107  return gain;
108  }
109 
111  Point3f position;
112  Sound_Renderer_AL::alGetSourcefv()(source, AL_POSITION, reinterpret_cast<ALfloat *>(&position));
113  return position;
114  }
115 
117  Vector3f velocity;
118  Sound_Renderer_AL::alGetSourcefv()(source, AL_VELOCITY, reinterpret_cast<ALfloat *>(&velocity));
119  return velocity;
120  }
121 
123  return looping;
124  }
125 
127  return float(ov_time_tell(const_cast<OggVorbis_File *>(&oggStream)));
128  }
129 
131  float reference_distance = ZENI_DEFAULT_REFERENCE_DISTANCE;
133  return reference_distance;
134  }
135 
137  float max_distance = ZENI_DEFAULT_MAX_SOUND_DISTANCE;
139  return max_distance;
140  }
141 
143  float rolloff = ZENI_DEFAULT_ROLLOFF;
145  return rolloff;
146  }
147 
149  if(oggStream.offset == oggStream.end)
150  ov_raw_seek(&oggStream, 0);
151 
152  update();
153 
155  }
156 
159  }
160 
163  }
164 
166  ALenum state;
168  return state == AL_PLAYING;
169  }
170 
172  ALenum state;
174  return state == AL_PAUSED;
175  }
176 
178  ALenum state;
180  return state == AL_STOPPED;
181  }
182 
184  if(oggStream.offset == oggStream.end && !looping)
185  return;
186 
187  int processed;
189  processed += NUM_BUFFERS - buffers_used;
190 
191  while(processed--) {
192  ALuint buffer;
193  if(buffers_used == NUM_BUFFERS)
195  else
196  buffer = buffers[buffers_used++];
197 
198  stream(buffer);
199 
201 
202  if(oggStream.offset == oggStream.end && !looping)
203  break;
204  }
205  }
206 
207  void Sound_Stream_AL::destroy() {
211 
212  ov_clear(&oggStream);
213  }
214 
215  void Sound_Stream_AL::stream(ALuint buffer) {
216  char data[BUFFER_SIZE];
217  int size = 0;
218  int section;
219  int result;
220 
221  while(size < int(BUFFER_SIZE)) {
222  result = ov_read(&oggStream, data + size, BUFFER_SIZE - size, 0, 2, 1, & section);
223 
224  if(result > 0)
225  size += result;
226  else {
227  if(result < 0)
228  throw Sound_Stream_Ogg_Read_Failure();
229  else if(looping)
230  ov_raw_seek(&oggStream, 0);
231  else
232  break;
233  }
234  }
235 
236  if(size == 0)
237  throw Error("Failed to stream data from .ogg file.");
238  //else if(size < BUFFER_SIZE)
239  // memset(data + size, 0, BUFFER_SIZE - size);
240 
242 
244  if(error != AL_NO_ERROR) {
245  std::cerr << "OpenAL error: " << Sound_Renderer_AL::errorString(error) << std::endl;
246  throw Sound_Stream_Update_Failure();
247  }
248  }
249 
250  //String Sound_Stream_AL::errorString(int code) {
251  // switch(code) {
252  // case OV_EREAD:
253  // return "Read from media.";
254  // case OV_ENOTVORBIS:
255  // return "Not Vorbis data.";
256  // case OV_EVERSION:
257  // return "Vorbis version mismatch.";
258  // case OV_EBADHEADER:
259  // return "Invalid Vorbis header.";
260  // case OV_EFAULT:
261  // return "Internal logic fault (bug or heap/stack corruption.";
262  // default:
263  // return "Unknown Ogg error.";
264  // }
265  //}
266 
267 }
268 
269 namespace Zeni {
270  void * this_pointer_is_silent_sound_two = (void *)0xDEADBEEF;
271 }
272 
273 #include <Zeni/Undefine.h>
274 
275 #endif
static alGetSourcefv_fcn alGetSourcefv()
#define AL_STOPPED
Definition: al.h:241
#define AL_TRUE
Definition: al.h:86
ogg_int64_t end
Definition: vorbisfile.h:115
static alSourceStop_fcn alSourceStop()
#define ZENI_DEFAULT_PITCH
Definition: Define.h:54
static alDeleteSources_fcn alDeleteSources()
static alGetSourcei_fcn alGetSourcei()
void set_reference_distance(const float &reference_distance=ZENI_DEFAULT_REFERENCE_DISTANCE)
GLuint GLuint stream
Definition: glew.h:6573
void set_max_distance(const float &max_distance=ZENI_DEFAULT_MAX_SOUND_DISTANCE)
double ov_time_total(OggVorbis_File *vf, int i)
Definition: vorbisfile.c:1208
#define AL_MAX_DISTANCE
Definition: al.h:309
ogg_int64_t offset
Definition: vorbisfile.h:114
Point3f get_position() const
Get the position of the Sound_Buffer.
#define AL_SOURCE_STATE
Definition: al.h:235
static alSourcei_fcn alSourcei()
int ov_clear(OggVorbis_File *vf)
Definition: vorbisfile.c:955
float get_gain() const
Get the gain.
static alGetSourcef_fcn alGetSourcef()
int ov_fopen(const char *path, OggVorbis_File *vf)
Definition: vorbisfile.c:1011
float ALfloat
Definition: al.h:68
float get_reference_distance() const
#define AL_PAUSED
Definition: al.h:240
GLsizei const GLchar *const * path
Definition: glew.h:5828
#define ZENI_DEFAULT_GAIN
Definition: Define.h:55
#define AL_POSITION
Definition: al.h:144
static alSourcef_fcn alSourcef()
double ov_time_tell(OggVorbis_File *vf)
Definition: vorbisfile.c:1762
Vector3f get_velocity() const
Get the velocity of the Sound_Buffer.
A 3D Point represented with floats.
Definition: Coordinate.h:133
#define ZENI_DEFAULT_MAX_SOUND_DISTANCE
Definition: Define.h:57
float get_duration() const
Get the duration of the Sound_Buffer in seconds.
const GLuint * buffers
Definition: glew.h:1669
void * this_pointer_is_silent_sound_two
void set_rolloff(const float &rolloff=ZENI_DEFAULT_ROLLOFF)
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
int channels
Definition: codec.h:30
int ov_raw_seek(OggVorbis_File *vf, ogg_int64_t pos)
Definition: vorbisfile.c:1229
Sound & get_Sound()
Get access to the singleton.
Definition: Sound.cpp:220
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
GLuint64EXT * result
Definition: glew.h:12708
static alSourceQueueBuffers_fcn alSourceQueueBuffers()
#define AL_PLAYING
Definition: al.h:239
#define AL_ROLLOFF_FACTOR
Definition: al.h:286
float get_pitch() const
Get the pitch.
vorbis_info * ov_info(OggVorbis_File *vf, int link)
Definition: vorbisfile.c:1791
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
FT_Error error
Definition: cffdrivr.c:407
void set_time(const float &time)
Set the current position in the Sound_Buffer, offset in seconds.
void pause()
Pause the Sound_Source.
bool is_looping() const
Check to see if the Sound_Buffer is set to loop back to the start once it is done playing...
#define AL_PITCH
Definition: al.h:128
void set_gain(const float &gain=ZENI_DEFAULT_GAIN)
Set the gain.
static alBufferData_fcn alBufferData()
float get_time() const
Get the current position in the Sound_Buffer, offset in seconds.
#define ZENI_DEFAULT_REFERENCE_DISTANCE
Definition: Define.h:56
int ALenum
Definition: al.h:65
#define AL_BUFFERS_PROCESSED
Definition: al.h:262
#define AL_FORMAT_MONO16
Definition: al.h:340
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
void stop()
Stop the Sound_Source. (Essentially the same as pause but resets the current time.)
unsigned int ALuint
Definition: al.h:59
long ov_read(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream)
Definition: vorbisfile.c:2007
float get_rolloff() const
#define ZENI_DEFAULT_ROLLOFF
Definition: Define.h:58
static alDeleteBuffers_fcn alDeleteBuffers()
#define AL_SOURCE_RELATIVE
Definition: al.h:97
static alSourcefv_fcn alSourcefv()
static alGetError_fcn alGetError()
static alSourcePlay_fcn alSourcePlay()
long rate
Definition: codec.h:31
void play()
Begin playing or unpause the Sound_Source.
#define AL_DIRECTION
Definition: al.h:154
#define AL_REFERENCE_DISTANCE
Definition: al.h:274
vorbis_comment * ov_comment(OggVorbis_File *vf, int link)
Definition: vorbisfile.c:1809
#define AL_FORMAT_STEREO16
Definition: al.h:342
static alSourceUnqueueBuffers_fcn alSourceUnqueueBuffers()
bool is_playing() const
Check to see if the Sound_Source is playing.
static alSourcePause_fcn alSourcePause()
bool is_stopped() const
Check to see if the Sound_Source is stopped.
bool is_paused() const
Check to see if the Sound_Source is paused.
void set_looping(const bool &looping)
Set whether the Sound_Buffer should loop back to the start once it is done playing.
#define AL_GAIN
Definition: al.h:196
int ov_time_seek(OggVorbis_File *vf, double pos)
Definition: vorbisfile.c:1690
static alGenSources_fcn alGenSources()
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:994
void set_position(const Point3f &position)
Set the position of the Sound_Source_HW.
void set_pitch(const float &pitch=ZENI_DEFAULT_PITCH)
Set the pitch.
#define AL_NO_ERROR
Definition: al.h:364
void set_velocity(const Vector3f &velocity)
Set the velocity of the Sound_Source_HW for the doppler effect.
#define AL_VELOCITY
Definition: al.h:163
static alGenBuffers_fcn alGenBuffers()
float get_max_distance() const
GLsizei size
Definition: gl2ext.h:1467