string: Add a function for converting seconds to string

This is used to get a string representation of the number of seconds
passed in.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-27 08:34:32 -05:00
parent 6c6437c2bd
commit 12260a3de9
11 changed files with 44 additions and 53 deletions

View File

@ -5,9 +5,7 @@
#include <core/audio.h>
#include <core/deck.h>
#include <core/library.h>
#include <sstream>
#include <string.h>
#include <core/string.h>
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());

View File

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

View File

@ -3,6 +3,7 @@
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/filter.h>
#include <core/string.h>
#include <core/tags/track.h>
#include <sstream>
@ -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;

View File

@ -6,6 +6,7 @@
* what options are supported.
*/
#include <core/audio.h>
#include <core/string.h>
#include <gui/ocarina.h>
#include <gst/gst.h>
@ -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;

View File

@ -5,6 +5,7 @@
* https://git.gnome.org/browse/gtkmm-documentation/tree/examples/others/treemodelcustom
*/
#include <core/audio.h>
#include <core/string.h>
#include <gui/model.h>
#include <stdlib.h>
@ -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:

View File

@ -9,10 +9,6 @@
#include <string>
/** 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. */

View File

@ -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 */

View File

@ -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.

View File

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

View File

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

View File

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