zenilib: zenilib/jni/external/zenilib/zeni_audio/Sound_Stream_AL.cpp Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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()))
16 throw Sound_Stream_Init_Failure();
17
18 const int result = ov_fopen((path + ".ogg").c_str(), &oggStream);
19 if(result < 0)
20 throw Sound_Stream_Ogg_Read_Failure();
21
22 vorbisInfo = ov_info(&oggStream, -1);
23 vorbisComment = ov_comment(&oggStream, -1);
24
25 if(vorbisInfo->channels == 1)
26 format = AL_FORMAT_MONO16;
27 else
28 format = AL_FORMAT_STEREO16;
29
30 ov_time_seek(&oggStream, time_);
31
32 Sound_Renderer_AL::alGenBuffers()(NUM_BUFFERS, buffers);
33 Sound_Renderer_AL::alGenSources()(1, &source);
34
35 ALfloat pos[3] = {0.0f, 0.0f, 0.0f};
36 Sound_Renderer_AL::alSourcefv()(source, AL_POSITION, pos );
37 Sound_Renderer_AL::alSourcefv()(source, AL_VELOCITY, pos );
38 Sound_Renderer_AL::alSourcefv()(source, AL_DIRECTION, pos );
39 Sound_Renderer_AL::alSourcef() (source, AL_ROLLOFF_FACTOR, 0.0 );
40 Sound_Renderer_AL::alSourcei() (source, AL_SOURCE_RELATIVE, AL_TRUE);
41
42 const ALenum error = Sound_Renderer_AL::alGetError()();
43 if(error != AL_NO_ERROR) {
44 destroy();
45 std::cerr << "OpenAL error: " << Sound_Renderer_AL::errorString(error) << std::endl;
46 throw Sound_Stream_Init_Failure();
47 }
48 }
49
50 Sound_Stream_AL::~Sound_Stream_AL() {
51 destroy();
52 }
53
54 void Sound_Stream_AL::set_pitch(const float &pitch) {
55 Sound_Renderer_AL::alSourcef()(source, AL_PITCH, pitch);
56 }
57
58 void Sound_Stream_AL::set_gain(const float &gain) {
59 if(dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer()))
60 Sound_Renderer_AL::alSourcef()(source, AL_GAIN, gain);
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) {
81 Sound_Renderer_AL::alSourcef()(source, AL_REFERENCE_DISTANCE, reference_distance);
82 }
83
84 void Sound_Stream_AL::set_max_distance(const float &max_distance) {
85 Sound_Renderer_AL::alSourcef()(source, AL_MAX_DISTANCE, max_distance);
86 }
87
88 void Sound_Stream_AL::set_rolloff(const float &rolloff) {
89 Sound_Renderer_AL::alSourcef()(source, AL_ROLLOFF_FACTOR, rolloff);
90 }
91
92 float Sound_Stream_AL::get_duration() const {
93 return float(ov_time_total(const_cast<OggVorbis_File *>(&oggStream), -1));
94 }
95
96 float Sound_Stream_AL::get_pitch() const {
97 float pitch = ZENI_DEFAULT_PITCH;
98 Sound_Renderer_AL::alGetSourcef()(source, AL_PITCH, &pitch);
99 return pitch;
100 }
101
102 float Sound_Stream_AL::get_gain() const {
103 float gain = ZENI_DEFAULT_GAIN;
104 if(dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer())) {
105 Sound_Renderer_AL::alGetSourcef()(source, AL_GAIN, &gain);
106 }
107 return gain;
108 }
109
110 Point3f Sound_Stream_AL::get_position() const {
111 Point3f position;
112 Sound_Renderer_AL::alGetSourcefv()(source, AL_POSITION, reinterpret_cast< ALfloat *>(&position));
113 return position;
114 }
115
116 Vector3f Sound_Stream_AL::get_velocity() const {
117 Vector3f velocity;
118 Sound_Renderer_AL::alGetSourcefv()(source, AL_VELOCITY, reinterpret_cast< ALfloat *>(&velocity));
119 return velocity;
120 }
121
122 bool Sound_Stream_AL::is_looping() const {
123 return looping;
124 }
125
126 float Sound_Stream_AL::get_time() const {
127 return float(ov_time_tell(const_cast<OggVorbis_File *>(&oggStream)));
128 }
129
130 float Sound_Stream_AL::get_reference_distance() const {
131 float reference_distance = ZENI_DEFAULT_REFERENCE_DISTANCE;
132 Sound_Renderer_AL::alGetSourcef()(source, AL_REFERENCE_DISTANCE, &reference_distance);
133 return reference_distance;
134 }
135
136 float Sound_Stream_AL::get_max_distance() const {
137 float max_distance = ZENI_DEFAULT_MAX_SOUND_DISTANCE;
138 Sound_Renderer_AL::alGetSourcef()(source, AL_MAX_DISTANCE, &max_distance);
139 return max_distance;
140 }
141
142 float Sound_Stream_AL::get_rolloff() const {
143 float rolloff = ZENI_DEFAULT_ROLLOFF;
144 Sound_Renderer_AL::alGetSourcef()(source, AL_ROLLOFF_FACTOR, &rolloff);
145 return rolloff;
146 }
147
148 void Sound_Stream_AL::play() {
149 if(oggStream.offset == oggStream.end)
150 ov_raw_seek(&oggStream, 0);
151
152 update();
153
154 Sound_Renderer_AL::alSourcePlay()(source);
155 }
156
157 void Sound_Stream_AL::pause() {
158 Sound_Renderer_AL::alSourcePause()(source);
159 }
160
161 void Sound_Stream_AL::stop() {
162 Sound_Renderer_AL::alSourceStop()(source);
163 }
164
165 bool Sound_Stream_AL::is_playing() const {
166 ALenum state;
167 Sound_Renderer_AL::alGetSourcei()(source, AL_SOURCE_STATE, &state);
168 return state == AL_PLAYING;
169 }
170
171 bool Sound_Stream_AL::is_paused() const {
172 ALenum state;
173 Sound_Renderer_AL::alGetSourcei()(source, AL_SOURCE_STATE, &state);
174 return state == AL_PAUSED;
175 }
176
177 bool Sound_Stream_AL::is_stopped() const {
178 ALenum state;
179 Sound_Renderer_AL::alGetSourcei()(source, AL_SOURCE_STATE, &state);
180 return state == AL_STOPPED;
181 }
182
183 void Sound_Stream_AL::update() {
184 if(oggStream.offset == oggStream.end && !looping)
185 return;
186
187 int processed;
188 Sound_Renderer_AL::alGetSourcei()(source, AL_BUFFERS_PROCESSED, &processed);
189 processed += NUM_BUFFERS - buffers_used;
190
191 while(processed--) {
192 ALuint buffer;
193 if(buffers_used == NUM_BUFFERS)
194 Sound_Renderer_AL::alSourceUnqueueBuffers()(source, 1, &buffer);
195 else
196 buffer = buffers[buffers_used++];
197
198 stream(buffer);
199
200 Sound_Renderer_AL::alSourceQueueBuffers()(source, 1, &buffer);
201
202 if(oggStream.offset == oggStream.end && !looping)
203 break;
204 }
205 }
206
207 void Sound_Stream_AL::destroy() {
208 Sound_Renderer_AL::alSourceStop()(source);
209 Sound_Renderer_AL::alDeleteSources()(1, &source);
210 Sound_Renderer_AL::alDeleteBuffers()(NUM_BUFFERS, buffers);
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
241 Sound_Renderer_AL::alBufferData()(buffer, format, data, size, vorbisInfo->rate);
242
243 const ALenum error = Sound_Renderer_AL::alGetError()();
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
Zeni::Sound_Renderer_AL::alGetSourcefv
static alGetSourcefv_fcn alGetSourcefv()
Definition: Sound_Renderer_AL.h:95
AL_STOPPED
#define AL_STOPPED
Definition: al.h:241
AL_TRUE
#define AL_TRUE
Definition: al.h:86
OggVorbis_File::end
ogg_int64_t end
Definition: vorbisfile.h:115
Zeni::Sound_Stream_Init_Failure
Definition: Sound_Stream_AL.h:71
Zeni::Sound_Stream_AL::~Sound_Stream_AL
~Sound_Stream_AL()
Definition: Sound_Stream_AL.cpp:50
Zeni::Sound_Renderer_AL::alSourceStop
static alSourceStop_fcn alSourceStop()
Definition: Sound_Renderer_AL.h:105
ZENI_DEFAULT_PITCH
#define ZENI_DEFAULT_PITCH
Definition: Define.h:54
Zeni::Sound_Renderer_AL::alDeleteSources
static alDeleteSources_fcn alDeleteSources()
Definition: Sound_Renderer_AL.h:89
Zeni::Sound_Renderer_AL::alGetSourcei
static alGetSourcei_fcn alGetSourcei()
Definition: Sound_Renderer_AL.h:96
Zeni::Sound_Stream_AL::set_reference_distance
void set_reference_distance(const float &reference_distance=ZENI_DEFAULT_REFERENCE_DISTANCE)
Definition: Sound_Stream_AL.cpp:80
stream
GLuint GLuint stream
Definition: glew.h:6573
Zeni::Sound_Stream_AL::set_max_distance
void set_max_distance(const float &max_distance=ZENI_DEFAULT_MAX_SOUND_DISTANCE)
Definition: Sound_Stream_AL.cpp:84
ov_time_total
double ov_time_total(OggVorbis_File *vf, int i)
Definition: vorbisfile.c:1208
AL_MAX_DISTANCE
#define AL_MAX_DISTANCE
Definition: al.h:309
OggVorbis_File::offset
ogg_int64_t offset
Definition: vorbisfile.h:114
Zeni::Sound_Stream_AL::get_position
Point3f get_position() const
Get the position of the Sound_Buffer.
Definition: Sound_Stream_AL.cpp:110
AL_SOURCE_STATE
#define AL_SOURCE_STATE
Definition: al.h:235
Zeni::Sound_Renderer_AL::alSourcei
static alSourcei_fcn alSourcei()
Definition: Sound_Renderer_AL.h:102
ov_clear
int ov_clear(OggVorbis_File *vf)
Definition: vorbisfile.c:955
Zeni::Sound_Stream_AL::get_gain
float get_gain() const
Get the gain.
Definition: Sound_Stream_AL.cpp:102
Zeni::Sound_Renderer_AL::alGetSourcef
static alGetSourcef_fcn alGetSourcef()
Definition: Sound_Renderer_AL.h:94
Undefine.h
ov_fopen
int ov_fopen(const char *path, OggVorbis_File *vf)
Definition: vorbisfile.c:1011
ALfloat
float ALfloat
Definition: al.h:68
Zeni::Sound_Stream_AL::get_reference_distance
float get_reference_distance() const
Definition: Sound_Stream_AL.cpp:130
AL_PAUSED
#define AL_PAUSED
Definition: al.h:240
path
GLsizei const GLchar *const * path
Definition: glew.h:5828
ZENI_DEFAULT_GAIN
#define ZENI_DEFAULT_GAIN
Definition: Define.h:55
AL_POSITION
#define AL_POSITION
Definition: al.h:144
Zeni::Sound_Renderer_AL::alSourcef
static alSourcef_fcn alSourcef()
Definition: Sound_Renderer_AL.h:100
Zeni::String
Definition: String.h:29
Zeni::Sound_Stream_Ogg_Read_Failure
Definition: Sound_Stream_AL.h:79
ov_time_tell
double ov_time_tell(OggVorbis_File *vf)
Definition: vorbisfile.c:1762
Zeni::Sound_Stream_AL::get_velocity
Vector3f get_velocity() const
Get the velocity of the Sound_Buffer.
Definition: Sound_Stream_AL.cpp:116
Zeni::Point3f
A 3D Point represented with floats.
Definition: Coordinate.h:133
ZENI_DEFAULT_MAX_SOUND_DISTANCE
#define ZENI_DEFAULT_MAX_SOUND_DISTANCE
Definition: Define.h:57
Zeni::Sound_Stream_AL::get_duration
float get_duration() const
Get the duration of the Sound_Buffer in seconds.
Definition: Sound_Stream_AL.cpp:92
buffers
const GLuint * buffers
Definition: glew.h:1669
Zeni::this_pointer_is_silent_sound_two
void * this_pointer_is_silent_sound_two
Definition: Sound_Stream_AL.cpp:270
Zeni::Sound_Stream_AL::set_rolloff
void set_rolloff(const float &rolloff=ZENI_DEFAULT_ROLLOFF)
Definition: Sound_Stream_AL.cpp:88
buffer
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
vorbis_info::channels
int channels
Definition: codec.h:30
ov_raw_seek
int ov_raw_seek(OggVorbis_File *vf, ogg_int64_t pos)
Definition: vorbisfile.c:1229
Zeni::get_Sound
Sound & get_Sound()
Get access to the singleton.
Definition: Sound.cpp:220
Zeni::Vector3f
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
result
GLuint64EXT * result
Definition: glew.h:12708
Zeni::Sound_Renderer_AL::alSourceQueueBuffers
static alSourceQueueBuffers_fcn alSourceQueueBuffers()
Definition: Sound_Renderer_AL.h:106
AL_PLAYING
#define AL_PLAYING
Definition: al.h:239
AL_ROLLOFF_FACTOR
#define AL_ROLLOFF_FACTOR
Definition: al.h:286
Zeni::Sound_Stream_AL::get_pitch
float get_pitch() const
Get the pitch.
Definition: Sound_Stream_AL.cpp:96
Define.h
ov_info
vorbis_info * ov_info(OggVorbis_File *vf, int link)
Definition: vorbisfile.c:1791
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
error
FT_Error error
Definition: cffdrivr.c:407
Zeni::Sound_Stream_AL::set_time
void set_time(const float &time)
Set the current position in the Sound_Buffer, offset in seconds.
Definition: Sound_Stream_AL.cpp:75
Zeni::Sound_Stream_AL::pause
void pause()
Pause the Sound_Source.
Definition: Sound_Stream_AL.cpp:157
Zeni::Sound_Stream_AL::is_looping
bool is_looping() const
Check to see if the Sound_Buffer is set to loop back to the start once it is done playing...
Definition: Sound_Stream_AL.cpp:122
AL_PITCH
#define AL_PITCH
Definition: al.h:128
Zeni::Sound_Stream_AL::set_gain
void set_gain(const float &gain=ZENI_DEFAULT_GAIN)
Set the gain.
Definition: Sound_Stream_AL.cpp:58
Zeni::Sound_Renderer_AL::errorString
static String errorString()
Definition: Sound_Renderer_AL.cpp:214
Zeni::Sound_Renderer_AL::alBufferData
static alBufferData_fcn alBufferData()
Definition: Sound_Renderer_AL.h:81
Zeni::Sound_Stream_AL::get_time
float get_time() const
Get the current position in the Sound_Buffer, offset in seconds.
Definition: Sound_Stream_AL.cpp:126
ZENI_DEFAULT_REFERENCE_DISTANCE
#define ZENI_DEFAULT_REFERENCE_DISTANCE
Definition: Define.h:56
ALenum
int ALenum
Definition: al.h:65
AL_BUFFERS_PROCESSED
#define AL_BUFFERS_PROCESSED
Definition: al.h:262
AL_FORMAT_MONO16
#define AL_FORMAT_MONO16
Definition: al.h:340
format
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
Zeni::Sound_Stream_AL::stop
void stop()
Stop the Sound_Source. (Essentially the same as pause but resets the current time.)
Definition: Sound_Stream_AL.cpp:161
ALuint
unsigned int ALuint
Definition: al.h:59
ov_read
long ov_read(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream)
Definition: vorbisfile.c:2007
Zeni::Sound_Stream_AL::get_rolloff
float get_rolloff() const
Definition: Sound_Stream_AL.cpp:142
ZENI_DEFAULT_ROLLOFF
#define ZENI_DEFAULT_ROLLOFF
Definition: Define.h:58
Zeni::Sound_Renderer_AL::alDeleteBuffers
static alDeleteBuffers_fcn alDeleteBuffers()
Definition: Sound_Renderer_AL.h:88
AL_SOURCE_RELATIVE
#define AL_SOURCE_RELATIVE
Definition: al.h:97
Zeni::Sound_Renderer_AL::alSourcefv
static alSourcefv_fcn alSourcefv()
Definition: Sound_Renderer_AL.h:101
Zeni::Sound_Renderer_AL::alGetError
static alGetError_fcn alGetError()
Definition: Sound_Renderer_AL.h:91
Zeni::Sound_Renderer_AL::alSourcePlay
static alSourcePlay_fcn alSourcePlay()
Definition: Sound_Renderer_AL.h:104
vorbis_info::rate
long rate
Definition: codec.h:31
Zeni::Sound_Stream_AL::play
void play()
Begin playing or unpause the Sound_Source.
Definition: Sound_Stream_AL.cpp:148
AL_DIRECTION
#define AL_DIRECTION
Definition: al.h:154
AL_REFERENCE_DISTANCE
#define AL_REFERENCE_DISTANCE
Definition: al.h:274
ov_comment
vorbis_comment * ov_comment(OggVorbis_File *vf, int link)
Definition: vorbisfile.c:1809
AL_FORMAT_STEREO16
#define AL_FORMAT_STEREO16
Definition: al.h:342
Zeni::Sound_Renderer_AL::alSourceUnqueueBuffers
static alSourceUnqueueBuffers_fcn alSourceUnqueueBuffers()
Definition: Sound_Renderer_AL.h:107
Zeni::Sound_Stream_AL::is_playing
bool is_playing() const
Check to see if the Sound_Source is playing.
Definition: Sound_Stream_AL.cpp:165
Zeni::Sound_Renderer_AL::alSourcePause
static alSourcePause_fcn alSourcePause()
Definition: Sound_Renderer_AL.h:103
Zeni::Sound_Stream_AL::is_stopped
bool is_stopped() const
Check to see if the Sound_Source is stopped.
Definition: Sound_Stream_AL.cpp:177
Zeni::Sound_Stream_AL::is_paused
bool is_paused() const
Check to see if the Sound_Source is paused.
Definition: Sound_Stream_AL.cpp:171
Zeni::Sound_Stream_AL::set_looping
void set_looping(const bool &looping)
Set whether the Sound_Buffer should loop back to the start once it is done playing.
Definition: Sound_Stream_AL.cpp:71
AL_GAIN
#define AL_GAIN
Definition: al.h:196
ov_time_seek
int ov_time_seek(OggVorbis_File *vf, double pos)
Definition: vorbisfile.c:1690
Zeni::Sound_Stream_AL::update
void update()
Definition: Sound_Stream_AL.cpp:183
Zeni::Sound_Renderer_AL::alGenSources
static alGenSources_fcn alGenSources()
Definition: Sound_Renderer_AL.h:97
source
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:994
zeni_audio.h
Zeni::Sound_Stream_AL::set_position
void set_position(const Point3f &position)
Set the position of the Sound_Source_HW.
Definition: Sound_Stream_AL.cpp:63
Zeni::Sound_Stream_AL::set_pitch
void set_pitch(const float &pitch=ZENI_DEFAULT_PITCH)
Set the pitch.
Definition: Sound_Stream_AL.cpp:54
AL_NO_ERROR
#define AL_NO_ERROR
Definition: al.h:364
Zeni::Sound_Stream_AL::set_velocity
void set_velocity(const Vector3f &velocity)
Set the velocity of the Sound_Source_HW for the doppler effect.
Definition: Sound_Stream_AL.cpp:67
AL_VELOCITY
#define AL_VELOCITY
Definition: al.h:163
Zeni::Sound_Renderer_AL::alGenBuffers
static alGenBuffers_fcn alGenBuffers()
Definition: Sound_Renderer_AL.h:90
Zeni::Sound_Stream_AL::get_max_distance
float get_max_distance() const
Definition: Sound_Stream_AL.cpp:136
size
GLsizei size
Definition: gl2ext.h:1467