From d8c3fb9ace3cf8d7766ff0524de91cc37401b321 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 18 Aug 2016 09:18:13 -0400 Subject: [PATCH] core/audio: Add support for changing audio volume Signed-off-by: Anna Schumaker --- core/audio.c | 26 ++++++++++++++++++++++++++ include/core/audio.h | 7 +++++++ tests/core/audio.c | 8 ++++++++ 3 files changed, 41 insertions(+) diff --git a/core/audio.c b/core/audio.c index 87a2b81b..0d7a08c5 100644 --- a/core/audio.c +++ b/core/audio.c @@ -7,6 +7,7 @@ #include static const char *SETTINGS_TRACK = "core.audio.cur"; +static const char *SETTINGS_VOLUME = "core.audio.volume"; static struct file audio_file = FILE_INIT("cur_track", 0, 0); static struct track *audio_track = NULL; static GstElement *audio_player = NULL; @@ -96,12 +97,14 @@ static bool __audio_init_idle(void *data) file_remove(&audio_file); __audio_load(track_get(track), GST_STATE_PAUSED); } + return true; } void audio_init(int *argc, char ***argv, struct audio_ops *ops) { + unsigned int volume = 100; GstBus *bus; gst_init(argc, argv); @@ -112,6 +115,10 @@ void audio_init(int *argc, char ***argv, struct audio_ops *ops) audio_bus = gst_bus_add_watch(bus, __audio_message, NULL); gst_object_unref(bus); + if (settings_has(SETTINGS_VOLUME)) + volume = settings_get(SETTINGS_VOLUME); + audio_set_volume(volume); + idle_schedule(IDLE_SYNC, __audio_init_idle, NULL); } @@ -155,6 +162,25 @@ GstState audio_cur_state() return cur; } +void audio_set_volume(unsigned int volume) +{ + gdouble vol; + + if (volume > 100) + volume = 100; + vol = (gdouble)volume / 100; + + settings_set(SETTINGS_VOLUME, volume); + g_object_set(G_OBJECT(audio_player), "volume", vol, NULL); +} + +unsigned int audio_get_volume() +{ + gdouble volume; + g_object_get(G_OBJECT(audio_player), "volume", &volume, NULL); + return volume * 100; +} + bool audio_play() { if (!audio_track) diff --git a/include/core/audio.h b/include/core/audio.h index 0a5b3d69..d9ddd0ce 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -43,6 +43,13 @@ struct track *audio_cur_track(); GstState audio_cur_state(); +/* Called to set the playback volume. */ +void audio_set_volume(unsigned int); + +/* Called to get the playback volume. */ +unsigned int audio_get_volume(); + + /* Called to begin playback. */ bool audio_play(); diff --git a/tests/core/audio.c b/tests/core/audio.c index 64154730..2fa526cb 100644 --- a/tests/core/audio.c +++ b/tests/core/audio.c @@ -70,6 +70,7 @@ static void test_init() g_assert_false(audio_seek(7)); g_assert_cmpuint(audio_position(), ==, 0); g_assert_cmpuint(audio_duration(), ==, 0); + g_assert_cmpuint(audio_get_volume(), ==, 100); g_assert_null(audio_cur_track()); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL); g_assert_cmpuint(load_count, ==, 0); @@ -100,6 +101,13 @@ static void test_playback() g_assert_cmpuint(audio_duration(), ==, tracks[0]->tr_length * GST_SECOND); } + audio_set_volume(0); + g_assert_cmpfloat(audio_get_volume(), ==, 0); + audio_set_volume(50); + g_assert_cmpfloat(audio_get_volume(), ==, 50); + audio_set_volume(150); + g_assert_cmpfloat(audio_get_volume(), ==, 100); + g_assert_true(audio_pause()); g_assert_false(audio_pause()); g_assert_cmpuint(state_count, ==, 2);