forked from simple2d/simple2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
142 lines (124 loc) · 3.15 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
#include <simple2d.h>
bool is_sound = true;
int sound = 1;
int music = 2;
S2D_Sound *snd1;
S2D_Sound *snd2;
S2D_Music *mus1;
S2D_Music *mus2;
S2D_Window *window;
void print_help() {
puts("");
puts(" |-------------------------------|");
puts(" | AUDIO TESTS KEYBOARD COMMANDS |");
puts(" |-------------------------------|");
puts(" | Q -> Switch between |");
puts(" | `Sound` and `Music` |");
puts(" | |");
puts(" | Sound: |");
puts(" | P -> S2D_PlaySound |");
puts(" | |");
puts(" | Music: |");
puts(" | P -> S2D_PlayMusic |");
puts(" | A -> S2D_PauseMusic |");
puts(" | S -> S2D_StopMusic |");
puts(" | R -> S2D_ResumeMusic |");
puts(" | F -> S2D_FadeOutMusic |");
puts(" | |");
puts(" | Switching Samples: |");
puts(" | 1 -> note.wav |");
puts(" | 2 -> music.ogg |");
puts(" | |");
puts(" | H -> Print this help message |");
puts(" | Esc -> Exit |");
puts(" |-------------------------------|");
puts("");
}
void on_key(S2D_Event e) {
if (e.type != S2D_KEY_DOWN) return;
if (strcmp(e.key, "Escape") == 0) {
S2D_Close(window);
}
switch (*e.key) {
case 'H':
print_help();
break;
case 'Q':
is_sound = is_sound ? false: true;
if (is_sound) {
puts("Switching to Sound...");
} else {
puts("Switching to Music...");
}
break;
case 'P':
if (is_sound) {
puts("S2D_PlaySound...");
if (sound == 1) {
S2D_PlaySound(snd1);
} else {
S2D_PlaySound(snd2);
}
} else {
puts("S2D_PlayMusic...");
if (music == 1) {
S2D_PlayMusic(mus1, true);
} else {
S2D_PlayMusic(mus2, true);
}
}
break;
case 'A':
puts("S2D_PauseMusic...");
S2D_PauseMusic();
break;
case 'R':
puts("S2D_ResumeMusic...");
S2D_ResumeMusic();
break;
case 'S':
puts("S2D_StopMusic...");
S2D_StopMusic();
break;
case 'F':
puts("S2D_FadeOutMusic...");
S2D_FadeOutMusic(2000);
break;
case '1':
puts("Switching to sound.wav...");
if (is_sound) {
sound = 1;
} else {
music = 1;
}
break;
case '2':
puts("Switching to music.ogg...");
if (is_sound) {
sound = 2;
} else {
music = 2;
}
break;
}
}
int main() {
S2D_Diagnostics(true);
window = S2D_CreateWindow(
"Simple 2D — Audio", 200, 150, NULL, NULL, 0
);
if (!window) return 1;
window->on_key = on_key;
snd1 = S2D_CreateSound("media/sound.wav");
snd2 = S2D_CreateSound("media/music.ogg");
mus1 = S2D_CreateMusic("media/sound.wav");
mus2 = S2D_CreateMusic("media/music.ogg");
print_help();
S2D_Show(window);
S2D_FreeSound(snd1);
S2D_FreeSound(snd2);
S2D_FreeMusic(mus1);
S2D_FreeMusic(mus2);
S2D_FreeWindow(window);
return 0;
}