cleanup: api tweaks

This commit is contained in:
bnnm 2024-08-24 13:03:48 +02:00
parent c8cc283e58
commit 8917fde7ac
7 changed files with 49 additions and 45 deletions

View File

@ -26,7 +26,7 @@ static FILE* get_output_file(const char* filename) {
}
static libstreamfile_t* get_streamfile(const char* filename) {
return libvgmstream_streamfile_open_from_stdio(filename);
return libstreamfile_open_from_stdio(filename);
}
static int api_example(const char* infile) {
@ -61,7 +61,7 @@ static int api_example(const char* infile) {
};
err = libvgmstream_open_song(lib, &opt);
// external SF is not needed after _open
libvgmstream_streamfile_close(opt.libsf);
libstreamfile_close(opt.libsf);
if (err < 0) {
printf("not a valid file\n");
@ -204,10 +204,10 @@ static libstreamfile_t* test_libsf_open() {
libstreamfile_t* libsf = NULL;
libsf = libvgmstream_streamfile_open_from_stdio("api.bin_wrong");
libsf = libstreamfile_open_from_stdio("api.bin_wrong");
assert(libsf == NULL);
libsf = libvgmstream_streamfile_open_from_stdio("api.bin");
libsf = libstreamfile_open_from_stdio("api.bin");
assert(libsf != NULL);
return libsf;
@ -335,7 +335,7 @@ static void test_lib_tags() {
libvgmstream_tags_t* tags = NULL;
bool more = false;
libsf = libvgmstream_streamfile_open_from_stdio("sample_!tags.m3u");
libsf = libstreamfile_open_from_stdio("sample_!tags.m3u");
assert(libsf != NULL);
tags = libvgmstream_tags_init(libsf);
@ -374,7 +374,7 @@ static void test_lib_tags() {
assert(!more);
libvgmstream_tags_free(tags);
libvgmstream_streamfile_close(libsf);
libstreamfile_close(libsf);
}

View File

@ -1,8 +1,6 @@
#include "api_internal.h"
#if LIBVGMSTREAM_ENABLE
#define INTERNAL_BUF_SAMPLES 1024
static void load_vgmstream(libvgmstream_priv_t* priv, libvgmstream_options_t* opt) {
STREAMFILE* sf_api = open_api_streamfile(opt->libsf);

View File

@ -1,37 +1,41 @@
#include "api_internal.h"
#if LIBVGMSTREAM_ENABLE
#define INTERNAL_BUF_SAMPLES 1024
//TODO: use internal
static void reset_buf(libvgmstream_priv_t* priv) {
/* state reset */
static bool reset_buf(libvgmstream_priv_t* priv) {
// state reset
priv->buf.samples = 0;
priv->buf.bytes = 0;
priv->buf.consumed = 0;
if (priv->buf.initialized)
return;
int output_channels = priv->vgmstream->channels;
int input_channels = priv->vgmstream->channels;
vgmstream_mixing_enable(priv->vgmstream, 0, &input_channels, &output_channels); //query
return true;
/* static config */
priv->buf.channels = input_channels;
if (priv->buf.channels < output_channels)
priv->buf.channels = output_channels;
vgmstream_mixing_enable(priv->vgmstream, 0, &priv->buf.input_channels, &priv->buf.output_channels); //query
// ???
if (priv->buf.input_channels <= 0)
priv->buf.input_channels = priv->vgmstream->channels;
if (priv->buf.output_channels <= 0)
priv->buf.output_channels = priv->vgmstream->channels;
// should be as big as output
if (priv->buf.input_channels < priv->buf.output_channels)
priv->buf.input_channels = priv->buf.output_channels;
priv->buf.sample_size = sizeof(sample_t);
priv->buf.max_samples = INTERNAL_BUF_SAMPLES;
priv->buf.max_bytes = priv->buf.max_samples * priv->buf.sample_size * priv->buf.channels;
priv->buf.max_bytes = priv->buf.max_samples * priv->buf.sample_size * priv->buf.input_channels;
priv->buf.data = malloc(priv->buf.max_bytes);
if (!priv->buf.data) return false;
priv->buf.initialized = true;
return true;
}
static void update_buf(libvgmstream_priv_t* priv, int samples_done) {
priv->buf.samples = samples_done;
priv->buf.bytes = samples_done * priv->buf.sample_size * priv->buf.channels;
priv->buf.bytes = samples_done * priv->buf.sample_size * priv->buf.output_channels;
//priv->buf.consumed = 0; //external
if (!priv->pos.play_forever) {
@ -59,8 +63,7 @@ LIBVGMSTREAM_API int libvgmstream_render(libvgmstream_t* lib) {
if (priv->decode_done)
return LIBVGMSTREAM_ERROR_GENERIC;
reset_buf(priv);
if (!priv->buf.data)
if (!reset_buf(priv))
return LIBVGMSTREAM_ERROR_GENERIC;
int to_get = priv->buf.max_samples;
@ -92,8 +95,8 @@ LIBVGMSTREAM_API int libvgmstream_fill(libvgmstream_t* lib, void* buf, int buf_s
int copy_samples = priv->buf.samples;
if (copy_samples > buf_samples)
copy_samples = buf_samples;
int copy_bytes = priv->buf.sample_size * priv->buf.channels * copy_samples;
int skip_bytes = priv->buf.sample_size * priv->buf.channels * priv->buf.consumed;
int copy_bytes = priv->buf.sample_size * priv->buf.output_channels * copy_samples;
int skip_bytes = priv->buf.sample_size * priv->buf.output_channels * priv->buf.consumed;
memcpy(buf, ((uint8_t*)priv->buf.data) + skip_bytes, copy_bytes);
priv->buf.consumed += copy_samples;

View File

@ -11,6 +11,8 @@
#define LIBVGMSTREAM_ERROR_GENERIC -1
#define LIBVGMSTREAM_ERROR_DONE -2
#define INTERNAL_BUF_SAMPLES 1024
/* self-note: various API functions are just bridges to internal stuff.
* Rather than changing the internal stuff to handle API structs/etc,
* leave internals untouched for a while so external plugins/users may adapt.
@ -21,7 +23,8 @@ typedef struct {
void* data;
/* config */
int channels;
int input_channels;
int output_channels;
int max_bytes;
int max_samples;
int sample_size;

View File

@ -1,7 +1,7 @@
#include "api_internal.h"
#if LIBVGMSTREAM_ENABLE
static libstreamfile_t* libvgmstream_streamfile_from_streamfile(STREAMFILE* sf);
static libstreamfile_t* libstreamfile_from_streamfile(STREAMFILE* sf);
/* libstreamfile_t for external use, as a default implementation calling some internal SF */
@ -29,12 +29,12 @@ static int64_t libsf_seek(void* user_data, int64_t offset, int whence) {
return -1;
switch (whence) {
case LIBVGMSTREAM_STREAMFILE_SEEK_SET: /* absolute */
case LIBSTREAMFILE_SEEK_SET: /* absolute */
break;
case LIBVGMSTREAM_STREAMFILE_SEEK_CUR: /* relative to current */
case LIBSTREAMFILE_SEEK_CUR: /* relative to current */
offset += data->offset;
break;
case LIBVGMSTREAM_STREAMFILE_SEEK_END: /* relative to file end (should be negative) */
case LIBSTREAMFILE_SEEK_END: /* relative to file end (should be negative) */
offset += data->size;
break;
default:
@ -80,7 +80,7 @@ struct libstreamfile_t* libsf_open(void* user_data, const char* filename) {
if (!sf)
return NULL;
libstreamfile_t* libsf = libvgmstream_streamfile_from_streamfile(sf);
libstreamfile_t* libsf = libstreamfile_from_streamfile(sf);
if (!libsf) {
close_streamfile(sf);
return NULL;
@ -101,7 +101,7 @@ static void libsf_close(struct libstreamfile_t* libsf) {
free(libsf);
}
static libstreamfile_t* libvgmstream_streamfile_from_streamfile(STREAMFILE* sf) {
static libstreamfile_t* libstreamfile_from_streamfile(STREAMFILE* sf) {
if (!sf)
return NULL;
@ -132,12 +132,12 @@ fail:
}
LIBVGMSTREAM_API libstreamfile_t* libvgmstream_streamfile_open_from_stdio(const char* filename) {
LIBVGMSTREAM_API libstreamfile_t* libstreamfile_open_from_stdio(const char* filename) {
STREAMFILE* sf = open_stdio_streamfile(filename);
if (!sf)
return NULL;
libstreamfile_t* libsf = libvgmstream_streamfile_from_streamfile(sf);
libstreamfile_t* libsf = libstreamfile_from_streamfile(sf);
if (!libsf) {
close_streamfile(sf);
return NULL;

View File

@ -16,7 +16,7 @@ typedef struct {
static size_t api_read(API_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
void* user_data = sf->libsf->user_data;
sf->libsf->seek(sf->libsf->user_data, offset, LIBVGMSTREAM_STREAMFILE_SEEK_SET);
sf->libsf->seek(sf->libsf->user_data, offset, LIBSTREAMFILE_SEEK_SET);
return sf->libsf->read(user_data, dst, length);
}
@ -50,7 +50,7 @@ static STREAMFILE* api_open(API_STREAMFILE* sf, const char* filename, size_t buf
STREAMFILE* new_sf = open_api_streamfile_internal(libsf, false);
if (!new_sf) {
libvgmstream_streamfile_close(libsf);
libstreamfile_close(libsf);
}
return new_sf;

View File

@ -12,14 +12,14 @@
enum {
LIBVGMSTREAM_STREAMFILE_SEEK_SET = 0,
LIBVGMSTREAM_STREAMFILE_SEEK_CUR = 1,
LIBVGMSTREAM_STREAMFILE_SEEK_END = 2,
//LIBVGMSTREAM_STREAMFILE_SEEK_GET_OFFSET = 3,
//LIBVGMSTREAM_STREAMFILE_SEEK_GET_SIZE = 5,
LIBSTREAMFILE_SEEK_SET = 0,
LIBSTREAMFILE_SEEK_CUR = 1,
LIBSTREAMFILE_SEEK_END = 2,
//LIBSTREAMFILE_SEEK_GET_OFFSET = 3,
//LIBSTREAMFILE_SEEK_GET_SIZE = 5,
};
// maybe libvgmstream_streamfile_t but it was getting unwieldly
// maybe "libvgmstream_streamfile_t" but it was getting unwieldly
typedef struct libstreamfile_t {
//uint32_t flags; // info flags for vgmstream
void* user_data; // any internal structure
@ -54,14 +54,14 @@ typedef struct libstreamfile_t {
/* helper */
static inline void libvgmstream_streamfile_close(libstreamfile_t* libsf) {
static inline void libstreamfile_close(libstreamfile_t* libsf) {
if (!libsf || !libsf->close)
return;
libsf->close(libsf);
}
LIBVGMSTREAM_API libstreamfile_t* libvgmstream_streamfile_open_from_stdio(const char* filename);
LIBVGMSTREAM_API libstreamfile_t* libstreamfile_open_from_stdio(const char* filename);
#endif
#endif