-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
145 lines (110 loc) · 4.92 KB
/
audio.c
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
#define CUTE_SOUND_IMPLEMENTATION
#include "cute_sound.h"
#define AUDIO_MASTER (1.0f)
enum AudioType {
AUDIO_GUN_0,
AUDIO_GUN_1,
AUDIO_SHOTGUN,
AUDIO_KNIFE,
AUDIO_MISSILE,
AUDIO_EXPLOSION,
AUDIO_FLAMETHROWER,
AUDIO_IMPACT,
AUDIO_SCREAM,
AUDIO_FOOTSTEP,
AUDIO_COUNT
};
static struct {
cs_context_t* context;
cs_loaded_sound_t loaded [AUDIO_COUNT];
cs_play_sound_def_t playing [AUDIO_COUNT];
} audio;
static void AudioInit(void) {
HWND hwnd = platform.native;
audio.context = cs_make_context(hwnd, 44100, 8192, 2048, NULL);
{
audio.loaded[AUDIO_GUN_0] = cs_load_wav("sound/sfx_weapon_singleshot3.wav");
audio.playing[AUDIO_GUN_0] = cs_make_def(&audio.loaded[AUDIO_GUN_0]);
audio.playing[AUDIO_GUN_0].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_GUN_0].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_GUN_1] = cs_load_wav("sound/sfx_weapon_singleshot4.wav");
audio.playing[AUDIO_GUN_1] = cs_make_def(&audio.loaded[AUDIO_GUN_1]);
audio.playing[AUDIO_GUN_1].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_GUN_1].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_SHOTGUN] = cs_load_wav("sound/sfx_weapon_shotgun3.wav");
audio.playing[AUDIO_SHOTGUN] = cs_make_def(&audio.loaded[AUDIO_SHOTGUN]);
audio.playing[AUDIO_SHOTGUN].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_SHOTGUN].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_KNIFE] = cs_load_wav("sound/sfx_wpn_punch3.wav");
audio.playing[AUDIO_KNIFE] = cs_make_def(&audio.loaded[AUDIO_KNIFE]);
audio.playing[AUDIO_KNIFE].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_KNIFE].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_MISSILE] = cs_load_wav("sound/sfx_wpn_missilelaunch.wav");
audio.playing[AUDIO_MISSILE] = cs_make_def(&audio.loaded[AUDIO_MISSILE]);
audio.playing[AUDIO_MISSILE].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_MISSILE].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_EXPLOSION] = cs_load_wav("sound/sfx_exp_short_hard2.wav");
audio.playing[AUDIO_EXPLOSION] = cs_make_def(&audio.loaded[AUDIO_EXPLOSION]);
audio.playing[AUDIO_EXPLOSION].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_EXPLOSION].volume_right = 0.5f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_IMPACT] = cs_load_wav("sound/sfx_sounds_impact6.wav");
audio.playing[AUDIO_IMPACT] = cs_make_def(&audio.loaded[AUDIO_IMPACT]);
audio.playing[AUDIO_IMPACT].volume_left = 0.3f * AUDIO_MASTER;
audio.playing[AUDIO_IMPACT].volume_right = 0.3f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_SCREAM] = cs_load_wav("sound/sfx_deathscream_human12.wav");
audio.playing[AUDIO_SCREAM] = cs_make_def(&audio.loaded[AUDIO_SCREAM]);
audio.playing[AUDIO_SCREAM].volume_left = 0.8f * AUDIO_MASTER;
audio.playing[AUDIO_SCREAM].volume_right = 0.8f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_FOOTSTEP] = cs_load_wav("sound/sfx_movement_footsteps1b.wav");
audio.playing[AUDIO_FOOTSTEP] = cs_make_def(&audio.loaded[AUDIO_FOOTSTEP]);
audio.playing[AUDIO_FOOTSTEP].volume_left = 0.15f * AUDIO_MASTER;
audio.playing[AUDIO_FOOTSTEP].volume_right = 0.15f * AUDIO_MASTER;
}
{
audio.loaded[AUDIO_FLAMETHROWER] = cs_load_wav("sound/flame.wav");
audio.playing[AUDIO_FLAMETHROWER] = cs_make_def(&audio.loaded[AUDIO_FLAMETHROWER]);
audio.playing[AUDIO_FLAMETHROWER].volume_left = 0.5f * AUDIO_MASTER;
audio.playing[AUDIO_FLAMETHROWER].volume_right = 0.5f * AUDIO_MASTER;
}
}
static void AudioPlay(int audio_type) {
cs_play_sound(audio.context, audio.playing[audio_type]);
}
static void AudioPlayLooped(int audio_type) {
cs_playing_sound_t* playing = cs_play_sound(audio.context, audio.playing[audio_type]);
//
playing->looped = 1;
}
static void AudioPlayFromSource(int audio_type, v2 listener, v2 source, f32 volume) {
f32 dst = 1.0f - fClamp(0.02f * v2_Dist(listener, source), 0.0f, 1.0f);
f32 pan = 0.5f * (fClamp(0.1f * (source.x - listener.x), -1.0f, 1.0f) + 1.0f);
f32 final_volume = AUDIO_MASTER * volume * dst;
cs_playing_sound_t* playing = cs_play_sound(audio.context, audio.playing[audio_type]);
cs_set_pan(playing, pan);
cs_set_volume(
playing,
audio.playing[audio_type].volume_left * final_volume,
audio.playing[audio_type].volume_right * final_volume);
}
static void AudioUpdate(void) {
cs_mix(audio.context);
}
static void AudioKillAll(void) {
cs_stop_all_sounds(audio.context);
}