Gorgon Game Engine
Controllers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mutex>
4 
5 #include "../Containers/Wave.h"
6 #include "../Containers/Collection.h"
7 
8 #include "../Geometry/Point3D.h"
9 
10 
11 namespace Gorgon { namespace Audio {
12 
16  enum class ControllerType {
17  Basic,
19  };
20 
24  class Controller {
25  friend void AudioLoop();
26  public:
28  Controller();
29 
30  virtual ~Controller();
31 
33  virtual ControllerType Type() const = 0;
34 
35  protected:
36  };
37 
41  class BasicController : public Controller {
42  friend void AudioLoop();
43  public:
44 
46  BasicController() = default;
47 
50  datachanged();
51  }
52 
54  bool HasData() const { return wavedata != nullptr; }
55 
57  void ReleaseData();
58 
63  void SetData(const Containers::Wave &wavedata);
64 
69 
74 
78 
81 
84  BasicController &Seek(float target);
85 
88  BasicController &SeekTo(float target);
89 
92  BasicController &SetVolume(float volume);
93 
95  float GetVolume() const;
96 
98  float GetDuration() const;
99 
101  float GetCurrentTime() const;
102 
104  float GetCurrentFraction() const;
105 
107  bool IsFinished() const;
108 
110  bool IsPlaying() const;
111 
113  bool IsLooping() const;
114 
115  virtual ControllerType Type() const override { return ControllerType::Basic; }
116 
117  protected:
119  const Containers::Wave *wavedata = nullptr;
120 
123  virtual void datachanged() { }
124  private:
125  float volume = 1;
126  float position = 0; //in seconds
127 
128  bool playing = false;
129  bool looping = false;
130  };
131 
141  friend void AudioLoop();
142  public:
143 
145  PositionalController() = default;
146 
149 
150  void Move(const Geometry::Point3D &target) {
151  location = target;
152  }
153 
154  void Move(const Geometry::Pointf &target) {
155  location = {target, 0};
156  }
157 
159  return location;
160  }
161 
162  virtual ControllerType Type() const override { return ControllerType::Positional; }
163  private:
164  Geometry::Point3D location = {0, 0, 0};
165  };
166 
167  namespace internal {
168  extern Containers::Collection<Controller> Controllers;
169  extern std::mutex ControllerMtx;
170  }
171 
172 } }
Gorgon::Audio::BasicController::ReleaseData
void ReleaseData()
Releases the data being played by this controller.
Definition: Controllers.cpp:19
Gorgon::Audio::PositionalController::Type
virtual ControllerType Type() const override
Returns the type of the controller.
Definition: Controllers.h:162
Gorgon::Audio::BasicController::Seek
BasicController & Seek(float target)
Changes the point of playback to the given time in seconds.
Definition: Controllers.cpp:68
Gorgon::Audio::BasicController::GetDuration
float GetDuration() const
Returns the duration of the audio buffer in seconds. This function will return 0 if data is not set.
Definition: Controllers.cpp:95
Gorgon::Audio::BasicController::IsLooping
bool IsLooping() const
Whether the audio is being looped.
Definition: Controllers.cpp:117
Gorgon::Audio::BasicController::Pause
BasicController & Pause()
Pauses the playback.
Definition: Controllers.cpp:56
Gorgon::Geometry::basic_Point3D
Definition: Point3D.h:12
Gorgon::Audio::Controller
This is the base class for all controllers.
Definition: Controllers.h:24
Gorgon::Audio::Controller::AudioLoop
friend void AudioLoop()
Definition: Audio.cpp:76
Gorgon::Audio::PositionalController::GetLocation
Geometry::Point3D GetLocation() const
Definition: Controllers.h:158
Gorgon::Audio::Controller::Type
virtual ControllerType Type() const =0
Returns the type of the controller.
Gorgon::Audio::BasicController::SeekTo
BasicController & SeekTo(float target)
Changes the point of playback to the given time in fraction: 1 would be the end of buffer.
Definition: Controllers.cpp:74
Gorgon::Audio::BasicController::SetVolume
BasicController & SetVolume(float volume)
Changes the volume of the playback.
Definition: Controllers.cpp:85
Gorgon::Audio::internal::ControllerMtx
std::mutex ControllerMtx
Definition: Controllers.cpp:127
Gorgon::Audio::BasicController::BasicController
BasicController(const Containers::Wave &wavedata)
Filling constructor.
Definition: Controllers.h:49
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Audio::internal::Controllers
Containers::Collection< Controller > Controllers
Definition: Controllers.cpp:126
Gorgon::Audio::BasicController::HasData
bool HasData() const
Returns whether this controller has data.
Definition: Controllers.h:54
Gorgon::Audio::internal::volume
std::vector< float > volume
Definition: Audio.cpp:35
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
Gorgon::Audio::BasicController::SetData
void SetData(const Containers::Wave &wavedata)
Sets the data to be played by this controller.
Definition: Controllers.cpp:26
Gorgon::Audio::BasicController::GetCurrentTime
float GetCurrentTime() const
Returns the current playback time in seconds.
Definition: Controllers.cpp:102
Gorgon::Audio::BasicController::GetVolume
float GetVolume() const
Returns the current volume of the playback.
Definition: Controllers.cpp:91
Gorgon::Audio::ControllerType
ControllerType
Identifies controller types.
Definition: Controllers.h:16
Gorgon::Audio::BasicController::GetCurrentFraction
float GetCurrentFraction() const
Returns the fraction of the audio that is played.
Definition: Controllers.cpp:106
Gorgon::Audio::BasicController::Loop
BasicController & Loop()
Plays this sound.
Definition: Controllers.cpp:49
Gorgon::Audio::Controller::Controller
Controller()
Default constructor.
Definition: Controllers.cpp:5
Gorgon::Audio::BasicController::wavedata
const Containers::Wave * wavedata
Contains the data for this controller.
Definition: Controllers.h:119
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Controllers.h
Gorgon::Audio::BasicController::Play
BasicController & Play()
Plays this sound.
Definition: Controllers.cpp:33
Gorgon::Audio::PositionalController::AudioLoop
friend void AudioLoop()
Definition: Audio.cpp:76
Gorgon::Audio::PositionalController::Move
void Move(const Geometry::Pointf &target)
Definition: Controllers.h:154
Gorgon::Audio::BasicController::IsPlaying
bool IsPlaying() const
Whether the audio is being played right now.
Definition: Controllers.cpp:121
Gorgon::Audio::BasicController::datachanged
virtual void datachanged()
Override this function to detect changes in the wave data.
Definition: Controllers.h:123
Gorgon::Audio::PositionalController::Move
void Move(const Geometry::Point3D &target)
Definition: Controllers.h:150
Gorgon::Audio::BasicController
Basic controller, allows non-positional playback of data buffer.
Definition: Controllers.h:41
Gorgon::Audio::BasicController::AudioLoop
friend void AudioLoop()
Definition: Audio.cpp:76
Gorgon::Audio::BasicController::BasicController
BasicController()=default
Default constructor.
Gorgon::Audio::PositionalController::PositionalController
PositionalController()=default
Default constructor.
Gorgon::Containers::Wave::GetLength
float GetLength() const
Returns the length of the wave data in seconds.
Definition: Wave.h:415
Gorgon::Containers::Wave
This class is a container for wave data.
Definition: Wave.h:17
Gorgon::Audio::PositionalController
Positional controller allows sounds to be played at a specific location.
Definition: Controllers.h:140
Gorgon::Audio::BasicController::Reset
BasicController & Reset()
Resets the playback to the beginning of the buffer. This function will not stop the playback.
Definition: Controllers.cpp:62
Gorgon::Audio::BasicController::IsFinished
bool IsFinished() const
Whether the audio is finished playing.
Definition: Controllers.cpp:113
Gorgon::Audio::ControllerType::Basic
@ Basic
Gorgon::Audio::PositionalController::PositionalController
PositionalController(const Containers::Wave &wavedata)
Filling constructor.
Definition: Controllers.h:148
Gorgon::Audio::BasicController::Type
virtual ControllerType Type() const override
Returns the type of the controller.
Definition: Controllers.h:115
Gorgon::Audio::Controller::~Controller
virtual ~Controller()
Definition: Controllers.cpp:11