From 8abf8a28613f3b62554f6e43807916bb6dccc6ef Mon Sep 17 00:00:00 2001 From: NicknineTheEagle Date: Sat, 13 Aug 2022 01:47:33 +0300 Subject: [PATCH 1/2] VAB: Use min and max note values --- src/meta/vab.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/meta/vab.c b/src/meta/vab.c index 6b5cc3f0..1a5855c5 100644 --- a/src/meta/vab.c +++ b/src/meta/vab.c @@ -3,10 +3,14 @@ static uint16_t SsPitchFromNote(int16_t note, int16_t fine, uint8_t center, uint8_t shift); +#define VAB_MIN(x,y) ((x)<(y)?(x):(y)) +#define VAB_MAX(x,y) ((x)>(y)?(x):(y)) +#define VAB_CLAMP(x,min,max) VAB_MIN(VAB_MAX(x,min),max) + /* .VAB - standard PS1 bank format */ VGMSTREAM* init_vgmstream_vab(STREAMFILE* sf) { uint16_t programs, wave_num, pitch; - uint8_t center, shift; + uint8_t center, shift, min_note, max_note; off_t programs_off, tones_off, waves_off, entry_off, data_offset; size_t data_size; int target_subsong = sf->stream_index, is_vh = 0, program_num, tone_num, total_subsongs, @@ -68,10 +72,12 @@ VGMSTREAM* init_vgmstream_vab(STREAMFILE* sf) { entry_off = tones_off + program_num * 16 * 0x20 + tone_num * 0x20; center = read_u8(entry_off + 0x04, sf); shift = read_u8(entry_off + 0x05, sf); + min_note = read_u8(entry_off + 0x06, sf); + max_note = read_u8(entry_off + 0x07, sf); wave_num = read_u16le(entry_off + 0x16, sf); /* play default note */ - pitch = SsPitchFromNote(60, 0, center, shift); + pitch = SsPitchFromNote(VAB_CLAMP(60, min_note, max_note), 0, center, shift); data_offset = is_vh ? 0x00 : (waves_off + 256 * 0x02); for (i = 0; i < wave_num; i++) { From 9e8fb3938c0bd0f184101d0d4f5d767f5237c8b0 Mon Sep 17 00:00:00 2001 From: NicknineTheEagle Date: Sat, 13 Aug 2022 20:10:20 +0300 Subject: [PATCH 2/2] VAB: Added companion config format --- src/meta/vab.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/meta/vab.c b/src/meta/vab.c index 1a5855c5..8ca5790e 100644 --- a/src/meta/vab.c +++ b/src/meta/vab.c @@ -7,6 +7,73 @@ static uint16_t SsPitchFromNote(int16_t note, int16_t fine, uint8_t center, uint #define VAB_MAX(x,y) ((x)>(y)?(x):(y)) #define VAB_CLAMP(x,min,max) VAB_MIN(VAB_MAX(x,min),max) +static int read_vabcfg_file(STREAMFILE* sf, int program, int tone, int* note, int* fine, int* uselimits) { + char filename[PATH_LIMIT]; + off_t txt_offset, file_size; + STREAMFILE* sf_cfg = NULL; + size_t file_len, key_len; + + sf_cfg = open_streamfile_by_filename(sf, ".vab_config"); + if (!sf_cfg) goto fail; + + get_streamfile_filename(sf, filename, sizeof(filename)); + + txt_offset = read_bom(sf_cfg); + file_size = get_streamfile_size(sf_cfg); + file_len = strlen(filename); + + /* read lines and find target filename, format is (filename): value1, ... valueN */ + while (txt_offset < file_size) { + char line[0x2000]; + char key[PATH_LIMIT] = { 0 }, val[0x2000] = { 0 }; + int ok, bytes_read, line_ok; + int cfg_program, cfg_tone, cfg_note, cfg_fine, cfg_limits; + + bytes_read = read_line(line, sizeof(line), txt_offset, sf_cfg, &line_ok); + if (!line_ok) goto fail; + + txt_offset += bytes_read; + + /* get key/val (ignores lead/trailing spaces, stops at comment/separator) */ + ok = sscanf(line, " %[^\t#:] : %[^\t#\r\n] ", key, val); + if (ok != 2) /* ignore line if no key=val (comment or garbage) */ + continue; + + if (key[0] == '*') { + key_len = strlen(key); + if (file_len < key_len) + continue; + + if (strcmp(filename + (file_len - key_len + 1), key + 1) != 0) + continue; + } else { + if (strcmp(filename, key) != 0) + continue; + } + + ok = sscanf(val, "%d , %d , %d , %d , %d", &cfg_program, &cfg_tone, &cfg_note, &cfg_fine, &cfg_limits); + if (ok != 5) + continue; + + if (cfg_program >= 0 && program != cfg_program) + continue; + + if (cfg_tone >= 0 && tone != cfg_tone) + continue; + + *note = cfg_note; + *fine = cfg_fine; + *uselimits = cfg_limits; + + close_streamfile(sf_cfg); + return 1; + } + +fail: + close_streamfile(sf_cfg); + return 0; +} + /* .VAB - standard PS1 bank format */ VGMSTREAM* init_vgmstream_vab(STREAMFILE* sf) { uint16_t programs, wave_num, pitch; @@ -14,6 +81,7 @@ VGMSTREAM* init_vgmstream_vab(STREAMFILE* sf) { off_t programs_off, tones_off, waves_off, entry_off, data_offset; size_t data_size; int target_subsong = sf->stream_index, is_vh = 0, program_num, tone_num, total_subsongs, + note, fine, uselimits, channels, loop_flag, loop_start = 0, loop_end = 0; int i; STREAMFILE* sf_data = NULL; @@ -76,8 +144,16 @@ VGMSTREAM* init_vgmstream_vab(STREAMFILE* sf) { max_note = read_u8(entry_off + 0x07, sf); wave_num = read_u16le(entry_off + 0x16, sf); + if (read_vabcfg_file(sf, program_num, tone_num, ¬e, &fine, &uselimits)) { + if (uselimits) + note = VAB_CLAMP(note, min_note, max_note); + } else { + note = VAB_CLAMP(60, min_note, max_note); + fine = 0; + } + /* play default note */ - pitch = SsPitchFromNote(VAB_CLAMP(60, min_note, max_note), 0, center, shift); + pitch = SsPitchFromNote(note, fine, center, shift); data_offset = is_vh ? 0x00 : (waves_off + 256 * 0x02); for (i = 0; i < wave_num; i++) {