22 #include <SDL_mixer.h>
23 #include <glib-object.h>
25 #include "client-vala.h"
50 if (SDL_Init(SDL_INIT_AUDIO) == -1) {
51 fprintf(stderr,
"SDL_Init: %s\n", SDL_GetError());
55 if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2,
57 fprintf(stderr,
"Mix_OpenAudio: %s\n", SDL_GetError());
62 const int mix_flags = MIX_INIT_OGG;
63 int mix_init = Mix_Init(mix_flags);
64 if ((mix_init & mix_flags) != mix_flags) {
66 "OGG support in SDL_mixer is required for sound; aborting!\n");
85 if (!g_setenv(
"CF_SOUND_DIR", CF_SOUND_DIR, FALSE)) {
86 perror(
"Couldn't set $CF_SOUND_DIR");
91 chunk_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
92 (
void *)Mix_FreeChunk);
93 sounds = load_snd_config();
107 Mix_Chunk* chunk = g_hash_table_lookup(
chunk_cache, name);
113 snprintf(path,
sizeof(path),
"%s/%s", g_getenv(
"CF_SOUND_DIR"), name);
114 chunk = Mix_LoadWAV(path);
116 fprintf(stderr,
"Could not load sound from '%s': %s\n", path,
135 char const sound[
static 1],
char const source[
static 1]) {
137 "Playing sound2 x=%hhd y=%hhd dir=%hhd volume=%hhd type=%hhd sound=%s "
138 "source=%s", x, y, dir, vol, type, sound, source);
140 SoundInfo* si = g_hash_table_lookup(
sounds, sound);
142 LOG(
LOG_WARNING,
"cf_play_sound",
"sound not defined: %s", sound);
150 Mix_VolumeChunk(chunk, si->vol * MIX_MAX_VOLUME / 100);
152 int channel = Mix_GroupAvailable(-1);
154 g_warning(
"No free channels available to play sound");
157 Mix_Volume(channel, vol * MIX_MAX_VOLUME / 100);
158 Mix_PlayChannel(channel, chunk, 0);
171 snprintf(path, len,
"%s/music/%s.ogg", g_getenv(
"CF_SOUND_DIR"), name);
172 if (g_file_test(path, G_FILE_TEST_EXISTS))
174 snprintf(path, len,
"%s/music/%s.mp3", g_getenv(
"CF_SOUND_DIR"), name);
175 if (g_file_test(path, G_FILE_TEST_EXISTS))
185 Mix_HookMusicFinished(NULL);
187 Mix_FreeMusic(
music);
196 fprintf(stderr,
"Could not find a music file (ogg or mp3) for %s in %s\n",
music_playing, path);
199 music = Mix_LoadMUS(path);
201 fprintf(stderr,
"Could not load music: %s\n", Mix_GetError());
231 Mix_FreeMusic(
music);
236 g_hash_table_destroy(
sounds);