Better reformatting of code

This commit is contained in:
Brandon 2015-07-09 19:32:53 -07:00
parent 3d774e7cec
commit e213d35911
3 changed files with 219 additions and 245 deletions

View File

@ -14,236 +14,222 @@ extern "C" {
#include <libaudcore/audio.h> #include <libaudcore/audio.h>
EXPORT VgmStreamPlugin aud_plugin_instance; VgmStreamPlugin aud_plugin_instance;
Settings vgmstream_cfg; Settings vgmstream_cfg;
VGMSTREAM *vgmstream = NULL; VGMSTREAM *vgmstream = NULL;
#define CFG_ID "vgmstream" //ID for storing in audacious #define CFG_ID "vgmstream" // ID for storing in audacious
const char* const VgmStreamPlugin::defaults[] = const char *const VgmStreamPlugin::defaults[] = {
{ "loop_forever", "1", "loop_count", "2", "fade_length",
"loop_forever", "1", "3", "fade_delay", "3", NULL};
"loop_count", "2",
"fade_length", "3",
"fade_delay", "3",
NULL
};
const char VgmStreamPlugin::about[] = const char VgmStreamPlugin::about[] =
"audacious-vgmstream version: " AUDACIOUSVGMSTREAM_VERSION "\n\n" "audacious-vgmstream version: " AUDACIOUSVGMSTREAM_VERSION "\n\n"
"ported to audacious 3.6 by Brandon Whitehead\n" "ported to audacious 3.6 by Brandon Whitehead\n"
"adopted from audacious 3 port by Thomas Eppers\n" "adopted from audacious 3 port by Thomas Eppers\n"
"originally written by Todd Jeffreys (http://voidpointer.org/)\n" "originally written by Todd Jeffreys (http://voidpointer.org/)\n"
"vgmstream written by hcs, FastElbja, manakoAT, and bxaimc (http://www.sf.net/projects/vgmstream)"; "vgmstream written by hcs, FastElbja, manakoAT, and bxaimc "
"(http://www.sf.net/projects/vgmstream)";
const PreferencesWidget VgmStreamPlugin::widgets[] = { const PreferencesWidget VgmStreamPlugin::widgets[] = {
WidgetLabel(N_("<b>VGMStream Config</b>")), WidgetLabel(N_("<b>VGMStream Config</b>")),
WidgetCheck(N_("Loop Forever:"), WidgetBool(vgmstream_cfg.loop_forever)), WidgetCheck(N_("Loop Forever:"), WidgetBool(vgmstream_cfg.loop_forever)),
WidgetSpin(N_("Loop Count:"), WidgetInt(vgmstream_cfg.loop_count), {1, 20, 1}), WidgetSpin(N_("Loop Count:"), WidgetInt(vgmstream_cfg.loop_count),
WidgetSpin(N_("Fade Length:"), WidgetFloat(vgmstream_cfg.fade_length), {0, 60, 0.1}), {1, 20, 1}),
WidgetSpin(N_("Fade Delay:"), WidgetFloat(vgmstream_cfg.fade_delay), {0, 60, 0.1}), WidgetSpin(N_("Fade Length:"), WidgetFloat(vgmstream_cfg.fade_length),
{0, 60, 0.1}),
WidgetSpin(N_("Fade Delay:"), WidgetFloat(vgmstream_cfg.fade_delay),
{0, 60, 0.1}),
}; };
void vgmstream_cfg_load() void vgmstream_cfg_load() {
{ debugMessage("cfg_load called");
debugMessage("cfg_load called"); aud_config_set_defaults(CFG_ID, VgmStreamPlugin::defaults);
aud_config_set_defaults(CFG_ID, VgmStreamPlugin::defaults); vgmstream_cfg.loop_forever = aud_get_bool(CFG_ID, "loop_forever");
vgmstream_cfg.loop_forever = aud_get_bool(CFG_ID, "loop_forever"); vgmstream_cfg.loop_count = aud_get_int(CFG_ID, "loop_count");
vgmstream_cfg.loop_count = aud_get_int(CFG_ID, "loop_count"); vgmstream_cfg.fade_length = aud_get_double(CFG_ID, "fade_length");
vgmstream_cfg.fade_length = aud_get_double(CFG_ID, "fade_length"); vgmstream_cfg.fade_delay = aud_get_double(CFG_ID, "fade_delay");
vgmstream_cfg.fade_delay = aud_get_double(CFG_ID, "fade_delay");
} }
void vgmstream_cfg_save() void vgmstream_cfg_save() {
{ debugMessage("cfg_save called");
debugMessage("cfg_save called"); aud_set_bool(CFG_ID, "loop_forever", vgmstream_cfg.loop_forever);
aud_set_bool(CFG_ID, "loop_forever", vgmstream_cfg.loop_forever); aud_set_int(CFG_ID, "loop_count", vgmstream_cfg.loop_count);
aud_set_int(CFG_ID, "loop_count", vgmstream_cfg.loop_count); aud_set_double(CFG_ID, "fade_length", vgmstream_cfg.fade_length);
aud_set_double(CFG_ID, "fade_length", vgmstream_cfg.fade_length); aud_set_double(CFG_ID, "fade_delay", vgmstream_cfg.fade_delay);
aud_set_double(CFG_ID, "fade_delay", vgmstream_cfg.fade_delay);
} }
const PluginPreferences VgmStreamPlugin::prefs = { const PluginPreferences VgmStreamPlugin::prefs = {
{widgets}, {widgets}, vgmstream_cfg_load, vgmstream_cfg_save};
vgmstream_cfg_load,
vgmstream_cfg_save
};
bool VgmStreamPlugin::init() bool VgmStreamPlugin::init() {
{ debugMessage("init");
debugMessage("init"); vgmstream_cfg_load();
vgmstream_cfg_load(); debugMessage("after load cfg");
debugMessage("after load cfg"); return true;
return true;
} }
void VgmStreamPlugin::cleanup() void VgmStreamPlugin::cleanup() {
{ debugMessage("cleanup");
debugMessage("cleanup"); vgmstream_cfg_save();
vgmstream_cfg_save();
} }
// called every time the user adds a new file to playlist // called every time the user adds a new file to playlist
Tuple VgmStreamPlugin::read_tuple(const char *filename, VFSFile& file) Tuple VgmStreamPlugin::read_tuple(const char *filename, VFSFile &file) {
{ debugMessage("probe for tuple");
debugMessage("probe for tuple"); Tuple tuple;
Tuple tuple; int ms;
int ms; int rate;
int rate; VGMSTREAM *vgmstream = NULL;
VGMSTREAM* vgmstream = NULL; STREAMFILE *streamfile = NULL;
STREAMFILE* streamfile = NULL;
streamfile = open_vfs(filename); streamfile = open_vfs(filename);
vgmstream = init_vgmstream_from_STREAMFILE(streamfile); vgmstream = init_vgmstream_from_STREAMFILE(streamfile);
tuple.set_filename(filename); tuple.set_filename(filename);
rate = vgmstream->sample_rate * 2 * vgmstream->channels; rate = vgmstream->sample_rate * 2 * vgmstream->channels;
tuple.set_int(Tuple::Bitrate, rate); tuple.set_int(Tuple::Bitrate, rate);
ms = get_vgmstream_play_samples(vgmstream_cfg.loop_count, vgmstream_cfg.fade_length, vgmstream_cfg.fade_delay, vgmstream) * 1000LL / vgmstream->sample_rate; ms = get_vgmstream_play_samples(vgmstream_cfg.loop_count,
tuple.set_int(Tuple::Length, ms); vgmstream_cfg.fade_length,
vgmstream_cfg.fade_delay, vgmstream) *
1000LL / vgmstream->sample_rate;
tuple.set_int(Tuple::Length, ms);
close_streamfile(streamfile); close_streamfile(streamfile);
close_vgmstream(vgmstream); close_vgmstream(vgmstream);
return tuple; return tuple;
} }
bool VgmStreamPlugin::play(const char * filename, VFSFile& file) bool VgmStreamPlugin::play(const char *filename, VFSFile &file) {
{ int current_sample_pos = 0;
int current_sample_pos = 0; int rate;
int rate;
debugMessage("start play"); debugMessage("start play");
STREAMFILE* streamfile = open_vfs(filename); STREAMFILE *streamfile = open_vfs(filename);
if (!streamfile) if (!streamfile) {
{ printf("failed opening %s\n", filename);
printf("failed opening %s\n", filename); return false;
return false; }
}
vgmstream = init_vgmstream_from_STREAMFILE(streamfile); vgmstream = init_vgmstream_from_STREAMFILE(streamfile);
close_streamfile(streamfile); close_streamfile(streamfile);
if (!vgmstream || vgmstream->channels <= 0) if (!vgmstream || vgmstream->channels <= 0) {
{ printf("Error::Channels are zero or couldn't init plugin\n");
printf("Error::Channels are zero or couldn't init plugin\n");
if (vgmstream)
close_vgmstream(vgmstream);
vgmstream = NULL;
return false;
}
short buffer[576 * vgmstream->channels];
int max_buffer_samples = sizeof(buffer) / sizeof(buffer[0]) / vgmstream->channels;
int stream_samples_amount = get_vgmstream_play_samples(vgmstream_cfg.loop_count, vgmstream_cfg.fade_length, vgmstream_cfg.fade_delay, vgmstream);
rate = get_vgmstream_average_bitrate(vgmstream);
set_stream_bitrate(rate);
open_audio(FMT_S16_LE, vgmstream->sample_rate, 2);
int fade_samples = vgmstream_cfg.fade_length * vgmstream->sample_rate;
while (!check_stop())
{
int toget = max_buffer_samples;
int seek_value = check_seek();
if (seek_value > 0)
seek(seek_value, current_sample_pos);
// If we haven't configured the plugin to play forever
// or the current song is not loopable.
if (!vgmstream_cfg.loop_forever || !vgmstream->loop_flag)
{
if (current_sample_pos >= stream_samples_amount)
break;
if (current_sample_pos + toget > stream_samples_amount)
toget = stream_samples_amount - current_sample_pos;
}
render_vgmstream(buffer, toget, vgmstream);
if (vgmstream->loop_flag && fade_samples > 0 && !vgmstream_cfg.loop_forever)
{
int samples_into_fade = current_sample_pos - (stream_samples_amount - fade_samples);
if (samples_into_fade + toget > 0)
{
for (int j = 0; j < toget; j++, samples_into_fade++)
{
if (samples_into_fade > 0)
{
double fadedness = (double)(fade_samples - samples_into_fade) / fade_samples;
for (int k = 0; k < vgmstream->channels; k++)
buffer[j * vgmstream->channels + k] *= fadedness;
}
}
}
}
write_audio(buffer, toget * sizeof(short) * vgmstream->channels);
current_sample_pos += toget;
}
debugMessage("finished");
if (vgmstream) if (vgmstream)
close_vgmstream(vgmstream); close_vgmstream(vgmstream);
vgmstream = NULL; vgmstream = NULL;
return true; return false;
} }
void VgmStreamPlugin::seek(int seek_value, int& current_sample_pos) short buffer[576 * vgmstream->channels];
{ int max_buffer_samples =
debugMessage("seeking"); sizeof(buffer) / sizeof(buffer[0]) / vgmstream->channels;
// compute from ms to samples
int seek_needed_samples = (long long)seek_value * vgmstream->sample_rate / 1000L;
short buffer[576 * vgmstream->channels];
int max_buffer_samples = sizeof(buffer) / sizeof(buffer[0]) / vgmstream->channels;
int samples_to_do = 0; int stream_samples_amount = get_vgmstream_play_samples(
if (seek_needed_samples < current_sample_pos) vgmstream_cfg.loop_count, vgmstream_cfg.fade_length,
{ vgmstream_cfg.fade_delay, vgmstream);
// go back in time, reopen file rate = get_vgmstream_average_bitrate(vgmstream);
debugMessage("reopen file to seek backward");
reset_vgmstream(vgmstream); set_stream_bitrate(rate);
current_sample_pos = 0; open_audio(FMT_S16_LE, vgmstream->sample_rate, 2);
samples_to_do = seek_needed_samples;
} int fade_samples = vgmstream_cfg.fade_length * vgmstream->sample_rate;
else if (current_sample_pos < seek_needed_samples) while (!check_stop()) {
{ int toget = max_buffer_samples;
// go forward in time
samples_to_do = seek_needed_samples - current_sample_pos; int seek_value = check_seek();
if (seek_value > 0)
seek(seek_value, current_sample_pos);
// If we haven't configured the plugin to play forever
// or the current song is not loopable.
if (!vgmstream_cfg.loop_forever || !vgmstream->loop_flag) {
if (current_sample_pos >= stream_samples_amount)
break;
if (current_sample_pos + toget > stream_samples_amount)
toget = stream_samples_amount - current_sample_pos;
} }
// do the actual seeking render_vgmstream(buffer, toget, vgmstream);
if (samples_to_do >= 0)
{
debugMessage("render forward");
//render till seeked sample if (vgmstream->loop_flag && fade_samples > 0 &&
while (samples_to_do >0) !vgmstream_cfg.loop_forever) {
{ int samples_into_fade =
int seek_samples = std::min(max_buffer_samples, samples_to_do); current_sample_pos - (stream_samples_amount - fade_samples);
current_sample_pos += seek_samples; if (samples_into_fade + toget > 0) {
samples_to_do -= seek_samples; for (int j = 0; j < toget; j++, samples_into_fade++) {
render_vgmstream(buffer, seek_samples, vgmstream); if (samples_into_fade > 0) {
double fadedness =
(double)(fade_samples - samples_into_fade) / fade_samples;
for (int k = 0; k < vgmstream->channels; k++)
buffer[j * vgmstream->channels + k] *= fadedness;
}
} }
debugMessage("after render vgmstream"); }
} }
write_audio(buffer, toget * sizeof(short) * vgmstream->channels);
current_sample_pos += toget;
}
debugMessage("finished");
if (vgmstream)
close_vgmstream(vgmstream);
vgmstream = NULL;
return true;
} }
void debugMessage(const char *str) void VgmStreamPlugin::seek(int seek_value, int &current_sample_pos) {
{ debugMessage("seeking");
// compute from ms to samples
int seek_needed_samples =
(long long)seek_value * vgmstream->sample_rate / 1000L;
short buffer[576 * vgmstream->channels];
int max_buffer_samples =
sizeof(buffer) / sizeof(buffer[0]) / vgmstream->channels;
int samples_to_do = 0;
if (seek_needed_samples < current_sample_pos) {
// go back in time, reopen file
debugMessage("reopen file to seek backward");
reset_vgmstream(vgmstream);
current_sample_pos = 0;
samples_to_do = seek_needed_samples;
} else if (current_sample_pos < seek_needed_samples) {
// go forward in time
samples_to_do = seek_needed_samples - current_sample_pos;
}
// do the actual seeking
if (samples_to_do >= 0) {
debugMessage("render forward");
// render till seeked sample
while (samples_to_do > 0) {
int seek_samples = std::min(max_buffer_samples, samples_to_do);
current_sample_pos += seek_samples;
samples_to_do -= seek_samples;
render_vgmstream(buffer, seek_samples, vgmstream);
}
debugMessage("after render vgmstream");
}
}
void debugMessage(const char *str) {
#ifdef DEBUG #ifdef DEBUG
timeval curTime; timeval curTime;
gettimeofday(&curTime, NULL); gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000; int milli = curTime.tv_usec / 1000;
char buffer [80]; char buffer[80];
strftime(buffer, 80, "%H:%M:%S", localtime(&curTime.tv_sec)); strftime(buffer, 80, "%H:%M:%S", localtime(&curTime.tv_sec));
char currentTime[84] = ""; char currentTime[84] = "";
sprintf(currentTime, "%s:%d", buffer, milli); sprintf(currentTime, "%s:%d", buffer, milli);
printf("[%s] Debug: %s\n", currentTime, str); printf("[%s] Debug: %s\n", currentTime, str);
#endif #endif
} }

