Symphony Of Empires
audio.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // audio.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <map>
29 #include <string>
30 #include <exception>
31 #include <mutex>
32 #include <vector>
33 #include <memory>
34 
35 struct pa_simple;
36 namespace Eng3D {
37  class State;
38 
39  class AudioException : public std::exception {
40  std::string buffer;
41  public:
42  AudioException(const std::string& filename, const std::string& message) {
43  buffer = filename + message;
44  }
45 
46  virtual const char* what() const noexcept {
47  return buffer.c_str();
48  }
49  };
50 
51  struct Audio {
52  Audio() = default;
53  Audio(const std::string& path);
54  ~Audio();
55  void *stream = nullptr; // TODO: Use RAII pointers for this
56  };
57 
58  class AudioManager {
59  static void mixaudio(void* userdata, uint8_t* stream, int len);
60  std::map<std::string, std::shared_ptr<Eng3D::Audio>> sounds;
61  Eng3D::State& s;
62  int audio_dev_id = 0;
63  public:
64  AudioManager() = delete;
66  ~AudioManager();
67  const std::shared_ptr<Audio> load(const std::string& path);
68 
69  // Queue of sounds/music
70  std::mutex sound_lock;
71  std::vector<std::shared_ptr<Eng3D::Audio>> sound_queue;
72  std::vector<std::shared_ptr<Eng3D::Audio>> music_queue;
73  float music_fade_value = 1.f;
74  float music_volume = 0.5f, sound_volume = 0.5f;
75  };
76 }
virtual const char * what() const noexcept
Definition: audio.hpp:46
AudioException(const std::string &filename, const std::string &message)
Definition: audio.hpp:42
const std::shared_ptr< Audio > load(const std::string &path)
Definition: audio.cpp:119
float sound_volume
Definition: audio.hpp:74
std::mutex sound_lock
Definition: audio.hpp:70
float music_fade_value
Definition: audio.hpp:73
std::vector< std::shared_ptr< Eng3D::Audio > > sound_queue
Definition: audio.hpp:71
std::vector< std::shared_ptr< Eng3D::Audio > > music_queue
Definition: audio.hpp:72
float music_volume
Definition: audio.hpp:74
void * stream
Definition: audio.hpp:55
Audio()=default