Listener.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/audio/AL/Listener.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
34 Listener::Listener ( void )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38  // Defaults as per OpenAL/al.h
39  ALfloat position[] = { 0.0, 0.0, 0.0 };
40  ALfloat velocity[] = { 0.0, 0.0, 0.0 };
41  // Listener is facing into the screen
42  ALfloat direction[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
43  ALfloat gain = 1.0;
44 
45  // Initialize with sane defaults to be on the safe side; note that you must
46  // have the audio card initialized before-hand or these will be invalid
47  // presets
48 AL_CHECK_ERR ( alListenerf ( AL_GAIN, gain ) );
49 AL_CHECK_ERR ( alListenerfv ( AL_POSITION, position ) );
50 AL_CHECK_ERR ( alListenerfv ( AL_VELOCITY, velocity ) );
51 AL_CHECK_ERR ( alListenerfv ( AL_ORIENTATION, direction ) );
52 }
53 
54 Listener::~Listener ( void )
55 {
56 NOM_LOG_TRACE ( NOM );
57 
58  // Clean up instance variables
59 }
60 
61 float Listener::getVolume ( void ) const
62 {
63  ALfloat master_volume;
64 
65 AL_CHECK_ERR ( alGetListenerf ( AL_GAIN, &master_volume ) );
66 
67  return master_volume * 100.f;
68 }
69 
70 const Point3f Listener::getPosition ( void ) const
71 {
72  Point3f position;
73 
74 AL_CHECK_ERR ( alGetListener3f ( AL_POSITION, &position.x, &position.y, &position.z ) );
75 
76  return position;
77 }
78 
79 const Point3f Listener::getVelocity ( void ) const
80 {
81  Point3f velocity;
82 
83 AL_CHECK_ERR ( alGetListener3f ( AL_VELOCITY, &velocity.x, &velocity.y, &velocity.z ) );
84 
85  return velocity;
86 }
87 
88 const Point3f Listener::getDirection ( void ) const
89 {
90  Point3f direction;
91 
92 AL_CHECK_ERR ( alGetListener3f ( AL_ORIENTATION, &direction.x, &direction.y, &direction.z ) );
93 
94  return direction;
95 }
96 
97 void Listener::setPosition ( float x, float y, float z )
98 {
99 AL_CHECK_ERR ( alListener3f ( AL_POSITION, x, y, z ) );
100 }
101 
102 void Listener::setPosition ( const Point3f& position )
103 {
104  this->setPosition ( position.x, position.y, position.z );
105 }
106 
107 void Listener::setVelocity ( float x, float y, float z )
108 {
109 AL_CHECK_ERR ( alListener3f ( AL_VELOCITY, x, y, z ) );
110 }
111 
112 void Listener::setVelocity ( const Point3f& velocity )
113 {
114  this->setVelocity ( velocity.x, velocity.y, velocity.z );
115 }
116 
117 void Listener::setDirection ( float x, float y, float z )
118 {
119 AL_CHECK_ERR ( alListener3f ( AL_ORIENTATION, x, y, z ) );
120 }
121 
122 void Listener::setDirection ( const Point3f& direction )
123 {
124  this->setDirection ( direction.x, direction.y, direction.z );
125 }
126 
127 void Listener::setVolume ( float gain )
128 {
129 AL_CHECK_ERR ( alListenerf ( AL_GAIN, gain * 0.01f ) );
130 }
131 
132 
133  } // namespace OpenAL
134 } // namespace nom
const Point3f getDirection(void) const
Obtain direction.
Definition: Listener.cpp:88
const Point3f getVelocity(void) const
Obtain velocity.
Definition: Listener.cpp:79
void setVelocity(float x, float y, float z)
Set velocity.
Definition: Listener.cpp:107
3D Point class
Definition: Point3-inl.hpp:38
const Point3f getPosition(void) const
Obtain position.
Definition: Listener.cpp:70
float getVolume(void) const
Definition: Listener.cpp:61
void setPosition(float x, float y, float z)
Set position.
Definition: Listener.cpp:97
void setVolume(float gain)
Definition: Listener.cpp:127
void setDirection(float x, float y, float z)
Set direction.
Definition: Listener.cpp:117