View File

@ -7,43 +7,37 @@
#include <libaudcore/preferences.h> #include <libaudcore/preferences.h>
#include <libaudcore/runtime.h> #include <libaudcore/runtime.h>
class VgmStreamPlugin : public InputPlugin class VgmStreamPlugin : public InputPlugin {
{
public: public:
static const char *const exts[]; static const char *const exts[];
static const char *const defaults[]; static const char *const defaults[];
static const char about[]; static const char about[];
static const PreferencesWidget widgets[]; static const PreferencesWidget widgets[];
static const PluginPreferences prefs; static const PluginPreferences prefs;
static constexpr PluginInfo info = { static constexpr PluginInfo info = {
N_("VGMStream Decoder"), N_("VGMStream Decoder"), N_("vgmstream"), about, &prefs,
N_("vgmstream"), };
about,
& prefs,
};
static constexpr auto iinfo = InputInfo() static constexpr auto iinfo = InputInfo().with_exts(exts);
.with_exts(exts);
constexpr VgmStreamPlugin() : InputPlugin(info, iinfo) {} constexpr VgmStreamPlugin() : InputPlugin(info, iinfo) {}
bool init(); bool init();
void cleanup(); void cleanup();
bool is_our_file(const char *filename, VFSFile &file) {return false;} bool is_our_file(const char *filename, VFSFile &file) { return false; }
Tuple read_tuple(const char *filename, VFSFile &file); Tuple read_tuple(const char *filename, VFSFile &file);
bool play(const char *filename, VFSFile &file); bool play(const char *filename, VFSFile &file);
private: private:
void seek(int seek_value, int& current_sample_pos); void seek(int seek_value, int &current_sample_pos);
}; };
typedef struct typedef struct {
{
bool loop_forever; bool loop_forever;
int loop_count; int loop_count;
double fade_length; double fade_length;
double fade_delay; double fade_delay;
} Settings; } Settings;
extern Settings vgmstream_cfg; extern Settings vgmstream_cfg;

