Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
audiointerface.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2023 Conquer Space
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#pragma once
18
19#include <AL/al.h>
20#include <AL/alc.h>
21#include <spdlog/spdlog.h>
22
23#include <atomic>
24#include <fstream>
25#include <future>
26#include <map>
27#include <memory>
28#include <string>
29#include <vector>
30
33
36 // Set all variables
38 alGenSources((ALuint)1, &channel);
39
40 alSourcef(channel, AL_PITCH, 1);
41 alSourcef(channel, AL_GAIN, 1);
42
43 alSource3f(channel, AL_POSITION, 0, 0, 0);
44 alSource3f(channel, AL_VELOCITY, 0, 0, 0);
45 alSourcei(channel, AL_LOOPING, AL_FALSE);
46 }
47
48 void SetGain(float gain) { alSourcef(channel, AL_GAIN, gain); }
49
50 void SetPitch(float pitch) { alSourcef(channel, AL_PITCH, pitch); }
51
52 void SetLooping(bool looping) { alSourcei(channel, AL_LOOPING, looping ? AL_TRUE : AL_FALSE); }
53
54 void Play() { alSourcePlay(channel); }
55
56 void Stop() { alSourceStop(channel); }
57
58 void Resume() { alSourcePlay(channel); }
59
60 void Pause() { alSourcePause(channel); }
61
62 void Rewind() { alSourceRewind(channel); }
63
64 bool IsStopped() {
65 ALint source_state;
66 alGetSourcei(channel, AL_SOURCE_STATE, &source_state);
67 return (source_state == AL_STOPPED || source_state == AL_PAUSED);
68 }
69
70 bool IsPlaying() {
71 ALint source_state;
72 alGetSourcei(channel, AL_SOURCE_STATE, &source_state);
73 return (source_state == AL_PLAYING);
74 }
75
76 float PlayPosition() {
77 float length;
78 alGetSourcef(channel, AL_SEC_OFFSET, &length);
79 return length;
80 }
81
82 void EmptyBuffer() { alSourcei(channel, AL_BUFFER, (ALint)NULL); }
83
85 float length = 0;
86
88 // Halt playing
89 Stop();
90
92 alDeleteSources(1, &channel);
93 }
94
95 private:
96 ALuint channel;
97};
98
100 public:
102 ALCdevice *device;
103 ALCcontext *context;
104 ALboolean enumeration;
105
106 void Initialize() override;
107 void Pause(bool to_pause) override;
108 void PauseMusic(bool to_pause) override;
109 std::string GetAudioVersion() override;
110 void Destruct() override;
111 void StartWorker() override;
112 void RequestPlayAudio() override;
113 void SetMusicVolume(float volume) override;
114
115 void AddAudioClip(const std::string &key, cqsp::asset::AudioAsset *asset) override;
116 void PlayAudioClip(const std::string &key) override;
117 void PlayAudioClip(cqsp::asset::AudioAsset *asset, int channel) override;
118 void SetChannelVolume(int channel, float gain) override;
119 void OnFrame() override;
120
122
123 std::unique_ptr<cqsp::asset::AudioAsset> LoadWav(std::ifstream &input);
124 std::unique_ptr<cqsp::asset::AudioAsset> music_asset = nullptr;
125
126 std::shared_ptr<spdlog::logger> logger;
127
128 std::atomic_bool to_quit;
129
130 Hjson::Value playlist;
131
132 private:
133 void PrintInformation();
134 void InitListener();
135 void InitALContext();
136 std::unique_ptr<cqsp::asset::AudioAsset> LoadNextFile();
137 std::map<std::string, cqsp::asset::AudioAsset *> assets;
138 std::vector<std::unique_ptr<AudioChannel>> channels;
139
140 float music_volume = 0;
141
142 static const int MUSIC_CHANNEL = 0;
143 static const int UI_CHANNEL = 1;
144
148 std::future<std::unique_ptr<cqsp::asset::AudioAsset>> audio_future;
149};
150} // namespace cqsp::engine::audio
Definition: audioasset.h:23
Definition: audiointerface.h:99
std::unique_ptr< cqsp::asset::AudioAsset > LoadNextFile()
Definition: audiointerface.cpp:253
void OnFrame() override
Definition: audiointerface.cpp:102
static const int UI_CHANNEL
Definition: audiointerface.h:143
void RequestPlayAudio() override
Definition: audiointerface.cpp:72
std::shared_ptr< spdlog::logger > logger
Definition: audiointerface.h:126
std::string GetAudioVersion() override
Definition: audiointerface.cpp:57
void Initialize() override
Definition: audiointerface.cpp:33
void StartWorker() override
Definition: audiointerface.cpp:67
~AudioInterface()
Definition: audiointerface.cpp:141
static const int MUSIC_CHANNEL
Definition: audiointerface.h:142
AudioInterface()
Definition: audiointerface.cpp:31
ALboolean enumeration
Definition: audiointerface.h:104
std::unique_ptr< cqsp::asset::AudioAsset > LoadWav(std::ifstream &input)
Definition: audiointerface.cpp:177
void PrintInformation()
Definition: audiointerface.cpp:218
void SetChannelVolume(int channel, float gain) override
Definition: audiointerface.cpp:98
std::future< std::unique_ptr< cqsp::asset::AudioAsset > > audio_future
Async load audio files
Definition: audiointerface.h:148
ALCdevice * device
Definition: audiointerface.h:102
void InitALContext()
Definition: audiointerface.cpp:233
float music_volume
Definition: audiointerface.h:140
void InitListener()
Definition: audiointerface.cpp:225
void Destruct() override
Definition: audiointerface.cpp:59
std::atomic_bool to_quit
Definition: audiointerface.h:128
ALCcontext * context
Definition: audiointerface.h:103
Hjson::Value playlist
Definition: audiointerface.h:130
void PauseMusic(bool to_pause) override
Definition: audiointerface.cpp:55
std::map< std::string, cqsp::asset::AudioAsset * > assets
Definition: audiointerface.h:137
void SetMusicVolume(float volume) override
Definition: audiointerface.cpp:74
void Pause(bool to_pause) override
Definition: audiointerface.cpp:53
void PlayAudioClip(const std::string &key) override
Definition: audiointerface.cpp:85
std::vector< std::unique_ptr< AudioChannel > > channels
Definition: audiointerface.h:138
void AddAudioClip(const std::string &key, cqsp::asset::AudioAsset *asset) override
Definition: audiointerface.cpp:81
std::unique_ptr< cqsp::asset::AudioAsset > music_asset
Definition: audiointerface.h:124
Definition: iaudiointerface.h:29
Definition: audiointerface.h:34
Definition: audiointerface.h:35
void SetLooping(bool looping)
Definition: audiointerface.h:52
float PlayPosition()
Definition: audiointerface.h:76
void EmptyBuffer()
Definition: audiointerface.h:82
void Resume()
Definition: audiointerface.h:58
void Play()
Definition: audiointerface.h:54
void Pause()
Definition: audiointerface.h:60
ALuint channel
Definition: audiointerface.h:96
bool IsStopped()
Definition: audiointerface.h:64
~AudioChannel()
Definition: audiointerface.h:87
bool IsPlaying()
Definition: audiointerface.h:70
void Rewind()
Definition: audiointerface.h:62
void SetBuffer(cqsp::asset::AudioAsset *buffer)
Definition: audiointerface.cpp:274
float length
Definition: audiointerface.h:85
void SetGain(float gain)
Definition: audiointerface.h:48
AudioChannel()
Definition: audiointerface.h:37
void Stop()
Definition: audiointerface.h:56
void SetPitch(float pitch)
Definition: audiointerface.h:50