1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2017 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  */
24 
25 /**
26  * The audio listener defines the global properties of the audio environment, it
27  * defines where and how sounds and musics are heard.
28  *
29  * If $(VIEW_LINK) is the eyes of the user, then
30  * $(U Listener) is his ears (by the way, they are often linked together – same
31  * position, orientation, etc.).
32  *
33  * $(U Listener) is a simple interface, which allows to setup the listener in
34  * the 3D audio environment (position and direction), and to adjust the global
35  * volume.
36  *
37  * Because the listener is unique in the scene, $(U Listener) only contains
38  * static functions and doesn't have to be instanciated.
39  *
40  * Example:
41  * ---
42  * // Move the listener to the position (1, 0, -5)
43  * Listener.position = Vector3f(1, 0, -5);
44  *
45  * // Make it face the right axis (1, 0, 0)
46  * Listener.direction = Vector3f(1, 0, 0);
47  *
48  * // Reduce the global volume
49  * Listener.globalVolume = 50;
50  * ---
51  */
52 module dsfml.audio.listener;
53 
54 import dsfml.system.vector3;
55 
56 /**
57  * The audio listener is the point in the scene from where all the sounds are
58  * heard.
59  */
60 final abstract class Listener
61 {
62     @property
63     {
64         /**
65          * The orientation of the listener in the scene.
66          *
67          * The orientation defines the 3D axes of the listener (left, up, front)
68          * in the scene. The orientation vector doesn't have to be normalized.
69          *
70          * The default listener's orientation is (0, 0, -1).
71          */
72         static void direction(Vector3f orientation)
73         {
74             sfListener_setDirection(orientation.x, orientation.y,
75                                     orientation.z);
76         }
77         /// ditto
78         static Vector3f direction()
79         {
80             Vector3f temp;
81 
82             sfListener_getDirection(&temp.x, &temp.y, &temp.z);
83 
84             return temp;
85         }
86     }
87 
88     @property
89     {
90         /**
91          * The upward vector of the listener in the scene.
92          *
93          * The upward vector defines the 3D axes of the listener (left, up,
94          * front) in the scene. The upward vector doesn't have to be normalized.
95          *
96          * The default listener's upward vector is (0, 1, 0).
97          */
98         static void upVector(Vector3f orientation)
99         {
100             sfListener_setUpVector(orientation.x, orientation.y, orientation.z);
101         }
102         /// ditto
103         static Vector3f upVector()
104         {
105             Vector3f temp;
106 
107             sfListener_getUpVector(&temp.x, &temp.y, &temp.z);
108 
109             return temp;
110         }
111     }
112 
113     @property
114     {
115         /**
116          * The global volume of all the sounds and musics.
117          *
118          * The volume is a number between 0 and 100; it is combined with the
119          * individual volume of each sound / music.
120          *
121          * The default value for the volume is 100 (maximum).
122          */
123         static void globalVolume(float volume)
124         {
125             sfListener_setGlobalVolume(volume);
126         }
127         /// ditto
128         static float globalVolume()
129         {
130             return sfListener_getGlobalVolume();
131         }
132 
133     }
134 
135     @property
136     {
137         /**
138          * The position of the listener in the scene.
139          *
140          * The default listener's position is (0, 0, 0).
141          */
142         static void position(Vector3f pos)
143         {
144             sfListener_setPosition(pos.x, pos.y, pos.z);
145         }
146         /// ditto
147         static Vector3f position()
148         {
149             Vector3f temp;
150 
151             sfListener_getPosition(&temp.x, &temp.y, &temp.z);
152 
153             return temp;
154         }
155     }
156 
157     deprecated("Use the 'direction' property instead.")
158     @property
159     {
160         /**
161          * The orientation of the listener in the scene.
162          *
163          * The orientation defines the 3D axes of the listener (left, up, front)
164          * in the scene. The orientation vector doesn't have to be normalized.
165          *
166          * The default listener's orientation is (0, 0, -1).
167          *
168          * Deprecated: Use the 'direction' property instead.
169          */
170         static void Direction(Vector3f orientation)
171         {
172             sfListener_setDirection(orientation.x, orientation.y,
173                                     orientation.z);
174         }
175         /// ditto
176         static Vector3f Direction()
177         {
178             Vector3f temp;
179 
180             sfListener_getDirection(&temp.x, &temp.y, &temp.z);
181 
182             return temp;
183         }
184     }
185 
186     deprecated("Use the 'upVector' property instead.")
187     @property
188     {
189         /**
190          * The upward vector of the listener in the scene.
191          *
192          * The upward vector defines the 3D axes of the listener (left, up,
193          * front) in the scene. The upward vector doesn't have to be normalized.
194          *
195          * The default listener's upward vector is (0, 1, 0).
196          *
197          * Deprecated: Use the 'upVector' property instead.
198          */
199         static void UpVector(Vector3f orientation)
200         {
201             sfListener_setUpVector(orientation.x, orientation.y, orientation.z);
202         }
203         /// ditto
204         static Vector3f UpVector()
205         {
206             Vector3f temp;
207 
208             sfListener_getUpVector(&temp.x, &temp.y, &temp.z);
209 
210             return temp;
211         }
212     }
213 
214     deprecated("Use the 'globalVolume' property instead.")
215     @property
216     {
217         /**
218          * The global volume of all the sounds and musics. The volume is a
219          * number between 0 and 100; it is combined with the individual volume
220          * of each sound / music.
221          *
222          * The default value for the volume is 100 (maximum).
223          *
224          * Deprecated: Use the 'globalVolume' property instead.
225          */
226         static void GlobalVolume(float volume)
227         {
228             sfListener_setGlobalVolume(volume);
229         }
230         /// ditto
231         static float GlobalVolume()
232         {
233             return sfListener_getGlobalVolume();
234         }
235 
236     }
237 
238     deprecated("Use the 'position' property instead.")
239     @property
240     {
241         /**
242          * The position of the listener in the scene.
243          *
244          * The default listener's position is (0, 0, 0).
245          *
246          * Deprecated: Use the 'position' property instead.
247          */
248         static void Position(Vector3f position)
249         {
250             sfListener_setPosition(position.x, position.y, position.z);
251         }
252         /// ditto
253         static Vector3f Position()
254         {
255             Vector3f temp;
256 
257             sfListener_getPosition(&temp.x, &temp.y, &temp.z);
258 
259             return temp;
260         }
261     }
262 }
263 
264 unittest
265 {
266     version (DSFML_Unittest_Audio)
267     {
268         import std.stdio;
269 
270         writeln("Unit test for Listener");
271 
272         float volume = Listener.GlobalVolume;
273         volume -= 10;
274         Listener.GlobalVolume = volume;
275 
276         Vector3f pos = Listener.Position;
277         pos.x += 10;
278         pos.y -= 10;
279         pos.z *= 3;
280         Listener.Position = pos;
281 
282         Vector3f dir = Listener.Direction;
283         dir.x += 10;
284         dir.y -= 10;
285         dir.z *= 3;
286         Listener.Direction = dir;
287         writeln("Unit tests pass!");
288         writeln();
289     }
290 }
291 
292 private extern (C):
293 
294 void sfListener_setGlobalVolume(float volume);
295 
296 float sfListener_getGlobalVolume();
297 
298 void sfListener_setPosition(float x, float y, float z);
299 
300 void sfListener_getPosition(float* x, float* y, float* z);
301 
302 void sfListener_setDirection(float x, float y, float z);
303 
304 void sfListener_getDirection(float* x, float* y, float* z);
305 
306 void sfListener_setUpVector(float x, float y, float z);
307 
308 void sfListener_getUpVector(float* x, float* y, float* z);