zenilib  0.5.3.0
 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 
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 
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 
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 
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);
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 
221  return Sound::get();
222  }
223 
224 }
225 
226 #include <Zeni/Undefine.h>
void set_buffer(const Sound_Buffer &buffer)
Set the Sound_Buffer to be played.
void stop()
Stop the Sound_Source. (Essentially the same as pause but resets the current time.)
GLclampf f
Definition: glew.h:3390
virtual void set_listener_position(const Point3f &)
Set the position of the listener and BGM.
void set_reference_distance(const float &reference_distance=ZENI_DEFAULT_REFERENCE_DISTANCE)
void set_max_distance(const float &max_distance=ZENI_DEFAULT_MAX_SOUND_DISTANCE)
#define ZENI_DEFAULT_MUSIC_PRIORITY
Definition: Define.h:60
void set_listener_gain(const float &gain)
Set the listener gain.
Definition: Sound.cpp:108
Point3f get_position() const
Get the position of the Sound_Buffer.
Sound_Buffer & get_Hello_World_Buffer() const
Initialize m_bgm if needed and return a corresponding Sound_Source.
Definition: Sound.cpp:193
Sound_Renderer & get_Renderer()
Get the current Sound_Renderer.
Definition: Sound.hxx:30
float get_gain() const
Get the gain.
float get_reference_distance() const
void play()
Begin playing or unpause the Sound_Source.
std::pair< Vector3f, Vector3f > get_listener_forward_and_up() const
Set the orientation of the listener.
Definition: Sound.cpp:138
virtual void set_listener_forward_and_up(const Vector3f &, const Vector3f &)
Set the orientation of the listener.
Vector3f get_velocity() const
Get the velocity of the Sound_Buffer.
A 3D Point represented with floats.
Definition: Coordinate.h:133
void set_rolloff(const float &rolloff=ZENI_DEFAULT_ROLLOFF)
Plays Sound Data.
Definition: Sound_Source.h:147
Sound & get_Sound()
Get access to the singleton.
Definition: Sound.cpp:220
virtual void set_listener_velocity(const Vector3f &)
Set the velocity of the listener and BGM for the doppler effect.
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
float get_pitch() const
Get the pitch.
void set_listener_forward_and_up(const Vector3f &forward, const Vector3f &up)
Set the orientation of the listener.
Definition: Sound.cpp:104
bool is_looping() const
Check to see if the Sound_Buffer is set to loop back to the start once it is done playing...
static Sound & get()
void set_gain(const float &gain=ZENI_DEFAULT_GAIN)
Set the gain.
void set_time(const float &time)
Set the current position in the Sound_Buffer, offset in seconds.
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...
Stores Sound Data.
Definition: Sound_Buffer.h:83
void set_listener_muted(const bool &muted)
Set whether the listener is muted.
Definition: Sound.cpp:114
float get_rolloff() const
void set_listener_velocity(const Vector3f &velocity)
Set the velocity of the listener and BGM for the doppler effect.
Definition: Sound.cpp:92
virtual void set_listener_gain(const float &)
Set the listener gain.
void set_listener_position(const Point3f &position)
Set the position of the listener and BGM.
Definition: Sound.cpp:80
void play()
Begin playing or unpause the Sound_Source.
The Sound Singleton.
Definition: Sound.h:53
bool is_playing() const
Check to see if the Sound_Source is playing.
bool is_playing() const
Check to see if the Sound_Source is playing.
virtual std::pair< Vector3f, Vector3f > get_listener_forward_and_up() const
Set the orientation of the listener.
Point3f get_listener_position() const
Get the position of the listener and BGM.
Definition: Sound.cpp:120
void set_BGM(const String &filename)
Set BackGround Music.
Definition: Sound.cpp:142
Vector3f get_listener_velocity() const
Get the velocity of the listener and BGM.
Definition: Sound.cpp:129
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.
void set_velocity(const Vector3f &velocity)
Set the velocity of the Sound_Source_HW for the doppler effect.
float get_max_distance() const