ocarina/libsaria/audio/progress.cpp

92 lines
1.6 KiB
C++

#include <sstream>
using namespace std;
#include <libsaria/audio.h>
#include "audio.h"
static bool get_duration(gint64 &duration)
{
GstFormat fmt = GST_FORMAT_TIME;
return gst_element_query_duration(GST_ELEMENT(get_player()),
&fmt,
&duration);
}
static bool get_position(gint64 &position)
{
GstFormat fmt = GST_FORMAT_TIME;
return gst_element_query_position(GST_ELEMENT(get_player()),
&fmt,
&position);
}
namespace libsaria
{
void audio::seek_to(double pos)
{
gst_element_seek_simple(GST_ELEMENT(get_player()),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
pos);
}
/* Use to seek forward or backward in a song */
bool audio::seek(int dt)
{
bool ret;
gint64 cur_pos;
gint64 new_pos;
ret = get_position(cur_pos);
if (ret == true) {
/* Convert seconds to nano-seconds */
new_pos = cur_pos + (dt * GST_SECOND);
if (new_pos < 0)
new_pos = 0;
gst_element_seek_simple(GST_ELEMENT(get_player()),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
new_pos);
}
return ret;
}
gint64 audio::duration()
{
gint64 duration;
if (!get_duration(duration))
return 0;
return duration;
}
gint64 audio::position()
{
gint64 position;
if (!get_position(position))
return 0;
return position;
}
string audio::posstr()
{
stringstream stream;
gint64 pos;
int minutes, seconds;
if (!get_position(pos))
return "";
pos = pos / GST_SECOND;
minutes = pos / 60;
seconds = pos - (minutes * 60);
stream << minutes << ":";
if (seconds < 10)
stream << "0";
stream << seconds;
return stream.str();
}
};