-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioutils.c
107 lines (95 loc) · 2.92 KB
/
audioutils.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
#include "audioutils.h"
#include "cflags.h"
HASHMAP_SOURCE(SoundMap, const char*, Mix_Chunk*, hashS)
HASHMAP_SOURCE(MusicMap, const char*, Mix_Music*, hashS)
void audio_init(AudioHandler* ah){
ah->audio_rate = 22050;
ah->audio_format = AUDIO_S16SYS;
ah->audio_channels = 2;
ah->audio_buffers = 4096;
ah->flags = 0;
ah->sounds = SoundMapInit();
ah->music = MusicMapInit();
if (Mix_OpenAudio(ah->audio_rate, ah->audio_format, ah->audio_channels, ah->audio_buffers)!=0){
printf("[!] Unable to open audio channel\n\taudio_rate : %u\n\taudio_format : %u\n\taudio_channels : %u\n\taudio_buffers : %u\n%s\n", ah->audio_rate, ah->audio_format, ah->audio_channels, ah->audio_buffers, SDL_GetError());
}
}
void add_sound(AudioHandler* ah, const char* source, const char* name){
Mix_Chunk* sound = Mix_LoadWAV(source);
if (!sound){
printf("[!] Unable to open audio file \'%s\'\n%s\n", source, SDL_GetError());
return;
}
Mix_Chunk* prev = SoundMapPop(&ah->sounds, name).val;
Mix_FreeChunk(prev);
prev = NULL;
SoundMapPush(&ah->sounds, name, sound);
}
void add_music(AudioHandler* ah, const char* source, const char* name){
Mix_Music* song = Mix_LoadMUS(source);
if (!song){
printf("[!] Unable to open audio file \'%s\'\n%s\n", source, SDL_GetError());
return;
}
Mix_Music* prev = MusicMapPop(&ah->music, name).val;
Mix_FreeMusic(prev);
prev = NULL;
MusicMapPush(&ah->music, name, song);
}
void play_sound(AudioHandler* ah, const char* name){
Mix_Chunk* sound = SoundMapGet(&ah->sounds, name).val;
if (!sound){
return;
}
if (Mix_PlayChannel(-1, sound, 1)==-1){
printf("[!] Unable to play sound \'%s\'\n%s\n", name, Mix_GetError());
}
}
void play_music(AudioHandler* ah, const char* name){
Mix_Music* song = MusicMapGet(&ah->music, name).val;
if (!song){
return;
}
if (Mix_PlayMusic(song, -1)==-1){
printf("[!] Unable to play song \'%s\'\n%s\n", name, Mix_GetError());
return;
}
ah->flags = bit_on(ah->flags, AUDIO_PLAYING);
}
uint8_t music_paused(AudioHandler* ah){
return bit_check(ah->flags, AUDIO_PAUSED);
}
uint8_t music_playing(AudioHandler* ah){
return bit_check(ah->flags, AUDIO_PLAYING);
}
void music_toggle_paused(AudioHandler* ah){
if (music_paused(ah)){
Mix_ResumeMusic();
ah->flags = bit_off(ah->flags, AUDIO_PAUSED);
return;
}
Mix_PauseMusic();
ah->flags = bit_on(ah->flags, AUDIO_PAUSED);
}
void music_set_paused(AudioHandler* ah, uint8_t p){
if (p){
Mix_PauseMusic();
ah->flags = bit_on(ah->flags, AUDIO_PAUSED);
return;
}
Mix_ResumeMusic();
ah->flags = bit_off(ah->flags, AUDIO_PAUSED);
}
void audio_close(AudioHandler* ah){
SoundMapIterator sit = SoundMapIteratorInit(&ah->sounds);
while (SoundMapIteratorHasNext(&sit)){
Mix_FreeChunk(SoundMapIteratorNext(&sit).val);
}
MusicMapIterator mit = MusicMapIteratorInit(&ah->music);
while (MusicMapIteratorHasNext(&mit)){
Mix_FreeMusic(MusicMapIteratorNext(&mit).val);
}
SoundMapFree(&ah->sounds);
MusicMapFree(&ah->music);
Mix_CloseAudio();
}