void* Init(const char* strFile, unsigned int filecache, int* channels, int* samplerate, int* bitspersample, int64_t* totaltime, int* bitrate, AEDataFormat* format, const AEChannel** channelinfo) { if (!strFile) return NULL; VGMContext* result = new VGMContext; if (!result) return NULL; open_XBMC((struct _STREAMFILE*)result, strFile, 0); result->stream = init_vgmstream_from_STREAMFILE((struct _STREAMFILE*)result); if (!result->stream) { delete result; return NULL; } SET_IF(channels, result->stream->channels) SET_IF(samplerate, result->stream->sample_rate) SET_IF(bitspersample, 16) SET_IF(totaltime, result->stream->num_samples/result->stream->sample_rate*1000) SET_IF(format, AE_FMT_S16NE) static enum AEChannel map[8][9] = { {AE_CH_FC, AE_CH_NULL}, {AE_CH_FL, AE_CH_FR, AE_CH_NULL}, {AE_CH_FL, AE_CH_FC, AE_CH_FR, AE_CH_NULL}, {AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, {AE_CH_FL, AE_CH_FC, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, {AE_CH_FL, AE_CH_FC, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_LFE, AE_CH_NULL}, {AE_CH_FL, AE_CH_FC, AE_CH_FR, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, {AE_CH_FL, AE_CH_FC, AE_CH_FR, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_LFE, AE_CH_NULL} }; SET_IF(channelinfo, NULL) if (result->stream->channels <= 8) SET_IF(channelinfo, map[result->stream->channels-1]) SET_IF(bitrate, 0) return result; }
static int play_vgmstream(const char *filename, struct params *par) { int ret = 0; STREAMFILE *sf; VGMSTREAM *vgms; FILE *save_fps[4]; int loop_count; int64_t total_samples; size_t buffer_size; int64_t buffer_samples; int64_t fade_time_samples, fade_start; int time_total_min; double time_total_sec; int64_t s; int i; sf = open_stdio_streamfile(filename); if (!sf) { fprintf(stderr, "%s: cannot open file\n", filename); return -1; } sf->stream_index = par->stream_index; vgms = init_vgmstream_from_STREAMFILE(sf); close_streamfile(sf); if (!vgms) { fprintf(stderr, "%s: error opening stream\n", filename); return -1; } printf("Playing stream: %s\n", filename); /* Print metadata in verbose mode */ if (verbose) { char description[4096] = { '\0' }; describe_vgmstream(vgms, description, sizeof(description)); puts(description); putchar('\n'); } /* If the audio device hasn't been opened yet, then describe it */ if (!device) { ao_info *info = ao_driver_info(driver_id); printf("Audio device: %s\n", info->name); printf("Comment: %s\n", info->comment); putchar('\n'); } /* Stupid hack to hang onto a few low-numbered file descriptors * so that play_compressed_file() doesn't break, due to POSIX * wackiness like https://bugs.debian.org/590920 */ for (i = 0; i < 4; i++) save_fps[i] = fopen("/dev/null", "r"); ret = set_sample_format(vgms); if (ret) goto fail; loop_count = par->loop_count; if (vgms->loop_flag && loop_count < 0) { /* * Calculate how many loops are needed to achieve a minimum * playback time. Note: This calculation is derived from the * logic in get_vgmstream_play_samples(). */ double intro = (double)vgms->loop_start_sample / vgms->sample_rate; double loop = (double)(vgms->loop_end_sample - vgms->loop_start_sample) / vgms->sample_rate; double end = par->fade_time + par->fade_delay; if (loop < 1.0) loop = 1.0; loop_count = (int)((par->min_time - intro - end) / loop + 0.99); if (loop_count < 1) loop_count = 1; } total_samples = get_vgmstream_play_samples(loop_count, par->fade_time, par->fade_delay, vgms); { double total = (double)total_samples / vgms->sample_rate; time_total_min = (int)total / 60; time_total_sec = total - 60 * time_total_min; } /* Buffer size in bytes */ buffer_size = 1024 * buffer_size_kb; if (!buffer) { if (buffer_size_kb < 1) { fprintf(stderr, "Invalid buffer size '%d'\n", buffer_size_kb); return -1; } buffer = malloc(buffer_size); if (!buffer) goto fail; } buffer_samples = buffer_size / (vgms->channels * sizeof(sample)); fade_time_samples = (int64_t)(par->fade_time * vgms->sample_rate); fade_start = total_samples - fade_time_samples; if (fade_start < 0) fade_start = total_samples; for (s = 0; s < total_samples && !interrupted; s += buffer_samples) { int64_t buffer_used_samples = MIN(buffer_samples, total_samples - s); char *suffix = ""; render_vgmstream(buffer, buffer_used_samples, vgms); #ifdef LITTLE_ENDIAN_OUTPUT swap_samples_le(buffer, vgms->channels * buffer_used_samples); #endif if (vgms->loop_flag && fade_time_samples > 0 && s >= fade_start) { /* Got to do the fade-out ourselves :p */ int64_t b; for (b = 0; b < buffer_used_samples; b++) { double factor = 1.0 - (double)(s + b - fade_start) / fade_time_samples; int c; for (c = 0; c < vgms->channels; c++) buffer[vgms->channels * b + c] *= factor; } suffix = " (fading)"; } if (verbose && !out_filename) { double played = (double)s / vgms->sample_rate; double remain = (double)(total_samples - s) / vgms->sample_rate; int time_played_min = (int)played / 60; double time_played_sec = played - 60 * time_played_min; int time_remain_min = (int)remain / 60; double time_remain_sec = remain - 60 * time_remain_min; /* Time: 01:02.34 [08:57.66] of 10:00.00 */ printf("\rTime: %02d:%05.2f [%02d:%05.2f] of %02d:%05.2f%s ", time_played_min, time_played_sec, time_remain_min, time_remain_sec, time_total_min, time_total_sec, suffix); fflush(stdout); } if (!ao_play(device, (char *)buffer, buffer_used_samples * vgms->channels * sizeof(sample))) { fputs("\nAudio playback error\n", stderr); ao_close(device); device = NULL; ret = -1; break; } } if (verbose && !ret) { /* Clear time status line */ putchar('\r'); for (i = 0; i < 64; i++) putchar(' '); putchar('\r'); fflush(stdout); } if (out_filename && !ret) printf("Wrote %02d:%05.2f of audio to %s\n\n", time_total_min, time_total_sec, out_filename); if (interrupted) { fputs("Playback terminated.\n\n", stdout); ret = record_interrupt(); if (ret) fputs("Exiting...\n", stdout); } fail: close_vgmstream(vgms); for (i = 0; i < 4; i++) fclose(save_fps[i]); return ret; }