int canberra_play_sound_file(char *file, char *device, char* driver) { int curid = ++id; setlocale(LC_ALL, ""); ca_context *ca = connect_canberra_context(device, driver); if (ca == NULL) { return -1; } int playret = ca_context_play(ca, curid, CA_PROP_MEDIA_FILENAME, file, NULL); if (playret != CA_SUCCESS) { g_warning("play_sound_file filename:%s id=%d %s\n", file, curid, ca_strerror(playret)); ca_context_destroy(ca); return playret; } // wait for end int playing; do { g_usleep(500 * 1000); // sleep 0.5s int ret = ca_context_playing(ca, curid, &playing); if (ret != CA_SUCCESS) { g_warning("ca_context_playing id=%d %s\n", curid, ca_strerror(ret)); } } while (playing > 0); ca_context_destroy(ca); return playret; }
static void _eventd_libcanberra_event_action(EventdPluginContext *context, const gchar *config_id, EventdEvent *event) { EventdCanberraEvent *canberra_event; canberra_event = g_hash_table_lookup(context->events, config_id); if ( canberra_event == NULL ) return; int error; error = ca_context_play(context->context, 1, CA_PROP_EVENT_ID, canberra_event->sound_name, CA_PROP_MEDIA_ROLE, "event", NULL); if ( error < 0 ) g_warning("Couldn't play named sound '%s': %s", canberra_event->sound_name, ca_strerror(error)); #ifndef ENABLE_SOUND error = ca_context_play(context->context, 1, CA_PROP_MEDIA_FILENAME, canberra_event->sound_file, CA_PROP_MEDIA_ROLE, "event", NULL); if ( error < 0 ) g_warning("Couldn't play sound file '%s': %s", canberra_event->sound_file, ca_strerror(error)); #endif /* ! ENABLE_SOUND */ }
int canberra_play_system_sound(char *theme, char *event_id, char *device, char* driver) { int curid = ++id; setlocale(LC_ALL, ""); ca_context *ca = connect_canberra_context(device, driver); if (ca == NULL) { return -1; } int playret = ca_context_play(ca, curid, CA_PROP_CANBERRA_XDG_THEME_NAME, theme, CA_PROP_EVENT_ID, event_id, NULL); if (playret != CA_SUCCESS) { g_warning("play: id=%d %s\n", curid, ca_strerror(playret)); ca_context_destroy(ca); return playret; } // wait for end int playing; do { g_usleep(500 * 1000); // sleep 0.5s int ret = ca_context_playing(ca, curid, &playing); if (ret != CA_SUCCESS) { g_warning("ca_context_playing id=%d %s\n", curid, ca_strerror(ret)); } } while (playing > 0); ca_context_destroy(ca); return playret; }
static gboolean test_return(int code, GError **error) { if (code == CA_SUCCESS) return TRUE; g_set_error_literal (error, GSOUND_ERROR, code, ca_strerror(code)); return FALSE; }
static void _eventd_libcanberra_stop(EventdPluginContext *context) { int error; error = ca_context_cancel(context->context, 1); if ( error < 0 ) g_warning("Couldn't cancel sounds: %s", ca_strerror(error)); context->started = FALSE; }
static void _eventd_libcanberra_start(EventdPluginContext *context) { int error; error = ca_context_open(context->context); if ( error < 0 ) g_warning("Couldn't open libcanberra context: %s", ca_strerror(error)); else context->started = TRUE; }
static PyObject* osk_audio_play(PyObject* self, PyObject* args) { OskAudio* audio = (OskAudio*) self; GdkScreen* screen; ca_proplist* props; const char* event_id; float x, y; float xs, ys; int sw, sh, ret; if (!PyArg_ParseTuple(args, "sffff", &event_id, &x, &y, &xs, &ys)) return NULL; screen = gdk_screen_get_default(); sw = gdk_screen_get_width(screen); sh = gdk_screen_get_height(screen); ca_proplist_create(&props); ca_proplist_sets(props, CA_PROP_EVENT_ID, event_id); /* report mouse position for accessibility */ if (x != -1 && y != -1) { ca_proplist_setf(props, CA_PROP_EVENT_MOUSE_X, "%0.0f", x); ca_proplist_setf(props, CA_PROP_EVENT_MOUSE_Y, "%0.0f", y); } /* place in space between speakers */ if (xs != -1 && ys != -1) { /* comment from canberra-gtk.c: * We use these strange format strings here to avoid that libc * applies locale information on the formatting of floating numbers. */ ca_proplist_setf(props, CA_PROP_EVENT_MOUSE_HPOS, "%i.%03i", (int) x / (sw - 1), (int) (1000.0 * x / (sw - 1)) % 1000); ca_proplist_setf(props, CA_PROP_EVENT_MOUSE_VPOS, "%i.%03i", (int) y / (sh - 1), (int) (1000.0 * y / (sh - 1)) % 1000); } ret = ca_context_play_full(audio->ca, DEFAULT_SOUND_ID, props, NULL, NULL); ca_proplist_destroy(props); if (ret < 0) { PyErr_SetString(OSK_EXCEPTION, ca_strerror(ret)); return NULL; } Py_RETURN_NONE; }
static PyObject* osk_audio_cancel(PyObject* self, PyObject* args) { OskAudio* audio = (OskAudio*) self; int ret; ret = ca_context_cancel(audio->ca, DEFAULT_SOUND_ID); if (ret < 0) { PyErr_SetString(OSK_EXCEPTION, ca_strerror(ret)); return NULL; } Py_RETURN_NONE; }
static void playing_finished_cb (ca_context *c, guint id, int error_code, gpointer user_data) { EmpathyRepeatableSound *repeatable_sound = user_data; if (error_code != CA_SUCCESS) { DEBUG ("Error: %s", ca_strerror (error_code)); g_hash_table_remove (repeatable_sound->self->priv->repeating_sounds, GINT_TO_POINTER (repeatable_sound->sound_id)); return; } repeatable_sound->replay_timeout_id = g_timeout_add ( repeatable_sound->play_interval, playing_timeout_cb, user_data); }
static int canberra_connect () { int error; if (c_context) return TRUE; ca_context_create (&c_context); error = ca_context_open (c_context); if (error) { N_WARNING (LOG_CAT "can't connect to canberra! %s", ca_strerror (error)); ca_context_destroy (c_context); c_context = 0; return FALSE; } return TRUE; }
static void on_ca_play_full_finished(ca_context *ca, guint32 id, int error_code, gpointer user_data) { GSimpleAsyncResult *result = user_data; if (error_code != CA_SUCCESS) { g_simple_async_result_set_error(result, GSOUND_ERROR, error_code, ca_strerror(error_code)); } g_simple_async_result_complete_in_idle (result); g_object_unref(result); }
static PyObject* osk_audio_set_theme(PyObject* self, PyObject* args) { OskAudio* audio = (OskAudio*) self; const char* theme; int ret; if (!PyArg_ParseTuple(args, "s", &theme)) return NULL; ret = ca_context_change_props(audio->ca, CA_PROP_CANBERRA_XDG_THEME_NAME, theme, NULL); if (ret < 0) { PyErr_SetString(OSK_EXCEPTION, ca_strerror(ret)); return NULL; } Py_RETURN_NONE; }
static EventdPluginContext * _eventd_libcanberra_init(EventdCoreContext *core, EventdCoreInterface *interface) { EventdPluginContext *context; libeventd_regex_init(); context = g_new0(EventdPluginContext, 1); int error; error = ca_context_create(&context->context); if ( error < 0 ) { g_warning("Couldn't create libcanberra context: %s", ca_strerror(error)); g_free(context); return NULL; } context->events = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, _eventd_canberra_event_free); return context; }
static PyObject* osk_audio_cache_sample(PyObject* self, PyObject* args) { OskAudio* audio = (OskAudio*) self; ca_proplist* props; const char* event_id; int ret; if (!PyArg_ParseTuple(args, "s", &event_id)) return NULL; ca_proplist_create(&props); ca_proplist_sets(props, CA_PROP_EVENT_ID, event_id); ret = ca_context_cache_full(audio->ca, props); ca_proplist_destroy(props); if (ret < 0) { PyErr_SetString(OSK_EXCEPTION, ca_strerror(ret)); return NULL; } Py_RETURN_NONE; }
static int canberra_sink_play (NSinkInterface *iface, NRequest *request) { CanberraData *data = (CanberraData*) n_request_get_data (request, CANBERRA_KEY); (void) iface; N_DEBUG (LOG_CAT "sink play"); g_assert (data != NULL); if (!data->sound_enabled) goto complete; if (canberra_connect () == FALSE) return FALSE; static GHashTable *cached_samples = NULL; if (!cached_samples) { cached_samples = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); } NProplist *props = props = (NProplist*) n_request_get_properties (request); ca_proplist *ca_props = 0; int error; ca_proplist_create (&ca_props); /* TODO: don't hardcode */ ca_proplist_sets (ca_props, CA_PROP_CANBERRA_XDG_THEME_NAME, "jolla-ambient"); ca_proplist_sets (ca_props, CA_PROP_EVENT_ID, data->filename); /* convert all properties within the request that begin with "sound.stream." prefix. */ n_proplist_foreach (props, proplist_to_structure_cb, ca_props); if (g_hash_table_lookup_extended (cached_samples, data->filename, NULL, NULL) == FALSE) { N_DEBUG (LOG_CAT "caching sample %s", data->filename); error = ca_context_cache_full (c_context, ca_props); if (error) { N_WARNING (LOG_CAT "canberra couldn't cache sample %s", data->filename); return FALSE; } g_hash_table_add (cached_samples, strdup(data->filename)); } else { N_DEBUG (LOG_CAT "sample %s is cached", data->filename); } error = ca_context_play_full (c_context, 0, ca_props, NULL, NULL); ca_proplist_destroy (ca_props); if (error) { N_WARNING (LOG_CAT "sink play had a warning: %s", ca_strerror (error)); if (error == CA_ERROR_NOTAVAILABLE || error == CA_ERROR_DISCONNECTED || error == CA_ERROR_STATE || error == CA_ERROR_DESTROYED) { ca_context_destroy (c_context); c_context = 0; } return FALSE; } complete: /* We do not know how long our samples play, but let's guess we * are done in 200ms. */ data->complete_cb_id = g_timeout_add (200, canberra_complete_cb, data); return TRUE; }
/* * Volume is a value from 0.0 to 1.0 */ static void update_volume_label (MpdVolumeTile *self, double volume) { MpdVolumeTilePrivate *priv = GET_PRIVATE (self); char *old_level; float label_width; float slider_width; float x; g_return_if_fail (0.0 <= volume && volume <= 1.0); old_level = g_strdup (mx_label_get_text (MX_LABEL (priv->volume_label))); /* Label text */ if (volume == 1.0) mx_label_set_text (MX_LABEL (priv->volume_label), _("Turned up to 11")); else if (volume >= 0.90) mx_label_set_text (MX_LABEL (priv->volume_label), _("Very loud")); else if (volume >= 0.75) mx_label_set_text (MX_LABEL (priv->volume_label), _("Loud")); else if (volume > 0.50) mx_label_set_text (MX_LABEL (priv->volume_label), _("Fairly loud")); else if (volume == 0.50) mx_label_set_text (MX_LABEL (priv->volume_label), _("Middle of the road")); else if (volume >= 0.25) mx_label_set_text (MX_LABEL (priv->volume_label), _("Fairly quiet")); else if (volume >= 0.10) mx_label_set_text (MX_LABEL (priv->volume_label), _("Quiet")); else if (volume > 0.0) mx_label_set_text (MX_LABEL (priv->volume_label), _("Very quiet")); else mx_label_set_text (MX_LABEL (priv->volume_label), _("Silent")); /* Label position */ label_width = clutter_actor_get_width (priv->volume_label); slider_width = clutter_actor_get_width (priv->volume_slider); x = slider_width * volume - label_width / 2; x = CLAMP (x, 0.0, slider_width - label_width); clutter_actor_set_x (priv->volume_label, x); /* Notification */ if (0 != g_strcmp0 (old_level, mx_label_get_text (MX_LABEL (priv->volume_label)))) { gint res; ca_proplist *proplist; ca_context *context; if (g_atomic_int_get (&priv->playing_event_sound) > 0) return; context = ca_gtk_context_get (); ca_proplist_create (&proplist); ca_proplist_sets (proplist, CA_PROP_EVENT_ID, VOLUME_CHANGED_EVENT); res = ca_context_play_full (context, 1, proplist, (ca_finish_callback_t )_play_sound_completed_cb, self); ca_proplist_destroy (proplist); if (res != CA_SUCCESS) { g_warning ("%s: Error playing test sound: %s", G_STRLOC, ca_strerror (res)); } else { g_atomic_int_inc (&priv->playing_event_sound); } } g_free (old_level); }