diff --git a/core/audio.cpp b/core/audio.cpp index c8341999..0a7aaff6 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -14,9 +14,7 @@ static struct file audio_file; static struct track *audio_track = NULL; static GstElement *audio_player = NULL; static struct audio_ops *audio_ops = NULL; - -static bool _pause_enabled = false; -static unsigned int _pause_count = 0; +static int audio_pause_count = -1; static AudioDriver *cur_driver = NULL; @@ -60,17 +58,12 @@ static bool __audio_load(struct track *track, GstState state) static GstState continue_playback() { - GstState ret = GST_STATE_PLAYING; - - if (_pause_enabled) { - if (_pause_count == 0) { - ret = GST_STATE_PAUSED; - _pause_enabled = false; - } else - _pause_count--; + if (audio_pause_count >= 0) { + audio_pause_after(audio_pause_count - 1); + if (audio_pause_count == -1) + return GST_STATE_PAUSED; } - - return ret; + return GST_STATE_PLAYING; } @@ -97,7 +90,8 @@ void AudioDriver :: eos() track = tempq_next(); if (!track) track = queue_next(collection_get_queue()); - __audio_load(track, continue_playback()); + if (__audio_load(track, continue_playback())) + history_add(track); } @@ -224,23 +218,12 @@ struct track *audio_prev() return track; } -void audio :: pause_after(bool enabled, unsigned int n) +void audio_pause_after(int n) { - if (n > _pause_count) - enabled = true; - - _pause_enabled = enabled; - _pause_count = n; -} - -bool audio :: pause_enabled() -{ - return _pause_enabled; -} - -unsigned int audio :: pause_count() -{ - return _pause_count; + if (n != audio_pause_count) { + audio_pause_count = n; + audio_ops->on_config_pause(audio_pause_count); + } } AudioDriver *audio :: get_driver() diff --git a/gui/gst.cpp b/gui/gst.cpp index 3d9fc26b..1f546d03 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -69,11 +69,22 @@ static void on_load(struct track *track, GstState state) on_pause(); } +static void on_config_pause(int n) +{ + if (n == -1) + o_enabled->set_active(false); + else { + o_count->set_value(n); + o_enabled->set_active(true); + } +} + class GSTDriver : public AudioDriver {} gst_driver; struct audio_ops audio_ops = { on_load, + on_config_pause, }; @@ -109,8 +120,6 @@ static gboolean on_gst_message(GstBus *bus, GstMessage *message, gpointer data) break; case GST_MESSAGE_EOS: gst_driver.eos(); - o_count->set_value(audio :: pause_count()); - o_enabled->set_active(audio :: pause_enabled()); break; default: break; @@ -128,12 +137,15 @@ static bool on_seek(Gtk::ScrollType type, double value) static void on_pause_count() { o_enabled->set_active(true); - audio :: pause_after(true, o_count->get_value()); + audio_pause_after(o_count->get_value()); } static void on_pause_enabled() { - audio :: pause_after(o_enabled->get_active(), o_count->get_value()); + if (!o_enabled->get_active()) + audio_pause_after(-1); + else + audio_pause_after(o_count->get_value()); } static bool on_timeout() diff --git a/include/core/audio.h b/include/core/audio.h index 13c36789..28b9cf29 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -15,6 +15,9 @@ extern "C" { struct audio_ops { /* Called when a track is loaded. */ void (*on_load)(struct track *, GstState); + + /* Called when the automatic pause state changes. */ + void (*on_config_pause)(int); }; @@ -40,28 +43,6 @@ public: namespace audio { - /** - * Configure the automatic pausing feature. - * - * @param enabled Set to true to enable pausing, false to disable. - * @param n Number of tracks to play before pausing. - */ - void pause_after(bool, unsigned int); - - /** - * Call to find the current automatic pausing state. - * - * @return True if automatic pausing is enabled, false otherwise. - */ - bool pause_enabled(); - - /** - * Call to find the number of tracks remaining before pausing. - * - * @return The number of tracks before pausing. - */ - unsigned int pause_count(); - /** * Called to access an audio driver. * @@ -113,5 +94,8 @@ struct track *audio_next(); /* Called to load the previous track. */ struct track *audio_prev(); +/* Called to configure automatic pausing. */ +void audio_pause_after(int); + GstElement *audio_get_player(); #endif /* OCARINA_CORE_AUDIO_H */ diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 706b1d11..196b2511 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -12,7 +12,8 @@ extern "C" { #include "test.h" struct track *TRACK_NULL = NULL; -static unsigned int load_count = 0; +static unsigned int load_count = 0; +static int pause_count = 0; static bool test_audio_seek(gint64 pos) @@ -41,8 +42,11 @@ static void test_audio_load(struct track *track, GstState state) load_count++; } +static void test_config_pause(int n) { pause_count = n; } + static struct audio_ops test_audio_ops = { test_audio_load, + test_config_pause, }; static struct core_init_data test_init_data = { @@ -190,6 +194,37 @@ static void test_prev() test_equal(audio_cur_track()->tr_dbe.dbe_index, 2); } +void test_autopause() +{ + TestDriver *driver = (TestDriver *)audio :: get_driver(); + struct queue *history_q = history_get_queue(); + int i; + + test_equal(audio_play(), true); + test_equal(pause_count, 0); + + audio_pause_after(3); + test_equal(pause_count, 3); + + pause_count = 0; + audio_pause_after(3); + test_equal(pause_count, 0); + + audio_pause_after(5); + test_equal(pause_count, 5); + + for (i = 4; i > -1; i--) { + driver->eos(); + test_loop_equal(pause_count, i, i); + test_loop_equal(audio_cur_state(), GST_STATE_PLAYING, i); + test_loop_equal(queue_at(history_q, 0), audio_cur_track(), i); + } test_loop_passed(); + + driver->eos(); + test_equal(pause_count, -1); + test_not_equal(audio_cur_state(), GST_STATE_PLAYING); +} + static void test_deinit() { core :: deinit(); @@ -199,45 +234,11 @@ static void test_deinit() } -void test_autopause() -{ - TestDriver *driver = (TestDriver *)audio :: get_driver(); - - core :: init(NULL, NULL, &test_init_data); - audio_play(); - test_equal(audio :: pause_enabled(), false); - test_equal(audio :: pause_count(), (unsigned)0); - - audio :: pause_after(true, 3); - test_equal(audio :: pause_enabled(), true); - test_equal(audio :: pause_count(), (unsigned)3); - - audio :: pause_after(false, 3); - test_equal(audio :: pause_enabled(), false); - test_equal(audio :: pause_count(), (unsigned)3); - - audio :: pause_after(false, 5); - test_equal(audio :: pause_enabled(), true); - test_equal(audio :: pause_count(), (unsigned)5); - - for (int i = 4; i >= 0; i--) { - driver->eos(); - test_equal(audio :: pause_enabled(), true); - test_equal(audio :: pause_count(), (unsigned)i); - test_equal(driver->is_playing(), true); - } - - driver->eos(); - test_equal(audio :: pause_enabled(), false); - test_equal(audio :: pause_count(), (unsigned)0); - test_equal(driver->is_playing(), false); -} - DECLARE_UNIT_TESTS( UNIT_TEST("Audio Initialization", test_init), UNIT_TEST("Audio Playback", test_playback), UNIT_TEST("Audio Next", test_next), UNIT_TEST("Audio Previous", test_prev), + UNIT_TEST("Audio Automatic Pausing", test_autopause), UNIT_TEST("Audio Deinitialization", test_deinit), - UNIT_TEST("Test Audio Automatic Pausing", test_autopause), );