diff --git a/core/audio.cpp b/core/audio.cpp index e5b4ceb8..372871f5 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -5,9 +5,7 @@ #include #include #include - -#include -#include +#include static bool _pause_enabled = false; @@ -18,7 +16,6 @@ static File f_cur_track("cur_track", 0); static AudioDriver *cur_driver = NULL; - static void save_state() { f_cur_track.open(OPEN_WRITE); @@ -129,20 +126,6 @@ long audio :: duration() return 0; } -std::string audio :: position_str() -{ - std::stringstream ss; - long cur = position() / O_SECOND; - unsigned int minutes = cur / 60; - unsigned int seconds = cur % 60; - - ss << minutes << ":"; - if (seconds < 10) - ss << "0"; - ss << seconds; - return ss.str(); -} - void audio :: next() { _load_track(deck :: next(), cur_driver->is_playing()); diff --git a/core/string.cpp b/core/string.cpp index 9951073e..5e056c61 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -11,3 +11,16 @@ const std::string string :: utos(unsigned int u) ss << u; return ss.str(); } + +const std::string string :: sec2str(unsigned int sec) +{ + std::stringstream ss; + unsigned int minutes = sec / 60; + unsigned int seconds = sec % 60; + + ss << minutes << ":"; + if (seconds < 10) + ss << "0"; + ss << seconds; + return ss.str(); +} diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 826f416a..09384381 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -3,6 +3,7 @@ * Copyright 2014 (c) Anna Schumaker. */ #include +#include #include #include @@ -78,19 +79,6 @@ const std::string Track :: path() const return ""; } -const std::string Track :: length_str() const -{ - std::stringstream ss; - unsigned int minutes = _length / 60; - unsigned int seconds = _length % 60; - - ss << minutes << ":"; - if (seconds < 10) - ss << 0; - ss << seconds; - return ss.str(); -} - const std::string Track :: primary_key() const { std::stringstream ss; diff --git a/gui/gst.cpp b/gui/gst.cpp index eff6ca32..5fc91abb 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -6,6 +6,7 @@ * what options are supported. */ #include +#include #include #include @@ -65,7 +66,7 @@ public: set_markup(o_album, "x-large", "From: " + track->album()->name()); set_markup(o_artist, "x-large", "By: " + track->artist()->name()); set_markup(o_title, "xx-large", track->name()); - o_duration->set_text(track->length_str()); + o_duration->set_text(string :: sec2str(track->length())); plist :: track_loaded(track); } @@ -178,7 +179,7 @@ static void on_pause_enabled() static bool on_timeout() { - o_position->set_text(audio :: position_str()); + o_position->set_text(string :: sec2str(audio :: position() / GST_SECOND)); o_progress->set_upper(audio :: duration()); o_progress->set_value(audio :: position()); return true; diff --git a/gui/model.cpp b/gui/model.cpp index 09194c6b..f637982d 100644 --- a/gui/model.cpp +++ b/gui/model.cpp @@ -5,6 +5,7 @@ * https://git.gnome.org/browse/gtkmm-documentation/tree/examples/others/treemodelcustom */ #include +#include #include #include @@ -108,7 +109,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column, case 1: return set_val(track->name(), value); case 2: - return set_val(track->length_str(), value); + return set_val(string :: sec2str(track->length()), value); case 3: return set_val(track->artist()->name(), value); case 4: diff --git a/include/core/audio.h b/include/core/audio.h index 4c0818d2..fb8ac410 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -9,10 +9,6 @@ #include -/** Use to convert nanoseconds to seconds */ -static const unsigned long O_SECOND = 1000000000; - - /** * The audio driver class gives us an interface for using multiple * audio frameworks for audio playback. @@ -111,11 +107,6 @@ namespace audio */ long duration(); - /** - * @return The current audio position, in a human readable format. - */ - std::string position_str(); - void next(); /**< Find and load the next track that should be played. */ void prev(); /**< Call the deck :: previous() function and load the result. */ diff --git a/include/core/string.h b/include/core/string.h index 55fefe5f..10d44b3f 100644 --- a/include/core/string.h +++ b/include/core/string.h @@ -20,6 +20,12 @@ namespace string */ const std::string utos(unsigned int); + /** + * Convert from seconds to a time string. + * @param sec Number of seconds. + * @return A string in mm:ss format. + */ + const std::string sec2str(unsigned int); } #endif /* OCARINA_CORE_STRING_H */ diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 1939826e..c76a1f2c 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -86,9 +86,6 @@ public: /** @return The full path of this track. */ const std::string path() const; - /** @return Track::_length in mm:ss format. */ - const std::string length_str() const; - /** * A track's primary key is the concatenation of the library index * and the relative path. diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 3a0385da..2a5bef99 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -113,9 +113,6 @@ void test_playback_controls() driver->cur_duration = 424242; test_equal(audio :: position(), (long)4242); test_equal(audio :: duration(), (long)424242); - - audio :: seek_to(83 * O_SECOND); - test_equal(audio :: position_str(), (std::string)"1:23"); } void test_track_controls() diff --git a/tests/core/string.cpp b/tests/core/string.cpp index 64586bb3..fe979255 100644 --- a/tests/core/string.cpp +++ b/tests/core/string.cpp @@ -13,7 +13,24 @@ void test_utos() test_equal(string :: utos(999), "999"); } +void test_sec2str() +{ + test_equal(string :: sec2str(0), "0:00"); + test_equal(string :: sec2str(5), "0:05"); + test_equal(string :: sec2str(10), "0:10"); + test_equal(string :: sec2str(60), "1:00"); + test_equal(string :: sec2str(65), "1:05"); + test_equal(string :: sec2str(75), "1:15"); + test_equal(string :: sec2str(600), "10:00"); + test_equal(string :: sec2str(605), "10:05"); + test_equal(string :: sec2str(615), "10:15"); + test_equal(string :: sec2str(660), "11:00"); + test_equal(string :: sec2str(665), "11:05"); + test_equal(string :: sec2str(675), "11:15"); +} + int main(int argc, char **argv) { - test :: run("utos Test", test_utos); + test :: run("Unsigned to String Test", test_utos); + test :: run("Seconds to String Test", test_sec2str); } diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 4941a232..05b960b7 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -30,7 +30,6 @@ static void test_track_tag_default() test_equal(track.primary_key(), (std::string)""); test_equal(track.date(), (std::string)"Never"); test_equal(track.path(), (std::string)""); - test_equal(track.length_str(), (std::string)"0:00"); test_equal(track.track(), (unsigned)0); test_equal(track.length(), (unsigned)0); @@ -48,7 +47,6 @@ static void verify_track_tag(Track *track, unsigned int size) test_equal(track->lowercase(), (std::string)"legend of zelda medley"); test_equal(track->date(), (std::string)"Never"); test_equal(track->path(), (std::string)MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3"); - test_equal(track->length_str(), (std::string)"4:48"); test_equal(track->primary_key(), (std::string)"0/Hyrule Symphony/13 - Legend of Zelda Medley.mp3"); test_equal(track->track(), (unsigned)13); @@ -203,7 +201,6 @@ static void test_track_tag_locale() b = tags :: add_track(album, artist, genre, library, MUSIC_DIR + "/Hyrule Symphony/0 - intro.mp3", "Intro", 56, 0); - test_equal(b->length_str(), (std::string)"0:56"); test_equal(a->compare_date(a), 0); test_equal(a->compare_date(b), 2015);