Gorgon Game Engine
Audio.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Utils/Logging.h"
4 #include "Event.h"
5 #include "Audio/Basic.h"
6 
7 #include <vector>
8 #include <string>
9 #include <thread>
10 
11 namespace Gorgon {
13 namespace Audio {
14 
16  void Initialize();
17 
19  bool IsAvailable();
20 
22  void SetVolume(float volume);
23 
26  void SetVolume(Channel channel, float volume);
27 
29  float GetVolume();
30 
33  float GetVolume(Channel channel);
34 
36  class Device {
37  public:
38 
39  Device() = default;
40 
41  Device(const std::string &id, const std::string &name, int rate, Format format, bool headphones, const std::vector<Channel> &channels) :
42  name(name), id(id), rate(rate), format(format), headphones(headphones), channels(channels)
43  { }
44 
46  std::string GetID() const {
47  return id;
48  }
49 
51  std::string GetName() const {
52  return name;
53  }
54 
56  int GetSampleRate() const {
57  return rate;
58  }
59 
62  Format GetFormat() const {
63  return format;
64  }
65 
67  int GetChannelCount() const {
68  return (int)channels.size();
69  }
70 
72  Channel GetChannel(int index) const {
73  return channels[index];
74  }
75 
77  int FindChannel(Channel type) const {
78  for(int i = 0; i<(int)channels.size(); i++)
79  if(type==channels[i])
80  return i;
81 
82  return -1;
83  }
84 
86  bool IsValid() const {
87  return rate != 0;
88  }
89 
91  bool IsHeadphones() const {
92  return headphones;
93  }
94 
96  static const std::vector<Device> &Devices() {
97  return devices;
98  }
99 
101  static Device Default() {
102  return def;
103  }
104 
106  static void Refresh();
107 
110  static Device Find(const std::string &id) {
111  for(auto &dev : devices) {
112  if(dev.id == id) return dev;
113  }
114 
115  throw std::runtime_error("Cannot find device: "+id);
116  }
117 
122 
123  private:
124  std::string id;
125  std::string name;
126 
127  bool headphones = false;
128 
129  int rate = 0;
130  Format format;
131 
132  std::vector<Channel> channels;
133 
134  static std::vector<Device> devices;
135  static Device def;
136  };
137 
138  extern Utils::Logger Log;
139  extern Device Current;
140 
141  namespace internal {
142  extern std::thread audiothread;
143 
144  extern float mastervolume;
145  extern std::vector<float> volume;
146  }
147 }
148 }
Gorgon::Audio::Initialize
void Initialize()
Starts audio subsystem with the default device.
Definition: Filesystem.cpp:14
Gorgon::Audio::internal::audiothread
std::thread audiothread
Definition: Audio.cpp:29
Gorgon::Audio::Device::FindChannel
int FindChannel(Channel type) const
Returns the index of the given type of channel. If that channel type does not exists,...
Definition: Audio.h:77
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Gorgon::Audio::Device::Refresh
static void Refresh()
Refreshes the list of audio devices. Calling this function triggers ChangedEvent.
Gorgon::Audio::IsAvailable
bool IsAvailable()
Whether the audio is available.
Gorgon::Audio::Device::IsValid
bool IsValid() const
Returns if this is a valid device.
Definition: Audio.h:86
Gorgon::Audio::Device::GetChannel
Channel GetChannel(int index) const
Returns the channel type with the given index.
Definition: Audio.h:72
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Audio::Device::Find
static Device Find(const std::string &id)
Name based device lookup.
Definition: Audio.h:110
Gorgon::Audio::internal::volume
std::vector< float > volume
Definition: Audio.cpp:35
Gorgon::Utils::Logger
Eases logging procedure by appending necessary information to the given data and streams to a standar...
Definition: Logging.h:19
Gorgon::Audio::Device::GetName
std::string GetName() const
Returns the readable name of this device.
Definition: Audio.h:51
Gorgon::Audio::Current
Device Current
Definition: Audio.cpp:24
Gorgon::Audio::Device::ChangedEvent
static Event ChangedEvent
Triggers when the audio device configuration is changed.
Definition: Audio.h:121
Gorgon::Audio::internal::mastervolume
float mastervolume
Definition: Audio.cpp:34
Gorgon::Audio::Device::Device
Device(const std::string &id, const std::string &name, int rate, Format format, bool headphones, const std::vector< Channel > &channels)
Definition: Audio.h:41
Gorgon::Audio::Device::IsHeadphones
bool IsHeadphones() const
Returns if this device is connected to headphones.
Definition: Audio.h:91
Gorgon::Audio::SetVolume
void SetVolume(float volume)
Changes the master volume.
Definition: Audio.cpp:44
Gorgon::Audio::Device::GetID
std::string GetID() const
Returns the ID of this device.
Definition: Audio.h:46
Gorgon::Audio::Device
Represents an audio device.
Definition: Audio.h:36
Gorgon::Audio::Log
Utils::Logger Log
Event.h
contains event distribution mechanism
Gorgon::Audio::Device::GetSampleRate
int GetSampleRate() const
Returns number of samples per second.
Definition: Audio.h:56
Gorgon::Audio::Channel
Channel
Names for channels.
Definition: Basic.h:16
Gorgon::Audio::Device::Device
Device()=default
Gorgon::Audio::Device::GetChannelCount
int GetChannelCount() const
Returns the number of channels available.
Definition: Audio.h:67
Gorgon::Audio::Device::Devices
static const std::vector< Device > & Devices()
Returns the devices in the current system.
Definition: Audio.h:96
Gorgon::Audio::GetVolume
float GetVolume()
Returns the master volume.
Definition: Audio.cpp:63
Basic.h
Logging.h
Gorgon::Audio::Device::Default
static Device Default()
Returns the default device of the current system.
Definition: Audio.h:101
Gorgon::Audio::Format
Format
Sample format.
Definition: Basic.h:9
Gorgon::Audio::Device::GetFormat
Format GetFormat() const
Returns the format of this device.
Definition: Audio.h:62