core/audio: Add support for changing audio volume

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-18 09:18:13 -04:00
parent f02dff4177
commit d8c3fb9ace
3 changed files with 41 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <core/settings.h>
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)

View File

@ -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();

View File

@ -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);