View File

@ -7,8 +7,7 @@
#include "plugin.h" #include "plugin.h"
#include "vfs.h" #include "vfs.h"
typedef struct _VFSSTREAMFILE typedef struct _VFSSTREAMFILE {
{
STREAMFILE sf; STREAMFILE sf;
VFSFile *vfsFile; VFSFile *vfsFile;
off_t offset; off_t offset;
@ -18,94 +17,89 @@ typedef struct _VFSSTREAMFILE
static STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path); static STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path);
static size_t read_vfs(VFSSTREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length) static size_t read_vfs(VFSSTREAMFILE *streamfile, uint8_t *dest, off_t offset,
{ size_t length) {
size_t sz; size_t sz;
// if the offsets don't match, then we need to perform a seek // if the offsets don't match, then we need to perform a seek
if (streamfile->offset != offset) if (streamfile->offset != offset) {
{
streamfile->vfsFile->fseek(offset, VFS_SEEK_SET); streamfile->vfsFile->fseek(offset, VFS_SEEK_SET);
streamfile->offset = offset; streamfile->offset = offset;
} }
sz = streamfile->vfsFile->fread(dest, 1, length); sz = streamfile->vfsFile->fread(dest, 1, length);
// increment our current offset // increment our current offset
streamfile->offset += sz; streamfile->offset += sz;
return sz; return sz;
} }
static void close_vfs(VFSSTREAMFILE *streamfile) static void close_vfs(VFSSTREAMFILE *streamfile) {
{
debugMessage("close_vfs"); debugMessage("close_vfs");
free(streamfile->vfsFile); free(streamfile->vfsFile);
free(streamfile); free(streamfile);
} }
static size_t get_size_vfs(VFSSTREAMFILE *streamfile) static size_t get_size_vfs(VFSSTREAMFILE *streamfile) {
{
return streamfile->vfsFile->fsize(); return streamfile->vfsFile->fsize();
} }
static size_t get_offset_vfs(VFSSTREAMFILE *streamfile) static size_t get_offset_vfs(VFSSTREAMFILE *streamfile) {
{
return streamfile->offset; return streamfile->offset;
} }
static void get_name_vfs(VFSSTREAMFILE *streamfile, char *buffer, size_t length) static void get_name_vfs(VFSSTREAMFILE *streamfile, char *buffer,
{ size_t length) {
strncpy(buffer, streamfile->name, length); strncpy(buffer, streamfile->name, length);
buffer[length-1] = '\0'; buffer[length - 1] = '\0';
} }
static void get_realname_vfs(VFSSTREAMFILE *streamfile, char *buffer, size_t length) static void get_realname_vfs(VFSSTREAMFILE *streamfile, char *buffer,
{ size_t length) {
strncpy(buffer, streamfile->realname, length); strncpy(buffer, streamfile->realname, length);
buffer[length-1]='\0'; buffer[length - 1] = '\0';
} }
static STREAMFILE *open_vfs_impl(VFSSTREAMFILE *streamfile, const char * const filename, size_t buffersize) static STREAMFILE *open_vfs_impl(VFSSTREAMFILE *streamfile,
{ const char *const filename,
size_t buffersize) {
if (!filename) if (!filename)
return NULL; return NULL;
return open_vfs(filename); return open_vfs(filename);
} }
STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path) STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path) {
{ VFSSTREAMFILE *streamfile = (VFSSTREAMFILE *)malloc(sizeof(VFSSTREAMFILE));
VFSSTREAMFILE *streamfile = (VFSSTREAMFILE*)malloc(sizeof(VFSSTREAMFILE));
if (!streamfile) if (!streamfile)
return NULL; return NULL;
// success, set our pointers // success, set our pointers
memset(streamfile, 0, sizeof(VFSSTREAMFILE)); memset(streamfile, 0, sizeof(VFSSTREAMFILE));
streamfile->sf.read = read_vfs; streamfile->sf.read = read_vfs;
streamfile->sf.get_size = get_size_vfs; streamfile->sf.get_size = get_size_vfs;
streamfile->sf.get_offset = get_offset_vfs; streamfile->sf.get_offset = get_offset_vfs;
streamfile->sf.get_name = get_name_vfs; streamfile->sf.get_name = get_name_vfs;
streamfile->sf.get_realname = get_realname_vfs; streamfile->sf.get_realname = get_realname_vfs;
streamfile->sf.open = open_vfs_impl; streamfile->sf.open = open_vfs_impl;
streamfile->sf.close = close_vfs; streamfile->sf.close = close_vfs;
streamfile->vfsFile = file; streamfile->vfsFile = file;
streamfile->offset = 0; streamfile->offset = 0;
strncpy(streamfile->name, path, sizeof(streamfile->name)); strncpy(streamfile->name, path, sizeof(streamfile->name));
streamfile->name[sizeof(streamfile->name)-1] = '\0'; streamfile->name[sizeof(streamfile->name) - 1] = '\0';
{ {
gchar* realname = g_filename_from_uri(path, NULL, NULL); gchar *realname = g_filename_from_uri(path, NULL, NULL);
strncpy(streamfile->realname, realname, sizeof(streamfile->realname)); strncpy(streamfile->realname, realname, sizeof(streamfile->realname));
streamfile->realname[sizeof(streamfile->realname)-1] = '\0'; streamfile->realname[sizeof(streamfile->realname) - 1] = '\0';
g_free(realname); g_free(realname);
} }
return &streamfile->sf; return &streamfile->sf;
} }
STREAMFILE *open_vfs(const char *path) STREAMFILE *open_vfs(const char *path) {
{ VFSFile *vfsFile = new VFSFile(path, "rb");
VFSFile* vfsFile = new VFSFile(path, "rb");
if (!vfsFile) if (!vfsFile)
return NULL; return NULL;