* @file gtksound.h GTK+ Sound * Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA struct gaim_sound_event { #define PLAY_SOUND_TIMEOUT 15000 static guint mute_login_sounds_timeout = 0; static gboolean mute_login_sounds = FALSE; static gboolean sound_initialized = FALSE; static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { {N_("Buddy logs in"), "login", "login.wav"}, {N_("Buddy logs out"), "logout", "logout.wav"}, {N_("Message received"), "im_recv", "receive.wav"}, {N_("Message received begins conversation"), "first_im_recv", "receive.wav"}, {N_("Message sent"), "send_im", "send.wav"}, {N_("Person enters chat"), "join_chat", "login.wav"}, {N_("Person leaves chat"), "left_chat", "logout.wav"}, {N_("You talk in chat"), "send_chat_msg", "send.wav"}, {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"}, /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ {NULL, "pounce_default", "alert.wav"}, {N_("Someone says your screen name in chat"), "nick_said", "alert.wav"} static int ao_driver = -1; unmute_login_sounds_cb(gpointer data) mute_login_sounds = FALSE; mute_login_sounds_timeout = 0; chat_nick_matches_name(GaimConversation *conv, const char *aname) GaimConvChat *chat = NULL; chat = gaim_conversation_get_chat_data(conv); nick = g_strdup(gaim_normalize(conv->account, chat->nick)); name = g_strdup(gaim_normalize(conv->account, aname)); if (g_utf8_collate(nick, name) == 0) * play a sound event for a conversation, honoring make_sound flag * of conversation and checking for focus if conv_focus pref is set play_conv_event(GaimConversation *conv, GaimSoundEventID event) /* If we should not play the sound for some reason, then exit early */ GaimGtkConversation *gtkconv; gtkconv = GAIM_GTK_CONVERSATION(conv); has_focus = gaim_conversation_has_focus(conv); if (!gtkconv->make_sound || (has_focus && !gaim_prefs_get_bool("/gaim/gtk/sound/conv_focus"))) gaim_sound_play_event(event, conv ? gaim_conversation_get_account(conv) : NULL); buddy_state_cb(GaimBuddy *buddy, GaimSoundEventID event) gaim_sound_play_event(event, gaim_buddy_get_account(buddy)); im_msg_received_cb(GaimAccount *account, char *sender, char *message, GaimConversation *conv, int flags, GaimSoundEventID event) if (flags & GAIM_MESSAGE_DELAYED) gaim_sound_play_event(GAIM_SOUND_FIRST_RECEIVE, account); play_conv_event(conv, event); im_msg_sent_cb(GaimAccount *account, const char *receiver, const char *message, GaimSoundEventID event) GaimConversation *conv = gaim_find_conversation_with_account( GAIM_CONV_TYPE_ANY, receiver, account); play_conv_event(conv, event); chat_buddy_join_cb(GaimConversation *conv, const char *name, GaimConvChatBuddyFlags flags, gboolean new_arrival, if (new_arrival && !chat_nick_matches_name(conv, name)) play_conv_event(conv, event); chat_buddy_left_cb(GaimConversation *conv, const char *name, const char *reason, GaimSoundEventID event) if (!chat_nick_matches_name(conv, name)) play_conv_event(conv, event); chat_msg_sent_cb(GaimAccount *account, const char *message, int id, GaimSoundEventID event) GaimConnection *conn = gaim_account_get_connection(account); GaimConversation *conv = NULL; conv = gaim_find_chat(conn,id); play_conv_event(conv, event); chat_msg_received_cb(GaimAccount *account, char *sender, char *message, GaimConversation *conv, GaimMessageFlags flags, GaimSoundEventID event) if (flags & GAIM_MESSAGE_DELAYED) chat = gaim_conversation_get_chat_data(conv); g_return_if_fail(chat != NULL); if (gaim_conv_chat_is_user_ignored(chat, sender)) if (chat_nick_matches_name(conv, sender)) if (flags & GAIM_MESSAGE_NICK || gaim_utf8_has_word(message, chat->nick)) play_conv_event(conv, GAIM_SOUND_CHAT_NICK); play_conv_event(conv, event); * We mute sounds for the 10 seconds after you log in so that * you don't get flooded with sounds when the blist shows all * your buddies logging in. account_signon_cb(GaimConnection *gc, gpointer data) if (mute_login_sounds_timeout != 0) g_source_remove(mute_login_sounds_timeout); mute_login_sounds = TRUE; mute_login_sounds_timeout = gaim_timeout_add(10000, unmute_login_sounds_cb, NULL); _pref_sound_method_changed(const char *name, GaimPrefType type, gconstpointer val, gpointer data) if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method")) sound_initialized = TRUE; ao_driver = ao_driver_id("esd"); else if(!strcmp(val, "arts")) ao_driver = ao_driver_id("arts"); else if(!strcmp(val, "nas")) ao_driver = ao_driver_id("nas"); else if(!strcmp(val, "automatic")) ao_driver = ao_default_driver_id(); ao_info *info = ao_driver_info(ao_driver); "Sound output driver loaded: %s\n", info->name); gaim_gtk_sound_get_event_option(GaimSoundEventID event) if(event >= GAIM_NUM_SOUNDS) return sounds[event].pref; gaim_gtk_sound_get_event_label(GaimSoundEventID event) if(event >= GAIM_NUM_SOUNDS) return sounds[event].label; gaim_gtk_sound_get_handle() gaim_gtk_sound_init(void) void *gtk_sound_handle = gaim_gtk_sound_get_handle(); void *blist_handle = gaim_blist_get_handle(); void *conv_handle = gaim_conversations_get_handle(); gaim_signal_connect(gaim_connections_get_handle(), "signed-on", gtk_sound_handle, GAIM_CALLBACK(account_signon_cb), gaim_prefs_add_none("/gaim/gtk/sound"); gaim_prefs_add_none("/gaim/gtk/sound/enabled"); gaim_prefs_add_none("/gaim/gtk/sound/file"); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE); gaim_prefs_add_string("/gaim/gtk/sound/file/login", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE); gaim_prefs_add_string("/gaim/gtk/sound/file/logout", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE); gaim_prefs_add_string("/gaim/gtk/sound/file/im_recv", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/first_im_recv", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE); gaim_prefs_add_string("/gaim/gtk/sound/file/send_im", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/join_chat", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/left_chat", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/send_chat_msg", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/chat_msg_recv", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/file/nick_said", ""); gaim_prefs_add_bool("/gaim/gtk/sound/enabled/pounce_default", TRUE); gaim_prefs_add_string("/gaim/gtk/sound/file/pounce_default", ""); gaim_prefs_add_bool("/gaim/gtk/sound/conv_focus", TRUE); gaim_prefs_add_bool("/gaim/gtk/sound/mute", FALSE); gaim_prefs_add_string("/gaim/gtk/sound/command", ""); gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic"); gaim_prefs_add_int("/gaim/gtk/sound/volume", 50); gaim_debug_info("sound", "Initializing sound output drivers.\n"); gaim_prefs_connect_callback(gaim_gtk_sound_get_handle(), "/gaim/gtk/sound/method", _pref_sound_method_changed, NULL); gaim_signal_connect(blist_handle, "buddy-signed-on", gtk_sound_handle, GAIM_CALLBACK(buddy_state_cb), GINT_TO_POINTER(GAIM_SOUND_BUDDY_ARRIVE)); gaim_signal_connect(blist_handle, "buddy-signed-off", gtk_sound_handle, GAIM_CALLBACK(buddy_state_cb), GINT_TO_POINTER(GAIM_SOUND_BUDDY_LEAVE)); gaim_signal_connect(conv_handle, "received-im-msg", gtk_sound_handle, GAIM_CALLBACK(im_msg_received_cb), GINT_TO_POINTER(GAIM_SOUND_RECEIVE)); gaim_signal_connect(conv_handle, "sent-im-msg", gtk_sound_handle, GAIM_CALLBACK(im_msg_sent_cb), GINT_TO_POINTER(GAIM_SOUND_SEND)); gaim_signal_connect(conv_handle, "chat-buddy-joined", gtk_sound_handle, GAIM_CALLBACK(chat_buddy_join_cb), GINT_TO_POINTER(GAIM_SOUND_CHAT_JOIN)); gaim_signal_connect(conv_handle, "chat-buddy-left", gtk_sound_handle, GAIM_CALLBACK(chat_buddy_left_cb), GINT_TO_POINTER(GAIM_SOUND_CHAT_LEAVE)); gaim_signal_connect(conv_handle, "sent-chat-msg", gtk_sound_handle, GAIM_CALLBACK(chat_msg_sent_cb), GINT_TO_POINTER(GAIM_SOUND_CHAT_YOU_SAY)); gaim_signal_connect(conv_handle, "received-chat-msg", gtk_sound_handle, GAIM_CALLBACK(chat_msg_received_cb), GINT_TO_POINTER(GAIM_SOUND_CHAT_SAY)); gaim_gtk_sound_uninit(void) sound_initialized = FALSE; gaim_signals_disconnect_by_handle(gaim_gtk_sound_get_handle()); expire_old_child(gpointer data) pid_t pid = GPOINTER_TO_INT(data); ret = waitpid(pid, NULL, WNOHANG | WUNTRACED); if(kill(pid, SIGKILL) < 0) gaim_debug_error("gtksound", "Killing process %d failed (%s)\n", return FALSE; /* do not run again */ /* Uncomment the following line to enable debugging of clipping in the scaling. */ /* #define DEBUG_CLIPPING */ scale_pcm_data(char *data, int nframes, int bits, int channels, double intercept, double minclip, double maxclip, gint16 *data16 = (gint16*)data; gint32 *data32 = (gint32*)data; gint64 *data64 = (gint64*)data; for(i = 0; i < nframes * channels; i++) { v = ((data16[i] - intercept) * scale) + intercept; printf("Clipping detected!\n"); printf("Clipping detected!\n"); v = CLAMP(v, minclip, maxclip); for(i = 0; i < nframes * channels; i++) { v = ((data32[i] - intercept) * scale) + intercept; printf("Clipping detected!\n"); printf("Clipping detected!\n"); v = CLAMP(v, minclip, maxclip); for(i = 0; i < nframes * channels; i++) { v = ((data64[i] - intercept) * scale) + intercept; printf("Clipping detected!\n"); printf("Clipping detected!\n"); v = CLAMP(v, minclip, maxclip); gaim_debug_warning("gtksound", "Scaling of %d bit pcm data not supported.\n", bits); gaim_gtk_sound_play_file(const char *filename) gaim_prefs_trigger_callback("/gaim/gtk/sound/method"); if (gaim_prefs_get_bool("/gaim/gtk/sound/mute")) method = gaim_prefs_get_string("/gaim/gtk/sound/method"); if (!strcmp(method, "none")) { } else if (!strcmp(method, "beep")) { if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename); gaim_notify_error(NULL, NULL, tmp, NULL); if (!strcmp(method, "custom")) { sound_cmd = gaim_prefs_get_string("/gaim/gtk/sound/command"); if (!sound_cmd || *sound_cmd == '\0') { gaim_notify_error(NULL, NULL, _("Unable to play sound because the " "'Command' sound method has been chosen, " "but no command has been set."), NULL); if(strstr(sound_cmd, "%s")) command = gaim_strreplace(sound_cmd, "%s", filename); command = g_strdup_printf("%s %s", sound_cmd, filename); if(!g_spawn_command_line_async(command, &error)) { char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); gaim_notify_error(NULL, NULL, tmp, NULL); volume = gaim_prefs_get_int("/gaim/gtk/sound/volume"); volume = CLAMP(volume, 0, 100); /* calculating the scaling factor: * scale(x) = (x+30)^2 / 6400 * scale(0) = 0.1406 (quiet) * scale(50) = 1.0 (no scaling, normal volume) * scale(100) = 2.6406 (roughly maximized without clipping) float scale = ( ((float)volume + 30) * ((float)volume + 30) ) / 6400; file = afOpenFile(filename, "rb", NULL); double slope, intercept, minclip, maxclip; format.rate = afGetRate(file, AF_DEFAULT_TRACK); format.channels = afGetChannels(file, AF_DEFAULT_TRACK); afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, afGetPCMMapping(file, AF_DEFAULT_TRACK, &slope, &intercept, &minclip, &maxclip); /* XXX: libao doesn't seem to like 8-bit sounds, so we'll * let libaudiofile make them a bit better for us */ afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, format.bits); #if G_BYTE_ORDER == G_BIG_ENDIAN format.byte_format = AO_FMT_BIG; afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, #elif G_BYTE_ORDER == G_LITTLE_ENDIAN format.byte_format = AO_FMT_LITTLE; afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN); #warning Unknown endianness bytes_per_frame = format.bits * format.channels / 8; device = ao_open_live(ao_driver, &format, NULL); int buf_frames = sizeof(buf) / bytes_per_frame; while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, /* no need to scale at volume == 50 */ scale_pcm_data(buf, frames_read, format.bits, format.channels, intercept, minclip, maxclip, scale); if(!ao_play(device, buf, frames_read * bytes_per_frame)) gaim_timeout_add(PLAY_SOUND_TIMEOUT, expire_old_child, GINT_TO_POINTER(pid)); gaim_debug_info("sound", "Playing %s\n", filename); if (G_WIN32_HAVE_WIDECHAR_API ()) { wchar_t *wc_filename = g_utf8_to_utf16(filename, if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME)) gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); char *l_filename = g_locale_from_utf8(filename, if (!PlaySoundA(l_filename, NULL, SND_ASYNC | SND_FILENAME)) gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); gaim_gtk_sound_play_event(GaimSoundEventID event) if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) if (event >= GAIM_NUM_SOUNDS) { gaim_debug_error("sound", "got request for unknown sound: %d\n", event); enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s", file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref); /* check NULL for sounds that don't have an option, ie buddy pounce */ if (gaim_prefs_get_bool(enable_pref)) { char *filename = g_strdup(gaim_prefs_get_string(file_pref)); if(!filename || !strlen(filename)) { if(filename) g_free(filename); filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); gaim_sound_play_file(filename, NULL); static GaimSoundUiOps sound_ui_ops = gaim_gtk_sound_play_file, gaim_gtk_sound_play_event gaim_gtk_sound_get_ui_ops(void)