libsaria: Massive audio cleanup
I removed libsaria/audio.cpp and did some namespace shuffling. The new code should be easier to follow and work with.
This commit is contained in:
parent
0425df6d63
commit
90ac1ecfed
|
@ -1,6 +1,29 @@
|
|||
#ifndef LIBSARIA_AUDIO_H
|
||||
#define LIBSARIA_AUDIO_H
|
||||
|
||||
void audio_init(int, char **);
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
namespace audio
|
||||
{
|
||||
void init(int, char**);
|
||||
|
||||
/* Playback control functions */
|
||||
void load(string);
|
||||
void play();
|
||||
void pause();
|
||||
void stop();
|
||||
|
||||
/* Position related functions */
|
||||
bool seek(int);
|
||||
bool seek_to(double);
|
||||
|
||||
/* Volume functions */
|
||||
double get_volume();
|
||||
void set_volume(double);
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* LIBSARIA_AUDIO_H */
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
#ifndef LIBSARIA_CONTROLS_H
|
||||
#define LIBSARIA_CONTROLS_H
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
void play();
|
||||
void pause();
|
||||
void stop();
|
||||
void load(string);
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_CONTROLS_H */
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef LIBSARIA_PROGRESS_H
|
||||
#define LIBSARIA_PROGRESS_H
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
void seek(int);
|
||||
void seek_to(double);
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_PROGRESS_H */
|
|
@ -1,60 +0,0 @@
|
|||
|
||||
#include <libsaria/libsaria.h>
|
||||
#include <libsaria/controls.h>
|
||||
#include <libsaria/progress.h>
|
||||
#include <libsaria/volume.h>
|
||||
#include <libsaria/callback.h>
|
||||
|
||||
#include "audio/audio.h"
|
||||
|
||||
static Audio audio;
|
||||
|
||||
void audio_init(int argc, char **argv)
|
||||
{
|
||||
audio.init(argc, argv);
|
||||
}
|
||||
|
||||
void libsaria::seek_to(double prcnt)
|
||||
{
|
||||
audio.seek_to(prcnt);
|
||||
}
|
||||
|
||||
void libsaria::seek(int dt)
|
||||
{
|
||||
audio.seek(dt);
|
||||
}
|
||||
|
||||
void libsaria::play()
|
||||
{
|
||||
if(audio.play())
|
||||
trigger_callback(PLAY);
|
||||
}
|
||||
|
||||
void libsaria::pause()
|
||||
{
|
||||
if(audio.pause())
|
||||
trigger_callback(PAUSE);
|
||||
}
|
||||
|
||||
void libsaria::stop()
|
||||
{
|
||||
libsaria::pause();
|
||||
libsaria::seek_to(0);
|
||||
}
|
||||
|
||||
void libsaria::load(string filepath)
|
||||
{
|
||||
audio.load(filepath);
|
||||
libsaria::play();
|
||||
}
|
||||
|
||||
void libsaria::set_volume(double volume)
|
||||
{
|
||||
audio.set_volume(volume);
|
||||
trigger_callback(VOLUME);
|
||||
}
|
||||
|
||||
double libsaria::get_volume()
|
||||
{
|
||||
return audio.get_volume();
|
||||
}
|
|
@ -1,43 +1,24 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/print.h>
|
||||
#include "audio.h"
|
||||
|
||||
static bool initialized = false;
|
||||
static void init_gstreamer(int argc, char **argv)
|
||||
static GstElement *player = NULL;
|
||||
|
||||
GstElement *get_player()
|
||||
{
|
||||
if (initialized == true)
|
||||
return;
|
||||
gst_init(&argc, &argv);
|
||||
initialized = true;
|
||||
return player;
|
||||
}
|
||||
|
||||
Audio::Audio()
|
||||
namespace libsaria
|
||||
{
|
||||
println("Creating audio driver");
|
||||
}
|
||||
|
||||
Audio::~Audio()
|
||||
{
|
||||
}
|
||||
void audio::init(int argc, char **argv)
|
||||
{
|
||||
println("Initializing audio driver");
|
||||
gst_init(&argc, &argv);
|
||||
player = gst_element_factory_make("playbin2", "player");
|
||||
set_volume(1.0);
|
||||
}
|
||||
|
||||
void Audio::init(int argc, char **argv)
|
||||
{
|
||||
println("Initializing gstreamer");
|
||||
init_gstreamer(argc, argv);
|
||||
player = gst_element_factory_make("playbin2", "player");
|
||||
set_volume(1.0);
|
||||
}
|
||||
|
||||
void Audio::reset()
|
||||
{
|
||||
cur_file = "";
|
||||
change_state(GST_STATE_NULL);
|
||||
}
|
||||
|
||||
void Audio::load(string file)
|
||||
{
|
||||
string uri = "file://" + file;
|
||||
reset();
|
||||
cur_file = file;
|
||||
g_object_set(G_OBJECT(player), "uri", uri.c_str(), NULL);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -8,35 +8,6 @@ extern "C" {
|
|||
#include <gst/gst.h>
|
||||
}
|
||||
|
||||
class Audio
|
||||
{
|
||||
private:
|
||||
GstElement *player;
|
||||
string cur_file;
|
||||
double cur_volume;
|
||||
GstElement *get_player();
|
||||
|
||||
void reset();
|
||||
bool change_state(GstState);
|
||||
|
||||
public:
|
||||
Audio();
|
||||
~Audio();
|
||||
|
||||
/* Other functions */
|
||||
void init(int, char **);
|
||||
void load(string);
|
||||
void set_volume(double);
|
||||
double get_volume();
|
||||
|
||||
/* Control functions */
|
||||
bool play();
|
||||
bool pause();
|
||||
|
||||
/* Functions related to position in the song */
|
||||
bool seek(int);
|
||||
bool seek_to(double);
|
||||
bool get_position(gint64 &);
|
||||
bool get_duration(gint64 &);
|
||||
};
|
||||
|
||||
#endif /* LIBSARIA_AUDIO_COMPONENT_H */
|
||||
#endif /* LIBSARIA_AUDIO_PRIVATE_H */
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/callback.h>
|
||||
#include "audio.h"
|
||||
|
||||
bool Audio::change_state(GstState new_state)
|
||||
static string cur_file;
|
||||
|
||||
static bool change_state(GstState new_state)
|
||||
{
|
||||
GstStateChangeReturn ret;
|
||||
ret = gst_element_set_state(GST_ELEMENT(player), new_state);
|
||||
ret = gst_element_set_state(
|
||||
GST_ELEMENT(get_player()),
|
||||
new_state
|
||||
);
|
||||
|
||||
switch(ret) {
|
||||
case GST_STATE_CHANGE_SUCCESS:
|
||||
case GST_STATE_CHANGE_ASYNC:
|
||||
|
@ -14,12 +22,40 @@ bool Audio::change_state(GstState new_state)
|
|||
}
|
||||
}
|
||||
|
||||
bool Audio::play()
|
||||
static void reset()
|
||||
{
|
||||
return change_state(GST_STATE_PLAYING);
|
||||
cur_file = "";
|
||||
change_state(GST_STATE_NULL);
|
||||
}
|
||||
|
||||
bool Audio::pause()
|
||||
namespace libsaria
|
||||
{
|
||||
return change_state(GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void audio::play()
|
||||
{
|
||||
if (change_state(GST_STATE_PLAYING))
|
||||
trigger_callback(PLAY);
|
||||
}
|
||||
|
||||
void audio::pause()
|
||||
{
|
||||
if (change_state(GST_STATE_PAUSED))
|
||||
trigger_callback(PAUSE);
|
||||
}
|
||||
|
||||
void audio::stop()
|
||||
{
|
||||
pause();
|
||||
seek_to(0);
|
||||
}
|
||||
|
||||
void audio::load(string file)
|
||||
{
|
||||
string uri = "file://" + file;
|
||||
reset();
|
||||
cur_file = file;
|
||||
g_object_set(G_OBJECT(get_player()), "uri", uri.c_str(), NULL);
|
||||
play();
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -1,54 +1,60 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include "audio.h"
|
||||
|
||||
bool Audio::get_duration(gint64 &duration)
|
||||
static bool get_duration(gint64 &duration)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_duration(GST_ELEMENT(player),
|
||||
return gst_element_query_duration(GST_ELEMENT(get_player()),
|
||||
&fmt,
|
||||
&duration);
|
||||
}
|
||||
|
||||
bool Audio::get_position(gint64 &position)
|
||||
static bool get_position(gint64 &position)
|
||||
{
|
||||
GstFormat fmt = GST_FORMAT_TIME;
|
||||
return gst_element_query_position(GST_ELEMENT(player),
|
||||
return gst_element_query_position(GST_ELEMENT(get_player()),
|
||||
&fmt,
|
||||
&position);
|
||||
}
|
||||
|
||||
bool Audio::seek_to(double prcnt)
|
||||
namespace libsaria
|
||||
{
|
||||
bool ret;
|
||||
gint64 duration;
|
||||
|
||||
ret = get_duration(duration);
|
||||
if (ret == true) {
|
||||
gst_element_seek_simple(GST_ELEMENT(player),
|
||||
GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH,
|
||||
duration * prcnt);
|
||||
bool audio::seek_to(double prcnt)
|
||||
{
|
||||
bool ret;
|
||||
gint64 duration;
|
||||
|
||||
ret = get_duration(duration);
|
||||
if (ret == true) {
|
||||
gst_element_seek_simple(GST_ELEMENT(get_player()),
|
||||
GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH,
|
||||
duration * prcnt);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Use to seek forward or backward in a song */
|
||||
bool Audio::seek(int dt)
|
||||
{
|
||||
bool ret;
|
||||
gint64 cur_pos;
|
||||
gint64 new_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(player),
|
||||
GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH,
|
||||
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;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include "audio.h"
|
||||
|
||||
void Audio::set_volume(double volume)
|
||||
{
|
||||
GValue value = { 0, };
|
||||
g_value_init(&value, G_TYPE_DOUBLE);
|
||||
g_value_set_double(&value, volume);
|
||||
static double cur_volume;
|
||||
|
||||
g_object_set_property(G_OBJECT(player), "volume", &value);
|
||||
cur_volume = volume;
|
||||
}
|
||||
|
||||
double Audio::get_volume()
|
||||
namespace libsaria
|
||||
{
|
||||
return cur_volume;
|
||||
}
|
||||
|
||||
void audio::set_volume(double volume)
|
||||
{
|
||||
GValue value = { 0, };
|
||||
g_value_init(&value, G_TYPE_DOUBLE);
|
||||
g_value_set_double(&value, volume);
|
||||
|
||||
g_object_set_property(G_OBJECT(get_player()), "volume", &value);
|
||||
cur_volume = volume;
|
||||
}
|
||||
|
||||
double audio::get_volume()
|
||||
{
|
||||
return cur_volume;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/callback.h>
|
||||
#include <libsaria/controls.h>
|
||||
#include <libsaria/library.h>
|
||||
#include "library.h"
|
||||
|
||||
|
@ -48,7 +48,7 @@ bool LibraryPath::play_id(ino_t id)
|
|||
it = file_map.find(id);
|
||||
if (it == file_map.end())
|
||||
return false;
|
||||
libsaria::load(it->second.get_filepath());
|
||||
libsaria::audio::load(it->second.get_filepath());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
void libsaria::init(int argc, char **argv)
|
||||
{
|
||||
println("Initializing libsaria");
|
||||
audio_init(argc, argv);
|
||||
audio::init(argc, argv);
|
||||
print("saria dir: ");
|
||||
println(get_saria_dir());
|
||||
make_saria_dir();
|
||||
|
|
Loading…
Reference in New Issue