Allolib  1.0
C++ Components For Interactive Multimedia
al_Speaker.hpp
1 #ifndef INCLUDE_AL_SOUND_SPEAKER_HPP
2 #define INCLUDE_AL_SOUND_SPEAKER_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Abstraction of a loudspeaker used for sound spatialization algorithms
42 
43  File author(s):
44  Lance Putnam, 2006, putnam.lance@gmail.com
45 */
46 
47 #include <cmath>
48 #include <vector>
49 
50 #include "al/math/al_Constants.hpp"
51 #include "al/math/al_Vec.hpp"
52 
53 namespace al {
54 
58 class Speaker {
59 public:
60  unsigned int deviceChannel;
61  float azimuth;
62  float elevation;
64  int group;
65  float radius;
66  float gain;
67 
73  Speaker(unsigned int deviceChan = 0, float az = 0.f, float el = 0.f,
74  int group = 0, float radius = 1.f, float gain = 1.f);
75 
77  template <class T> Speaker &posCart(T *xyz) {
78  using namespace std;
79  float elr = toRad(elevation);
80  float azr = toRad(azimuth);
81  float cosel = cos(elr);
82  xyz[0] = -cos(azr) * cosel * radius;
83  xyz[1] = sin(azr) * cosel * radius;
84  xyz[2] = sin(elr) * radius;
85  return *this;
86  }
87 
88  void posCart2(Vec3d xyz);
89 
91  Vec3d vec() const;
92 
94  Vec3d vecGraphics() const;
95 
96  static double toRad(double d) { return d * M_PI / 180.; }
97  static float toRad(float d) { return d * float(M_PI) / 180.f; }
98 };
99 
101 typedef std::vector<Speaker> Speakers;
102 
112 template <int N>
113 Speakers SpeakerRingLayout(unsigned int deviceChannelStart = 0,
114  float phase = 0.f, float radius = 1.f,
115  float gain = 1.f) {
116  Speakers mSpeakers;
117  mSpeakers.reserve(N);
118  for (unsigned int i = 0; i < N; ++i) {
119  mSpeakers.emplace_back(Speaker(
120  i + deviceChannelStart, (360.f / N) * i + phase, 0.f, 0, radius, gain));
121  }
122  return mSpeakers;
123 };
124 
128 Speakers HeadsetSpeakerLayout(unsigned int deviceChannelStart = 0,
129  float radius = 1.f, float gain = 1.f);
130 
134 Speakers StereoSpeakerLayout(unsigned int deviceChannelStart = 0,
135  float angle = 30.f, float distance = 1.f,
136  float gain = 1.f);
137 
141 Speakers OctalSpeakerLayout(unsigned int deviceChannelStart = 0,
142  float phase = 0.f, float radius = 1.f,
143  float gain = 1.f);
144 
148 Speakers CubeLayout(unsigned int deviceChannelStart = 0);
149 
150 } // namespace al
151 #endif
float gain
Gain of speaker.
Definition: al_Speaker.hpp:66
unsigned int deviceChannel
Index in the output device channels array.
Definition: al_Speaker.hpp:60
float elevation
Definition: al_Speaker.hpp:62
Vec3d vecGraphics() const
Get position as Cartesian coordinate (in graphics space)
float azimuth
Angle from forward to right vector (i.e. CW)
Definition: al_Speaker.hpp:61
int group
Group identifier.
Definition: al_Speaker.hpp:64
Speaker & posCart(T *xyz)
Get position in Cartesian coordinate (in audio space)
Definition: al_Speaker.hpp:77
Speaker(unsigned int deviceChan=0, float az=0.f, float el=0.f, int group=0, float radius=1.f, float gain=1.f)
float radius
Distance from center of listening space.
Definition: al_Speaker.hpp:65
Vec3d vec() const
Get position as Cartesian coordinate (in audio space)
Speakers SpeakerRingLayout(unsigned int deviceChannelStart=0, float phase=0.f, float radius=1.f, float gain=1.f)
Definition: al_Speaker.hpp:113
Speakers StereoSpeakerLayout(unsigned int deviceChannelStart=0, float angle=30.f, float distance=1.f, float gain=1.f)
Speakers OctalSpeakerLayout(unsigned int deviceChannelStart=0, float phase=0.f, float radius=1.f, float gain=1.f)
Definition: al_App.hpp:23
Speakers CubeLayout(unsigned int deviceChannelStart=0)
std::vector< Speaker > Speakers
A set of speakers.
Definition: al_Speaker.hpp:101
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
Definition: al_Vec.hpp:634
Speakers HeadsetSpeakerLayout(unsigned int deviceChannelStart=0, float radius=1.f, float gain=1.f)