22 #include <SDL_mixer.h>
23 #include <glib-object.h>
25 #include "client-vala.h"
47 if (SDL_Init(SDL_INIT_AUDIO) == -1) {
48 fprintf(stderr,
"SDL_Init: %s\n", SDL_GetError());
52 if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2,
54 fprintf(stderr,
"Mix_OpenAudio: %s\n", SDL_GetError());
59 const int mix_flags = MIX_INIT_OGG;
60 int mix_init = Mix_Init(mix_flags);
61 if ((mix_init & mix_flags) != mix_flags) {
63 "OGG support in SDL_mixer is required for sound; aborting!\n");
82 if (!g_setenv(
"CF_SOUND_DIR", CF_SOUND_DIR, FALSE)) {
83 perror(
"Couldn't set $CF_SOUND_DIR");
88 chunk_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
89 (
void *)Mix_FreeChunk);
90 sounds = load_snd_config();
104 Mix_Chunk* chunk = g_hash_table_lookup(
chunk_cache, name);
110 snprintf(path,
sizeof(path),
"%s/%s", g_getenv(
"CF_SOUND_DIR"), name);
111 chunk = Mix_LoadWAV(path);
113 fprintf(stderr,
"Could not load sound from '%s': %s\n", path,
132 char const sound[
static 1],
char const source[
static 1]) {
134 "Playing sound2 x=%hhd y=%hhd dir=%hhd volume=%hhd type=%hhd sound=%s "
135 "source=%s", x, y, dir, vol, type, sound, source);
137 SoundInfo* si = g_hash_table_lookup(
sounds, sound);
139 LOG(
LOG_WARNING,
"cf_play_sound",
"sound not defined: %s", sound);
147 Mix_VolumeChunk(chunk, si->vol * MIX_MAX_VOLUME / 100);
149 int channel = Mix_GroupAvailable(-1);
151 g_warning(
"No free channels available to play sound");
154 Mix_Volume(channel, vol * MIX_MAX_VOLUME / 100);
155 Mix_PlayChannel(channel, chunk, 0);
160 if (strcmp(
music, last_played) != 0) {
178 Mix_FadeOutMusic(500);
180 Mix_FreeMusic(
music);
184 if (strcmp(music_name,
"NONE") == 0) {
188 snprintf(path,
sizeof(path),
"%s/music/%s.ogg", g_getenv(
"CF_SOUND_DIR"),
190 music = Mix_LoadMUS(path);
192 fprintf(stderr,
"Could not load music: %s\n", Mix_GetError());
195 Mix_VolumeMusic(MIX_MAX_VOLUME * 3/4);
196 Mix_FadeInMusic(
music, -1, 500);
201 Mix_FreeMusic(
music);
206 g_hash_table_destroy(
sounds);