Gorgon Game Engine
Environment.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Geometry/Point3D.h"
4 #include "../Geometry/Transform3D.h"
5 
6 
7 namespace Gorgon { namespace Audio {
8 
9  class Environment;
10 
17  class Listener {
18  friend void AudioLoop();
19  friend class Environment;
20  public:
21 
22  Listener(Environment &env) : env(&env) {
23  orientcalc();
24  }
25 
28  void SetLocation(const Geometry::Point3D &location) {
29  this->location = location;
30 
31  poscalc();
32  }
33 
37  void SetOrientation(const Geometry::Point3D &up, const Geometry::Point3D &orientation) {
38  this->up = up;
39  this->orientation = location;
40 
41  orientcalc();
42  }
43 
47  void SetOrientation(const Geometry::Point3D &location) {
48  this->orientation = location;
49 
50  orientcalc();
51  }
52 
55  return location;
56  }
57 
58  private:
59  void poscalc();
60  void orientcalc();
61 
62  Geometry::Point3D location = {0, 0, 0};
63  Geometry::Point3D orientation = {0, 1, 0};
64  Geometry::Point3D up = {0, 0, 1};
65 
66  Geometry::Transform3D transform;
67  Geometry::Transform3D invtransform;
68 
69  Geometry::Point3D leftpos, rightpos; //position of the left and right ear
70 
71  Environment *env;
72  };
73 
74 
79  class Environment {
80  friend void AudioLoop();
81  friend class Listener;
82  public:
83  Environment() : listener(*this) {
84  init();
85  }
86 
88  void SetAttuniationFactor(float value) {
89  attuniationfactor=value;
90  }
91 
93  void SetHeadRadius(float value) {
94  headradius = value;
95 
96  listener.poscalc();
97  }
98 
100  void SetNonBlocked(float value) {
101  nonblocked = value;
102  }
103 
105  void SetAuricleAngle(float value) {
106  auricleangle = value;
107 
108  left = { std::cos(PI-auricleangle), -std::sin(PI-auricleangle), 0};
109  right = { std::cos( auricleangle), -std::sin( auricleangle), 0};
110  }
111 
113  void SetSpeakerLocation(int index, Geometry::Point3D value) {
114  if(index<0 || index>=4)
115  throw std::runtime_error("There are 4 speakers that can be configured");
116 
117  speaker_locations[index] = value;
118 
119  init();
120  }
121 
124  return listener;
125  }
126 
129 
130  private:
131  void init();
132 
133  void updateorientation();
134 
135  float unitspermeter = 1;
136 
137  float speedofsound = 340.29f; //units/sec
138 
139  float recordingdistance = 1.f; //units
140 
141  float attuniationfactor = 0.1f; //Higher values will attenuation more
142 
143  float maxdistance = 200.f; //units
144 
145  float nonblocked = 0.2f; //amount of sound that is not blocked by head
146 
147  float headradius = 0.15f; //units, rough size of the head
148 
149  float hrtfdistance = 0.666f; //units, rough perimeter that the sound should travel to reach other ear ²
150 
151  Geometry::Point3D left, right; //vectors for left and right ear hearing ²
152 
153  float auricleangle = 0.0f; //this angle in radians covers the angle difference caused by auircle.
154 
155  Geometry::Point3D speaker_locations[4] = {
156  { .1f,-.1f, 0.f},
157  {-.1f,-.1f, 0.f},
158  { .1f, .3f, 0.f},
159  {-.1f, .3f, 0.f},
160  };
161 
162  Geometry::Point3D speaker_vectors[4]; // ²
163  float speaker_boost[4]; // ²
164 
165  // ² calculated
166 
168  Listener listener;
169  };
170 
171  inline void Listener::poscalc() {
172  leftpos = location - transform * Geometry::Point3D(env->headradius, 0, 0);
173  rightpos= location + transform * Geometry::Point3D(env->headradius, 0, 0);
174 
175  }
176 
177 } }
Gorgon::Geometry::basic_Point3D::CrossProduct
basic_Point3D CrossProduct(const basic_Point3D &other) const
Definition: Point3D.h:60
Gorgon::Geometry::basic_Point3D::Y
T_ Y
Definition: Point3D.h:67
Gorgon::Audio::Environment::AudioLoop
friend void AudioLoop()
Definition: Audio.cpp:76
Gorgon::Audio::Environment::Current
static Environment Current
Currently active environment.
Definition: Environment.h:128
Gorgon::Geometry::basic_Point3D::Normalize
basic_Point3D Normalize() const
Definition: Point3D.h:46
Gorgon::Audio::Listener::SetLocation
void SetLocation(const Geometry::Point3D &location)
Changes the current location of the listener, if automatic speed calculation is set,...
Definition: Environment.h:28
Gorgon::Audio::Environment::SetAuricleAngle
void SetAuricleAngle(float value)
Changes the difference of hearing direction cause by the auricles.
Definition: Environment.h:105
Gorgon::Geometry::basic_Point3D::Z
T_ Z
Definition: Point3D.h:68
Gorgon::WindowManager::init
void init()
Definition: X11.cpp:143
Gorgon::Audio::Environment::Environment
Environment()
Definition: Environment.h:83
Gorgon::Geometry::basic_Point3D
Definition: Point3D.h:12
Gorgon::Audio::Listener::SetOrientation
void SetOrientation(const Geometry::Point3D &up, const Geometry::Point3D &orientation)
Changes the orientation of the listener.
Definition: Environment.h:37
Gorgon::Audio::Listener::AudioLoop
friend void AudioLoop()
Definition: Audio.cpp:76
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Audio::Environment::GetListener
Listener & GetListener()
Returns the current listener object.
Definition: Environment.h:123
Gorgon::Audio::Environment::Listener
friend class Listener
Definition: Environment.h:81
Gorgon::Geometry::basic_Point3D::X
T_ X
Definition: Point3D.h:66
Gorgon::Audio::Environment::SetAttuniationFactor
void SetAttuniationFactor(float value)
Changes the attunation factor, higher values will attunate sound more, causing a faster fall off.
Definition: Environment.h:88
Gorgon::Audio::Environment
The environment which the audio system works on.
Definition: Environment.h:79
Environment.h
Gorgon::Audio::Listener::GetLocation
Geometry::Point3D GetLocation() const
Returns the current location of the listener.
Definition: Environment.h:54
Gorgon::Audio::Environment::SetNonBlocked
void SetNonBlocked(float value)
Changes the percent of sound not blocked by the head. This calculation might change in time.
Definition: Environment.h:100
Gorgon::Audio::Listener::Listener
Listener(Environment &env)
Definition: Environment.h:22
Gorgon::Audio::Listener
Listener class sets the properties for the audio listener.
Definition: Environment.h:17
Gorgon::Geometry::basic_Point3D::Distance
Float Distance() const
Definition: Point3D.h:52
Gorgon::Audio::Listener::Environment
friend class Environment
Definition: Environment.h:19
Gorgon::Audio::Listener::SetOrientation
void SetOrientation(const Geometry::Point3D &location)
Changes the orientation of the listener.
Definition: Environment.h:47
Gorgon::Geometry::Point3D
basic_Point3D< Float > Point3D
Definition: Point3D.h:84
Gorgon::Geometry::Transform3D
basic_Transform3D< Float > Transform3D
Definition: Transform3D.h:176
Gorgon::Audio::Environment::SetHeadRadius
void SetHeadRadius(float value)
Changes the radius of the head of listener.
Definition: Environment.h:93
Gorgon::Audio::Environment::SetSpeakerLocation
void SetSpeakerLocation(int index, Geometry::Point3D value)
Sets the real world location of the speakers.
Definition: Environment.h:113