2017-05-01 10:56:40 +02:00
|
|
|
/**
|
|
|
|
* vgmstream for Audacious
|
|
|
|
*/
|
|
|
|
|
2014-08-10 21:33:01 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <algorithm>
|
2017-05-01 10:56:40 +02:00
|
|
|
#include <string.h>
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2015-06-22 10:03:44 +02:00
|
|
|
#if DEBUG
|
|
|
|
#include <ctime>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
#include <libaudcore/audio.h>
|
|
|
|
|
|
|
|
|
2014-08-10 21:33:01 +02:00
|
|
|
extern "C" {
|
|
|
|
#include "../src/vgmstream.h"
|
|
|
|
}
|
2015-06-22 10:03:44 +02:00
|
|
|
#include "plugin.h"
|
2014-08-10 21:33:01 +02:00
|
|
|
#include "vfs.h"
|
|
|
|
|
2015-06-22 10:03:44 +02:00
|
|
|
|
2017-05-01 13:33:45 +02:00
|
|
|
#ifndef VERSION
|
2019-03-18 05:17:08 +01:00
|
|
|
#include "version.h"
|
2017-05-01 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
#ifndef VERSION
|
|
|
|
#define VERSION "(unknown version)"
|
|
|
|
#endif
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
#define CFG_ID "vgmstream" // ID for storing in audacious
|
|
|
|
#define MIN_BUFFER_SIZE 576
|
|
|
|
|
|
|
|
/* internal state */
|
|
|
|
VgmstreamPlugin aud_plugin_instance;
|
2015-06-22 10:03:44 +02:00
|
|
|
Settings vgmstream_cfg;
|
2014-08-10 21:33:01 +02:00
|
|
|
VGMSTREAM *vgmstream = NULL;
|
2015-07-10 04:32:53 +02:00
|
|
|
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
const char *const VgmstreamPlugin::defaults[] = {
|
|
|
|
"loop_forever", "1",
|
|
|
|
"loop_count", "2",
|
|
|
|
"fade_length", "3",
|
|
|
|
"fade_delay", "3",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char VgmstreamPlugin::about[] =
|
2017-05-01 14:18:33 +02:00
|
|
|
"vgmstream plugin " VERSION " " __DATE__ "\n"
|
|
|
|
"by hcs, FastElbja, manakoAT, bxaimc, snakemeat, soneek, kode54, bnnm and many others\n"
|
2017-05-01 10:56:40 +02:00
|
|
|
"\n"
|
2017-05-01 14:18:33 +02:00
|
|
|
"Audacious plugin:\n"
|
2017-05-01 10:56:40 +02:00
|
|
|
"ported to Audacious 3.6 by Brandon Whitehead\n"
|
|
|
|
"adopted from Audacious 3 port by Thomas Eppers\n"
|
2015-06-22 10:03:44 +02:00
|
|
|
"originally written by Todd Jeffreys (http://voidpointer.org/)\n"
|
2017-05-01 14:18:33 +02:00
|
|
|
"\n"
|
|
|
|
"https://github.com/kode54/vgmstream/\n"
|
|
|
|
"https://sourceforge.net/projects/vgmstream/ (original)";
|
2015-06-22 10:03:44 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
const PreferencesWidget VgmstreamPlugin::widgets[] = {
|
|
|
|
WidgetLabel(N_("<b>vgmstream Config</b>")),
|
2015-06-22 10:03:44 +02:00
|
|
|
WidgetCheck(N_("Loop Forever:"), WidgetBool(vgmstream_cfg.loop_forever)),
|
2017-05-01 10:56:40 +02:00
|
|
|
WidgetSpin(N_("Loop Count:"), WidgetInt(vgmstream_cfg.loop_count), {1, 20, 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}),
|
2015-06-22 10:03:44 +02:00
|
|
|
};
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
void vgmstream_cfg_load() {
|
2017-05-01 10:56:40 +02:00
|
|
|
debugMessage("cfg_load called");
|
|
|
|
aud_config_set_defaults(CFG_ID, VgmstreamPlugin::defaults);
|
|
|
|
vgmstream_cfg.loop_forever = aud_get_bool(CFG_ID, "loop_forever");
|
|
|
|
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_delay = aud_get_double(CFG_ID, "fade_delay");
|
2015-06-22 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
void vgmstream_cfg_save() {
|
2017-05-01 10:56:40 +02:00
|
|
|
debugMessage("cfg_save called");
|
|
|
|
aud_set_bool(CFG_ID, "loop_forever", vgmstream_cfg.loop_forever);
|
|
|
|
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_delay", vgmstream_cfg.fade_delay);
|
2015-06-22 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
const PluginPreferences VgmstreamPlugin::prefs = {
|
|
|
|
{widgets}, vgmstream_cfg_load, vgmstream_cfg_save
|
|
|
|
};
|
|
|
|
|
|
|
|
// validate extension
|
|
|
|
bool VgmstreamPlugin::is_our_file(const char *filename, VFSFile &file) {
|
|
|
|
const char * ext = strrchr(filename,'.');
|
|
|
|
if (ext==NULL)
|
|
|
|
ext = filename+strlen(filename); /* point to null, i.e. an empty string for the extension */
|
|
|
|
else
|
|
|
|
ext = ext+1; /* skip the dot */
|
|
|
|
|
2017-11-10 20:26:44 +01:00
|
|
|
size_t ext_list_len = 0;
|
|
|
|
const char ** ext_list = vgmstream_get_formats(&ext_list_len);
|
2017-05-01 10:56:40 +02:00
|
|
|
|
|
|
|
for (int i=0; i < ext_list_len; i++) {
|
|
|
|
if (!strcasecmp(ext, ext_list[i]))
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-22 10:03:44 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
return false;
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
bool VgmstreamPlugin::init() {
|
|
|
|
debugMessage("init");
|
|
|
|
|
|
|
|
vgmstream_cfg_load();
|
|
|
|
|
|
|
|
debugMessage("after load cfg");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VgmstreamPlugin::cleanup() {
|
|
|
|
debugMessage("cleanup");
|
|
|
|
|
|
|
|
vgmstream_cfg_save();
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 10:03:44 +02:00
|
|
|
// called every time the user adds a new file to playlist
|
2017-05-01 10:56:40 +02:00
|
|
|
bool read_data(const char * filename, Tuple & tuple) {
|
|
|
|
|
|
|
|
STREAMFILE *streamfile = open_vfs(filename);
|
|
|
|
if (!streamfile) return false;
|
|
|
|
|
|
|
|
VGMSTREAM *vgmstream = init_vgmstream_from_STREAMFILE(streamfile);
|
|
|
|
if (!vgmstream) {
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-01 13:33:45 +02:00
|
|
|
tuple.set_filename(filename); //may leak string???
|
2017-05-01 10:56:40 +02:00
|
|
|
int rate = get_vgmstream_average_bitrate(vgmstream);
|
|
|
|
tuple.set_int(Tuple::Bitrate, rate);
|
|
|
|
|
|
|
|
int ms = get_vgmstream_play_samples(vgmstream_cfg.loop_count, vgmstream_cfg.fade_length, vgmstream_cfg.fade_delay, vgmstream);
|
|
|
|
ms = ms* 1000LL / vgmstream->sample_rate;
|
|
|
|
tuple.set_int(Tuple::Length, ms);
|
|
|
|
|
|
|
|
tuple.set_str(Tuple::Codec, "vgmstream codec");//doesn't show?
|
|
|
|
// here we could call describe_vgmstream() and get substring to add tags and stuff
|
|
|
|
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
|
|
|
|
return true;
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
//for Audacious <= 3.7 (unused otherwise)
|
|
|
|
Tuple VgmstreamPlugin::read_tuple(const char *filename, VFSFile &file) {
|
|
|
|
debugMessage("read_tuple");
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
Tuple tuple;
|
|
|
|
read_data(filename, tuple);
|
|
|
|
return tuple;
|
|
|
|
}
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
//for Audacious >= 3.8 (unused otherwise)
|
|
|
|
bool VgmstreamPlugin::read_tag(const char * filename, VFSFile & file, Tuple & tuple, Index<char> * image) {
|
|
|
|
debugMessage("read_tag");
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
return read_data(filename, tuple);
|
|
|
|
}
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
bool VgmstreamPlugin::play(const char *filename, VFSFile &file) {
|
|
|
|
debugMessage("start play");
|
|
|
|
|
|
|
|
int current_sample_pos = 0;
|
|
|
|
int rate;
|
|
|
|
|
|
|
|
STREAMFILE *streamfile = open_vfs(filename);
|
|
|
|
if (!streamfile) {
|
|
|
|
printf("failed opening %s\n", filename);
|
|
|
|
return false;
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
vgmstream = init_vgmstream_from_STREAMFILE(streamfile);
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
|
|
|
|
if (!vgmstream || vgmstream->channels <= 0) {
|
|
|
|
printf("Error::Channels are zero or couldn't init plugin\n");
|
|
|
|
if (vgmstream)
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
vgmstream = NULL;
|
|
|
|
return false;
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
short buffer[MIN_BUFFER_SIZE * vgmstream->channels];
|
|
|
|
int max_buffer_samples = sizeof(buffer) / sizeof(buffer[0]) / vgmstream->channels;
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
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);
|
2018-07-21 20:11:42 +02:00
|
|
|
open_audio(FMT_S16_LE, vgmstream->sample_rate, vgmstream->channels);
|
2017-05-01 10:56:40 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
vgmstream = NULL;
|
|
|
|
return true;
|
2015-07-10 04:32:53 +02:00
|
|
|
}
|
2014-08-10 21:33:01 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
void VgmstreamPlugin::seek(int seek_value, int ¤t_sample_pos) {
|
|
|
|
debugMessage("seeking");
|
|
|
|
|
|
|
|
// compute from ms to samples
|
|
|
|
int seek_needed_samples = (long long)seek_value * vgmstream->sample_rate / 1000L;
|
|
|
|
short buffer[MIN_BUFFER_SIZE * 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");
|
2015-06-22 10:03:44 +02:00
|
|
|
}
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
void debugMessage(const char *str) {
|
2015-06-22 10:03:44 +02:00
|
|
|
#ifdef DEBUG
|
2017-05-01 10:56:40 +02:00
|
|
|
timeval curTime;
|
|
|
|
gettimeofday(&curTime, NULL);
|
|
|
|
int milli = curTime.tv_usec / 1000;
|
2015-06-22 10:03:44 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
char buffer[80];
|
|
|
|
strftime(buffer, 80, "%H:%M:%S", localtime(&curTime.tv_sec));
|
2015-06-22 10:03:44 +02:00
|
|
|
|
2017-05-01 10:56:40 +02:00
|
|
|
char currentTime[84] = "";
|
|
|
|
sprintf(currentTime, "%s:%d", buffer, milli);
|
|
|
|
printf("[%s] Debug: %s\n", currentTime, str);
|
2015-06-22 10:03:44 +02:00
|
|
|
#endif
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|