2009-03-24 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-sound.c
1 /*
2  * This file is a part of libhildon
3  *
4  * Copyright (C) 2005-2008 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Kimmo Hämäläinen <kimmo.hamalainen@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-sound
27  * @short_description: libcanberra-based utility function for playing a sound.
28  * 
29  */
30
31 #include <unistd.h>
32 #include <gconf/gconf-client.h>
33 #include <canberra.h>
34
35 #include "hildon-sound.h"
36
37 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
38
39 /**
40  * hildon_play_system_sound:
41  * @sample: sound file to play
42  * 
43  * Plays the given sample using libcanberra.
44  * Volume level is received from gconf. 
45  */
46 void 
47 hildon_play_system_sound(const gchar *sample)
48 {
49     float volume = 0;
50     int ret;
51     ca_context *ca_con = NULL;
52     ca_proplist *pl = NULL;
53
54 #if 0 /* FIXME: Check volume handling. Would be great not to use Gconf... */
55     GConfClient *client;
56     GConfValue *value;
57     gint gconf_vol;
58
59     /*
60      * The volume is from -0dB to -6dB,
61        The full volume is marked as 2 in gconf */
62     client = gconf_client_get_default ();
63     value = gconf_client_get (client, ALARM_GCONF_PATH, NULL);
64
65     /* We want error cases to match full volume, not silence, so
66        we do not want to use gconf_client_get_int */
67     if (!value || value->type != GCONF_VALUE_INT)
68         gconf_vol = 2;
69     else
70         gconf_vol = gconf_value_get_int(value);
71
72     if (value)
73         gconf_value_free(value);
74     g_object_unref (client);
75
76     volume = ((1.0 - (float)gconf_vol / 2.0)) * (-6.0);
77 #endif
78
79     if ((ret = ca_context_create(&ca_con)) != CA_SUCCESS) {
80         g_warning("ca_context_create: %s\n", ca_strerror(ret));
81         return;
82     }
83     if ((ret = ca_context_open(ca_con)) != CA_SUCCESS) {
84         g_warning("ca_context_open: %s\n", ca_strerror(ret));
85         ca_context_destroy(ca_con);
86         return;
87     }
88     ca_proplist_create(&pl);
89     ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, sample);
90     ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", volume);
91
92     ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
93     g_debug("ca_context_play_full (vol %f): %s\n", volume, ca_strerror(ret));
94
95     ca_proplist_destroy(pl);
96     ca_context_destroy(ca_con);
97 }