Welcome to mirror list, hosted at ThFree Co, Russian Federation.

threaded_sound_wrapper.h « Sound « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f33437dd029837d1ab6a676e9513ab53e1adb805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//-----------------------------------------------------------------------------
//           Name: threaded_sound_wrapper.h
//      Developer: Wolfire Games LLC
//         Author: Max Danielsson
//    Description: Wrapper around the entire sound system, allowing all sound to be run
//                  in a separate thread. This is done using a message queue structure and a
//                  synchronized data structure for immediate reads.
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include <Sound/soundplayinfo.h>
#include <Sound/al_audio.h>
#include <Sound/ambienttriangle.h>

#include <Math/vec3.h>
#include <Internal/filesystem.h>

#include <queue>
#include <thread>
#include <mutex>
#include <chrono>

typedef unsigned long wrapper_sound_handle;
typedef unsigned long real_sound_handle;

class ThreadedSoundMessage {
   public:
    static const unsigned char LOCAL_DATA_SIZE = 64;

    enum Type {
        None,
        Initialize,
        Dispose,

        InFUpdateGameTimestep,
        InFUpdateGameTimescale,
        InFSetAirWhoosh,
        InFUpdateListener,
        InFPlay,
        InFPlayGroup,
        InFSetPosition,
        InFTranslatePosition,
        InFSetVelocity,
        InFSetPitch,
        InFSetVolume,
        InFStop,
        InFAddMusic,
        InFRemoveMusic,
        InFQueueSegment,
        InFTransitionToSegment,
        InFTransitionToSong,
        InFSetSegment,
        InFSetLayerGain,
        InFSetSong,
        InFAddAmbientTriangle,
        InFClear,
        InFClearTransient,
        InFSetMusicVolume,
        InFSetMasterVolume,
        InFSetOcclusionPosition,
        InFLoadSoundFile,
        InFEnableLayeredSoundtrackLimiter,

        InRCreateHandle
    };

   private:
    Type type;

    unsigned char local_data[LOCAL_DATA_SIZE];

    void Free();

    void* data;
    size_t datalen;

    bool has_handle;
    wrapper_sound_handle wsh;

    bool string_data;

   public:
    void Allocate(size_t size);

    ThreadedSoundMessage();
    ThreadedSoundMessage(Type t);
    ~ThreadedSoundMessage();

    ThreadedSoundMessage(const ThreadedSoundMessage& tsm);
    ThreadedSoundMessage& operator=(const ThreadedSoundMessage& tsm);

    Type GetType();

    void SetHandle(wrapper_sound_handle _wsh);
    wrapper_sound_handle GetWrapperHandle();
    real_sound_handle GetRealHandle();

    // Different ways of storing data in same memory chunk.
    void SetData(const void* mem_ptr, size_t data_len);
    void SetData(const void* mem_ptr, size_t offset, size_t data_len);
    void GetData(void* mem_ptr, size_t max_len);
    void GetData(void* mem_ptr, size_t offset, size_t data_len);

    void SetStringData(const std::string& str);
    std::string GetStringData();
};

class ThreadedSoundMessageQueue {
    std::queue<ThreadedSoundMessage> messages;
    std::mutex mutex;

   public:
    void Queue(const ThreadedSoundMessage& ev);
    ThreadedSoundMessage Pop();
    size_t Count();
};

struct SoundSourceInfo {
    vec3 pos;
    char name[128];
};

struct SoundDataCopy {
    std::string current_segment;
    std::string current_song_name;
    std::string current_song_type;

    std::string next_song_name;
    std::string next_song_type;

    std::vector<AmbientTriangle> ambient_triangles;

    std::map<std::string, float> current_layer_gains;
    std::vector<std::string> current_layer_names;

    std::vector<SoundSourceInfo> sound_source;
};

struct SoundInstanceDataCopy {
    static const int ID_SIZE = 8;
    static const int NAME_SIZE = 32;
    static const int TYPE_SIZE = 8;
    char id[ID_SIZE];
    char name[NAME_SIZE];
    char type[TYPE_SIZE];
    vec3 position;
};

/*
 * Class containing data that is used for copies, reads and mapping internal to external handles.
 */
class ThreadedSoundDataBridge {
   private:
    std::mutex mutex;

    wrapper_sound_handle handle_counter;
    std::map<wrapper_sound_handle, real_sound_handle> handle_map;
    std::map<wrapper_sound_handle, SoundInstanceDataCopy> data_copy_map;

    SoundDataCopy sound_data;

    std::vector<std::string> available_devices;
    std::string preferred_device;
    std::string used_device;

   public:
    ThreadedSoundDataBridge();

