-
Notifications
You must be signed in to change notification settings - Fork 4
/
sound.h
61 lines (47 loc) · 1.25 KB
/
sound.h
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
#pragma once
#include <array>
#include "rand.h"
#include "vec.h"
struct Mix_Chunk;
// Note: By default, SDL_Mixer can only play 8 sounds at once
struct Sound
{
void Load(const char* path);
~Sound();
int Play() const; // returns a channel id
int Play(vec source, vec listener, float silenceDistance = 300.f) const; // returns a channel id
int PlayInLoop() const; // plays forever until stopped, returns a channel id
static bool Playing(int channel_id);
static void Stop(int channel_id);
void SetVolume(float volume); // between 0 and 1
float Volume() const; // between 0 and 1
private:
Mix_Chunk* sound = nullptr;
};
template<size_t Size>
struct MultiSound
{
template<std::size_t N>
void Load(const char * const (&paths)[N]) {
static_assert(N == Size);
for (int i = 0; i < Size; i++) {
sounds[i].Load(paths[i]);
}
}
void SetVolumeForAll(float volume) {
for (int i = 0; i < Size; i++) {
sounds[i].SetVolume(volume);
}
}
int Play() {
return sounds[Rand::roll(Size)].Play();
}
int Play(vec source, vec listener, float silenceDistance = 300.f) const {
return sounds[Rand::roll(Size)].Play(source, listener, silenceDistance);
}
const Sound& operator[](int i) {
return sounds[i];
}
private:
std::array<Sound, Size> sounds;
};