zenilib: zenilib/jni/external/zenilib/zeni_audio/Sound.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.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2 * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3 *
4 * zenilib is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * zenilib is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <zeni_audio.h>
19
20 #include <iostream>
21 #include <vector>
22 #include <iomanip>
23
24 #ifndef DISABLE_AL
25 #define OV_EXCLUDE_STATIC_CALLBACKS
26 #include <vorbis/vorbisfile.h>
27 #undef OV_EXCLUDE_STATIC_CALLBACKS
28 #endif
29
30 #include <Zeni/Define.h>
31
32 #if defined(_DEBUG) && defined(_WINDOWS)
33 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
34 #define new DEBUG_NEW
35 #endif
36
37 #include <Zeni/Singleton.hxx>
38
39 namespace Zeni {
40
41 template class Singleton<Sound>;
42
43 Sound * Sound::create() {
44 return new Sound;
45 }
46
47 Sound::Sound()
48 : m_hello_world_buffer(0),
49 m_bgm(0),
50 m_listener_gain(1.0f),
51 m_listener_muted(false)
52 {
53 #if !defined(DISABLE_AL)
54 try {
55 m_sound_renderer = new Sound_Renderer_AL;
56 }
57 catch(Sound_Init_Failure &) {
58 #endif
59 #if defined(ENABLE_SLES)
60 try {
61 m_sound_renderer = new Sound_Renderer_SLES;
62 }
63 catch(Sound_Init_Failure &) {
64 #endif
65 m_sound_renderer = new Sound_Renderer_NULL;
66 #if defined(ENABLE_SLES)
67 }
68 #endif
69 #if !defined(DISABLE_AL)
70 }
71 #endif
72 }
73
74 Sound::~Sound() {
75 delete m_bgm;
76 delete m_hello_world_buffer;
77 delete m_sound_renderer;
78 }
79
80 void Sound::set_listener_position(const Point3f &position) {
81 #ifdef DISABLE_AL
82 get_BGM_Source().set_position(position);
83 #else
84 init_BGM_Sound_Stream_AL();
85 if(m_bgm)
86 m_bgm->set_position(position);
87 #endif
88
89 m_sound_renderer->set_listener_position(position);
90 }
91
92 void Sound::set_listener_velocity(const Vector3f &velocity) {
93 #ifdef DISABLE_AL
94 get_BGM_Source().set_velocity(velocity);
95 #else
96 init_BGM_Sound_Stream_AL();
97 if(m_bgm)
98 m_bgm->set_velocity(velocity);
99 #endif
100
101 m_sound_renderer->set_listener_velocity(velocity);
102 }
103
104 void Sound::set_listener_forward_and_up(const Vector3f &forward, const Vector3f &up) {
105 m_sound_renderer->set_listener_forward_and_up(forward, up);
106 }
107
108 void Sound::set_listener_gain(const float &gain) {
109 m_listener_gain = gain;
110
111 m_sound_renderer->set_listener_gain(m_listener_muted ? 0.0f : m_listener_gain);
112 }
113
114 void Sound::set_listener_muted(const bool &muted) {
115 m_listener_muted = muted;
116
117 m_sound_renderer->set_listener_gain(m_listener_muted ? 0.0f : m_listener_gain);
118 }
119
120 Point3f Sound::get_listener_position() const {
121 #ifdef DISABLE_AL
122 return get_BGM_Source().get_position();
123 #else
124 init_BGM_Sound_Stream_AL();
125 return m_bgm ? m_bgm->get_position() : Point3f();
126 #endif
127 }
128
129 Vector3f Sound::get_listener_velocity() const {
130 #ifdef DISABLE_AL
131 return get_BGM_Source().get_velocity();
132 #else
133 init_BGM_Sound_Stream_AL();
134 return m_bgm ? m_bgm->get_velocity() : Vector3f();
135 #endif
136 }
137
138 std::pair<Vector3f, Vector3f> Sound::get_listener_forward_and_up() const {
139 return m_sound_renderer->get_listener_forward_and_up();
140 }
141
142 void Sound::set_BGM(const String &filename) {
143 #ifdef DISABLE_AL
144 Sound_Source &bgm_source = get_BGM_Source();
145
146 bool playing = bgm_source.is_playing() ? true : false;
147
148 bgm_source.stop();
149 bgm_source.set_buffer(get_Hello_World_Buffer());
150 delete m_bgm;
151 m_bgm = 0;
152
153 m_bgmusic = filename;
154 m_bgm = new Sound_Buffer(m_bgmusic);
155 bgm_source.set_buffer(*m_bgm);
156 bgm_source.set_time(0.0f);
157
158 if(playing)
159 bgm_source.play();
160 #else
161 init_BGM_Sound_Stream_AL();
162
163 if(m_bgm) {
164 const float pitch = m_bgm->get_pitch();
165 const float gain = m_bgm->get_gain();
166 const Point3f position = m_bgm->get_position();
167 const Vector3f velocity = m_bgm->get_velocity();
168 const bool looping = m_bgm->is_looping();
169 const float reference_distance = m_bgm->get_reference_distance();
170 const float max_distance = m_bgm->get_max_distance();
171 const float rolloff = m_bgm->get_rolloff();
172 const bool playing = m_bgm->is_playing();
173
174 delete m_bgm;
175 m_bgm = 0;
176
177 m_bgmusic = filename;
178 m_bgm = new Sound_Stream_AL(filename, looping);
179 m_bgm->set_pitch(pitch);
180 m_bgm->set_gain(gain);
181 m_bgm->set_position(position);
182 m_bgm->set_velocity(velocity);
183 m_bgm->set_reference_distance(reference_distance);
184 m_bgm->set_max_distance(max_distance);
185 m_bgm->set_rolloff(rolloff);
186
187 if(playing)
188 m_bgm->play();
189 }
190 #endif
191 }
192
193 Sound_Buffer & Sound::get_Hello_World_Buffer() const {
194 if(!m_hello_world_buffer)
195 m_hello_world_buffer = new Sound_Buffer();
196
197 return *m_hello_world_buffer;
198 }
199
200 #ifdef DISABLE_AL
201 Sound_Source & Sound::get_BGM_Source() const {
202 static Sound_Source bgm_source;
203
204 if(!m_bgm) {
205 m_bgm = new Sound_Buffer();
206
207 bgm_source.set_buffer(*m_bgm);
208 bgm_source.set_priority(ZENI_DEFAULT_MUSIC_PRIORITY);
209 }
210
211 return bgm_source;
212 }
213 #else
214 void Sound::init_BGM_Sound_Stream_AL() const {
215 if(!m_bgm && dynamic_cast<Sound_Renderer_AL *>(&get_Sound().get_Renderer()))
216 m_bgm = new Sound_Stream_AL("sfx/104469__dkmedic__world");
217 }
218 #endif
219
220 Sound & get_Sound() {
221 return Sound::get();
222 }
223
224 }
225
226 #include <Zeni/Undefine.h>
Zeni::Sound_Source::set_buffer
void set_buffer(const Sound_Buffer &buffer)
Set the Sound_Buffer to be played.
Definition: Sound_Source.hxx:40
Zeni::Sound_Source::stop
void stop()
Stop the Sound_Source. (Essentially the same as pause but resets the current time.)
Definition: Sound_Source.hxx:75
f
GLclampf f
Definition: glew.h:3390
Zeni::Sound_Renderer::set_listener_position
virtual void set_listener_position(const Point3f &)
Set the position of the listener and BGM.
Definition: Sound_Renderer.h:48
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
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
ZENI_DEFAULT_MUSIC_PRIORITY
#define ZENI_DEFAULT_MUSIC_PRIORITY
Definition: Define.h:60
Zeni::Sound::set_listener_gain
void set_listener_gain(const float &gain)
Set the listener gain.
Definition: Sound.cpp:108
Zeni::Sound_Stream_AL::get_position
Point3f get_position() const
Get the position of the Sound_Buffer.
Definition: Sound_Stream_AL.cpp:110
Zeni::Sound::get_Hello_World_Buffer
Sound_Buffer & get_Hello_World_Buffer() const
Initialize m_bgm if needed and return a corresponding Sound_Source.
Definition: Sound.cpp:193
Zeni::Sound::get_Renderer
Sound_Renderer & get_Renderer()
Get the current Sound_Renderer.
Definition: Sound.hxx:30
Zeni::Sound_Stream_AL
Definition: Sound_Stream_AL.h:12
Zeni::Sound_Stream_AL::get_gain
float get_gain() const
Get the gain.
Definition: Sound_Stream_AL.cpp:102
Undefine.h
Zeni::Sound_Stream_AL::get_reference_distance
float get_reference_distance() const
Definition: Sound_Stream_AL.cpp:130
Zeni::Sound_Source::play
void play()
Begin playing or unpause the Sound_Source.
Definition: Sound_Source.hxx:73
Zeni::Sound::get_listener_forward_and_up
std::pair< Vector3f, Vector3f > get_listener_forward_and_up() const
Set the orientation of the listener.
Definition: Sound.cpp:138
Zeni::Sound_Renderer::set_listener_forward_and_up
virtual void set_listener_forward_and_up(const Vector3f &, const Vector3f &)
Set the orientation of the listener.
Definition: Sound_Renderer.h:50
Zeni::String
Definition: String.h:29
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::Sound_Stream_AL::set_rolloff
void set_rolloff(const float &rolloff=ZENI_DEFAULT_ROLLOFF)
Definition: Sound_Stream_AL.cpp:88
Zeni::Sound_Source
Plays Sound Data.
Definition: Sound_Source.h:147
Zeni::get_Sound
Sound & get_Sound()
Get access to the singleton.
Definition: Sound.cpp:220
Zeni::Sound_Renderer::set_listener_velocity
virtual void set_listener_velocity(const Vector3f &)
Set the velocity of the listener and BGM for the doppler effect.
Definition: Sound_Renderer.h:49
Zeni::Vector3f
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
Zeni::Sound_Stream_AL::get_pitch
float get_pitch() const
Get the pitch.
Definition: Sound_Stream_AL.cpp:96
Define.h
Singleton.hxx
Zeni::Sound::set_listener_forward_and_up
void set_listener_forward_and_up(const Vector3f &forward, const Vector3f &up)
Set the orientation of the listener.
Definition: Sound.cpp:104
sort_controllers.filename
string filename
Definition: sort_controllers.py:8
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
Zeni::Singleton< Sound >::get
static Sound & get()
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_Source::set_time
void set_time(const float &time)
Set the current position in the Sound_Buffer, offset in seconds.
Definition: Sound_Source.hxx:47
Zeni::Sound_Source::set_priority
void set_priority(const int &priority=ZENI_DEFAULT_SOUND_PRIORITY)
Set the priority that this Sound_Source should have. Higher numbers are more likely to be selected fo...
Definition: Sound_Source.hxx:39
Zeni::Sound_Buffer
Stores Sound Data.
Definition: Sound_Buffer.h:83
Zeni::Sound::set_listener_muted
void set_listener_muted(const bool &muted)
Set whether the listener is muted.
Definition: Sound.cpp:114
Zeni::Sound_Stream_AL::get_rolloff
float get_rolloff() const
Definition: Sound_Stream_AL.cpp:142
Zeni::Sound::set_listener_velocity
void set_listener_velocity(const Vector3f &velocity)
Set the velocity of the listener and BGM for the doppler effect.
Definition: Sound.cpp:92
Zeni::Sound_Renderer::set_listener_gain
virtual void set_listener_gain(const float &)
Set the listener gain.
Definition: Sound_Renderer.h:51
Zeni::Sound::set_listener_position
void set_listener_position(const Point3f &position)
Set the position of the listener and BGM.
Definition: Sound.cpp:80
Zeni::Sound_Stream_AL::play
void play()
Begin playing or unpause the Sound_Source.
Definition: Sound_Stream_AL.cpp:148
Zeni::Sound
The Sound Singleton.
Definition: Sound.h:53
Zeni::Sound_Source::is_playing
bool is_playing() const
Check to see if the Sound_Source is playing.
Definition: Sound_Source.hxx:77
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::get_listener_forward_and_up
virtual std::pair< Vector3f, Vector3f > get_listener_forward_and_up() const
Set the orientation of the listener.
Definition: Sound_Renderer.h:55
Zeni::Sound::get_listener_position
Point3f get_listener_position() const
Get the position of the listener and BGM.
Definition: Sound.cpp:120
vorbisfile.h
Zeni::Sound::set_BGM
void set_BGM(const String &filename)
Set BackGround Music.
Definition: Sound.cpp:142
Zeni::Sound::get_listener_velocity
Vector3f get_listener_velocity() const
Get the velocity of the listener and BGM.
Definition: Sound.cpp:129
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
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
Zeni::Sound_Stream_AL::get_max_distance
float get_max_distance() const
Definition: Sound_Stream_AL.cpp:136