    wrapper_sound_handle CreateWrapperHandle();

    void SetHandle(wrapper_sound_handle wsh, real_sound_handle rsh);
    void SetIdentifier(wrapper_sound_handle wsh, const char* id);
    void SetValues(wrapper_sound_handle wsh, const char* name, const char* type);
    void SetPosition(wrapper_sound_handle wsh, const vec3& position);

    void RemoveHandle(wrapper_sound_handle wsh);
    real_sound_handle GetHandle(wrapper_sound_handle wsh);
    bool IsHandleValid(wrapper_sound_handle wsh);

    std::map<wrapper_sound_handle, real_sound_handle> GetAllHandles();
    SoundInstanceDataCopy GetHandleData(wrapper_sound_handle wsh);
    // void SetHandleData( wrapper_sound_handle wsh, const SoundInstanceDataCopy &sidc );

    SoundDataCopy GetData();
    size_t GetSoundHandleCount();
    size_t GetSoundInstanceCount();
    void SetData(const SoundDataCopy& dat);

    void SetPreferredDevice(const std::string& name);
    std::string GetPreferredDevice();

    void SetUsedDevice(const std::string& name);
    std::string GetUsedDevice();

    void SetAvailableDevices(std::vector<std::string> devices);
    std::vector<std::string> GetAvailableDevices();
};

class ThreadedSound {
   private:
    std::thread thread;
    bool initialized;

   public:
    ThreadedSound();
    ~ThreadedSound();

    void Initialize(const char* preferred_device);

    // Following is a copy of the Sound interface
    void Update();
    void UpdateGameTimestep(float timestep);
    void UpdateGameTimescale(float time_scale);

    void setAirWhoosh(float amount, float pitch = 1.0f);

    void updateListener(vec3 pos, vec3 vel, vec3 facing, vec3 up);

    unsigned long CreateHandle(const char* ident);

    void Play(const unsigned long& handle, SoundPlayInfo spi);

    void PlayGroup(const unsigned long& handle, const SoundGroupPlayInfo& sgpi);
    const vec3 GetPosition(const unsigned long& handle);
    const std::string GetName(const unsigned long& handle);
    const std::string GetType(const unsigned long& handle);
    const std::string GetID(const unsigned long& handle);
    void SetPosition(const unsigned long& handle, const vec3& new_pos);
    void TranslatePosition(const unsigned long& handle, const vec3& movement);
    void SetVelocity(const unsigned long& handle, const vec3& new_vel);
    void SetPitch(const unsigned long& handle, float pitch);
    void SetVolume(const unsigned long& handle, float volume);
    void Stop(const unsigned long& handle);
    bool IsHandleValid(const unsigned long& handle);

    void AddMusic(const Path& path);
    void RemoveMusic(const Path& path);

    void QueueSegment(const std::string& string);
    void TransitionToSegment(const std::string& string);
    void TransitionToSong(const std::string& string);

    void SetSegment(const std::string& string);
    void SetLayerGain(const std::string& layer, float gain);
    void SetSong(const std::string& string);

    const std::string GetSegment() const;
    const std::map<std::string, float> GetLayerGains() const;
    const std::vector<std::string> GetLayerNames() const;
    const float GetLayerGain(const std::string& layer) const;
    const std::string GetSongName() const;
    const std::string GetSongType() const;

    const std::string GetNextSongName() const;
    const std::string GetNextSongType() const;

    void AddAmbientTriangle(const std::string& path);
    std::vector<AmbientTriangle> GetAmbientTriangles();
    void Clear();
    // Clear all sounds that are considered transient (that is sound effects in a level and not music)
    // This is called when a level is unloaded, or loaded. Basically between major state changes.
    void ClearTransient();

    // std::vector<AudioEmitter*> GetActiveSounds();
    // alAudio &getAlAudio();
    // void changeSoundtrack(char *name, char *type)

    void Dispose();
    void SetMusicVolume(float val);
    void SetMasterVolume(float val);

    // void UpdateSoundOcclusion( );
    // void SetSoundOcclusionState( int id, bool occluded );
    void SetOcclusionPosition(const unsigned long& handle, const vec3& new_pos);
    void LoadSoundFile(const std::string& path);
    // End of Sound interface.

    std::vector<SoundSourceInfo> GetCurrentSoundSources();
    size_t GetSoundHandleCount();
    size_t GetSoundInstanceCount();

    std::map<wrapper_sound_handle, real_sound_handle> GetAllHandles();

    std::string GetPreferredDevice();
    std::string GetUsedDevice();
    std::vector<std::string> GetAvailableDevices();

    void EnableLayeredSoundtrackLimiter(bool val);
};