libsaria: Fold most audio code into a single file
This puts everything together and should help me remove the subdirectory. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
7557acc239
commit
56f15b4c44
|
@ -1,10 +1,84 @@
|
|||
// Copyright (c) 2011 Bryan Schumaker.
|
||||
#include <deck.h>
|
||||
#include <audio.h>
|
||||
#include "audio.h"
|
||||
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
GstElement *player = NULL;
|
||||
GstBus *bus = NULL;
|
||||
static GstState cur_state = GST_STATE_READY;
|
||||
|
||||
static void notify_state_change(GstState state)
|
||||
{
|
||||
if (state == GST_STATE_PLAYING)
|
||||
libsaria::notify(PLAYING, NULL);
|
||||
else if (state == GST_STATE_PAUSED)
|
||||
libsaria::notify(PAUSED, NULL);
|
||||
}
|
||||
|
||||
static bool change_state(GstState new_state)
|
||||
{
|
||||
GstStateChangeReturn ret;
|
||||
ret = gst_element_set_state(GST_ELEMENT(player), new_state);
|
||||
|
||||
switch(ret) {
|
||||
case GST_STATE_CHANGE_SUCCESS:
|
||||
case GST_STATE_CHANGE_ASYNC:
|
||||
cur_state = new_state;
|
||||
notify_state_change(cur_state);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void load_file(GstElement *playbin, string file, GstState state)
|
||||
{
|
||||
if (file == "" || !libsaria::exists(file))
|
||||
return;
|
||||
|
||||
string uri = "file://" + file;
|
||||
println("Loading uri: " + uri);
|
||||
/* Set pipeline to the requested state */
|
||||
change_state(GST_STATE_READY);
|
||||
g_object_set(G_OBJECT(playbin), "uri", uri.c_str(), NULL);
|
||||
change_state(state);
|
||||
}
|
||||
|
||||
static bool get_duration(gint64 &duration)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_duration(GST_ELEMENT(player),
|
||||
&fmt,
|
||||
&duration);
|
||||
}
|
||||
|
||||
static bool get_position(gint64 &position)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_position(GST_ELEMENT(player),
|
||||
&fmt,
|
||||
&position);
|
||||
}
|
||||
|
||||
static string to_string(gint64 time)
|
||||
{
|
||||
stringstream stream;
|
||||
int minutes, seconds;
|
||||
|
||||
if (time == 0)
|
||||
return "";
|
||||
|
||||
time = time / GST_SECOND;
|
||||
minutes = time / 60;
|
||||
seconds = time - (minutes * 60);
|
||||
stream << minutes << ":";
|
||||
if (seconds < 10)
|
||||
stream << "0";
|
||||
stream << seconds;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
static void parse_error(GstMessage *error)
|
||||
{
|
||||
|
@ -33,6 +107,85 @@ static gboolean on_message(GstBus *bus, GstMessage *message, gpointer data)
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
void audio::play()
|
||||
{
|
||||
change_state(GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void audio::pause()
|
||||
{
|
||||
change_state(GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void audio::toggle_play()
|
||||
{
|
||||
if (cur_state == GST_STATE_PLAYING)
|
||||
pause();
|
||||
else
|
||||
play();
|
||||
}
|
||||
|
||||
GstState audio::get_state()
|
||||
{
|
||||
return cur_state;
|
||||
}
|
||||
|
||||
void audio::stop()
|
||||
{
|
||||
pause();
|
||||
seek_to(0);
|
||||
}
|
||||
|
||||
void audio::load(string file, bool play)
|
||||
{
|
||||
GstState state = GST_STATE_PLAYING;
|
||||
if (!play)
|
||||
state = GST_STATE_PAUSED;
|
||||
load_file(player, file, state);
|
||||
}
|
||||
|
||||
/* Seek to this many seconds into the song */
|
||||
void audio::seek_to(double pos)
|
||||
{
|
||||
gst_element_seek_simple(GST_ELEMENT(player),
|
||||
GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH,
|
||||
pos);
|
||||
}
|
||||
|
||||
gint64 audio::duration()
|
||||
{
|
||||
gint64 duration;
|
||||
if (!get_duration(duration))
|
||||
return 0;
|
||||
if (duration < 0)
|
||||
duration = 0;
|
||||
return duration;
|
||||
}
|
||||
|
||||
string audio::durstr()
|
||||
{
|
||||
return to_string(duration());
|
||||
}
|
||||
|
||||
gint64 audio::position()
|
||||
{
|
||||
gint64 position;
|
||||
if (!get_position(position))
|
||||
return 0;
|
||||
return position;
|
||||
}
|
||||
|
||||
string audio::posstr()
|
||||
{
|
||||
return to_string(position());
|
||||
}
|
||||
|
||||
bool audio::played()
|
||||
{
|
||||
return position() > ((duration() * 3) / 4);
|
||||
}
|
||||
|
||||
void audio::init(int argc, char **argv)
|
||||
{
|
||||
println("Initializing libsaria audio");
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
#ifndef LIBSARIA_AUDIO_COMPONENT_H
|
||||
#define LIBSARIA_AUDIO_COMPONENT_H
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
extern "C" {
|
||||
#include <gst/gst.h>
|
||||
}
|
||||
|
||||
extern GstElement *player;
|
||||
extern GstBus *bus;
|
||||
|
||||
struct StoredAudioState
|
||||
{
|
||||
string cur_file;
|
||||
double position;
|
||||
bool was_playing;
|
||||
};
|
||||
|
||||
void load_file(GstElement *, string);
|
||||
|
||||
void set_audio_sink(GstElement *);
|
||||
void save_audio_state(struct StoredAudioState *);
|
||||
void load_audio_state(struct StoredAudioState *);
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
namespace audio
|
||||
{
|
||||
void init_volume();
|
||||
void init_alsa();
|
||||
void quit_alsa();
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_AUDIO_PRIVATE_H */
|
|
@ -1,87 +0,0 @@
|
|||
// Copyright (c) 2011 Bryan Schumaker.
|
||||
#include <notify.h>
|
||||
#include <audio.h>
|
||||
#include <print.h>
|
||||
#include <fs.h>
|
||||
#include "audio.h"
|
||||
|
||||
static GstState cur_state = GST_STATE_READY;
|
||||
|
||||
static void notify_state_change(GstState state)
|
||||
{
|
||||
if (state == GST_STATE_PLAYING)
|
||||
libsaria::notify(PLAYING, NULL);
|
||||
else if (state == GST_STATE_PAUSED)
|
||||
libsaria::notify(PAUSED, NULL);
|
||||
}
|
||||
|
||||
static bool change_state(GstState new_state)
|
||||
{
|
||||
GstStateChangeReturn ret;
|
||||
ret = gst_element_set_state(GST_ELEMENT(player), new_state);
|
||||
|
||||
switch(ret) {
|
||||
case GST_STATE_CHANGE_SUCCESS:
|
||||
case GST_STATE_CHANGE_ASYNC:
|
||||
cur_state = new_state;
|
||||
notify_state_change(cur_state);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void load_file(GstElement *playbin, string file, GstState state)
|
||||
{
|
||||
if (file == "" || !libsaria::exists(file))
|
||||
return;
|
||||
|
||||
string uri = "file://" + file;
|
||||
println("Loading uri: " + uri);
|
||||
/* Set pipeline to the requested state */
|
||||
change_state(GST_STATE_READY);
|
||||
g_object_set(G_OBJECT(playbin), "uri", uri.c_str(), NULL);
|
||||
change_state(state);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void audio::play()
|
||||
{
|
||||
change_state(GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void audio::pause()
|
||||
{
|
||||
change_state(GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void audio::toggle_play()
|
||||
{
|
||||
if (cur_state == GST_STATE_PLAYING)
|
||||
pause();
|
||||
else
|
||||
play();
|
||||
}
|
||||
|
||||
GstState audio::get_state()
|
||||
{
|
||||
return cur_state;
|
||||
}
|
||||
|
||||
void audio::stop()
|
||||
{
|
||||
pause();
|
||||
seek_to(0);
|
||||
}
|
||||
|
||||
void audio::load(string file, bool play)
|
||||
{
|
||||
GstState state = GST_STATE_PLAYING;
|
||||
if (!play)
|
||||
state = GST_STATE_PAUSED;
|
||||
load_file(player, file, state);
|
||||
}
|
||||
|
||||
};
|
|
@ -1,87 +0,0 @@
|
|||
// Copyright (c) 2011 Bryan Schumaker.
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
#include <audio.h>
|
||||
#include "audio.h"
|
||||
|
||||
static bool get_duration(gint64 &duration)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_duration(GST_ELEMENT(player),
|
||||
&fmt,
|
||||
&duration);
|
||||
}
|
||||
|
||||
static bool get_position(gint64 &position)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_position(GST_ELEMENT(player),
|
||||
&fmt,
|
||||
&position);
|
||||
}
|
||||
|
||||
static string to_string(gint64 time)
|
||||
{
|
||||
stringstream stream;
|
||||
int minutes, seconds;
|
||||
|
||||
if (time == 0)
|
||||
return "";
|
||||
|
||||
time = time / GST_SECOND;
|
||||
minutes = time / 60;
|
||||
seconds = time - (minutes * 60);
|
||||
stream << minutes << ":";
|
||||
if (seconds < 10)
|
||||
stream << "0";
|
||||
stream << seconds;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
/* Seek to this many seconds into the song */
|
||||
void audio::seek_to(double pos)
|
||||
{
|
||||
gst_element_seek_simple(GST_ELEMENT(player),
|
||||
GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH,
|
||||
pos);
|
||||
}
|
||||
|
||||
gint64 audio::duration()
|
||||
{
|
||||
gint64 duration;
|
||||
if (!get_duration(duration))
|
||||
return 0;
|
||||
if (duration < 0)
|
||||
duration = 0;
|
||||
return duration;
|
||||
}
|
||||
|
||||
string audio::durstr()
|
||||
{
|
||||
return to_string(duration());
|
||||
}
|
||||
|
||||
gint64 audio::position()
|
||||
{
|
||||
gint64 position;
|
||||
if (!get_position(position))
|
||||
return 0;
|
||||
return position;
|
||||
}
|
||||
|
||||
string audio::posstr()
|
||||
{
|
||||
return to_string(position());
|
||||
}
|
||||
|
||||
bool audio::played()
|
||||
{
|
||||
return position() > ((duration() * 3) / 4);
|
||||
}
|
||||
|
||||
};
|
Loading…
Reference in New Issue