From a6b59671a49e2b0ec91c8a0fbbfe4804ca126f24 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 14 Jul 2011 00:55:12 +0200 Subject: [PATCH] Implemented glk_schannel_set_volume_ext() partly Still need to do: - Volume change notifications - What happens when channel is not playing --- configure.ac | 2 +- libchimara/schannel.c | 80 ++++++++++++++++++++++++++++++++++++++++--- tests/soundtest.c | 4 +-- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 3fb5d01..f83e755 100644 --- a/configure.ac +++ b/configure.ac @@ -118,7 +118,7 @@ AC_ARG_WITH([gstreamer], SOUND_MODULE= AS_IF([$TEST "x$with_gstreamer" != xno], [AC_DEFINE([GSTREAMER_SOUND], [1], [Define to enable sound support with GStreamer]) - SOUND_MODULE="gstreamer-0.10 >= 0.10.12"]) + SOUND_MODULE="gstreamer-0.10 >= 0.10.12 gstreamer-controller-0.10"]) ### CHECK FOR LIBRARIES ####################################################### diff --git a/libchimara/schannel.c b/libchimara/schannel.c index d8c924d..321354e 100644 --- a/libchimara/schannel.c +++ b/libchimara/schannel.c @@ -4,6 +4,7 @@ #include #ifdef GSTREAMER_SOUND #include +#include #endif #include "magic.h" #include "schannel.h" @@ -678,6 +679,22 @@ glk_schannel_unpause(schanid_t chan) } } +static double +volume_glk_to_gstreamer(glui32 volume_glk) +{ + return CLAMP(((double)volume_glk / 0x10000), 0.0, 10.0); +} + +static void +channel_set_volume_immediately(schanid_t chan, double volume, glui32 notify) +{ + g_object_set(chan->filter, "volume", volume, NULL); + + if(notify != 0) { + /* Send a notification */ + } +} + /** * glk_schannel_set_volume: * @chan: Channel to set the volume of. @@ -710,7 +727,13 @@ glk_schannel_unpause(schanid_t chan) void glk_schannel_set_volume(schanid_t chan, glui32 vol) { - glk_schannel_set_volume_ext(chan, vol, 0, 0); + VALID_SCHANNEL(chan, return); + /* Silently ignore out-of-range volume values */ + +#ifdef GSTREAMER_SOUND + double volume = volume_glk_to_gstreamer(vol); + channel_set_volume_immediately(chan, volume, 0); +#endif } /** @@ -758,11 +781,58 @@ glk_schannel_set_volume_ext(schanid_t chan, glui32 vol, glui32 duration, glui32 /* Silently ignore out-of-range volume values */ #ifdef GSTREAMER_SOUND - gdouble volume_gst = (gdouble)vol / 0x10000; - g_object_set(chan->filter, "volume", CLAMP(volume_gst, 0.0, 10.0), NULL); -#endif + double volume = volume_glk_to_gstreamer(vol); + + /* TODO: If channel is not playing, change the volume anyway */ + + if(duration == 0) { + channel_set_volume_immediately(chan, volume, notify); + return; + } - /* Not implemented */ + /* Get the volume levels as GValues */ + GValue current_volume = { 0 }; + GValue target_volume = { 0 }; + g_value_init(¤t_volume, G_TYPE_DOUBLE); + g_value_init(&target_volume, G_TYPE_DOUBLE); + g_object_get_property(G_OBJECT(chan->filter), "volume", ¤t_volume); + g_value_set_double(&target_volume, volume); + + /* Make a controller for the volume */ + GstController *controller = gst_object_control_properties(G_OBJECT(chan->filter), "volume", NULL); + if(controller == NULL) { + WARNING(_("Couldn't get controller for volume change")); + goto fail; + } + GstInterpolationControlSource *csource = gst_interpolation_control_source_new(); + gst_interpolation_control_source_set_interpolation_mode(csource, GST_INTERPOLATE_LINEAR); + if(!gst_controller_set_control_source(controller, "volume", GST_CONTROL_SOURCE(csource))) { + WARNING(_("Couldn't set control source for volume change")); + goto fail; + } + + /* Get the current time on the pipeline */ + GstClock *clock = gst_pipeline_get_clock(GST_PIPELINE(chan->pipeline)); + GstClockTime current = gst_clock_get_time(clock); + g_object_unref(clock); + + if(!gst_interpolation_control_source_set(csource, current, ¤t_volume)) { + WARNING(_("Couldn't program volume change")); + goto fail; + } + if(!gst_interpolation_control_source_set(csource, current + duration * GST_MSECOND, &target_volume)) { + WARNING(_("Couldn't program volume change")); + goto fail; + } + + /* TODO: SET UP NOTIFICATION */ + + return; + +fail: + /* Changing the volume dynamically didn't work; just do it immediately. */ + channel_set_volume_immediately(chan, volume, notify); +#endif } /** diff --git a/tests/soundtest.c b/tests/soundtest.c index 55759dc..b0a3800 100644 --- a/tests/soundtest.c +++ b/tests/soundtest.c @@ -109,11 +109,11 @@ glk_main(void) glk_put_string("Ramping volume to "); if(ramp == 0) { glk_put_string("HALF.\n"); - glk_schannel_set_volume_ext(sc[0], 0x8000, 1000, 42); + glk_schannel_set_volume_ext(sc[0], 0x8000, 3000, 42); ramp = 1; } else if(ramp == 1) { glk_put_string("FULL.\n"); - glk_schannel_set_volume_ext(sc[0], 0x10000, 1000, 69); + glk_schannel_set_volume_ext(sc[0], 0x10000, 3000, 69); ramp = 0; } } else if(strcmp(buffer, "multi") == 0) { -- 2.30.2