2012-08-24 17:36:40 +00:00
|
|
|
#include "layout.h"
|
|
|
|
#include "../vgmstream.h"
|
|
|
|
|
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
/* NOTE: if loop settings change the layered vgmstreams must be notified (preferably using vgmstream_force_loop) */
|
2018-04-22 02:19:37 +02:00
|
|
|
#define LAYER_BUF_SIZE 512
|
|
|
|
#define LAYER_MAX_CHANNELS 6 /* at least 2, but let's be generous */
|
2018-03-30 21:28:32 +02:00
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
/* Decodes samples for layered streams.
|
|
|
|
* Similar to interleave layout, but decodec samples are mixed from complete vgmstreams, each
|
|
|
|
* with custom codecs and different number of channels, creating a single super-vgmstream.
|
|
|
|
* Usually combined with custom streamfiles to handle data interleaved in weird ways. */
|
2019-02-23 02:54:23 +01:00
|
|
|
void render_vgmstream_layered(sample_t * buffer, int32_t sample_count, VGMSTREAM * vgmstream) {
|
2018-08-25 20:46:54 +02:00
|
|
|
int samples_written = 0;
|
2018-03-30 21:28:32 +02:00
|
|
|
layered_layout_data *data = vgmstream->layout_data;
|
2019-02-23 02:54:23 +01:00
|
|
|
sample_t interleave_buf[LAYER_BUF_SIZE*LAYER_MAX_CHANNELS];
|
2012-08-24 17:36:40 +00:00
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
|
|
|
|
while (samples_written < sample_count) {
|
|
|
|
int samples_to_do = LAYER_BUF_SIZE;
|
2018-08-11 17:58:59 +02:00
|
|
|
int layer, ch = 0;
|
2018-04-22 02:19:37 +02:00
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
if (samples_to_do > sample_count - samples_written)
|
|
|
|
samples_to_do = sample_count - samples_written;
|
2012-08-24 17:36:40 +00:00
|
|
|
|
2018-04-22 02:19:37 +02:00
|
|
|
for (layer = 0; layer < data->layer_count; layer++) {
|
2018-08-25 20:46:54 +02:00
|
|
|
int s, layer_ch;
|
2018-04-22 02:19:37 +02:00
|
|
|
int layer_channels = data->layers[layer]->channels;
|
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
/* each layer will handle its own looping internally */
|
|
|
|
|
2018-04-22 02:19:37 +02:00
|
|
|
render_vgmstream(interleave_buf, samples_to_do, data->layers[layer]);
|
2012-08-24 17:36:40 +00:00
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
/* mix layer samples to main samples */
|
|
|
|
for (layer_ch = 0; layer_ch < layer_channels; layer_ch++) {
|
2018-04-22 02:19:37 +02:00
|
|
|
for (s = 0; s < samples_to_do; s++) {
|
2018-08-25 20:46:54 +02:00
|
|
|
size_t layer_sample = s*layer_channels + layer_ch;
|
|
|
|
size_t buffer_sample = (samples_written+s)*vgmstream->channels + ch;
|
2012-08-24 17:36:40 +00:00
|
|
|
|
2018-04-22 02:19:37 +02:00
|
|
|
buffer[buffer_sample] = interleave_buf[layer_sample];
|
|
|
|
}
|
2018-08-11 17:58:59 +02:00
|
|
|
ch++;
|
2012-08-24 17:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 20:46:54 +02:00
|
|
|
samples_written += samples_to_do;
|
|
|
|
vgmstream->current_sample = data->layers[0]->current_sample; /* just in case it's used for info */
|
|
|
|
//vgmstream->samples_into_block = 0; /* handled in each layer */
|
2018-03-30 21:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
layered_layout_data* init_layout_layered(int layer_count) {
|
|
|
|
layered_layout_data *data = NULL;
|
|
|
|
|
|
|
|
if (layer_count <= 0 || layer_count > 255)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data = calloc(1, sizeof(layered_layout_data));
|
|
|
|
if (!data) goto fail;
|
|
|
|
|
|
|
|
data->layer_count = layer_count;
|
2012-08-24 17:36:40 +00:00
|
|
|
|
2018-03-30 21:28:32 +02:00
|
|
|
data->layers = calloc(layer_count, sizeof(VGMSTREAM*));
|
|
|
|
if (!data->layers) goto fail;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
fail:
|
|
|
|
free_layout_layered(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int setup_layout_layered(layered_layout_data* data) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* setup each VGMSTREAM (roughly equivalent to vgmstream.c's init_vgmstream_internal stuff) */
|
|
|
|
for (i = 0; i < data->layer_count; i++) {
|
|
|
|
if (!data->layers[i])
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (data->layers[i]->num_samples <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
2018-04-22 02:19:37 +02:00
|
|
|
if (data->layers[i]->channels > LAYER_MAX_CHANNELS)
|
2018-03-30 21:28:32 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
/* a bit weird, but no matter */
|
|
|
|
if (data->layers[i]->sample_rate != data->layers[i-1]->sample_rate) {
|
|
|
|
VGM_LOG("layered layout: layer %i has different sample rate\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* also weird */
|
|
|
|
if (data->layers[i]->coding_type != data->layers[i-1]->coding_type) {
|
|
|
|
VGM_LOG("layered layout: layer %i has different coding type\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:28:20 +01:00
|
|
|
/* loops and other values could be mismatched but hopefully not */
|
2018-03-30 21:28:32 +02:00
|
|
|
|
2019-02-15 22:28:20 +01:00
|
|
|
setup_vgmstream(data->layers[i]); /* final setup in case the VGMSTREAM was created manually */
|
2018-03-30 21:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
return 0; /* caller is expected to free */
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_layout_layered(layered_layout_data *data) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (data->layers) {
|
|
|
|
for (i = 0; i < data->layer_count; i++) {
|
|
|
|
close_vgmstream(data->layers[i]);
|
|
|
|
}
|
|
|
|
free(data->layers);
|
2012-08-24 17:36:40 +00:00
|
|
|
}
|
2018-03-30 21:28:32 +02:00
|
|
|
free(data);
|
2012-08-24 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:28:32 +02:00
|
|
|
void reset_layout_layered(layered_layout_data *data) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < data->layer_count; i++) {
|
|
|
|
reset_vgmstream(data->layers[i]);
|
|
|
|
}
|
|
|
|
}
|