diff options
author | Wohlstand <admin@wohlnet.ru> | 2017-07-30 05:06:18 +0300 |
---|---|---|
committer | Wohlstand <admin@wohlnet.ru> | 2017-07-30 05:06:18 +0300 |
commit | b15f8552d629021c3cadef3e7afcae2fa98dad1b (patch) | |
tree | aa72effc4fb6690c73428fa26d1f8615383a6311 /src/gen_adldata | |
parent | fd80dc0af0617a17f4604a9a12592398476eb5ed (diff) | |
download | libADLMIDI-b15f8552d629021c3cadef3e7afcae2fa98dad1b.tar.gz libADLMIDI-b15f8552d629021c3cadef3e7afcae2fa98dad1b.tar.bz2 libADLMIDI-b15f8552d629021c3cadef3e7afcae2fa98dad1b.zip |
Improve gen_adldata program
- Now it caches all generated data, so, we won't have to re-calculate same
- File is writing by gen_adldata nor by stdout forward
- Instead of hardcoded list of banks, I made the INI file which declares list of banks to generate
- Add simple validators to tell which bank is absense and can't be loaded
- Split code of gen_adldata.cc into multiple files of different role
Diffstat (limited to 'src/gen_adldata')
-rw-r--r-- | src/gen_adldata/file_formats/load_ail.h | 102 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_bisqwit.h | 53 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_bnk.h | 152 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_bnk2.h | 102 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_ibk.h | 72 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_jv.h | 105 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_op2.h | 148 | ||||
-rw-r--r-- | src/gen_adldata/file_formats/load_tmb.h | 68 | ||||
-rw-r--r-- | src/gen_adldata/gen_adldata.cc | 1704 | ||||
-rw-r--r-- | src/gen_adldata/gen_adldata.pro | 36 | ||||
-rw-r--r-- | src/gen_adldata/ini/IniProcessor.pri | 10 | ||||
-rw-r--r-- | src/gen_adldata/ini/ini_processing.cpp | 1385 | ||||
-rw-r--r-- | src/gen_adldata/ini/ini_processing.h | 677 | ||||
-rw-r--r-- | src/gen_adldata/ini/ini_processing_variant.h | 243 | ||||
-rw-r--r-- | src/gen_adldata/measurer.cpp | 543 | ||||
-rw-r--r-- | src/gen_adldata/measurer.h | 105 | ||||
-rw-r--r-- | src/gen_adldata/midi_inst_list.h | 5 | ||||
-rw-r--r-- | src/gen_adldata/progs_cache.cpp | 106 | ||||
-rw-r--r-- | src/gen_adldata/progs_cache.h | 85 | ||||
-rw-r--r-- | src/gen_adldata/scrapped.txt | 254 |
20 files changed, 4631 insertions, 1324 deletions
diff --git a/src/gen_adldata/file_formats/load_ail.h b/src/gen_adldata/file_formats/load_ail.h new file mode 100644 index 0000000..3a0e975 --- /dev/null +++ b/src/gen_adldata/file_formats/load_ail.h @@ -0,0 +1,102 @@ +#ifndef LOAD_AIL_H +#define LOAD_AIL_H + +#include "../progs_cache.h" +#include "../midi_inst_list.h" + +static bool LoadMiles(const char *fn, unsigned bank, const char *prefix) +{ + #ifdef HARD_BANKS + writeIni("AIL", fn, prefix, bank, INI_Both); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + for(unsigned a = 0; a < 2000; ++a) + { + unsigned gm_patch = data[a * 6 + 0]; + unsigned gm_bank = data[a * 6 + 1]; + unsigned offset = *(unsigned *)&data[a * 6 + 2]; + + if(gm_patch == 0xFF) + break; + + int gmno = gm_bank == 0x7F ? int(gm_patch + 0x80) : int(gm_patch); + int midi_index = gmno < 128 ? gmno + : gmno < 128 + 35 ? -1 + : gmno < 128 + 88 ? gmno - 35 + : -1; + unsigned length = data[offset] + data[offset + 1] * 256; + signed char notenum = ((signed char)data[offset + 2]); + + /*printf("%02X %02X %08X ", gmnumber,gmnumber2, offset); + for(unsigned b=0; b<length; ++b) + { + if(b > 3 && (b-3)%11 == 0) printf("\n "); + printf("%02X ", data[offset+b]); + } + printf("\n");*/ + + if(gm_bank != 0 && gm_bank != 0x7F) + continue; + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, + (gmno < 128 ? 'M' : 'P'), gmno & 127); + + insdata tmp[200]; + + const unsigned inscount = (length - 3) / 11; + for(unsigned i = 0; i < inscount; ++i) + { + unsigned o = offset + 3 + i * 11; + tmp[i].finetune = (gmno < 128 && i == 0) ? notenum : 0; + tmp[i].diff = false; + tmp[i].data[0] = data[o + 0]; // 20 + tmp[i].data[8] = data[o + 1]; // 40 (vol) + tmp[i].data[2] = data[o + 2]; // 60 + tmp[i].data[4] = data[o + 3]; // 80 + tmp[i].data[6] = data[o + 4]; // E0 + tmp[i].data[1] = data[o + 6]; // 23 + tmp[i].data[9] = data[o + 7]; // 43 (vol) + tmp[i].data[3] = data[o + 8]; // 63 + tmp[i].data[5] = data[o + 9]; // 83 + tmp[i].data[7] = data[o + 10]; // E3 + + unsigned fb_c = data[offset + 3 + 5]; + tmp[i].data[10] = uint8_t(fb_c); + if(i == 1) + { + tmp[0].data[10] = fb_c & 0x0F; + tmp[1].data[10] = uint8_t((fb_c & 0x0E) | (fb_c >> 7)); + } + } + if(inscount == 1) + tmp[1] = tmp[0]; + + if(inscount <= 2) + { + struct ins tmp2; + tmp2.notenum = gmno < 128 ? 0 : (unsigned char)notenum; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + std::string name; + if(midi_index >= 0) name = std::string(1, '\377') + MidiInsName[midi_index]; + size_t resno = InsertIns(tmp[0], tmp[1], tmp2, name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + } + return true; +} + +#endif // LOAD_AIL_H diff --git a/src/gen_adldata/file_formats/load_bisqwit.h b/src/gen_adldata/file_formats/load_bisqwit.h new file mode 100644 index 0000000..98fcd83 --- /dev/null +++ b/src/gen_adldata/file_formats/load_bisqwit.h @@ -0,0 +1,53 @@ +#ifndef LOAD_BISQWIT_H +#define LOAD_BISQWIT_H + +#include "../progs_cache.h" +#include "../midi_inst_list.h" + +static bool LoadBisqwit(const char *fn, unsigned bank, const char *prefix) +{ + #ifdef HARD_BANKS + writeIni("Bisqwit", fn, prefix, bank, INI_Both); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + + for(uint32_t a = 0; a < 256; ++a) + { + //unsigned offset = a * 25; + uint32_t gmno = a; + int32_t midi_index = gmno < 128 ? int32_t(gmno) + : gmno < 128 + 35 ? -1 + : gmno < 128 + 88 ? int32_t(gmno - 35) + : -1; + + struct ins tmp2; + tmp2.notenum = (uint8_t)std::fgetc(fp); + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + insdata tmp[2]; + for(int side = 0; side < 2; ++side) + { + tmp[side].finetune = (int8_t)std::fgetc(fp); + tmp[side].diff = false; + if(std::fread(tmp[side].data, 1, 11, fp) != 11) + return false; + } + + std::string name; + if(midi_index >= 0) name = std::string(1, '\377') + MidiInsName[midi_index]; + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, + (gmno < 128 ? 'M' : 'P'), gmno & 127); + + size_t resno = InsertIns(tmp[0], tmp[1], tmp2, name, name2); + SetBank(bank, gmno, resno); + } + std::fclose(fp); + return true; +} + +#endif // LOAD_BISQWIT_H diff --git a/src/gen_adldata/file_formats/load_bnk.h b/src/gen_adldata/file_formats/load_bnk.h new file mode 100644 index 0000000..2b6e1c4 --- /dev/null +++ b/src/gen_adldata/file_formats/load_bnk.h @@ -0,0 +1,152 @@ +#ifndef LOAD_BNK_H +#define LOAD_BNK_H + +#include "../progs_cache.h" + +#include <cstdio> +#include <cstdint> +#include <string> + +static bool LoadBNK(const char *fn, unsigned bank, const char *prefix, bool is_fat, bool percussive) +{ + #ifdef HARD_BANKS + writeIni("HMI", fn, prefix, bank, percussive ? INI_Drums : INI_Melodic); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + /*printf("%s:\n", fn);*/ + //unsigned short version = *(short*)&data[0]; // major,minor (2 bytes) + // "ADLIB-" (6 bytes) + uint16_t entries_used = *(uint16_t *)&data[8]; // entries used + //unsigned short total_entries = *(short*)&data[10]; // total entries + unsigned name_offset = *(unsigned *)&data[12]; // name_offset + unsigned data_offset = *(unsigned *)&data[16]; // data_offset + // 16..23: 8 byte sof filler + /*printf("version=%u %u %u %u %u\n", + version, entries_used,total_entries,name_offset,data_offset);*/ + + for(unsigned n = 0; n < entries_used; ++n) + { + const size_t offset1 = name_offset + n * 12; + + unsigned short data_index = data[offset1 + 0] + data[offset1 + 1] * 256; + unsigned char usage_flag = data[offset1 + 2]; + std::string name; + for(unsigned p = 0; p < 9; ++p) + { + if(data[offset1 + 3 + p] == '\0') break; + name += char(data[offset1 + 3 + p]); + } + + const size_t offset2 = data_offset + data_index * 30; + //const unsigned char mode = data[offset2+0]; + const unsigned char voice_num = data[offset2 + 1]; + const unsigned char *op1 = &data[offset2 + 2]; // 13 bytes + const unsigned char *op2 = &data[offset2 + 15]; + const unsigned char waveform_mod = data[offset2 + 28]; + const unsigned char waveform_car = data[offset2 + 29]; + + /*printf("%5d %3d %8s mode=%02X voice=%02X: ", data_index,usage_flag, name.c_str(), + mode,voice_num);*/ + + int gmno = int(is_fat + ? ((n & 127) + percussive * 128) + : (n + percussive * 128)); + + if(is_fat && percussive) + { + if(name[2] == 'O' + || name[2] == 'S') + { + gmno = 128 + std::stoi(name.substr(3)); + } + } + + char name2[512]; + if(is_fat) + sprintf(name2, "%s%c%u", prefix, percussive ? 'P' : 'M', gmno & 127); + else + sprintf(name2, "%s%u", prefix, n); + + insdata tmp; + tmp.data[0] = uint8_t( + (op1[ 9] << 7) // TREMOLO FLAG + + (op1[10] << 6) // VIBRATO FLAG + + (op1[ 5] << 5) // SUSTAIN FLAG + + (op1[11] << 4) // SCALING FLAG + + op1[ 1]); // FREQMUL + + tmp.data[1] = uint8_t((op2[ 9] << 7) + + (op2[10] << 6) + + (op2[ 5] << 5) + + (op2[11] << 4) + + op2[ 1]); + tmp.data[2] = op1[3] * 0x10 + op1[6]; // ATTACK, DECAY + tmp.data[3] = op2[3] * 0x10 + op2[6]; + tmp.data[4] = op1[4] * 0x10 + op1[7]; // SUSTAIN, RELEASE + tmp.data[5] = op2[4] * 0x10 + op2[7]; + tmp.data[6] = waveform_mod; + tmp.data[7] = waveform_car; + tmp.data[8] = op1[0] * 0x40 + op1[8]; // KSL , LEVEL + tmp.data[9] = op2[0] * 0x40 + op2[8]; // KSL , LEVEL + tmp.data[10] = op1[2] * 2 + op1[12]; // FEEDBACK, ADDITIVEFLAG + tmp.finetune = 0; + tmp.diff = false; + // Note: op2[2] and op2[12] are unused and contain garbage. + ins tmp2; + tmp2.notenum = is_fat ? voice_num : (percussive ? usage_flag : 0); + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + if(is_fat) tmp.data[10] ^= 1; + + size_t resno = InsertIns(tmp, tmp, tmp2, std::string(1, '\377') + name, name2); + + if(!is_fat) + { + SetBank(bank, (unsigned int)gmno, resno); + } + else + { + if(name[2] == 'O' || name[1] == 'M') SetBank(bank + 0, (unsigned int)gmno, resno); + if(name[2] == 'S' || name[1] == 'M') SetBank(bank + 1, (unsigned int)gmno, resno); + } + + /* + for(unsigned p=0; p<30; ++p) + { + unsigned char value = data[offset2+p]; + if(value > maxvalues[p]) maxvalues[p] = value; + + #define dot(maxval) if(value<=maxval) printf("."); else printf("?[%u]%X",p,value); + + { + //if(p==6 || p==7 || p==19||p==20) value=15-value; + + if(p==4 || p==10 || p==17 || p==23)// || p==25) + printf(" %2X", value); + else + printf(" %X", value); + } + nl:; + //if(p == 12) printf("\n%*s", 22, ""); + //if(p == 25) printf("\n%*s", 22, ""); + } + printf("\n"); + */ + } + return true; +} + +#endif // LOAD_BNK_H diff --git a/src/gen_adldata/file_formats/load_bnk2.h b/src/gen_adldata/file_formats/load_bnk2.h new file mode 100644 index 0000000..84ba06c --- /dev/null +++ b/src/gen_adldata/file_formats/load_bnk2.h @@ -0,0 +1,102 @@ +#ifndef LOAD_BNK2_H +#define LOAD_BNK2_H + +#include "../progs_cache.h" + +static bool LoadBNK2(const char *fn, unsigned bank, const char *prefix, + const std::string &melo_filter, + const std::string &perc_filter) +{ + #ifdef HARD_BANKS + writeIni("AdLibGold", fn, prefix, bank, INI_Both, melo_filter.c_str(), perc_filter.c_str()); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + unsigned short ins_entries = *(unsigned short *)&data[28 + 2 + 10]; + unsigned char *records = &data[48]; + + for(unsigned n = 0; n < ins_entries; ++n) + { + const size_t offset1 = n * 28; + int used = records[offset1 + 15]; + //int attrib = *(int*)&records[offset1 + 16]; + int offset2 = *(int *)&records[offset1 + 20]; + if(used == 0) continue; + + std::string name; + for(unsigned p = 0; p < 12; ++p) + { + if(records[offset1 + 3 + p] == '\0') break; + name += char(records[offset1 + 3 + p]); + } + + int gmno = 0; + if(name.substr(0, melo_filter.size()) == melo_filter) + gmno = std::stoi(name.substr(melo_filter.size())); + else if(name.substr(0, perc_filter.size()) == perc_filter) + gmno = std::stoi(name.substr(perc_filter.size())) + 128; + else + continue; + + const unsigned char *insdata = &data[size_t(offset2)]; + const unsigned char *ops[4] = { insdata + 0, insdata + 6, insdata + 12, insdata + 18 }; + unsigned char C4xxxFFFC = insdata[24]; + unsigned char xxP24NNN = insdata[25]; + unsigned char TTTTTTTT = insdata[26]; + //unsigned char xxxxxxxx = insdata[27]; + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, (gmno & 128) ? 'P' : 'M', gmno & 127); + + struct insdata tmp[2]; + for(unsigned a = 0; a < 2; ++a) + { + tmp[a].data[0] = ops[a * 2 + 0][0]; + tmp[a].data[1] = ops[a * 2 + 1][0]; + tmp[a].data[2] = ops[a * 2 + 0][2]; + tmp[a].data[3] = ops[a * 2 + 1][2]; + tmp[a].data[4] = ops[a * 2 + 0][3]; + tmp[a].data[5] = ops[a * 2 + 1][3]; + tmp[a].data[6] = ops[a * 2 + 0][4] & 0x07; + tmp[a].data[7] = ops[a * 2 + 1][4] & 0x07; + tmp[a].data[8] = ops[a * 2 + 0][1]; + tmp[a].data[9] = ops[a * 2 + 1][1]; + tmp[a].finetune = (int8_t)TTTTTTTT; + tmp[a].diff = false; + } + tmp[0].data[10] = C4xxxFFFC & 0x0F; + tmp[1].data[10] = (tmp[0].data[10] & 0x0E) | (C4xxxFFFC >> 7); + + ins tmp2; + tmp2.notenum = (gmno & 128) ? 35 : 0; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + if(xxP24NNN & 8) + { + // dual-op + size_t resno = InsertIns(tmp[0], tmp[1], tmp2, std::string(1, '\377') + name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + else + { + // single-op + size_t resno = InsertIns(tmp[0], tmp[0], tmp2, std::string(1, '\377') + name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + } + return true; +} + +#endif // LOAD_BNK2_H diff --git a/src/gen_adldata/file_formats/load_ibk.h b/src/gen_adldata/file_formats/load_ibk.h new file mode 100644 index 0000000..cb5505e --- /dev/null +++ b/src/gen_adldata/file_formats/load_ibk.h @@ -0,0 +1,72 @@ +#ifndef LOAD_IBK_H +#define LOAD_IBK_H + +#include "../progs_cache.h" + +static bool LoadIBK(const char *fn, unsigned bank, const char *prefix, bool percussive) +{ + #ifdef HARD_BANKS + writeIni("IBK", fn, prefix, bank, percussive ? INI_Drums : INI_Melodic); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + unsigned offs1_base = 0x804, offs1_len = 9; + unsigned offs2_base = 0x004, offs2_len = 16; + + for(unsigned a = 0; a < 128; ++a) + { + unsigned offset1 = offs1_base + a * offs1_len; + unsigned offset2 = offs2_base + a * offs2_len; + + std::string name; + for(unsigned p = 0; p < 9; ++p) + if(data[offset1] != '\0') + name += char(data[offset1 + p]); + + int gmno = int(a + 128 * percussive); + /*int midi_index = gmno < 128 ? gmno + : gmno < 128+35 ? -1 + : gmno < 128+88 ? gmno-35 + : -1;*/ + char name2[512]; + sprintf(name2, "%s%c%u", prefix, + (gmno < 128 ? 'M' : 'P'), gmno & 127); + + insdata tmp; + tmp.data[0] = data[offset2 + 0]; + tmp.data[1] = data[offset2 + 1]; + tmp.data[8] = data[offset2 + 2]; + tmp.data[9] = data[offset2 + 3]; + tmp.data[2] = data[offset2 + 4]; + tmp.data[3] = data[offset2 + 5]; + tmp.data[4] = data[offset2 + 6]; + tmp.data[5] = data[offset2 + 7]; + tmp.data[6] = data[offset2 + 8]; + tmp.data[7] = data[offset2 + 9]; + tmp.data[10] = data[offset2 + 10]; + // [+11] seems to be used also, what is it for? + tmp.finetune = 0; + tmp.diff = false; + struct ins tmp2; + tmp2.notenum = gmno < 128 ? 0 : 35; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + size_t resno = InsertIns(tmp, tmp, tmp2, std::string(1, '\377') + name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + return true; +} + +#endif // LOAD_IBK_H diff --git a/src/gen_adldata/file_formats/load_jv.h b/src/gen_adldata/file_formats/load_jv.h new file mode 100644 index 0000000..b48013b --- /dev/null +++ b/src/gen_adldata/file_formats/load_jv.h @@ -0,0 +1,105 @@ +#ifndef LOAD_JV_H +#define LOAD_JV_H + +#include "../progs_cache.h" +#include "../midi_inst_list.h" + +static bool LoadJunglevision(const char *fn, unsigned bank, const char *prefix) +{ + #ifdef HARD_BANKS + writeIni("Junglevision", fn, prefix, bank, INI_Both); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + uint16_t ins_count = uint16_t(data[0x20] + (data[0x21] << 8)); + uint16_t drum_count = uint16_t(data[0x22] + (data[0x23] << 8)); + uint16_t first_ins = uint16_t(data[0x24] + (data[0x25] << 8)); + uint16_t first_drum = uint16_t(data[0x26] + (data[0x27] << 8)); + + unsigned total_ins = ins_count + drum_count; + + for(unsigned a = 0; a < total_ins; ++a) + { + unsigned offset = 0x28 + a * 0x18; + unsigned gmno = (a < ins_count) ? (a + first_ins) : (a + first_drum); + int midi_index = gmno < 128 ? int(gmno) + : gmno < 128 + 35 ? -1 + : gmno < 128 + 88 ? int(gmno - 35) + : -1; + + insdata tmp[2]; + + tmp[0].data[0] = data[offset + 2]; + tmp[0].data[1] = data[offset + 8]; + tmp[0].data[2] = data[offset + 4]; + tmp[0].data[3] = data[offset + 10]; + tmp[0].data[4] = data[offset + 5]; + tmp[0].data[5] = data[offset + 11]; + tmp[0].data[6] = data[offset + 6]; + tmp[0].data[7] = data[offset + 12]; + tmp[0].data[8] = data[offset + 3]; + tmp[0].data[9] = data[offset + 9]; + tmp[0].data[10] = data[offset + 7] & 0x0F;//~0x30; + tmp[0].finetune = 0; + tmp[0].diff = false; + + tmp[1].data[0] = data[offset + 2 + 11]; + tmp[1].data[1] = data[offset + 8 + 11]; + tmp[1].data[2] = data[offset + 4 + 11]; + tmp[1].data[3] = data[offset + 10 + 11]; + tmp[1].data[4] = data[offset + 5 + 11]; + tmp[1].data[5] = data[offset + 11 + 11]; + tmp[1].data[6] = data[offset + 6 + 11]; + tmp[1].data[7] = data[offset + 12 + 11]; + tmp[1].data[8] = data[offset + 3 + 11]; + tmp[1].data[9] = data[offset + 9 + 11]; + tmp[1].data[10] = data[offset + 7 + 11] & 0x0F;//~0x30; + tmp[1].finetune = 0; + tmp[1].diff = false; + + struct ins tmp2; + tmp2.notenum = data[offset + 1]; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + while(tmp2.notenum && tmp2.notenum < 20) + { + tmp2.notenum += 12; + tmp[0].finetune -= 12; + tmp[1].finetune -= 12; + } + + std::string name; + if(midi_index >= 0) name = std::string(1, '\377') + MidiInsName[midi_index]; + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, + (gmno < 128 ? 'M' : 'P'), gmno & 127); + + if(!data[offset]) + { + size_t resno = InsertIns(tmp[0], tmp[0], tmp2, name, name2); + SetBank(bank, gmno, resno); + } + else // Double instrument + { + size_t resno = InsertIns(tmp[0], tmp[1], tmp2, name, name2); + SetBank(bank, gmno, resno); + } + } + return true; +} + +#endif // LOAD_JV_H + diff --git a/src/gen_adldata/file_formats/load_op2.h b/src/gen_adldata/file_formats/load_op2.h new file mode 100644 index 0000000..0d95f59 --- /dev/null +++ b/src/gen_adldata/file_formats/load_op2.h @@ -0,0 +1,148 @@ +#ifndef LOAD_OP2_H +#define LOAD_OP2_H + +#include "../progs_cache.h" + +#pragma pack(push, 1) +struct Doom_OPL2instrument +{ + unsigned char trem_vibr_1; /* OP 1: tremolo/vibrato/sustain/KSR/multi */ + unsigned char att_dec_1; /* OP 1: attack rate/decay rate */ + unsigned char sust_rel_1; /* OP 1: sustain level/release rate */ + unsigned char wave_1; /* OP 1: waveform select */ + unsigned char scale_1; /* OP 1: key scale level */ + unsigned char level_1; /* OP 1: output level */ + unsigned char feedback; /* feedback/AM-FM (both operators) */ + unsigned char trem_vibr_2; /* OP 2: tremolo/vibrato/sustain/KSR/multi */ + unsigned char att_dec_2; /* OP 2: attack rate/decay rate */ + unsigned char sust_rel_2; /* OP 2: sustain level/release rate */ + unsigned char wave_2; /* OP 2: waveform select */ + unsigned char scale_2; /* OP 2: key scale level */ + unsigned char level_2; /* OP 2: output level */ + unsigned char unused; + short basenote; /* base note offset */ +} __attribute__((packed)); +#pragma pack(pop) + +struct Doom_opl_instr +{ + unsigned short flags; +#define FL_FIXED_PITCH 0x0001 // note has fixed pitch (drum note) +#define FL_UNKNOWN 0x0002 // ??? (used in instrument #65 only) +#define FL_DOUBLE_VOICE 0x0004 // use two voices instead of one + + unsigned char finetune; + unsigned char note; + struct Doom_OPL2instrument patchdata[2]; +} __attribute__((packed)); + + +static bool LoadDoom(const char *fn, unsigned bank, const char *prefix) +{ + #ifdef HARD_BANKS + writeIni("OP2", fn, prefix, bank, INI_Both); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + for(unsigned a = 0; a < 175; ++a) + { + const size_t offset1 = 0x18A4 + a * 32; + const size_t offset2 = 8 + a * 36; + + std::string name; + for(unsigned p = 0; p < 32; ++p) + if(data[offset1] != '\0') + name += char(data[offset1 + p]); + + //printf("%3d %3d %3d %8s: ", a,b,c, name.c_str()); + int gmno = int(a < 128 ? a : ((a | 128) + 35)); + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, (gmno < 128 ? 'M' : 'P'), gmno & 127); + + Doom_opl_instr &ins = *(Doom_opl_instr *) &data[offset2]; + + insdata tmp[2]; + tmp[0].diff = false; + tmp[1].diff = true; + for(unsigned index = 0; index < 2; ++index) + { + const Doom_OPL2instrument &src = ins.patchdata[index]; + tmp[index].data[0] = src.trem_vibr_1; + tmp[index].data[1] = src.trem_vibr_2; + tmp[index].data[2] = src.att_dec_1; + tmp[index].data[3] = src.att_dec_2; + tmp[index].data[4] = src.sust_rel_1; + tmp[index].data[5] = src.sust_rel_2; + tmp[index].data[6] = src.wave_1; + tmp[index].data[7] = src.wave_2; + tmp[index].data[8] = src.scale_1 | src.level_1; + tmp[index].data[9] = src.scale_2 | src.level_2; + tmp[index].data[10] = src.feedback; + tmp[index].finetune = int8_t(src.basenote + 12); + } + struct ins tmp2; + tmp2.notenum = ins.note; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + while(tmp2.notenum && tmp2.notenum < 20) + { + tmp2.notenum += 12; + tmp[0].finetune -= 12; + tmp[1].finetune -= 12; + } + + if(!(ins.flags & 4)) + { + size_t resno = InsertIns(tmp[0], tmp[0], tmp2, std::string(1, '\377') + name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + else // Double instrument + { + tmp2.pseudo4op = true; + tmp2.fine_tune = (((double)ins.finetune - 128.0) * 15.625) / 1000.0; + if(ins.finetune == 129) + tmp2.fine_tune = 0.000025; + else if(ins.finetune == 127) + tmp2.fine_tune = -0.000025; + //printf("/*DOOM FINE TUNE (flags %000X instrument is %d) IS %d -> %lf*/\n", ins.flags, a, ins.finetune, tmp2.fine_tune); + size_t resno = InsertIns(tmp[0], tmp[1], tmp2, std::string(1, '\377') + name, name2); + SetBank(bank, (unsigned int)gmno, resno); + } + + /*const Doom_OPL2instrument& A = ins.patchdata[0]; + const Doom_OPL2instrument& B = ins.patchdata[1]; + printf( + "flags:%04X fine:%02X note:%02X\n" + "{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n" + " t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d}\n" + "{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n" + " t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d} ", + ins.flags, ins.finetune, ins.note, + A.trem_vibr_1, A.att_dec_1, A.sust_rel_1, + A.wave_1, A.scale_1, A.level_1, A.feedback, + A.trem_vibr_2, A.att_dec_2, A.sust_rel_2, + A.wave_2, A.scale_2, A.level_2, A.unused, A.basenote, + B.trem_vibr_1, B.att_dec_1, B.sust_rel_1, + B.wave_1, B.scale_1, B.level_1, B.feedback, + B.trem_vibr_2, B.att_dec_2, B.sust_rel_2, + B.wave_2, B.scale_2, B.level_2, B.unused, B.basenote); + printf(" %s VS %s\n", name.c_str(), MidiInsName[a]); + printf("------------------------------------------------------------\n");*/ + } + return true; +} + +#endif // LOAD_OP2_H diff --git a/src/gen_adldata/file_formats/load_tmb.h b/src/gen_adldata/file_formats/load_tmb.h new file mode 100644 index 0000000..2b07c65 --- /dev/null +++ b/src/gen_adldata/file_formats/load_tmb.h @@ -0,0 +1,68 @@ +#ifndef LOAD_TMB_H +#define LOAD_TMB_H + +#include "../progs_cache.h" +#include "../midi_inst_list.h" + +static bool LoadTMB(const char *fn, unsigned bank, const char *prefix) +{ + #ifdef HARD_BANKS + writeIni("TMB", fn, prefix, bank, INI_Both); + #endif + FILE *fp = std::fopen(fn, "rb"); + if(!fp) + return false; + std::fseek(fp, 0, SEEK_END); + std::vector<unsigned char> data(size_t(std::ftell(fp))); + std::rewind(fp); + if(std::fread(&data[0], 1, data.size(), fp) != data.size()) + { + std::fclose(fp); + return false; + } + std::fclose(fp); + + for(unsigned a = 0; a < 256; ++a) + { + unsigned offset = a * 0x0D; + unsigned gmno = a; + int midi_index = gmno < 128 ? int(gmno) + : gmno < 128 + 35 ? -1 + : gmno < 128 + 88 ? int(gmno - 35) + : -1; + + insdata tmp; + + tmp.data[0] = data[offset + 0]; + tmp.data[1] = data[offset + 1]; + tmp.data[2] = data[offset + 4]; + tmp.data[3] = data[offset + 5]; + tmp.data[4] = data[offset + 6]; + tmp.data[5] = data[offset + 7]; + tmp.data[6] = data[offset + 8]; + tmp.data[7] = data[offset + 9]; + tmp.data[8] = data[offset + 2]; + tmp.data[9] = data[offset + 3]; + tmp.data[10] = data[offset + 10]; + tmp.finetune = 0; //data[offset + 12]; + tmp.diff = false; + + struct ins tmp2; + tmp2.notenum = data[offset + 11]; + tmp2.pseudo4op = false; + tmp2.fine_tune = 0.0; + + std::string name; + if(midi_index >= 0) name = std::string(1, '\377') + MidiInsName[midi_index]; + + char name2[512]; + sprintf(name2, "%s%c%u", prefix, + (gmno < 128 ? 'M' : 'P'), gmno & 127); + + size_t resno = InsertIns(tmp, tmp, tmp2, name, name2); + SetBank(bank, gmno, resno); + } + return true; +} + +#endif // LOAD_TMB_H diff --git a/src/gen_adldata/gen_adldata.cc b/src/gen_adldata/gen_adldata.cc index 52ac5e7..80c4910 100644 --- a/src/gen_adldata/gen_adldata.cc +++ b/src/gen_adldata/gen_adldata.cc @@ -2,1321 +2,312 @@ //typedef struct vswprintf {} swprintf; //#endif #include <cstdio> -#include <vector> #include <string> -#include <algorithm> #include <cstring> -#include <cmath> -#include <map> -#include <set> -#include "midi_inst_list.h" - -std::map<unsigned, - std::map<unsigned, unsigned> - > Correlate; -unsigned maxvalues[30] = { 0 }; - -struct insdata -{ - unsigned char data[11]; - signed char finetune; - bool diff; - bool operator==(const insdata& b) const - { - return std::memcmp(data, b.data, 11) == 0 && finetune == b.finetune && diff == b.diff; - } - bool operator< (const insdata& b) const - { - int c = std::memcmp(data, b.data, 11); - if(c != 0) return c < 0; - if(finetune != b.finetune) return finetune < b.finetune; - if(diff != b.diff) return (!diff)==(b.diff); - return 0; - } - bool operator!=(const insdata& b) const { return !operator==(b); } -}; -struct ins -{ - size_t insno1, insno2; - unsigned char notenum; - bool pseudo4op; - double fine_tune; - - bool operator==(const ins& b) const - { - return notenum == b.notenum - && insno1 == b.insno1 - && insno2 == b.insno2 - && pseudo4op == b.pseudo4op; - } - bool operator< (const ins& b) const - { - if(insno1 != b.insno1) return insno1 < b.insno1; - if(insno2 != b.insno2) return insno2 < b.insno2; - if(notenum != b.notenum) return notenum < b.notenum; - if(pseudo4op != b.pseudo4op) return pseudo4op < b.pseudo4op; - return 0; - } - bool operator!=(const ins& b) const { return !operator==(b); } -}; +#include "ini/ini_processing.h" -static std::map<insdata, std::pair<size_t, std::set<std::string> > > insdatatab; +#include "progs_cache.h" +#include "measurer.h" -static std::map<ins, std::pair<size_t, std::set<std::string> > > instab; -static std::map<unsigned, std::map<unsigned, size_t> > progs; +#include "midi_inst_list.h" -static void SetBank(unsigned bank, unsigned patch, size_t insno) -{ - progs[bank][patch] = insno+1; -} +#include "file_formats/load_ail.h" +#include "file_formats/load_bisqwit.h" +#include "file_formats/load_bnk2.h" +#include "file_formats/load_bnk.h" +#include "file_formats/load_ibk.h" +#include "file_formats/load_jv.h" +#include "file_formats/load_op2.h" +#include "file_formats/load_tmb.h" -static size_t InsertIns( - const insdata& id, - const insdata& id2, - ins& in, - const std::string& name, - const std::string& name2 = "") +int main(int argc, char**argv) { - if(true) - { - std::map<insdata, std::pair<size_t,std::set<std::string> > >::iterator - i = insdatatab.lower_bound(id); - - size_t insno = ~0; - if(i == insdatatab.end() || i->first != id) - { - std::pair<insdata, std::pair<size_t,std::set<std::string> > > res; - res.first = id; - res.second.first = insdatatab.size(); - if(!name.empty()) res.second.second.insert(name); - if(!name2.empty()) res.second.second.insert(name2); - insdatatab.insert(i, res); - insno = res.second.first; - } - else + if(argc == 1) { - if(!name.empty()) i->second.second.insert(name); - if(!name2.empty()) i->second.second.insert(name2); - insno = i->second.first; + printf("Usage:\n" + "\n" + "bin/gen_adldata src/adldata.cpp\n" + "\n"); + return 1; } - in.insno1 = insno; - } - if( id != id2 ) - { - std::map<insdata, std::pair<size_t,std::set<std::string> > >::iterator - i = insdatatab.lower_bound(id2); - size_t insno2 = ~0; - if(i == insdatatab.end() || i->first != id2) - { - std::pair<insdata, std::pair<size_t,std::set<std::string> > > res; - res.first = id2; - res.second.first = insdatatab.size(); - if(!name.empty()) res.second.second.insert(name); - if(!name2.empty()) res.second.second.insert(name2); - insdatatab.insert(i, res); - insno2 = res.second.first; - } - else - { - if(!name.empty()) i->second.second.insert(name); - if(!name2.empty()) i->second.second.insert(name2); - insno2 = i->second.first; - } - in.insno2 = insno2; - } - else - in.insno2 = in.insno1; - + const char *outFile_s = argv[1]; - { - std::map<ins, std::pair<size_t,std::set<std::string> > >::iterator - i = instab.lower_bound(in); - - size_t resno = ~0; - if(i == instab.end() || i->first != in) - { - std::pair<ins, std::pair<size_t,std::set<std::string> > > res; - res.first = in; - res.second.first = instab.size(); - if(!name.empty()) res.second.second.insert(name); - if(!name2.empty()) res.second.second.insert(name2); - instab.insert(i, res); - resno = res.second.first; - } - else + FILE *outFile = fopen(outFile_s, "w"); + if(!outFile) { - if(!name.empty()) i->second.second.insert(name); - if(!name2.empty()) i->second.second.insert(name2); - resno = i->second.first; + fprintf(stderr, "Can't open %s file for write!\n", outFile_s); + return 1; } - return resno; - } -} -// Create silent 'nosound' instrument -size_t InsertNoSoundIns() -{ - // { 0x0F70700,0x0F70710, 0xFF,0xFF, 0x0,+0 }, - insdata tmp1 = { {0x00, 0x10, 0x07, 0x07, 0xF7, 0xF7, 0x00, 0x00, 0xFF, 0xFF, 0x00}, 0, 0 }; - struct ins tmp2 = { 0, 0, 0, 0.0, 0 }; - return InsertIns(tmp1, tmp1, tmp2, "nosound"); -} - -static void LoadBNK(const char* fn, unsigned bank, const char* prefix, bool is_fat, bool percussive) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp), - std::fclose(fp); - - /*printf("%s:\n", fn);*/ - unsigned short version = *(short*)&data[0]; // major,minor (2 bytes) - // "ADLIB-" (6 bytes) - unsigned short entries_used = *(short*)&data[8]; // entries used - unsigned short total_entries = *(short*)&data[10]; // total entries - unsigned name_offset = *(unsigned*)&data[12];// name_offset - unsigned data_offset = *(unsigned*)&data[16];// data_offset - // 16..23: 8 byte sof filler - /*printf("version=%u %u %u %u %u\n", - version, entries_used,total_entries,name_offset,data_offset);*/ - - for(unsigned n=0; n<entries_used; ++n) + fprintf(outFile, "\ +#include \"adldata.hh\"\n\ +\n\ +/* THIS OPL-3 FM INSTRUMENT DATA IS AUTOMATICALLY GENERATED\n\ + * FROM A NUMBER OF SOURCES, MOSTLY PC GAMES.\n\ + * PREPROCESSED, CONVERTED, AND POSTPROCESSED OFF-SCREEN.\n\ + */\n\ +"); { - const size_t offset1 = name_offset + n*12; - - unsigned short data_index = data[offset1+0] + data[offset1+1]*256; - unsigned char usage_flag = data[offset1+2]; - std::string name; - for(unsigned p=0; p<9; ++p) - { - if(data[offset1+3+p] == '\0') break; - name += char(data[offset1+3+p]); - } - - const size_t offset2 = data_offset + data_index * 30; - const unsigned char mode = data[offset2+0]; - const unsigned char voice_num = data[offset2+1]; - const unsigned char* op1 = &data[offset2+2]; // 13 bytes - const unsigned char* op2 = &data[offset2+15]; - const unsigned char waveform_mod = data[offset2+28]; - const unsigned char waveform_car = data[offset2+29]; - - /*printf("%5d %3d %8s mode=%02X voice=%02X: ", data_index,usage_flag, name.c_str(), - mode,voice_num);*/ - - int gmno = is_fat - ? ((n & 127) + percussive*128) - : (n + percussive*128); - - if(is_fat && percussive) + IniProcessing ini; + if(!ini.open("banks.ini")) { - if(name[2] == 'O' - || name[2] == 'S') - { - gmno = 128 + std::stoi(name.substr(3)); - } + fprintf(stderr, "Can't open banks.ini!\n"); + return 1; } - char name2[512]; - if(is_fat) - sprintf(name2, "%s%c%u", prefix, percussive?'P':'M', gmno&127); - else - sprintf(name2, "%s%u", prefix, n); - - insdata tmp; - tmp.data[0] = (op1[ 9] << 7) // TREMOLO FLAG - + (op1[10] << 6) // VIBRATO FLAG - + (op1[ 5] << 5) // SUSTAIN FLAG - + (op1[11] << 4) // SCALING FLAG - + op1[ 1]; // FREQMUL - - tmp.data[1] = (op2[ 9] << 7) - + (op2[10] << 6) - + (op2[ 5] << 5) - + (op2[11] << 4) - + op2[ 1]; - tmp.data[2] = op1[3]*0x10 + op1[6]; // ATTACK, DECAY - tmp.data[3] = op2[3]*0x10 + op2[6]; - tmp.data[4] = op1[4]*0x10 + op1[7]; // SUSTAIN, RELEASE - tmp.data[5] = op2[4]*0x10 + op2[7]; - tmp.data[6] = waveform_mod; - tmp.data[7] = waveform_car; - tmp.data[8] = op1[0]*0x40 + op1[8]; // KSL , LEVEL - tmp.data[9] = op2[0]*0x40 + op2[8]; // KSL , LEVEL - tmp.data[10] = op1[2]*2 + op1[12]; // FEEDBACK, ADDITIVEFLAG - tmp.finetune = 0; - tmp.diff=false; - // Note: op2[2] and op2[12] are unused and contain garbage. - ins tmp2; - tmp2.notenum = is_fat ? voice_num : (percussive ? usage_flag : 0); - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - if(is_fat) tmp.data[10] ^= 1; - - size_t resno = InsertIns(tmp,tmp, tmp2, std::string(1,'\377')+name, name2); + uint32_t banks_count; + ini.beginGroup("General"); + ini.read("banks", banks_count, 0); + ini.endGroup(); - if(!is_fat) + if(!banks_count) { - SetBank(bank, gmno, resno); - } - else - { - if(name[2] == 'O' || name[1] == 'M') SetBank(bank+0, gmno, resno); - if(name[2] == 'S' || name[1] == 'M') SetBank(bank+1, gmno, resno); + fprintf(stderr, "Zero count of banks found in banks.ini!\n"); + return 1; } - /* - for(unsigned p=0; p<30; ++p) + for(uint32_t bank = 0; bank < banks_count; bank++) { - unsigned char value = data[offset2+p]; - if(value > maxvalues[p]) maxvalues[p] = value; + if(!ini.beginGroup(std::string("bank-") + std::to_string(bank))) + { + fprintf(stderr, "Failed to find bank %u!\n", bank); + return 1; + } + std::string bank_name; + std::string filepath; + std::string filepath_d; + std::string prefix; + std::string prefix_d; + std::string filter_m; + std::string filter_p; + std::string format; + + ini.read("name", bank_name, "Untitled"); + ini.read("format", format, "Unknown"); + ini.read("file", filepath, ""); + ini.read("file-p", filepath_d, ""); + ini.read("prefix", prefix, ""); + ini.read("prefix-p", prefix_d, ""); + ini.read("filter-m", filter_m, ""); + ini.read("filter-p", filter_p, ""); + + if(filepath.empty()) + { + fprintf(stderr, "Failed to load bank %u, file is empty!\n", bank); + return 1; + } - #define dot(maxval) if(value<=maxval) printf("."); else printf("?[%u]%X",p,value); + banknames.push_back(bank_name); - { - //if(p==6 || p==7 || p==19||p==20) value=15-value; + //printf("Loading %s...\n", filepath.c_str()); - if(p==4 || p==10 || p==17 || p==23)// || p==25) - printf(" %2X", value); + if(format == "AIL") + { + if(!LoadMiles(filepath.c_str(), bank, prefix.c_str())) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } else - printf(" %X", value); - } - nl:; - //if(p == 12) printf("\n%*s", 22, ""); - //if(p == 25) printf("\n%*s", 22, ""); - } - printf("\n"); - */ - } -} - -static void LoadBNK2(const char* fn, unsigned bank, const char* prefix, - const std::string& melo_filter, - const std::string& perc_filter) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp), - std::fclose(fp); - - unsigned short ins_entries = *(unsigned short*)&data[28+2+10]; - unsigned char* records = &data[48]; - - for(unsigned n=0; n<ins_entries; ++n) - { - const size_t offset1 = n*28; - int used = records[offset1 + 15]; - int attrib = *(int*)&records[offset1 + 16]; - int offset2 = *(int*)&records[offset1 + 20]; - if(used == 0) continue; - - std::string name; - for(unsigned p=0; p<12; ++p) - { - if(records[offset1+3+p] == '\0') break; - name += char(records[offset1+3+p]); - } - - int gmno = 0; - if(name.substr(0, melo_filter.size()) == melo_filter) - gmno = std::stoi(name.substr(melo_filter.size())); - else if(name.substr(0, perc_filter.size()) == perc_filter) - gmno = std::stoi(name.substr(perc_filter.size())) + 128; - else - continue; - - const unsigned char* insdata = &data[offset2]; - const unsigned char* ops[4] = { insdata+0, insdata+6, insdata+12, insdata+18 }; - unsigned char C4xxxFFFC = insdata[24]; - unsigned char xxP24NNN = insdata[25]; - unsigned char TTTTTTTT = insdata[26]; - unsigned char xxxxxxxx = insdata[27]; - - char name2[512]; - sprintf(name2, "%s%c%u", prefix, (gmno&128)?'P':'M', gmno&127); - - struct insdata tmp[2]; - for(unsigned a=0; a<2; ++a) - { - tmp[a].data[0] = ops[a*2+0][0]; - tmp[a].data[1] = ops[a*2+1][0]; - tmp[a].data[2] = ops[a*2+0][2]; - tmp[a].data[3] = ops[a*2+1][2]; - tmp[a].data[4] = ops[a*2+0][3]; - tmp[a].data[5] = ops[a*2+1][3]; - tmp[a].data[6] = ops[a*2+0][4] & 0x07; - tmp[a].data[7] = ops[a*2+1][4] & 0x07; - tmp[a].data[8] = ops[a*2+0][1]; - tmp[a].data[9] = ops[a*2+1][1]; - tmp[a].finetune = TTTTTTTT; - tmp[a].diff=false; - } - tmp[0].data[10] = C4xxxFFFC & 0x0F; - tmp[1].data[10] = (tmp[0].data[10] & 0x0E) | (C4xxxFFFC >> 7); - - ins tmp2; - tmp2.notenum = (gmno & 128) ? 35 : 0; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - if(xxP24NNN & 8) - { - // dual-op - size_t resno = InsertIns(tmp[0],tmp[1], tmp2, std::string(1,'\377')+name, name2); - SetBank(bank, gmno, resno); - } - else - { - // single-op - size_t resno = InsertIns(tmp[0],tmp[0], tmp2, std::string(1,'\377')+name, name2); - SetBank(bank, gmno, resno); - } - } -} - -struct Doom_OPL2instrument { - unsigned char trem_vibr_1; /* OP 1: tremolo/vibrato/sustain/KSR/multi */ - unsigned char att_dec_1; /* OP 1: attack rate/decay rate */ - unsigned char sust_rel_1; /* OP 1: sustain level/release rate */ - unsigned char wave_1; /* OP 1: waveform select */ - unsigned char scale_1; /* OP 1: key scale level */ - unsigned char level_1; /* OP 1: output level */ - unsigned char feedback; /* feedback/AM-FM (both operators) */ - unsigned char trem_vibr_2; /* OP 2: tremolo/vibrato/sustain/KSR/multi */ - unsigned char att_dec_2; /* OP 2: attack rate/decay rate */ - unsigned char sust_rel_2; /* OP 2: sustain level/release rate */ - unsigned char wave_2; /* OP 2: waveform select */ - unsigned char scale_2; /* OP 2: key scale level */ - unsigned char level_2; /* OP 2: output level */ - unsigned char unused; - short basenote; /* base note offset */ -} __attribute__ ((packed)); - -struct Doom_opl_instr { - unsigned short flags; -#define FL_FIXED_PITCH 0x0001 // note has fixed pitch (drum note) -#define FL_UNKNOWN 0x0002 // ??? (used in instrument #65 only) -#define FL_DOUBLE_VOICE 0x0004 // use two voices instead of one - - unsigned char finetune; - unsigned char note; - struct Doom_OPL2instrument patchdata[2]; -} __attribute__ ((packed)); - - -static void LoadDoom(const char* fn, unsigned bank, const char* prefix) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp), - std::fclose(fp); - - for(unsigned a=0; a<175; ++a) - { - const size_t offset1 = 0x18A4 + a*32; - const size_t offset2 = 8 + a*36; - - std::string name; - for(unsigned p=0; p<32; ++p) - if(data[offset1]!='\0') - name += char(data[offset1+p]); - - //printf("%3d %3d %3d %8s: ", a,b,c, name.c_str()); - int gmno = a<128 ? a : ((a|128)+35); - - char name2[512]; sprintf(name2, "%s%c%u", prefix, (gmno<128?'M':'P'), gmno&127); - - Doom_opl_instr& ins = *(Doom_opl_instr*) &data[offset2]; - - insdata tmp[2]; - tmp[0].diff=false; - tmp[1].diff=true; - for(unsigned index=0; index<2; ++index) - { - const Doom_OPL2instrument& src = ins.patchdata[index]; - tmp[index].data[0] = src.trem_vibr_1; - tmp[index].data[1] = src.trem_vibr_2; - tmp[index].data[2] = src.att_dec_1; - tmp[index].data[3] = src.att_dec_2; - tmp[index].data[4] = src.sust_rel_1; - tmp[index].data[5] = src.sust_rel_2; - tmp[index].data[6] = src.wave_1; - tmp[index].data[7] = src.wave_2; - tmp[index].data[8] = src.scale_1 | src.level_1; - tmp[index].data[9] = src.scale_2 | src.level_2; - tmp[index].data[10] = src.feedback; - tmp[index].finetune = src.basenote+12; - } - struct ins tmp2; - tmp2.notenum = ins.note; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - while(tmp2.notenum && tmp2.notenum < 20) - { - tmp2.notenum += 12; - tmp[0].finetune -= 12; - tmp[1].finetune -= 12; - } - - if(!(ins.flags&4)) - { - size_t resno = InsertIns(tmp[0],tmp[0],tmp2, std::string(1,'\377')+name, name2); - SetBank(bank, gmno, resno); - } - else // Double instrument - { - tmp2.pseudo4op = true; - tmp2.fine_tune = ( ((double)ins.finetune-128.0)*15.625)/1000.0; - if(ins.finetune==129) - tmp2.fine_tune=0.000025; - else if(ins.finetune==127) - tmp2.fine_tune=-0.000025; - //printf("/*DOOM FINE TUNE (flags %000X instrument is %d) IS %d -> %lf*/\n", ins.flags, a, ins.finetune, tmp2.fine_tune); - size_t resno = InsertIns(tmp[0], tmp[1], tmp2, std::string(1,'\377')+name, name2); - SetBank(bank, gmno, resno); - } - - /*const Doom_OPL2instrument& A = ins.patchdata[0]; - const Doom_OPL2instrument& B = ins.patchdata[1]; - printf( - "flags:%04X fine:%02X note:%02X\n" - "{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n" - " t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d}\n" - "{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n" - " t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d} ", - ins.flags, ins.finetune, ins.note, - A.trem_vibr_1, A.att_dec_1, A.sust_rel_1, - A.wave_1, A.scale_1, A.level_1, A.feedback, - A.trem_vibr_2, A.att_dec_2, A.sust_rel_2, - A.wave_2, A.scale_2, A.level_2, A.unused, A.basenote, - B.trem_vibr_1, B.att_dec_1, B.sust_rel_1, - B.wave_1, B.scale_1, B.level_1, B.feedback, - B.trem_vibr_2, B.att_dec_2, B.sust_rel_2, - B.wave_2, B.scale_2, B.level_2, B.unused, B.basenote); - printf(" %s VS %s\n", name.c_str(), MidiInsName[a]); - printf("------------------------------------------------------------\n");*/ - } -} -static void LoadMiles(const char* fn, unsigned bank, const char* prefix) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp), - std::fclose(fp); - - for(unsigned a=0; a<2000; ++a) - { - unsigned gm_patch = data[a*6+0]; - unsigned gm_bank = data[a*6+1]; - unsigned offset = *(unsigned*)&data[a*6+2]; - - if(gm_patch == 0xFF) - break; - - int gmno = gm_bank==0x7F ? gm_patch + 0x80 : gm_patch; - int midi_index = gmno < 128 ? gmno - : gmno < 128+35 ? -1 - : gmno < 128+88 ? gmno-35 - : -1; - unsigned length = data[offset] + data[offset+1]*256; - signed char notenum = ((signed char)data[offset+2]); - - /*printf("%02X %02X %08X ", gmnumber,gmnumber2, offset); - for(unsigned b=0; b<length; ++b) - { - if(b > 3 && (b-3)%11 == 0) printf("\n "); - printf("%02X ", data[offset+b]); - } - printf("\n");*/ - - if(gm_bank != 0 && gm_bank != 0x7F) - continue; - - char name2[512]; sprintf(name2, "%s%c%u", prefix, - (gmno < 128 ? 'M':'P'), gmno & 127); - - insdata tmp[200]; - - const unsigned inscount = (length-3)/11; - for(unsigned i=0; i<inscount; ++i) - { - unsigned o = offset + 3 + i*11; - tmp[i].finetune = (gmno < 128 && i == 0) ? notenum : 0; - tmp[i].diff = false; - tmp[i].data[0] = data[o+0]; // 20 - tmp[i].data[8] = data[o+1]; // 40 (vol) - tmp[i].data[2] = data[o+2]; // 60 - tmp[i].data[4] = data[o+3]; // 80 - tmp[i].data[6] = data[o+4]; // E0 - tmp[i].data[1] = data[o+6]; // 23 - tmp[i].data[9] = data[o+7]; // 43 (vol) - tmp[i].data[3] = data[o+8]; // 63 - tmp[i].data[5] = data[o+9]; // 83 - tmp[i].data[7] = data[o+10]; // E3 - - unsigned fb_c = data[offset+3+5]; - tmp[i].data[10] = fb_c; - if(i == 1) + if(format == "Bisqwit") { - tmp[0].data[10] = fb_c & 0x0F; - tmp[1].data[10] = (fb_c & 0x0E) | (fb_c >> 7); + if(!LoadBisqwit(filepath.c_str(), bank, prefix.c_str())) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + else + if(format == "OP2") + { + if(!LoadDoom(filepath.c_str(), bank, prefix.c_str())) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + else + if(format == "TMB") + { + if(!LoadTMB(filepath.c_str(), bank, prefix.c_str())) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + else + if(format == "Junglevision") + { + if(!LoadJunglevision(filepath.c_str(), bank, prefix.c_str())) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + else + if(format == "AdLibGold") + { + if(!LoadBNK2(filepath.c_str(), bank, prefix.c_str(), filter_m, filter_p)) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + else + if(format == "HMI") + { + if(!LoadBNK(filepath.c_str(), bank, prefix.c_str(), false, false)) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + if(!filepath_d.empty()) + { + if(!LoadBNK(filepath_d.c_str(),bank, prefix_d.c_str(), false, true)) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + } + else + if(format == "IBK") + { + if(!LoadIBK(filepath.c_str(), bank, prefix.c_str(), false)) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + if(!filepath_d.empty()) + { + //printf("Loading %s... \n", filepath_d.c_str()); + if(!LoadIBK(filepath_d.c_str(),bank, prefix_d.c_str(), true)) + { + fprintf(stderr, "Failed to load bank %u, file %s!\n", bank, filepath.c_str()); + return 1; + } + } + } + else + { + fprintf(stderr, "Failed to load bank %u, file %s!\nUnknown format type %s\n", + bank, + filepath.c_str(), + format.c_str()); + return 1; } - } - if(inscount == 1) - tmp[1] = tmp[0]; - - if(inscount <= 2) - { - struct ins tmp2; - tmp2.notenum = gmno < 128 ? 0 : notenum; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - std::string name; - if(midi_index >= 0) name = std::string(1,'\377')+MidiInsName[midi_index]; - size_t resno = InsertIns(tmp[0], tmp[1], tmp2, name, name2); - SetBank(bank, gmno, resno); - } - } -} - -static void LoadIBK(const char* fn, unsigned bank, const char* prefix, bool percussive) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp), - std::fclose(fp); - - unsigned offs1_base = 0x804, offs1_len = 9; - unsigned offs2_base = 0x004, offs2_len = 16; - - for(unsigned a=0; a<128; ++a) - { - unsigned offset1 = offs1_base + a*offs1_len; - unsigned offset2 = offs2_base + a*offs2_len; - - std::string name; - for(unsigned p=0; p<9; ++p) - if(data[offset1]!='\0') - name += char(data[offset1+p]); - - int gmno = a + 128*percussive; - int midi_index = gmno < 128 ? gmno - : gmno < 128+35 ? -1 - : gmno < 128+88 ? gmno-35 - : -1; - char name2[512]; sprintf(name2, "%s%c%u", prefix, - (gmno<128?'M':'P'), gmno&127); - - insdata tmp; - tmp.data[0] = data[offset2+0]; - tmp.data[1] = data[offset2+1]; - tmp.data[8] = data[offset2+2]; - tmp.data[9] = data[offset2+3]; - tmp.data[2] = data[offset2+4]; - tmp.data[3] = data[offset2+5]; - tmp.data[4] = data[offset2+6]; - tmp.data[5] = data[offset2+7]; - tmp.data[6] = data[offset2+8]; - tmp.data[7] = data[offset2+9]; - tmp.data[10] = data[offset2+10]; - // [+11] seems to be used also, what is it for? - tmp.finetune = 0; - tmp.diff=false; - struct ins tmp2; - tmp2.notenum = gmno < 128 ? 0 : 35; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - size_t resno = InsertIns(tmp,tmp, tmp2, std::string(1,'\377')+name, name2); - SetBank(bank, gmno, resno); - } -} - -static void LoadJunglevision(const char* fn, unsigned bank, const char* prefix) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp); - std::fclose(fp); - - unsigned ins_count = data[0x20] + (data[0x21] << 8); - unsigned drum_count = data[0x22] + (data[0x23] << 8); - unsigned first_ins = data[0x24] + (data[0x25] << 8); - unsigned first_drum = data[0x26] + (data[0x27] << 8); - - unsigned total_ins = ins_count + drum_count; - - for ( unsigned a = 0; a < total_ins; ++a ) - { - unsigned offset = 0x28 + a * 0x18; - unsigned gmno = (a < ins_count) ? (a + first_ins) : (a + first_drum); - int midi_index = gmno < 128 ? gmno - : gmno < 128+35 ? -1 - : gmno < 128+88 ? gmno-35 - : -1; - - insdata tmp[2]; - - tmp[0].data[0] = data[offset + 2]; - tmp[0].data[1] = data[offset + 8]; - tmp[0].data[2] = data[offset + 4]; - tmp[0].data[3] = data[offset + 10]; - tmp[0].data[4] = data[offset + 5]; - tmp[0].data[5] = data[offset + 11]; - tmp[0].data[6] = data[offset + 6]; - tmp[0].data[7] = data[offset + 12]; - tmp[0].data[8] = data[offset + 3]; - tmp[0].data[9] = data[offset + 9]; - tmp[0].data[10] = data[offset + 7] & 0x0F;//~0x30; - tmp[0].finetune = 0; - tmp[0].diff=false; - - tmp[1].data[0] = data[offset + 2 + 11]; - tmp[1].data[1] = data[offset + 8 + 11]; - tmp[1].data[2] = data[offset + 4 + 11]; - tmp[1].data[3] = data[offset + 10 + 11]; - tmp[1].data[4] = data[offset + 5 + 11]; - tmp[1].data[5] = data[offset + 11 + 11]; - tmp[1].data[6] = data[offset + 6 + 11]; - tmp[1].data[7] = data[offset + 12 + 11]; - tmp[1].data[8] = data[offset + 3 + 11]; - tmp[1].data[9] = data[offset + 9 + 11]; - tmp[1].data[10] = data[offset + 7 + 11] & 0x0F;//~0x30; - tmp[1].finetune = 0; - tmp[1].diff=false; - - struct ins tmp2; - tmp2.notenum = data[offset + 1]; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - while(tmp2.notenum && tmp2.notenum < 20) - { - tmp2.notenum += 12; - tmp[0].finetune -= 12; - tmp[1].finetune -= 12; - } - - std::string name; - if(midi_index >= 0) name = std::string(1,'\377')+MidiInsName[midi_index]; - - char name2[512]; sprintf(name2, "%s%c%u", prefix, - (gmno<128?'M':'P'), gmno&127); - - if(!data[offset]) - { - size_t resno = InsertIns(tmp[0],tmp[0],tmp2, name, name2); - SetBank(bank, gmno, resno); - } - else // Double instrument - { - size_t resno = InsertIns(tmp[0],tmp[1],tmp2, name, name2); - SetBank(bank, gmno, resno); - } - } -} - -static void LoadTMB(const char* fn, unsigned bank, const char* prefix) -{ - FILE* fp = std::fopen(fn, "rb"); - std::fseek(fp, 0, SEEK_END); - std::vector<unsigned char> data(std::ftell(fp)); - std::rewind(fp); - std::fread(&data[0], 1, data.size(), fp); - std::fclose(fp); - - for ( unsigned a = 0; a < 256; ++a ) - { - unsigned offset = a * 0x0D; - unsigned gmno = a; - int midi_index = gmno < 128 ? gmno - : gmno < 128+35 ? -1 - : gmno < 128+88 ? gmno-35 - : -1; - - insdata tmp; - - tmp.data[0] = data[offset + 0]; - tmp.data[1] = data[offset + 1]; - tmp.data[2] = data[offset + 4]; - tmp.data[3] = data[offset + 5]; - tmp.data[4] = data[offset + 6]; - tmp.data[5] = data[offset + 7]; - tmp.data[6] = data[offset + 8]; - tmp.data[7] = data[offset + 9]; - tmp.data[8] = data[offset + 2]; - tmp.data[9] = data[offset + 3]; - tmp.data[10] = data[offset + 10]; - tmp.finetune = 0; //data[offset + 12]; - tmp.diff=false; - - struct ins tmp2; - tmp2.notenum = data[offset + 11]; - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - std::string name; - if(midi_index >= 0) name = std::string(1,'\377')+MidiInsName[midi_index]; - - char name2[512]; sprintf(name2, "%s%c%u", prefix, - (gmno<128?'M':'P'), gmno&127); - - size_t resno = InsertIns(tmp,tmp,tmp2, name, name2); - SetBank(bank, gmno, resno); - } -} - -static void LoadBisqwit(const char* fn, unsigned bank, const char* prefix) -{ - FILE* fp = std::fopen(fn, "rb"); - for ( unsigned a = 0; a < 256; ++a ) - { - unsigned offset = a * 25; - unsigned gmno = a; - int midi_index = gmno < 128 ? gmno - : gmno < 128+35 ? -1 - : gmno < 128+88 ? gmno-35 - : -1; - - struct ins tmp2; - tmp2.notenum = std::fgetc(fp); - tmp2.pseudo4op = false; - tmp2.fine_tune = 0.0; - - insdata tmp[2]; - for(int side=0; side<2; ++side) - { - tmp[side].finetune = std::fgetc(fp); - tmp[side].diff=false; - std::fread(tmp[side].data, 1, 11, fp); - } - - std::string name; - if(midi_index >= 0) name = std::string(1,'\377')+MidiInsName[midi_index]; - - char name2[512]; sprintf(name2, "%s%c%u", prefix, - (gmno<128?'M':'P'), gmno&127); - - size_t resno = InsertIns(tmp[0],tmp[1],tmp2, name, name2); - SetBank(bank, gmno, resno); - } - std::fclose(fp); -} - -#include "../nukedopl3.h" - -std::vector<int16_t> stereoSampleBuf; - -struct DurationInfo -{ - long ms_sound_kon; - long ms_sound_koff; - bool nosound; -}; -static DurationInfo MeasureDurations(const ins& in) -{ - insdata id[2]; - bool found[2] = {false,false}; - for(std::map<insdata, std::pair<size_t, std::set<std::string> > > - ::const_iterator - j = insdatatab.begin(); - j != insdatatab.end(); - ++j) - { - if(j->second.first == in.insno1) { id[0] = j->first; found[0]=true; if(found[1]) break; } - if(j->second.first == in.insno2) { id[1] = j->first; found[1]=true; if(found[0]) break; } - } - const unsigned rate = 22050; - const unsigned interval = 150; - const unsigned samples_per_interval = rate / interval; - const int notenum = - in.notenum < 20 ? (44 + in.notenum) - : in.notenum >= 128 ? (44 + 128 - in.notenum) - : in.notenum; - - _opl3_chip opl; - static const short initdata[(2+3+2+2)*2] = - { 0x004,96, 0x004,128, // Pulse timer - 0x105, 0, 0x105,1, 0x105,0, // Pulse OPL3 enable, leave disabled - 0x001,32, 0x0BD,0 // Enable wave & melodic - }; - OPL3_Reset(&opl, rate); - for(unsigned a=0; a<18; a+=2) OPL3_WriteReg(&opl, initdata[a], initdata[a+1]); - - const unsigned n_notes = in.insno1 == in.insno2 ? 1 : 2; - unsigned x[2]; - - if (n_notes == 2 && !in.pseudo4op) - { - OPL3_WriteReg(&opl, 0x105, 1); - OPL3_WriteReg(&opl, 0x104, 1); - } - - for(unsigned n=0; n<n_notes; ++n) - { - static const unsigned char patchdata[11] = - {0x20,0x23,0x60,0x63,0x80,0x83,0xE0,0xE3,0x40,0x43,0xC0}; - for(unsigned a=0; a<10; ++a) OPL3_WriteReg(&opl, patchdata[a]+n*8, id[n].data[a]); - OPL3_WriteReg(&opl, patchdata[10]+n*8, id[n].data[10] | 0x30); - } - for(unsigned n=0; n<n_notes; ++n) - { - double hertz = 172.00093 * std::exp(0.057762265 * (notenum + id[n].finetune)); - if(hertz > 131071) - { - fprintf(stderr, "Why does note %d + finetune %d produce hertz %g?\n", - notenum, id[n].finetune, hertz); - hertz = 131071; - } - x[n] = 0x2000; - while(hertz >= 1023.5) { hertz /= 2.0; x[n] += 0x400; } // Calculate octave - x[n] += (int)(hertz + 0.5); - - // Keyon the note - OPL3_WriteReg(&opl, 0xA0+n*3, x[n]&0xFF); - OPL3_WriteReg(&opl, 0xB0+n*3, x[n]>>8); - } - - const unsigned max_on = 40; - const unsigned max_off = 60; - - // For up to 40 seconds, measure mean amplitude. - std::vector<double> amplitudecurve_on; - double highest_sofar = 0; - for(unsigned period=0; period<max_on*interval; ++period) - { - stereoSampleBuf.clear(); - stereoSampleBuf.resize(samples_per_interval * 2); - OPL3_GenerateStream(&opl, stereoSampleBuf.data(), samples_per_interval); - - double mean = 0.0; - for(unsigned long c=0; c<samples_per_interval; ++c) - mean += stereoSampleBuf[c*2]; - mean /= samples_per_interval; - double std_deviation = 0; - for(unsigned long c=0; c<samples_per_interval; ++c) - { - double diff = (stereoSampleBuf[c*2]-mean); - std_deviation += diff*diff; - } - std_deviation = std::sqrt(std_deviation / samples_per_interval); - amplitudecurve_on.push_back(std_deviation); - if(std_deviation > highest_sofar) - highest_sofar = std_deviation; - - if(period > 6*interval && std_deviation < highest_sofar*0.2) - break; - } - - // Keyoff the note - for(unsigned n=0; n<n_notes; ++n) - OPL3_WriteReg(&opl, 0xB0+n, (x[n]>>8) & 0xDF); - // Now, for up to 60 seconds, measure mean amplitude. - std::vector<double> amplitudecurve_off; - for(unsigned period=0; period<max_off*interval; ++period) - { - stereoSampleBuf.clear(); - stereoSampleBuf.resize(samples_per_interval * 2); - OPL3_GenerateStream(&opl, stereoSampleBuf.data(), samples_per_interval); - double mean = 0.0; - for(unsigned long c=0; c<samples_per_interval; ++c) - mean += stereoSampleBuf[c*2]; - mean /= samples_per_interval; - double std_deviation = 0; - for(unsigned long c=0; c<samples_per_interval; ++c) - { - double diff = (stereoSampleBuf[c*2]-mean); - std_deviation += diff*diff; + ini.endGroup(); } - std_deviation = std::sqrt(std_deviation / samples_per_interval); - amplitudecurve_off.push_back(std_deviation); - if(std_deviation < highest_sofar*0.2) break; + printf("Loaded %u banks!\n", banks_count); + fflush(stdout); } - /* Analyze the results */ - double begin_amplitude = amplitudecurve_on[0]; - double peak_amplitude_value = begin_amplitude; - size_t peak_amplitude_time = 0; - size_t quarter_amplitude_time = amplitudecurve_on.size(); - size_t keyoff_out_time = 0; - - for(size_t a=1; a<amplitudecurve_on.size(); ++a) + #if 0 + for(unsigned a = 0; a < 36 * 8; ++a) { - if(amplitudecurve_on[a] > peak_amplitude_value) - { - peak_amplitude_value = amplitudecurve_on[a]; - peak_amplitude_time = a; - } - } - for(size_t a=peak_amplitude_time; a<amplitudecurve_on.size(); ++a) - { - if(amplitudecurve_on[a] <= peak_amplitude_value * 0.2) - { - quarter_amplitude_time = a; - break; - } - } - for(size_t a=0; a<amplitudecurve_off.size(); ++a) - { - if(amplitudecurve_off[a] <= peak_amplitude_value * 0.2) - { - keyoff_out_time = a; - break; - } - } - - if(keyoff_out_time == 0 && amplitudecurve_on.back() < peak_amplitude_value*0.2) - keyoff_out_time = quarter_amplitude_time; + if((1 << (a % 8)) > maxvalues[a / 8]) continue; - if(peak_amplitude_time == 0) - { - printf( - " // Amplitude begins at %6.1f,\n" - " // fades to 20%% at %.1fs, keyoff fades to 20%% in %.1fs.\n", - begin_amplitude, - quarter_amplitude_time / double(interval), - keyoff_out_time / double(interval)); - } - else - { - printf( - " // Amplitude begins at %6.1f, peaks %6.1f at %.1fs,\n" - " // fades to 20%% at %.1fs, keyoff fades to 20%% in %.1fs.\n", - begin_amplitude, - peak_amplitude_value, - peak_amplitude_time / double(interval), - quarter_amplitude_time / double(interval), - keyoff_out_time / double(interval)); - } - - DurationInfo result; - result.ms_sound_kon = quarter_amplitude_time * 1000.0 / interval; - result.ms_sound_koff = keyoff_out_time * 1000.0 / interval; - result.nosound = (peak_amplitude_value < 0.5); - return result; -} - -int main() -{ - printf("\ -#include \"adldata.hh\"\n\ -\n\ -/* THIS ADLIB FM INSTRUMENT DATA IS AUTOMATICALLY GENERATED\n\ - * FROM A NUMBER OF SOURCES, MOSTLY PC GAMES.\n\ - * PREPROCESSED, CONVERTED, AND POSTPROCESSED OFF-SCREEN.\n\ - */\n\ -"); - size_t nosound = InsertNoSoundIns(); - - LoadMiles("fm_banks/opl_files/sc3.opl", 0, "G"); // Our "standard" bank! Same as file22.opl - - LoadBisqwit("fm_banks/op3_files/bisqwit.adlraw", 1, "Bisq"); - - LoadBNK("fm_banks/bnk_files/melodic.bnk", 2, "HMIGM", false, false); // same as file156.bnk - LoadBNK("fm_banks/bnk_files/drum.bnk", 2, "HMIGP", false, true); - LoadBNK("fm_banks/bnk_files/intmelo.bnk", 3, "intM", false, false); - LoadBNK("fm_banks/bnk_files/intdrum.bnk", 3, "intP", false, true); - LoadBNK("fm_banks/bnk_files/hammelo.bnk", 4, "hamM", false, false); - LoadBNK("fm_banks/bnk_files/hamdrum.bnk", 4, "hamP", false, true); - LoadBNK("fm_banks/bnk_files/rickmelo.bnk",5, "rickM", false, false); - LoadBNK("fm_banks/bnk_files/rickdrum.bnk",5, "rickP", false, true); - - LoadBNK("fm_banks/bnk_files/d2melo.bnk", 6, "b6M", false, false); - LoadBNK("fm_banks/bnk_files/d2drum.bnk", 6, "b6P", false, true); - LoadBNK("fm_banks/bnk_files/normmelo.bnk", 7, "b7M", false, false); - LoadBNK("fm_banks/bnk_files/normdrum.bnk", 7, "b7P", false, true); // same as file122.bnk - LoadBNK("fm_banks/bnk_files/ssmelo.bnk", 8, "b8M", false, false); - LoadBNK("fm_banks/bnk_files/ssdrum.bnk", 8, "b8P", false, true); - - LoadTMB("fm_banks/bnk_files/themepark.tmb", 9, "b9MP"); - //LoadBNK("fm_banks/bnk_files/file131.bnk", 9, "b9M", false, false); - //LoadBNK("fm_banks/bnk_files/file132.bnk", 9, "b9P", false, true); - LoadBNK("fm_banks/bnk_files/file133.bnk", 10,"b10P", false, true); - LoadBNK("fm_banks/bnk_files/file134.bnk", 10,"b10M", false, false); - LoadBNK("fm_banks/bnk_files/file142.bnk", 11, "b11P", false, true); - LoadBNK("fm_banks/bnk_files/file143.bnk", 11, "b11M", false, false); - LoadBNK("fm_banks/bnk_files/file145.bnk", 12, "b12M", false, false);//file145 is MELODIC - LoadBNK("fm_banks/bnk_files/file144.bnk", 12, "b12P", false, true);//file144 is DRUMS - LoadBNK("fm_banks/bnk_files/file167.bnk", 13, "b13P", false, true); - LoadBNK("fm_banks/bnk_files/file168.bnk", 13, "b13M", false, false); - - LoadDoom("fm_banks/doom2/genmidi.op2", 14, "dM"); - LoadDoom("fm_banks/doom2/genmidi.htc", 15, "hxM"); // same as genmidi.hxn - LoadDoom("fm_banks/doom2/default.op2", 16, "mus"); - - LoadMiles("fm_banks/opl_files/file17.opl", 17, "f17G"); - LoadMiles("fm_banks/opl_files/warcraft.ad", 18, "sG"); // same as file44, warcraft.opl - LoadMiles("fm_banks/opl_files/file19.opl", 19, "f19G"); - LoadMiles("fm_banks/opl_files/file20.opl", 20, "f20G"); - LoadMiles("fm_banks/opl_files/file21.opl", 21, "f21G"); - LoadMiles("fm_banks/opl_files/nemesis.opl", 22, "nem"); - LoadMiles("fm_banks/opl_files/file23.opl", 23, "f23G"); - LoadMiles("fm_banks/opl_files/file24.opl", 24, "f24G"); - LoadMiles("fm_banks/opl_files/file25.opl", 25, "f25G"); - LoadMiles("fm_banks/opl_files/file26.opl", 26, "f26G"); - LoadMiles("fm_banks/opl_files/file27.opl", 27, "f27G"); - LoadMiles("fm_banks/opl_files/nhlpa.opl", 28, "nhl"); - LoadMiles("fm_banks/opl_files/file29.opl", 29, "f29G"); - LoadMiles("fm_banks/opl_files/file30.opl", 30, "f30G"); - LoadMiles("fm_banks/opl_files/file31.opl", 31, "f31G"); - LoadMiles("fm_banks/opl_files/file32.opl", 32, "f32G"); - LoadMiles("fm_banks/opl_files/file13.opl", 33, "f13G"); - LoadMiles("fm_banks/opl_files/file34.opl", 34, "f34G"); - LoadMiles("fm_banks/opl_files/file35.opl", 35, "f35G"); - LoadMiles("fm_banks/opl_files/file36.opl", 36, "f36G"); - LoadMiles("fm_banks/opl_files/file37.opl", 37, "f37G"); - LoadMiles("fm_banks/opl_files/simfarm.opl", 38, "qG"); - LoadMiles("fm_banks/opl_files/simfarm.ad", 39, "mG"); // same as file18.opl - LoadMiles("fm_banks/opl_files/file12.opl", 40, "f12G"); - LoadMiles("fm_banks/opl_files/file41.opl", 41, "f41G"); - LoadMiles("fm_banks/opl_files/file42.opl", 42, "f42G"); - LoadMiles("fm_banks/opl_files/file47.opl", 43, "f47G"); - LoadMiles("fm_banks/opl_files/file48.opl", 44, "f48G"); - LoadMiles("fm_banks/opl_files/file49.opl", 45, "f49G"); - LoadMiles("fm_banks/opl_files/file50.opl", 46, "f50G"); - LoadMiles("fm_banks/opl_files/file53.opl", 47, "f53G"); - LoadBNK("fm_banks/bnk_files/file144.bnk", 47, "f53GD", false, true);//Attempt to append missing drums - LoadMiles("fm_banks/opl_files/file54.opl", 48, "f54G"); - - LoadMiles("fm_banks/opl_files/sample.ad", 49, "MG"); // same as file51.opl - LoadMiles("fm_banks/opl_files/sample.opl", 50, "oG"); // same as file40.opl - LoadMiles("fm_banks/opl_files/file15.opl", 51, "f15G"); - LoadMiles("fm_banks/opl_files/file16.opl", 52, "f16G"); - - LoadBNK2("fm_banks/bnk_files/file159.bnk", 53, "b50", "gm","gps"); // fat-opl3 - LoadBNK2("fm_banks/bnk_files/file159.bnk", 54, "b51", "gm","gpo"); - - LoadIBK("fm_banks/ibk_files/soccer-genmidi.ibk", 55, "b55M", false); - LoadIBK("fm_banks/ibk_files/soccer-percs.ibk", 55, "b55P", true); - LoadIBK("fm_banks/ibk_files/game.ibk", 56, "b56", false); - LoadIBK("fm_banks/ibk_files/mt_fm.ibk", 57, "b57", false); - - LoadJunglevision("fm_banks/op3_files/fat2.op3", 58, "fat2"); - LoadJunglevision("fm_banks/op3_files/fat4.op3", 59, "fat4"); - LoadJunglevision("fm_banks/op3_files/jv_2op.op3", 60, "b60"); - LoadJunglevision("fm_banks/op3_files/wallace.op3", 61, "b61"); - - LoadTMB("fm_banks/tmb_files/d3dtimbr.tmb", 62, "duke"); - LoadTMB("fm_banks/tmb_files/swtimbr.tmb", 63, "sw"); - - LoadDoom("fm_banks/raptor/genmidi.op2", 64, "rapt"); - - //LoadJunglevision("fm_banks/op3_files/fat2_modded.op3", 65, "b65M"); - LoadTMB("fm_banks/op3_files/gmopl_wohl_mod.tmb", 65, "b65"); - - //LoadIBK("fm_banks/ibk_files/JOconnel.IBK", 66, "b66M", false); - //LoadIBK("fm_banks/ibk_files/my-gmopldrums.IBK", 66, "b66P", true); - LoadTMB("fm_banks/op3_files/gmoconel.tmb", 66, "b66"); - - LoadTMB("fm_banks/tmb_files/default.tmb", 67, "3drm67"); - //LoadDoom("fm_banks/doom2/wolfinstein.op2", 67, "wolf"); //Small experiment! - - //LoadJunglevision("fm_banks/op3_files/2x2.op3", 68, "2x2byJAN"); - LoadMiles("fm_banks/op3_files/2x2.opl", 68, "2x2byJAN"); - - LoadTMB("fm_banks/tmb_files/bloodtmb.tmb", 69, "apgblood"); - LoadTMB("fm_banks/tmb_files/lee.tmb", 70, "apglee"); - LoadTMB("fm_banks/tmb_files/nam.tmb", 71, "apgnam"); - - LoadDoom("fm_banks/doom2/DMXOPL-by-sneakernets.op2", 72, "skeakernets"); - - //LoadBNK("bnk_files/grassman1.bnk", 63, "b63", false); - //LoadBNK("bnk_files/grassman2.bnk", 64, "b64", false); - - //LoadIBK("ibk_files/nitemare_3d.ibk", 65, "b65G", false); // Seems to be identical to wallace.op3 despite different format! - - static const char* const banknames[] = - {// 0 - "AIL (Star Control 3, Albion, Empire 2, Sensible Soccer, Settlers 2, many others)", - "Bisqwit (selection of 4op and 2op)", - "HMI (Descent, Asterix)", //melodic,drum - "HMI (Descent:: Int)", //intmelo,intdrum - "HMI (Descent:: Ham)", //hammelo,hamdrum - "HMI (Descent:: Rick)", //rickmelo,rickdrum - "HMI (Descent 2)", //d2melo,d2drum - "HMI (Normality)", //normmelo,normdrum - "HMI (Shattered Steel)", //ssmelo,ssdrum - "HMI (Theme Park)", // file131, file132 - // 10 - "HMI (3d Table Sports, Battle Arena Toshinden)", //file133, file134 - "HMI (Aces of the Deep)", //file142, file143 - "HMI (Earthsiege)", //file144, file145 - "HMI (Anvil of Dawn)", //file167,file168 - "DMX (Doom :: partially pseudo 4op)", - "DMX (Hexen, Heretic :: partially pseudo 4op)", - "DMX (MUS Play :: partially pseudo 4op)", - "AIL (Discworld, Grandest Fleet, Pocahontas, Slob Zone 3d, Ultima 4, Zorro)", // file17 - "AIL (Warcraft 2)", - "AIL (Syndicate)", // file19 - // 20 - "AIL (Guilty, Orion Conspiracy, Terra Nova Strike Force Centauri :: 4op)", // file20 - "AIL (Magic Carpet 2)", // file21 - "AIL (Nemesis)", - "AIL (Jagged Alliance)", //file23 - "AIL (When Two Worlds War :: 4op, MISSING INSTRUMENTS)", //file24 - "AIL (Bards Tale Construction :: MISSING INSTRUMENTS)", //file25 - "AIL (Return to Zork)", //file26 - "AIL (Theme Hospital)", //file27 - "AIL (National Hockey League PA)", - "AIL (Inherit The Earth)", //file29 - // 30 - "AIL (Inherit The Earth, file two)", //file30 - "AIL (Little Big Adventure :: 4op)", //file31 - "AIL (Wreckin Crew)", //file32 - "AIL (Death Gate)", // file13 - "AIL (FIFA International Soccer)", //file34 - "AIL (Starship Invasion)", //file35 - "AIL (Super Street Fighter 2 :: 4op)", //file36 - "AIL (Lords of the Realm :: MISSING INSTRUMENTS)", //file37 - "AIL (SimFarm, SimHealth :: 4op)", - "AIL (SimFarm, Settlers, Serf City)", - // 40 - "AIL (Caesar 2 :: partially 4op, MISSING INSTRUMENTS)", // file12 - "AIL (Syndicate Wars)", //file41 - "AIL (Bubble Bobble Feat. Rainbow Islands, Z)", //file42 - "AIL (Warcraft)", //file47 - "AIL (Terra Nova Strike Force Centuri :: partially 4op)", //file48 - "AIL (System Shock :: partially 4op)", //file49 - "AIL (Advanced Civilization)", //file50 - "AIL (Battle Chess 4000 :: partially 4op, melodic only)", //file53 - "AIL (Ultimate Soccer Manager :: partially 4op)", //file54 - "AIL (Air Bucks, Blue And The Gray, America Invades, Terminator 2029)", // sample.ad - // 50 - "AIL (Ultima Underworld 2)", // sample.opl - "AIL (Kasparov's Gambit)", // file15 - "AIL (High Seas Trader :: MISSING INSTRUMENTS)", // file16 - "AIL (Master of Magic, Master of Orion 2 :: 4op, std percussion)", //file159 - "AIL (Master of Magic, Master of Orion 2 :: 4op, orchestral percussion)", //file159 - "SB (Action Soccer)", - "SB (3d Cyberpuck :: melodic only)", - "SB (Simon the Sorcerer :: melodic only)", - "OP3 (The Fat Man 2op set)", - "OP3 (The Fat Man 4op set)", - // 60 - "OP3 (JungleVision 2op set :: melodic only)", - "OP3 (Wallace 2op set, Nitemare 3D :: melodic only)", - "TMB (Duke Nukem 3D)", - "TMB (Shadow Warrior)", - "DMX (Raptor)", - "OP3 (Modded GMOPL by Wohlstand)", - "SB (Jammey O'Connel's bank)", - "TMB (Default bank of Build Engine)", - "OP3 (4op bank by James Alan Nguyen)", - "TMB (Blood)", - // 70 - "TMB (Lee)", - "TMB (Nam)", - "DMX (Bank by Sneakernets)" - }; - -#if 0 - for(unsigned a=0; a<36*8; ++a) - { - if( (1 << (a%8)) > maxvalues[a/8]) continue; - - const std::map<unsigned,unsigned>& data = Correlate[a]; + const std::map<unsigned, unsigned> &data = Correlate[a]; if(data.empty()) continue; - std::vector< std::pair<unsigned,unsigned> > correlations; - for(std::map<unsigned,unsigned>::const_iterator + std::vector< std::pair<unsigned, unsigned> > correlations; + for(std::map<unsigned, unsigned>::const_iterator i = data.begin(); i != data.end(); ++i) { - correlations.push_back( std::make_pair( i->second,i->first ) ); + correlations.push_back(std::make_pair(i->second, i->first)); } std::sort(correlations.begin(), correlations.end()); - printf("Byte %2u bit %u=mask %02X:\n", a/8, a%8, 1<<(a%8)); - for(size_t c=0; c<correlations.size() && c < 10; ++c) - { - unsigned count = correlations[correlations.size()-1-c ].first; - unsigned index = correlations[correlations.size()-1-c ].second; - printf("\tAdldata index %u, bit %u=mask %02X (%u matches)\n", - index/8, index%8, 1 << (index%8), count); - } - } -#endif - - printf( - /* - "static const struct\n" - "{\n" - " unsigned modulator_E862, carrier_E862; // See below\n" - " unsigned char modulator_40, carrier_40; // KSL/attenuation settings\n" - " unsigned char feedconn; // Feedback/connection bits for the channel\n" - " signed char finetune; // Finetune\n" - "} adl[] =\n"*/ - "const adldata adl[%u] =\n" - "{ // ,---------+-------- Wave select settings\n" - " // | ,-------ч-+------ Sustain/release rates\n" - " // | | ,-----ч-ч-+---- Attack/decay rates\n" - " // | | | ,---ч-ч-ч-+-- AM/VIB/EG/KSR/Multiple bits\n" - " // | | | | | | | |\n" - " // | | | | | | | | ,----+-- KSL/attenuation settings\n" - " // | | | | | | | | | | ,----- Feedback/connection bits\n" - " // | | | | | | | | | | |\n", (unsigned)insdatatab.size()); - for(size_t b=insdatatab.size(), c=0; c<b; ++c) - for(std::map<insdata,std::pair<size_t,std::set<std::string> > > - ::const_iterator - i = insdatatab.begin(); - i != insdatatab.end(); - ++i) - { - if(i->second.first != c) continue; - printf(" { "); - - unsigned carrier_E862 = - (i->first.data[6] << 24) - + (i->first.data[4] << 16) - + (i->first.data[2] << 8) - + (i->first.data[0] << 0); - unsigned modulator_E862 = - (i->first.data[7] << 24) - + (i->first.data[5] << 16) - + (i->first.data[3] << 8) - + (i->first.data[1] << 0); - printf("0x%07X,0x%07X, 0x%02X,0x%02X, 0x%X, %+d, %s", - carrier_E862, - modulator_E862, - i->first.data[8], - i->first.data[9], - i->first.data[10], - i->first.finetune, - i->first.diff?"true":"false"); - - std::string names; - for(std::set<std::string>::const_iterator - j = i->second.second.begin(); - j != i->second.second.end(); - ++j) + fprintf(outFile, "Byte %2u bit %u=mask %02X:\n", a / 8, a % 8, 1 << (a % 8)); + for(size_t c = 0; c < correlations.size() && c < 10; ++c) + { + unsigned count = correlations[correlations.size() - 1 - c ].first; + unsigned index = correlations[correlations.size() - 1 - c ].second; + fprintf(outFile, "\tAdldata index %u, bit %u=mask %02X (%u matches)\n", + index / 8, index % 8, 1 << (index % 8), count); + } + } + #endif + + printf("Writing raw instrument data...\n"); + fflush(stdout); + { + fprintf(outFile, + /* + "static const struct\n" + "{\n" + " unsigned modulator_E862, carrier_E862; // See below\n" + " unsigned char modulator_40, carrier_40; // KSL/attenuation settings\n" + " unsigned char feedconn; // Feedback/connection bits for the channel\n" + " signed char finetune; // Finetune\n" + "} adl[] =\n"*/ + "const adldata adl[%u] =\n" + "{ // ,---------+-------- Wave select settings\n" + " // | ,-------ч-+------ Sustain/release rates\n" + " // | | ,-----ч-ч-+---- Attack/decay rates\n" + " // | | | ,---ч-ч-ч-+-- AM/VIB/EG/KSR/Multiple bits\n" + " // | | | | | | | |\n" + " // | | | | | | | | ,----+-- KSL/attenuation settings\n" + " // | | | | | | | | | | ,----- Feedback/connection bits\n" + " // | | | | | | | | | | | ,----- Fine tune\n\n" + " // | | | | | | | | | | | |\n" + " // | | | | | | | | | | | |\n", (unsigned)insdatatab.size()); + + for(size_t b = insdatatab.size(), c = 0; c < b; ++c) + { + for(std::map<insdata, std::pair<size_t, std::set<std::string> > > + ::const_iterator + i = insdatatab.begin(); + i != insdatatab.end(); + ++i) { - if(!names.empty()) names += "; "; - if((*j)[0] == '\377') - names += j->substr(1); - else - names += *j; + if(i->second.first != c) continue; + fprintf(outFile, " { "); + + uint32_t carrier_E862 = + uint32_t(i->first.data[6] << 24) + + uint32_t(i->first.data[4] << 16) + + uint32_t(i->first.data[2] << 8) + + uint32_t(i->first.data[0] << 0); + uint32_t modulator_E862 = + uint32_t(i->first.data[7] << 24) + + uint32_t(i->first.data[5] << 16) + + uint32_t(i->first.data[3] << 8) + + uint32_t(i->first.data[1] << 0); + + fprintf(outFile, "0x%07X,0x%07X, 0x%02X,0x%02X, 0x%X, %+d", + carrier_E862, + modulator_E862, + i->first.data[8], + i->first.data[9], + i->first.data[10], + i->first.finetune); + + std::string names; + for(std::set<std::string>::const_iterator + j = i->second.second.begin(); + j != i->second.second.end(); + ++j) + { + if(!names.empty()) names += "; "; + if((*j)[0] == '\377') + names += j->substr(1); + else + names += *j; + } + fprintf(outFile, " }, // %u: %s\n", (unsigned)c, names.c_str()); } - printf(" }, // %u: %s\n", (unsigned)c, names.c_str()); } - printf("};\n"); - /*printf("static const struct\n" + fprintf(outFile, "};\n"); + } + + /*fprintf(outFile, "static const struct\n" "{\n" " unsigned short adlno1, adlno2;\n" " unsigned char tone;\n" @@ -1324,30 +315,79 @@ int main() " long ms_sound_kon; // Number of milliseconds it produces sound;\n" " long ms_sound_koff;\n" "} adlins[] =\n");*/ - printf("const struct adlinsdata adlins[%u] =\n", (unsigned)instab.size()); - printf("{\n"); + + fprintf(outFile, "const struct adlinsdata adlins[%u] =\n", (unsigned)instab.size()); + fprintf(outFile, "{\n"); + + MeasureThreaded measureCounter; + { + printf("Beginning to generate measures data...\n"); + fflush(stdout); + measureCounter.LoadCache("fm_banks/adldata-cache.dat"); + measureCounter.m_total = instab.size(); + for(size_t b = instab.size(), c = 0; c < b; ++c) + { + for(std::map<ins, std::pair<size_t, std::set<std::string> > >::const_iterator i = instab.begin(); i != instab.end(); ++i) + { + if(i->second.first != c) continue; + measureCounter.run(i); + } + } + fflush(stdout); + measureCounter.waitAll(); + measureCounter.SaveCache("fm_banks/adldata-cache.dat"); + } + + printf("Writing generated measure data...\n"); + fflush(stdout); + std::vector<unsigned> adlins_flags; - for(size_t b=instab.size(), c=0; c<b; ++c) - for(std::map<ins,std::pair<size_t,std::set<std::string> > > + + for(size_t b = instab.size(), c = 0; c < b; ++c) + for(std::map<ins, std::pair<size_t, std::set<std::string> > > ::const_iterator i = instab.begin(); i != instab.end(); ++i) { if(i->second.first != c) continue; + //DurationInfo info = MeasureDurations(i->first); + MeasureThreaded::DurationInfoCache::iterator indo_i = measureCounter.m_durationInfo.find(i->first); + DurationInfo info = indo_i->second; + { + if(info.peak_amplitude_time == 0) + { + fprintf(outFile, + " // Amplitude begins at %6.1f,\n" + " // fades to 20%% at %.1fs, keyoff fades to 20%% in %.1fs.\n", + info.begin_amplitude, + info.quarter_amplitude_time / double(info.interval), + info.keyoff_out_time / double(info.interval)); + } + else + { + fprintf(outFile, + " // Amplitude begins at %6.1f, peaks %6.1f at %.1fs,\n" + " // fades to 20%% at %.1fs, keyoff fades to 20%% in %.1fs.\n", + info.begin_amplitude, + info.peak_amplitude_value, + info.peak_amplitude_time / double(info.interval), + info.quarter_amplitude_time / double(info.interval), + info.keyoff_out_time / double(info.interval)); + } + } - DurationInfo info = MeasureDurations(i->first); unsigned flags = (i->first.pseudo4op ? 1 : 0) | (info.nosound ? 2 : 0); - printf(" {"); - printf("%4d,%4d,%3d, %d, %6ld,%6ld,%lf", - (unsigned) i->first.insno1, - (unsigned) i->first.insno2, - (int)(i->first.notenum), - flags, - info.ms_sound_kon, - info.ms_sound_koff, - i->first.fine_tune); + fprintf(outFile, " {"); + fprintf(outFile, "%4d,%4d,%3d, %d, %6ld,%6ld,%lf", + (unsigned) i->first.insno1, + (unsigned) i->first.insno2, + (int)(i->first.notenum), + flags, + info.ms_sound_kon, + info.ms_sound_koff, + i->first.fine_tune); std::string names; for(std::set<std::string>::const_iterator j = i->second.second.begin(); @@ -1360,23 +400,34 @@ int main() else names += *j; } - printf(" }, // %u: %s\n\n", (unsigned)c, names.c_str()); - fflush(stdout); + fprintf(outFile, " }, // %u: %s\n\n", (unsigned)c, names.c_str()); + fflush(outFile); adlins_flags.push_back(flags); } - printf("};\n\n"); + fprintf(outFile, "};\n\n"); + + + printf("Writing banks data...\n"); + fflush(stdout); + + //fprintf(outFile, "static const unsigned short banks[][256] =\n"); + #ifdef HARD_BANKS + const unsigned bankcount = sizeof(banknames) / sizeof(*banknames); + #else + const size_t bankcount = banknames.size(); + #endif + + size_t nosound = InsertNoSoundIns(); - //printf("static const unsigned short banks[][256] =\n"); - const unsigned bankcount = sizeof(banknames)/sizeof(*banknames); - std::map<unsigned, std::vector<unsigned> > bank_data; - for(unsigned bank=0; bank<bankcount; ++bank) + std::map<size_t, std::vector<size_t> > bank_data; + for(size_t bank = 0; bank < bankcount; ++bank) { //bool redundant = true; - std::vector<unsigned> data(256); - for(unsigned p=0; p<256; ++p) + std::vector<size_t> data(256); + for(size_t p = 0; p < 256; ++p) { - unsigned v = progs[bank][p]; - if(v == 0 || (adlins_flags[v-1]&2)) + size_t v = progs[bank][p]; + if(v == 0 || (adlins_flags[v - 1] & 2)) v = nosound; // Blank.in else v -= 1; @@ -1384,57 +435,62 @@ int main() } bank_data[bank] = data; } - std::set<unsigned> listed; - - printf( - "\n\n//Returns total number of generated banks\n" - "int maxAdlBanks()\n" - "{" - " return %u;\n" - "}\n\n" - "const char* const banknames[%u] =\n", bankcount, bankcount); - printf("{\n"); - for(unsigned bank=0; bank<bankcount; ++bank) - printf(" \"%s\",\n", banknames[bank]); - printf("};\n"); - - printf("const unsigned short banks[%u][256] =\n", bankcount); - printf("{\n"); - for(unsigned bank=0; bank<bankcount; ++bank) - { - printf(" { // bank %u, %s\n", bank, banknames[bank]); + std::set<size_t> listed; + + fprintf(outFile, + "\n\n//Returns total number of generated banks\n" + "int maxAdlBanks()\n" + "{" + " return %u;\n" + "}\n\n" + "const char* const banknames[%u] =\n", (unsigned int)bankcount, (unsigned int)bankcount); + fprintf(outFile, "{\n"); + for(unsigned bank = 0; bank < bankcount; ++bank) + fprintf(outFile, " \"%s\",\n", banknames[bank].c_str()); + fprintf(outFile, "};\n"); + + fprintf(outFile, "const unsigned short banks[%u][256] =\n", (unsigned int)bankcount); + fprintf(outFile, "{\n"); + for(unsigned bank = 0; bank < bankcount; ++bank) + { + fprintf(outFile, " { // bank %u, %s\n", bank, banknames[bank].c_str()); bool redundant = true; - for(unsigned p=0; p<256; ++p) + for(unsigned p = 0; p < 256; ++p) { - unsigned v = bank_data[bank][p]; + size_t v = bank_data[bank][p]; if(listed.find(v) == listed.end()) { listed.insert(v); redundant = false; } - printf("%4d,", v); - if(p%16 == 15) printf("\n"); + fprintf(outFile, "%4d,", (unsigned int)v); + if(p % 16 == 15) fprintf(outFile, "\n"); } - printf(" },\n"); + fprintf(outFile, " },\n"); if(redundant) { - printf(" // Bank %u defines nothing new.\n", bank); - for(unsigned refbank=0; refbank<bank; ++refbank) + fprintf(outFile, " // Bank %u defines nothing new.\n", bank); + for(unsigned refbank = 0; refbank < bank; ++refbank) { bool match = true; - for(unsigned p=0; p<256; ++p) + for(unsigned p = 0; p < 256; ++p) if(bank_data[bank][p] != nosound - && bank_data[bank][p] != bank_data[refbank][p]) + && bank_data[bank][p] != bank_data[refbank][p]) { - match=false; + match = false; break; } if(match) - printf(" // Bank %u is just a subset of bank %u!\n", - bank, refbank); + fprintf(outFile, " // Bank %u is just a subset of bank %u!\n", + bank, refbank); } } } - printf("};\n"); + fprintf(outFile, "};\n"); + fflush(outFile); + fclose(outFile); + + printf("Generation of ADLMIDI data has been completed!\n"); + fflush(stdout); } diff --git a/src/gen_adldata/gen_adldata.pro b/src/gen_adldata/gen_adldata.pro new file mode 100644 index 0000000..f60d3a2 --- /dev/null +++ b/src/gen_adldata/gen_adldata.pro @@ -0,0 +1,36 @@ +TEMPLATE=app +CONFIG-=qt +CONFIG+=console +TARGET=gen_adldata +DESTDIR=$$PWD/../../bin +CONFIG += c++11 + +include($$PWD/ini/IniProcessor.pri) + +#DEFINES += ADLMIDI_USE_DOSBOX_OPL + +QMAKE_CXXFLAGS_RELEASE += -O3 -finline-functions +LIBS += -lpthread + +HEADERS += \ + midi_inst_list.h \ + ../nukedopl3.h \ + ../dbopl.h \ + progs_cache.h \ + file_formats/load_bnk.h \ + file_formats/load_bnk2.h \ + file_formats/load_op2.h \ + file_formats/load_ail.h \ + file_formats/load_ibk.h \ + file_formats/load_jv.h \ + file_formats/load_tmb.h \ + file_formats/load_bisqwit.h \ + measurer.h + +SOURCES += \ + gen_adldata.cc \ + ../nukedopl3.c \ + ../dbopl.cpp \ + progs_cache.cpp \ + measurer.cpp + diff --git a/src/gen_adldata/ini/IniProcessor.pri b/src/gen_adldata/ini/IniProcessor.pri new file mode 100644 index 0000000..cdadb59 --- /dev/null +++ b/src/gen_adldata/ini/IniProcessor.pri @@ -0,0 +1,10 @@ + +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/ini_processing.h \ + $$PWD/ini_processing_variant.h \ + +SOURCES += \ + $$PWD/ini_processing.cpp + diff --git a/src/gen_adldata/ini/ini_processing.cpp b/src/gen_adldata/ini/ini_processing.cpp new file mode 100644 index 0000000..2f15821 --- /dev/null +++ b/src/gen_adldata/ini/ini_processing.cpp @@ -0,0 +1,1385 @@ +/* +INI Processor - a small library which allows you parsing INI-files + +Copyright (c) 2017 Vitaliy Novichkov <admin@wohlnet.ru> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +*/ + +//#define USE_FILE_MAPPER + +/* Stop parsing on first error (default is to keep parsing). */ +//#define INI_STOP_ON_FIRST_ERROR + +#include "ini_processing.h" +#include <cstdio> +#include <cstring> +#include <cstdlib> +#include <clocale> +#include <sstream> +#include <algorithm> +#include <assert.h> +#ifdef _WIN32 +#include <windows.h> +#endif + +#ifdef USE_FILE_MAPPER +/*****Replace this with right path to file mapper class*****/ +#include "../fileMapper/file_mapper.h" +#endif + +static const unsigned char utfbom[3] = {0xEF, 0xBB, 0xBF}; + +enum { Space = 0x01, Special = 0x02, INIParamEq = 0x04 }; + +static const unsigned char charTraits[256] = +{ + // Space: '\t', '\n', '\r', ' ' + // Special: '\n', '\r', '"', ';', '=', '\\' + // INIParamEq: ':', '=' + + 0, 0, 0, 0, 0, 0, 0, 0, 0, Space, Space | Special, 0, 0, Space | Special, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Space, 0, Special, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INIParamEq, + Special, 0, Special | INIParamEq, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Special, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +#if 0//for speed comparison who faster - macro or inline function. Seems speeds are same +#define IS_SPACE(c) (charTraits[static_cast<unsigned char>(c)] & Space) +#define IS_SPECIAL(c) (charTraits[static_cast<unsigned char>(c)] & Special) +#define IS_INIEQUAL(c) (charTraits[static_cast<unsigned char>(c)] & INIParamEq) +#else +inline unsigned char IS_SPACE(char &c) +{ + return (charTraits[static_cast<unsigned char>(c)] & Space); +} +inline unsigned char IS_SPECIAL(char &c) +{ + return (charTraits[static_cast<unsigned char>(c)] & Special); +} +inline unsigned char IS_INIEQUAL(char &c) +{ + return (charTraits[static_cast<unsigned char>(c)] & INIParamEq); +} +#endif + +/* Strip whitespace chars off end of given string, in place. Return s. */ +inline char *rstrip(char *s) +{ + char *p = s + strlen(s); + + while(p > s && IS_SPACE(*--p)) + *p = '\0'; + + return s; +} + +/* Return pointer to first non-whitespace char in given string. */ +inline char *lskip(char *s) +{ + while(*s && IS_SPACE(*s)) + s++; + + return reinterpret_cast<char *>(s); +} + +inline char *lrtrim(char *s) +{ + while(*s && IS_SPACE(*s)) + s++; + + char *p = s + strlen(s); + + while(p > s && IS_SPACE(*--p)) + *p = '\0'; + + return s; +} + +/* Return pointer to first char c or ';' comment in given string, or pointer to + null at end of string if neither found. ';' must be prefixed by a whitespace + character to register as a comment. */ +inline char *find_char_or_comment(char *s, char c) +{ + unsigned char was_whitespace = 0; + + while(*s && *s != c && !(was_whitespace && *s == ';')) + { + was_whitespace = IS_SPACE(*s); + s++; + } + + return s; +} + +inline char *find_inieq_or_comment(char *s) +{ + unsigned char was_whitespace = 0; + + while(*s && (!IS_INIEQUAL(*s)) && !(was_whitespace && *s == ';')) + { + was_whitespace = IS_SPACE(*s); + s++; + } + + return s; +} + +inline char *removeQuotes(char *begin, char *end) +{ + if((*begin == '\0') || (begin == end)) + return begin; + + if((*begin == '"') && (begin + 1 != end)) + begin++; + else + return begin; + + if(*(end - 1) == '"') + *(end - 1) = '\0'; + + return begin; +} + +inline char *unescapeString(char* str) +{ + char *src, *dst; + src = str; + dst = str; + while(*src) + { + if(*src == '\\') + { + src++; + switch(*src) + { + case 'n': *dst = '\n'; break; + case 'r': *dst = '\r'; break; + case 't': *dst = '\t'; break; + default: *dst = *src; break; + } + } + else + if(src != dst) + { + *dst = *src; + } + src++; dst++; + } + *dst = '\0'; + return str; +} + +//Remove comment line from a tail of value +inline void skipcomment(char *value) +{ + unsigned char quoteDepth = 0; + + while(*value) + { + if(quoteDepth > 0) + { + if(*value == '\\') + { + value++; + continue; + } + + if(*value == '"') + --quoteDepth; + } + else if(*value == '"') + ++quoteDepth; + + if((quoteDepth == 0) && (*value == ';')) + { + *value = '\0'; + break; + } + + value++; + } +} + +inline bool memfgets(char *&line, char *data, char *&pos, char *end) +{ + line = pos; + + while(pos != end) + { + if(*pos == '\n') + { + if((pos > data) && (*(pos - 1) == '\r')) + *((pos++) - 1) = '\0';//Support CRLF too + else + *(pos++) = '\0'; + + break; + } + + ++pos; + } + + return (pos != line); + //EOF is a moment when position wasn't changed. + //If do check "pos != end", will be an inability to read last line. + //this logic allows detect true EOF when line is really eof +} + +/* See documentation in header file. */ +bool IniProcessing::parseHelper(char *data, size_t size) +{ + char *section = nullptr; + #if defined(INI_ALLOW_MULTILINE) + char *prev_name = nullptr; + #endif + char *start; + char *end; + char *name; + char *value; + int lineno = 0; + int error = 0; + char *line; + char *pos_end = data + size; + char *pos_cur = data; + params::IniKeys *recentKeys = nullptr; + + /* Scan through file line by line */ + //while (fgets(line, INI_MAX_LINE, file) != NULL) + while(memfgets(line, data, pos_cur, pos_end)) + { + lineno++; + start = line; + + if((lineno == 1) && (size >= 3) && (memcmp(start, utfbom, 3) == 0)) + start += 3; + + start = lrtrim(start); + + if(!*start)//if empty line - skip it away! + continue; + + switch(*start) + { + case ';': + case '#': + //if (*start == ';' || *start == '#') { + // /* Per Python ConfigParser, allow '#' comments at start of line */ + //} + continue; + + case '[': + { + /* A "[section]" line */ + end = find_char_or_comment(start + 1, ']'); + + if(*end == ']') + { + *end = '\0'; + section = start + 1; + //#if defined(INI_ALLOW_MULTILINE) + // prev_name = nullptr; + //#endif + recentKeys = &m_params.iniData[section]; + } + else if(!error) + { + /* No ']' found on section line */ + m_params.errorCode = ERR_SECTION_SYNTAX; + error = lineno; + } + } + break; + + default: + { + /* Not a comment, must be a name[=:]value pair */ + end = find_inieq_or_comment(start); + + if(IS_INIEQUAL(*end)) + { + *end = '\0'; + name = rstrip(start); + value = lskip(end + 1); + end = find_char_or_comment(value, '\0'); + + #ifndef CASE_SENSITIVE_KEYS + for(char *iter = name; *iter != '\0'; ++iter) + *iter = (char)tolower(*iter); + #endif + + if(*end == ';') + *end = '\0'; + + rstrip(value); + { + char *v = value; + skipcomment(v); + v = rstrip(v); + + if(!recentKeys) + recentKeys = &m_params.iniData["General"]; + + #ifdef INIDEBUG + printf("-> [%s]; %s = %s\n", section, name, v); + #endif + (*recentKeys)[name] = unescapeString( removeQuotes(v, v + strlen(v)) ); + } + } + else if(!error) + { + /* No '=' or ':' found on name[=:]value line */ + m_params.errorCode = ERR_KEY_SYNTAX; + error = lineno; + } + + break; + } + }//switch(*start) + + #if defined(INI_STOP_ON_FIRST_ERROR) + + if(error) + break; + + #endif + } + + m_params.lineWithError = error; + return (error == 0); +} + +/* See documentation in header file. */ +bool IniProcessing::parseFile(const char *filename) +{ + bool valid = true; + char *tmp = nullptr; + #ifdef USE_FILE_MAPPER + //By mystical reasons, reading whole file form fread() is faster than mapper :-P + PGE_FileMapper file(filename); + + if(!file.data) + { + m_params.errorCode = ERR_NOFILE; + return -1; + } + + tmp = reinterpret_cast<char *>(malloc(static_cast<size_t>(file.size + 1))); + + if(!tmp) + { + m_params.errorCode = ERR_NO_MEMORY; + return false; + } + + memcpy(tmp, file.data, static_cast<size_t>(file.size)); + *(tmp + file.size) = '\0';//null terminate last line + valid = ini_parse_file(tmp, static_cast<size_t>(file.size)); + #else + #ifdef _WIN32 + //Convert UTF8 file path into UTF16 to support non-ASCII paths on Windows + std::wstring dest; + dest.resize(std::strlen(filename)); + int newSize = MultiByteToWideChar(CP_UTF8, + 0, + filename, + dest.size(), + (wchar_t *)dest.c_str(), + dest.size()); + dest.resize(newSize); + FILE *cFile = _wfopen(dest.c_str(), L"rb"); + #else + FILE *cFile = fopen(filename, "rb"); + #endif + + if(!cFile) + { + m_params.errorCode = ERR_NOFILE; + return false; + } + + fseek(cFile, 0, SEEK_END); + ssize_t size = static_cast<ssize_t>(ftell(cFile)); + if(size < 0) + { + m_params.errorCode = ERR_KEY_SYNTAX; + fclose(cFile); + return false; + } + fseek(cFile, 0, SEEK_SET); + tmp = reinterpret_cast<char *>(malloc(static_cast<size_t>(size + 1))); + if(!tmp) + { + fclose(cFile); + m_params.errorCode = ERR_NO_MEMORY; + return false; + } + + if(fread(tmp, 1, static_cast<size_t>(size), cFile) != static_cast<size_t>(size)) + valid = false; + + fclose(cFile); + if(valid) + { + *(tmp + size) = '\0';//null terminate last line + try + { + valid = parseHelper(tmp, static_cast<size_t>(size)); + } + catch(...) + { + valid = false; + m_params.errorCode = ERR_SECTION_SYNTAX; + } + } + #endif + + free(tmp); + return valid; +} + +bool IniProcessing::parseMemory(char *mem, size_t size) +{ + bool valid = true; + char *tmp = nullptr; + tmp = reinterpret_cast<char *>(malloc(size + 1)); + + if(!tmp) + { + m_params.errorCode = ERR_NO_MEMORY; + return false; + } + + memcpy(tmp, mem, static_cast<size_t>(size)); + *(tmp + size) = '\0';//null terminate last line + valid = parseHelper(tmp, size); + free(tmp); + return valid; +} + + +IniProcessing::IniProcessing() : + m_params{"", false, -1, ERR_OK, false, params::IniSections(), nullptr, ""} +{} + +IniProcessing::IniProcessing(const char *iniFileName, int) : + m_params{iniFileName, false, -1, ERR_OK, false, params::IniSections(), nullptr, ""} +{ + open(iniFileName); +} + +IniProcessing::IniProcessing(const std::string &iniFileName, int) : + m_params{iniFileName, false, -1, ERR_OK, false, params::IniSections(), nullptr, ""} +{ + open(iniFileName); +} + +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +IniProcessing::IniProcessing(const QString &iniFileName, int) : + m_params{iniFileName.toStdString(), false, -1, ERR_OK, false, params::IniSections(), nullptr, ""} +{ + open(m_params.filePath); +} +#endif + +IniProcessing::IniProcessing(char *memory, size_t size): + m_params{"", false, -1, ERR_OK, false, params::IniSections(), nullptr, ""} +{ + openMem(memory, size); +} + +IniProcessing::IniProcessing(const IniProcessing &ip) : + m_params(ip.m_params) +{} + +bool IniProcessing::open(const std::string &iniFileName) +{ + std::setlocale(LC_NUMERIC, "C"); + + if(!iniFileName.empty()) + { + close(); + m_params.errorCode = ERR_OK; + m_params.filePath = iniFileName; + bool res = parseFile(m_params.filePath.c_str()); + #ifdef INIDEBUG + + if(res) + printf("\n==========WOOHOO!!!==============\n\n"); + else + printf("\n==========OOOUCH!!!==============\n\n"); + + #endif + m_params.opened = res; + return res; + } + + m_params.errorCode = ERR_NOFILE; + return false; +} + +bool IniProcessing::openMem(char *memory, size_t size) +{ + std::setlocale(LC_NUMERIC, "C"); + + if((memory != nullptr) && (size > 0)) + { + close(); + m_params.errorCode = ERR_OK; + m_params.filePath.clear(); + bool res = parseMemory(memory, size); + m_params.opened = res; + return res; + } + + m_params.errorCode = ERR_NOFILE; + return false; +} + +void IniProcessing::close() +{ + m_params.errorCode = ERR_OK; + m_params.iniData.clear(); + m_params.opened = false; + m_params.lineWithError = -1; +} + +IniProcessing::ErrCode IniProcessing::lastError() +{ + return m_params.errorCode; +} + +int IniProcessing::lineWithError() +{ + return m_params.lineWithError; +} + +bool IniProcessing::isOpened() +{ + return m_params.opened; +} + +bool IniProcessing::beginGroup(const std::string &groupName) +{ + //Keep the group name. If not exist, will be created on value write + m_params.currentGroupName = groupName; + + params::IniSections::iterator e = m_params.iniData.find(groupName); + + if(e == m_params.iniData.end()) + return false; + + params::IniKeys &k = e->second; + m_params.currentGroup = &k; + return true; +} + +bool IniProcessing::contains(const std::string &groupName) +{ + if(!m_params.opened) + return false; + + params::IniSections::iterator e = m_params.iniData.find(groupName); + return (e != m_params.iniData.end()); +} + +std::string IniProcessing::fileName() +{ + return m_params.filePath; +} + +std::string IniProcessing::group() +{ + return m_params.currentGroupName; +} + +std::vector<std::string> IniProcessing::childGroups() +{ + std::vector<std::string> groups; + groups.reserve(m_params.iniData.size()); + for(params::IniSections::iterator e = m_params.iniData.begin(); + e != m_params.iniData.end(); + e++) + { + groups.push_back(e->first); + } + return groups; +} + +bool IniProcessing::hasKey(const std::string &keyName) +{ + if(!m_params.opened) + return false; + + if(!m_params.currentGroup) + return false; + + params::IniKeys::iterator e = m_params.currentGroup->find(keyName); + return (e != m_params.currentGroup->end()); +} + +std::vector<std::string> IniProcessing::allKeys() +{ + std::vector<std::string> keys; + if(!m_params.opened) + return keys; + if(!m_params.currentGroup) + return keys; + + keys.reserve(m_params.currentGroup->size()); + + for(params::IniKeys::iterator it = m_params.currentGroup->begin(); + it != m_params.currentGroup->end(); + it++) + { + keys.push_back( it->first ); + } + + return keys; +} + +void IniProcessing::endGroup() +{ + m_params.currentGroup = nullptr; + m_params.currentGroupName.clear(); +} + +void IniProcessing::read(const char *key, bool &dest, bool defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + std::string &k = e->second; + size_t i = 0; + size_t ss = std::min(static_cast<size_t>(4ul), k.size()); + char buff[4] = {0, 0, 0, 0}; + const char *pbufi = k.c_str(); + char *pbuff = buff; + + for(; i < ss; i++) + (*pbuff++) = static_cast<char>(std::tolower(*pbufi++)); + + if(ss < 4) + { + if(ss == 0) + { + dest = false; + return; + } + + if(ss == 1) + { + dest = (buff[0] == '1'); + return; + } + + bool isNum = true; + isNum &= std::isdigit(buff[i]) || (buff[i] == '-') || (buff[i] == '+'); + + for(size_t i = 1; i < ss; i++) + isNum &= std::isdigit(buff[i]); + + if(isNum) + { + long num = std::strtol(buff, 0, 0); + dest = num != 0l; + return; + } + else + { + dest = (std::memcmp(buff, "yes", 3) == 0) || + (std::memcmp(buff, "on", 2) == 0); + return; + } + } + + if(std::memcmp(buff, "true", 4) == 0) + { + dest = true; + return; + } + + try + { + long num = std::strtol(buff, 0, 0); + dest = num != 0l; + return; + } + catch(...) + { + dest = false; + return; + } +} + +void IniProcessing::read(const char *key, unsigned char &dest, unsigned char defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + std::string &k = e->second; + + if(k.size() >= 1) + dest = static_cast<unsigned char>(k[0]); + else + dest = defVal; +} + +void IniProcessing::read(const char *key, char &dest, char defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + std::string &k = e->second; + + if(k.size() >= 1) + dest = k[0]; + else + dest = defVal; +} + +void IniProcessing::read(const char *key, unsigned short &dest, unsigned short defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = static_cast<unsigned short>(std::strtoul(e->second.c_str(), nullptr, 0)); +} + +void IniProcessing::read(const char *key, short &dest, short defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = static_cast<short>(std::strtol(e->second.c_str(), nullptr, 0)); +} + +void IniProcessing::read(const char *key, unsigned int &dest, unsigned int defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = static_cast<unsigned int>(std::strtoul(e->second.c_str(), nullptr, 0)); +} + +void IniProcessing::read(const char *key, int &dest, int defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = static_cast<int>(std::strtol(e->second.c_str(), nullptr, 0)); +} + +void IniProcessing::read(const char *key, unsigned long &dest, unsigned long defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = std::strtoul(e->second.c_str(), nullptr, 0); +} + +void IniProcessing::read(const char *key, long &dest, long defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = std::strtol(e->second.c_str(), nullptr, 0); +} + +void IniProcessing::read(const char *key, unsigned long long &dest, unsigned long long defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = std::strtoull(e->second.c_str(), nullptr, 0); +} + +void IniProcessing::read(const char *key, long long &dest, long long defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = std::strtoll(e->second.c_str(), nullptr, 0); +} + +void IniProcessing::read(const char *key, float &dest, float defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + if(!ok) + { + dest = defVal; + return; + } + dest = std::strtof(e->second.c_str(), nullptr); +} + +void IniProcessing::read(const char *key, double &dest, double defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + dest = std::strtod(e->second.c_str(), nullptr); +} + +void IniProcessing::read(const char *key, long double &dest, long double defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + if(!ok) + { + dest = defVal; + return; + } + dest = std::strtold(e->second.c_str(), nullptr); +} + +void IniProcessing::read(const char *key, std::string &dest, const std::string &defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = e->second; +} + +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +void IniProcessing::read(const char *key, QString &dest, const QString &defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + dest = QString::fromStdString(e->second); +} +#endif + +template<class TList> +inline void StrToNumVectorHelper(const std::string &source, TList &dest, const typename TList::value_type &def) +{ + typedef typename TList::value_type T; + dest.clear(); + + if(!source.empty()) + { + std::stringstream ss(source); + std::string item; + while(std::getline(ss, item, ',')) + { + std::remove(item.begin(), item.end(), ' '); + try + { + if(std::is_same<T, int>::value || + std::is_same<T, long>::value || + std::is_same<T, short>::value) + dest.push_back(static_cast<T>(std::strtol(item.c_str(), NULL, 0))); + else if(std::is_same<T, unsigned int>::value || + std::is_same<T, unsigned long>::value || + std::is_same<T, unsigned short>::value) + dest.push_back(static_cast<T>(std::strtoul(item.c_str(), NULL, 0))); + else if(std::is_same<T, float>::value) + dest.push_back(std::strtof(item.c_str(), NULL)); + else + dest.push_back(std::strtod(item.c_str(), NULL)); + } + catch(...) + { + dest.pop_back(); + } + } + + if(dest.empty()) + dest.push_back(def); + } + else + dest.push_back(def); +} + +template<class TList, typename T> +void readNumArrHelper(IniProcessing *self, const char *key, TList &dest, const TList &defVal) +{ + bool ok = false; + IniProcessing::params::IniKeys::iterator e = self->readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + StrToNumVectorHelper(e->second, dest, static_cast<T>(0)); +} + +void IniProcessing::read(const char *key, std::vector<unsigned short> &dest, const std::vector<unsigned short> &defVal) +{ + readNumArrHelper<std::vector<unsigned short>, unsigned short>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<short> &dest, const std::vector<short> &defVal) +{ + readNumArrHelper<std::vector<short>, short>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<unsigned int> &dest, const std::vector<unsigned int> &defVal) +{ + readNumArrHelper<std::vector<unsigned int>, unsigned int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<int> &dest, const std::vector<int> &defVal) +{ + readNumArrHelper<std::vector<int>, int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<unsigned long> &dest, const std::vector<unsigned long> &defVal) +{ + readNumArrHelper<std::vector<unsigned long>, unsigned long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<long> &dest, const std::vector<long> &defVal) +{ + readNumArrHelper<std::vector<long>, long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<unsigned long long> &dest, const std::vector<unsigned long long> &defVal) +{ + readNumArrHelper<std::vector<unsigned long long>, unsigned long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<long long> &dest, const std::vector<long long> &defVal) +{ + readNumArrHelper<std::vector<long long>, long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<float> &dest, const std::vector<float> &defVal) +{ + readNumArrHelper<std::vector<float>, float>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, std::vector<double> &dest, const std::vector<double> &defVal) +{ + readNumArrHelper<std::vector<double>, double>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, std::vector<long double> &dest, const std::vector<long double> &defVal) +{ + readNumArrHelper<std::vector<long double>, long double>(this, key, dest, defVal); +} + +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +void IniProcessing::read(const char *key, QList<short> &dest, const QList<short> &defVal) +{ + readNumArrHelper<QList<short>, short>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, QList<unsigned short> &dest, const QList<unsigned short> &defVal) +{ + readNumArrHelper<QList<unsigned short>, unsigned short>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, QList<int> &dest, const QList<int> &defVal) +{ + readNumArrHelper<QList<int>, int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<unsigned int> &dest, const QList<unsigned int> &defVal) +{ + readNumArrHelper<QList<unsigned int>, unsigned int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<long> &dest, const QList<long> &defVal) +{ + readNumArrHelper<QList<long>, long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<unsigned long> &dest, const QList<unsigned long> &defVal) +{ + readNumArrHelper<QList<unsigned long>, unsigned long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<long long> &dest, const QList<long long> &defVal) +{ + readNumArrHelper<QList<long long>, long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<unsigned long long> &dest, const QList<unsigned long long> &defVal) +{ + readNumArrHelper<QList<unsigned long long>, unsigned long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<float> &dest, const QList<float> &defVal) +{ + readNumArrHelper<QList<float>, float>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QList<double> &dest, const QList<double> &defVal) +{ + readNumArrHelper<QList<double>, double>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, QList<long double> &dest, const QList<long double> &defVal) +{ + readNumArrHelper<QList<long double>, long double>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, QVector<short> &dest, const QVector<short> &defVal) +{ + readNumArrHelper<QVector<short>, short>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<unsigned short> &dest, const QVector<unsigned short> &defVal) +{ + readNumArrHelper<QVector<unsigned short>, unsigned short>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<int> &dest, const QVector<int> &defVal) +{ + readNumArrHelper<QVector<int>, int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<unsigned int> &dest, const QVector<unsigned int> &defVal) +{ + readNumArrHelper<QVector<unsigned int>, unsigned int>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<long> &dest, const QVector<long> &defVal) +{ + readNumArrHelper<QVector<long>, long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<unsigned long> &dest, const QVector<unsigned long> &defVal) +{ + readNumArrHelper<QVector<unsigned long>, unsigned long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<long long> &dest, const QVector<long long> &defVal) +{ + readNumArrHelper<QVector<long long>, long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<unsigned long long> &dest, const QVector<unsigned long long> &defVal) +{ + readNumArrHelper<QVector<unsigned long long>, unsigned long long>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<float> &dest, const QVector<float> &defVal) +{ + readNumArrHelper<QVector<float>, float>(this, key, dest, defVal); +} +void IniProcessing::read(const char *key, QVector<double> &dest, const QVector<double> &defVal) +{ + readNumArrHelper<QVector<double>, double>(this, key, dest, defVal); +} + +void IniProcessing::read(const char *key, QVector<long double> &dest, const QVector<long double> &defVal) +{ + readNumArrHelper<QVector<long double>, long double>(this, key, dest, defVal); +} +#endif + +IniProcessingVariant IniProcessing::value(const char *key, const IniProcessingVariant &defVal) +{ + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + return defVal; + + std::string &k = e->second; + return IniProcessingVariant(&k); +} + +void IniProcessing::writeIniParam(const char *key, const std::string &value) +{ + if(m_params.currentGroupName.empty()) + return; + + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + if(ok) + { + e->second = value; + } + else + { + if(!m_params.currentGroup) + { + m_params.iniData.insert({m_params.currentGroupName, params::IniKeys()}); + m_params.currentGroup = &m_params.iniData[m_params.currentGroupName]; + } + m_params.currentGroup->insert({std::string(key), value}); + //Mark as opened + m_params.opened = true; + } +} + +void IniProcessing::setValue(const char *key, unsigned short value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, short value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, unsigned int value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, int value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, unsigned long value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, long value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, unsigned long long value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, long long value) +{ + writeIniParam(key, std::to_string(value)); +} + +void IniProcessing::setValue(const char *key, float value) +{ + writeIniParam(key, IniProcessing::to_string_with_precision(value)); +} + +void IniProcessing::setValue(const char *key, double value) +{ + writeIniParam(key, IniProcessing::to_string_with_precision(value)); +} + +void IniProcessing::setValue(const char *key, long double value) +{ + writeIniParam(key, IniProcessing::to_string_with_precision(value)); +} + +void IniProcessing::setValue(const char *key, const char *value) +{ + writeIniParam(key, value); +} + +void IniProcessing::setValue(const char *key, const std::string &value) +{ + writeIniParam(key, value); +} + +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +void IniProcessing::setValue(const char *key, const QString &value) +{ + writeIniParam(key, value.toStdString()); +} +#endif + +static inline bool isFloatValue(const std::string &str) +{ + enum State + { + ST_SIGN = 0, + ST_DOT, + ST_EXPONENT, + ST_EXPONENT_SIGN, + ST_TAIL + } st = ST_SIGN; + + for(const char &c : str) + { + if(!isdigit(c)) + { + switch(st) + { + case ST_SIGN: + if(c != '-') + return false; + st = ST_DOT; + continue; + case ST_DOT: + if(c != '.') + return false; + else + if((c == 'E') || (c == 'e')) + st = ST_EXPONENT_SIGN; + else + st = ST_EXPONENT; + continue; + case ST_EXPONENT: + if((c != 'E') && (c != 'e')) + return false; + st = ST_EXPONENT_SIGN; + continue; + case ST_EXPONENT_SIGN: + if(c != '-') + return false; + st = ST_TAIL; + continue; + case ST_TAIL: + return false; + } + return false; + } + else + { + if(st == ST_SIGN) + st = ST_DOT; + } + } + return true; +} + +bool IniProcessing::writeIniFile() +{ + #ifdef _WIN32 + //Convert UTF8 file path into UTF16 to support non-ASCII paths on Windows + std::wstring dest; + dest.resize(m_params.filePath.size()); + int newSize = MultiByteToWideChar(CP_UTF8, + 0, + m_params.filePath.c_str(), + dest.size(), + (wchar_t *)dest.c_str(), + dest.size()); + dest.resize(newSize); + FILE *cFile = _wfopen(dest.c_str(), L"wb"); + #else + FILE *cFile = fopen(m_params.filePath.c_str(), "wb"); + #endif + if(!cFile) + return false; + + for(params::IniSections::iterator group = m_params.iniData.begin(); + group != m_params.iniData.end(); + group++) + { + fprintf(cFile, "[%s]\n", group->first.c_str()); + for(params::IniKeys::iterator key = group->second.begin(); + key != group->second.end(); + key++) + { + if(isFloatValue(key->second)) + { + //Store as-is without quoting + fprintf(cFile, "%s = %s\n", key->first.c_str(), key->second.c_str()); + } + else + { + //Set escape quotes and put the string with a quotes + std::string &s = key->second; + std::string escaped; + escaped.reserve(s.length() * 2); + for(char &c : s) + { + switch(c) + { + case '\n': escaped += "\\n"; break; + case '\r': escaped += "\\r"; break; + case '\t': escaped += "\\t"; break; + default: + if((c == '\\') || (c == '"')) + escaped.push_back('\\'); + escaped.push_back(c); + } + } + fprintf(cFile, "%s = \"%s\"\n", key->first.c_str(), escaped.c_str()); + } + } + fprintf(cFile, "\n"); + fflush(cFile); + } + fclose(cFile); + return true; +} + diff --git a/src/gen_adldata/ini/ini_processing.h b/src/gen_adldata/ini/ini_processing.h new file mode 100644 index 0000000..52ab2e2 --- /dev/null +++ b/src/gen_adldata/ini/ini_processing.h @@ -0,0 +1,677 @@ +/* +INI Processor - a small library which allows you parsing INI-files + +Copyright (c) 2017 Vitaliy Novichkov <admin@wohlnet.ru> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef INIPROCESSING_H +#define INIPROCESSING_H + +#include <string> +#include <cstring> +#include <cstdlib> +#include <vector> +#include <unordered_map> +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +#include <QString> +#include <QList> +#include <QVector> +#endif + +#include "ini_processing_variant.h" + +/** + * @brief INI Processor - an utility which providing fast and flexible INI file parsing API + */ +class IniProcessing +{ +public: + /** + * @brief Available error codes + */ + enum ErrCode + { + //! Everything is fine + ERR_OK = 0, + //! File not found or memory pointer is null + ERR_NOFILE, + //! Invalid section declaration syntax + ERR_SECTION_SYNTAX, + //! Invalid key declaration syntax + ERR_KEY_SYNTAX, + //! No available memory + ERR_NO_MEMORY + }; + +private: + struct params + { + std::string filePath; + bool opened; + int lineWithError; + ErrCode errorCode; + bool modified; + typedef std::unordered_map<std::string, std::string> IniKeys; + typedef std::unordered_map<std::string, IniKeys> IniSections; + IniSections iniData; + IniKeys *currentGroup; + std::string currentGroupName; + } m_params; + + template<class TList, typename T> + friend void readNumArrHelper(IniProcessing* self, const char *key, TList &dest, const TList &defVal); + + bool parseHelper(char *data, size_t size); + + bool parseFile(const char *filename); + bool parseMemory(char *mem, size_t size); + + inline params::IniKeys::iterator readHelper(const char *key, bool &ok) + { + if(!m_params.opened) + return params::IniKeys::iterator(); + + if(!m_params.currentGroup) + return params::IniKeys::iterator(); + + #ifndef CASE_SENSITIVE_KEYS + std::string key1(key); + for(char *iter = &key1[0]; *iter != '\0'; ++iter) + *iter = (char)tolower(*iter); + #endif + + params::IniKeys::iterator e = m_params.currentGroup->find(key1); + + if(e != m_params.currentGroup->end()) + ok = true; + + return e; + } + + void writeIniParam(const char *key, const std::string &value); + +public: + IniProcessing(); + IniProcessing(const char *iniFileName, int dummy = 0); + IniProcessing(const std::string &iniFileName, int dummy = 0); +#ifdef INI_PROCESSING_ALLOW_QT_TYPES + IniProcessing(const QString &iniFileName, int dummy = 0); +#endif + IniProcessing(char *memory, size_t size); + IniProcessing(const IniProcessing &ip); + + /** + * @brief Open INI-file from disk and parse it + * @param iniFileName full path to INI-file to parse + * @return true if INI file has been passed, false if any error happen + */ + bool open(const std::string &iniFileName); + + /** + * @brief Open raw INI-data from memory and parse it + * @param memory pointer to memory block + * @param size size of memory block to process + * @return + */ + bool openMem(char *memory, size_t size); + + /** + * @brief Clear all internal buffers and close the file + */ + void close(); + + /** + * @brief Returns last happen error code + * @return Error code + */ + ErrCode lastError(); + /** + * @brief Line number which contains error + * @return line number wit herror + */ + int lineWithError(); + + /** + * @brief State of INI Processor + * @return true if any file is opened + */ + bool isOpened(); + + /** + * @brief Select a section to process + * @param groupName name of section to process + * @return true if group exists and opened + */ + bool beginGroup(const std::string &groupName); + + /** + * @brief Is this INI file contains section of specific name + * @param groupName name of section + * @return true if section is exists + */ + bool contains(const std::string &groupName); + + /** + * @brief Currently opened file name + * @return path to currently opened file + */ + std::string fileName(); + + /** + * @brief Currently processing section + * @return name of current section + */ + std::string group(); + + /** + * @brief Get list of available groups + * @return Array of strings + */ + std::vector<std::string> childGroups(); + + /** + * @brief Is current section contains specific key name + * @param keyName name of key + * @return true if key is presented in this section + */ + bool hasKey(const std::string &keyName); + + /** + * @brief Get list of available keys in current groul + * @return Array of strings + */ + std::vector<std::string> allKeys(); + + /** + * @brief Release current section to choice another for process + */ + void endGroup(); + + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, bool &dest, bool defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, unsigned char &dest, unsigned char defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, char &dest, char defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, unsigned short &dest, unsigned short defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, short &dest, short defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, unsigned int &dest, unsigned int defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, int &dest, int defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, unsigned long &dest, unsigned long defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, long &dest, long defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, unsigned long long &dest, unsigned long long defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, long long &dest, long long defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, float &dest, float defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, double &dest, double defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, long double &dest, long double defVal); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::string &dest, const std::string &defVal); + + #ifdef INI_PROCESSING_ALLOW_QT_TYPES + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QString &dest, const QString &defVal); + #endif + + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<unsigned short> &dest, const std::vector<unsigned short> &defVal = std::vector<unsigned short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<short> &dest, const std::vector<short> &defVal = std::vector<short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<unsigned int> &dest, const std::vector<unsigned int> &defVal = std::vector<unsigned int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<int> &dest, const std::vector<int> &defVal = std::vector<int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<unsigned long> &dest, const std::vector<unsigned long> &defVal = std::vector<unsigned long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<long> &dest, const std::vector<long> &defVal = std::vector<long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<unsigned long long> &dest, const std::vector<unsigned long long> &defVal = std::vector<unsigned long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<long long> &dest, const std::vector<long long> &defVal = std::vector<long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<float> &dest, const std::vector<float> &defVal = std::vector<float>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<double> &dest, const std::vector<double> &defVal = std::vector<double>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, std::vector<long double> &dest, const std::vector<long double> &defVal = std::vector<long double>()); + + #ifdef INI_PROCESSING_ALLOW_QT_TYPES + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<short> &dest, const QList<short> &defVal = QList<short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<unsigned short> &dest, const QList<unsigned short> &defVal = QList<unsigned short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<int> &dest, const QList<int> &defVal = QList<int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<unsigned int> &dest, const QList<unsigned int> &defVal = QList<unsigned int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<long> &dest, const QList<long> &defVal = QList<long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<unsigned long> &dest, const QList<unsigned long> &defVal = QList<unsigned long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<long long> &dest, const QList<long long> &defVal = QList<long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<unsigned long long> &dest, const QList<unsigned long long> &defVal = QList<unsigned long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<float> &dest, const QList<float> &defVal = QList<float>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<double> &dest, const QList<double> &defVal = QList<double>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QList<long double> &dest, const QList<long double> &defVal = QList<long double>()); + + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<short> &dest, const QVector<short> &defVal = QVector<short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<unsigned short> &dest, const QVector<unsigned short> &defVal = QVector<unsigned short>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<int> &dest, const QVector<int> &defVal = QVector<int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<unsigned int> &dest, const QVector<unsigned int> &defVal = QVector<unsigned int>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<long> &dest, const QVector<long> &defVal = QVector<long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<unsigned long> &dest, const QVector<unsigned long> &defVal = QVector<unsigned long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<long long> &dest, const QVector<long long> &defVal = QVector<long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<unsigned long long> &dest, const QVector<unsigned long long> &defVal = QVector<unsigned long long>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<float> &dest, const QVector<float> &defVal = QVector<float>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<double> &dest, const QVector<double> &defVal = QVector<double>()); + /** + * @brief Retreive value by specific key and pass it via reference + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + */ + void read(const char *key, QVector<long double> &dest, const QVector<long double> &defVal = QVector<long double>()); + #endif + + //! Hash-table for the fast string to enum conversion + typedef std::unordered_map<std::string, int> StrEnumMap; + + + template<typename T> + /** + * @brief Retreive value by string-based enum key + * @param [_IN] key name of key with value to retrieved + * @param [_OUT] dest Reference to destination variable to store retrieved value + * @param [_IN] defVal Default value for case of non-existing key + * @param [_IN] enumMap + */ + void readEnum(const char *key, T &dest, T defVal, IniProcessing::StrEnumMap enumMap) + { + bool ok = false; + params::IniKeys::iterator e = readHelper(key, ok); + + if(!ok) + { + dest = defVal; + return; + } + + StrEnumMap::iterator em = enumMap.find(e->second); + + if(em == enumMap.end()) + { + dest = defVal; + return; + } + + dest = static_cast<T>(em->second); + } + + /** + * @brief QSettings-compatible way to retreive value + * @param key key with value to retreive + * @param defVal default value key + * @return variant which contains a value + */ + IniProcessingVariant value(const char *key, const IniProcessingVariant &defVal = IniProcessingVariant()); + + void setValue(const char *key, unsigned short value); + void setValue(const char *key, short value); + void setValue(const char *key, unsigned int value); + void setValue(const char *key, int value); + void setValue(const char *key, unsigned long value); + void setValue(const char *key, long value); + void setValue(const char *key, unsigned long long value); + void setValue(const char *key, long long value); + void setValue(const char *key, float value); + void setValue(const char *key, double value); + void setValue(const char *key, long double value); + + template <typename T> + static inline std::string to_string_with_precision(const T a_value) + { + char buf[35]; + memset(buf, 0, 35); + snprintf(buf, 34, "%.15g", static_cast<double>(a_value)); + return buf; + } + + template<class TList> + static inline std::string fromVector(const TList &value) + { + typedef typename TList::value_type T; + std::string out; + for(const T &f: value) + { + if(!out.empty()) + out.push_back(','); + if(std::is_same<T, float>::value || + std::is_same<T, double>::value || + std::is_same<T, long double>::value) + out.append(to_string_with_precision(f)); + else + out.append(std::to_string(f)); + } + return out; + } + + template<typename T> + void setValue(const char *key, const std::vector<T> &value) + { + static_assert(std::is_arithmetic<T>::value, "Not arithmetic (integral or floating point required!)"); + writeIniParam(key, fromVector(value)); + } + + void setValue(const char *key, const char *value); + void setValue(const char *key, const std::string &value); + + #ifdef INI_PROCESSING_ALLOW_QT_TYPES + void setValue(const char *key, const QString &value); + + template<typename T> + void setValue(const char *key, const QList<T> &value) + { + static_assert(std::is_arithmetic<T>::value, "Not arithmetic (integral or floating point required!)"); + writeIniParam(key, fromVector(value)); + } + + template<typename T> + void setValue(const char *key, const QVector<T> &value) + { + static_assert(std::is_arithmetic<T>::value, "Not arithmetic (integral or floating point required!)"); + writeIniParam(key, fromVector(value)); + } + #endif + + /** + * @brief Write INI file by the recently given file path + * @return true if INI file was successfully written + */ + bool writeIniFile(); +}; + +#endif // INIPROCESSING_H diff --git a/src/gen_adldata/ini/ini_processing_variant.h b/src/gen_adldata/ini/ini_processing_variant.h new file mode 100644 index 0000000..34f999a --- /dev/null +++ b/src/gen_adldata/ini/ini_processing_variant.h @@ -0,0 +1,243 @@ +/* +INI Processor - a small library which allows you parsing INI-files + +Copyright (c) 2017 Vitaliy Novichkov <admin@wohlnet.ru> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +/* +A QVariant-like thing created just like a proxy between +INI Processor and target value (to be compatible with QSettings) +*/ + +#ifndef INI_PROCESSING_VARIANT_H +#define INI_PROCESSING_VARIANT_H + +#include <string> +#include <cstring> +#include <cstdlib> +#ifdef INI_PROCESSING_ALLOW_QT_TYPES +#include <QString> +#endif + +class IniProcessingVariant +{ + std::string m_data; + std::string *m_dataP; + inline std::string &data() + { + if(m_dataP) + return *m_dataP; + else + return m_data; + } +public: + IniProcessingVariant(): + m_data(""), + m_dataP(nullptr) {} + + IniProcessingVariant(const std::string &data): + m_data(data), + m_dataP(nullptr) {} + IniProcessingVariant(const char *data): + m_data(data), + m_dataP(nullptr) {} + #ifdef INI_PROCESSING_ALLOW_QT_TYPES + IniProcessingVariant(const QString &data): + m_data(data.toStdString()), + m_dataP(nullptr) {} + #endif + IniProcessingVariant(std::string *dataPointer): + m_data(""), + m_dataP(dataPointer) {} + + IniProcessingVariant(const IniProcessingVariant &v): + m_data(v.m_data), + m_dataP(v.m_dataP) {} + + IniProcessingVariant(char data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(unsigned char data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(bool data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(short data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(unsigned short data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(int data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + IniProcessingVariant(unsigned int data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(long data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + IniProcessingVariant(unsigned long data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(long long data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + IniProcessingVariant(unsigned long long data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(float data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(double data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + IniProcessingVariant(long double data): + m_data(std::to_string(data)), + m_dataP(nullptr) {} + + bool isNull() + { + return (m_data.empty() && !m_dataP); + } + + bool isValid() + { + return ((!m_data.empty()) || (static_cast<std::string *>(m_dataP))); + } + + std::string toString() + { + std::string out = data(); + + if((out.size() > 2) && (out[0] == '"')) + out.erase(0, 1); + + if((out.size() > 1) && (out[out.size() - 1] == '"')) + out.erase((out.size() - 1), 1); + + return out; + } + + #ifdef INI_PROCESSING_ALLOW_QT_TYPES + QString toQString() + { + return QString::fromStdString(toString()); + } + #endif + + bool toBool() + { + size_t i = 0; + size_t ss = std::min(static_cast<size_t>(4ul), data().size()); + char buff[4] = {0, 0, 0, 0}; + const char *pbufi = data().c_str(); + char *pbuff = buff; + + for(; i < ss; i++) + (*pbuff++) = static_cast<char>(std::tolower(*pbufi++)); + + if(ss < 4) + { + if(ss == 0) + return false; + + if(ss == 1) + return (buff[0] == '1'); + + try + { + long num = std::strtol(buff, 0, 0); + return num != 0l; + } + catch(...) + { + bool res = (std::memcmp(buff, "yes", 3) == 0) || + (std::memcmp(buff, "on", 2) == 0); + return res; + } + } + + if(std::memcmp(buff, "true", 4) == 0) + return true; + + try + { + long num = std::strtol(buff, 0, 0); + return num != 0l; + } + catch(...) + { + return false; + } + } + + int toInt() + { + return std::atoi(data().c_str()); + } + unsigned int toUInt() + { + return static_cast<unsigned int>(std::strtoul(data().c_str(), nullptr, 0)); + } + + long toLong() + { + return std::atol(data().c_str()); + } + unsigned long toULong() + { + return std::strtoul(data().c_str(), nullptr, 0); + } + + long long toLongLong() + { + return std::atoll(data().c_str()); + } + unsigned long long toULongLong() + { + return std::strtoull(data().c_str(), nullptr, 0); + } + + float toFloat() + { + return float(std::atof(data().c_str())); + } + + double toDouble() + { + return std::atof(data().c_str()); + } +}; + + +#endif // INI_PROCESSING_VARIANT_H diff --git a/src/gen_adldata/measurer.cpp b/src/gen_adldata/measurer.cpp new file mode 100644 index 0000000..93f0bf9 --- /dev/null +++ b/src/gen_adldata/measurer.cpp @@ -0,0 +1,543 @@ +#include "measurer.h" +#include <cmath> + +#ifndef ADLMIDI_USE_DOSBOX_OPL +#include "../nukedopl3.h" +#else +#include "../dbopl.h" +#endif + +DurationInfo MeasureDurations(const ins &in) +{ + std::vector<int16_t> stereoSampleBuf; +#ifdef ADLMIDI_USE_DOSBOX_OPL + std::vector<int32_t> stereoSampleBuf_32; +#endif + insdata id[2]; + bool found[2] = {false, false}; + for(InstrumentDataTab::const_iterator j = insdatatab.begin(); + j != insdatatab.end(); + ++j) + { + if(j->second.first == in.insno1) + { + id[0] = j->first; + found[0] = true; + if(found[1]) break; + } + if(j->second.first == in.insno2) + { + id[1] = j->first; + found[1] = true; + if(found[0]) break; + } + } + const unsigned rate = 22010; + const unsigned interval = 150; + const unsigned samples_per_interval = rate / interval; + const int notenum = + in.notenum < 20 ? (44 + in.notenum) + : in.notenum >= 128 ? (44 + 128 - in.notenum) + : in.notenum; + +#ifndef ADLMIDI_USE_DOSBOX_OPL +#define WRITE_REG(key, value) OPL3_WriteReg(&opl, (Bit8u)(key), (Bit8u)(value)) + _opl3_chip opl; +#else +#define WRITE_REG(key, value) opl.WriteReg((Bit8u)(key), (Bit8u)(value)); + DBOPL::Handler opl; +#endif + + static const short initdata[(2 + 3 + 2 + 2) * 2] = + { + 0x004, 96, 0x004, 128, // Pulse timer + 0x105, 0, 0x105, 1, 0x105, 0, // Pulse OPL3 enable, leave disabled + 0x001, 32, 0x0BD, 0 // Enable wave & melodic +}; +#ifndef ADLMIDI_USE_DOSBOX_OPL + OPL3_Reset(&opl, rate); +#else + opl.Init(rate); +#endif + for(unsigned a = 0; a < 18; a += 2) WRITE_REG(initdata[a], initdata[a + 1]); + + const unsigned n_notes = in.insno1 == in.insno2 ? 1 : 2; + unsigned x[2]; + + if(n_notes == 2 && !in.pseudo4op) + { + WRITE_REG(0x105, 1); + WRITE_REG(0x104, 1); + } + + for(unsigned n = 0; n < n_notes; ++n) + { + static const unsigned char patchdata[11] = + {0x20, 0x23, 0x60, 0x63, 0x80, 0x83, 0xE0, 0xE3, 0x40, 0x43, 0xC0}; + for(unsigned a = 0; a < 10; ++a) WRITE_REG(patchdata[a] + n * 8, id[n].data[a]); + WRITE_REG(patchdata[10] + n * 8, id[n].data[10] | 0x30); + } + + for(unsigned n = 0; n < n_notes; ++n) + { + double hertz = 172.00093 * std::exp(0.057762265 * (notenum + id[n].finetune)); + if(hertz > 131071) + { + fprintf(stderr, "Why does note %d + finetune %d produce hertz %g?\n", + notenum, id[n].finetune, hertz); + hertz = 131071; + } + x[n] = 0x2000; + while(hertz >= 1023.5) + { + hertz /= 2.0; // Calculate octave + x[n] += 0x400; + } + x[n] += (unsigned int)(hertz + 0.5); + + // Keyon the note + WRITE_REG(0xA0 + n * 3, x[n] & 0xFF); + WRITE_REG(0xB0 + n * 3, x[n] >> 8); + } + + const unsigned max_on = 40; + const unsigned max_off = 60; + + // For up to 40 seconds, measure mean amplitude. + std::vector<double> amplitudecurve_on; + double highest_sofar = 0; + for(unsigned period = 0; period < max_on * interval; ++period) + { + stereoSampleBuf.clear(); + stereoSampleBuf.resize(samples_per_interval * 2); +#ifndef ADLMIDI_USE_DOSBOX_OPL + OPL3_GenerateStream(&opl, stereoSampleBuf.data(), samples_per_interval); +#else + { + stereoSampleBuf_32.resize(samples_per_interval * 2); + Bitu samples = samples_per_interval; + opl.GenerateArr(stereoSampleBuf_32.data(), &samples); + size_t ssat = 0; + for(const int32_t &i : stereoSampleBuf_32) + stereoSampleBuf[ssat++] = (int16_t)i; + } +#endif + + double mean = 0.0; + for(unsigned long c = 0; c < samples_per_interval; ++c) + mean += stereoSampleBuf[c * 2]; + mean /= samples_per_interval; + double std_deviation = 0; + for(unsigned long c = 0; c < samples_per_interval; ++c) + { + double diff = (stereoSampleBuf[c * 2] - mean); + std_deviation += diff * diff; + } + std_deviation = std::sqrt(std_deviation / samples_per_interval); + amplitudecurve_on.push_back(std_deviation); + if(std_deviation > highest_sofar) + highest_sofar = std_deviation; + + if(period > 6 * interval && std_deviation < highest_sofar * 0.2) + break; + } + + // Keyoff the note + for(unsigned n = 0; n < n_notes; ++n) + WRITE_REG(0xB0 + n, (x[n] >> 8) & 0xDF); + + // Now, for up to 60 seconds, measure mean amplitude. + std::vector<double> amplitudecurve_off; + for(unsigned period = 0; period < max_off * interval; ++period) + { + stereoSampleBuf.clear(); + stereoSampleBuf.resize(samples_per_interval * 2); +#ifndef ADLMIDI_USE_DOSBOX_OPL + OPL3_GenerateStream(&opl, stereoSampleBuf.data(), samples_per_interval); +#else + { + stereoSampleBuf_32.resize(samples_per_interval * 2); + Bitu samples = samples_per_interval; + opl.GenerateArr(stereoSampleBuf_32.data(), &samples); + size_t ssat = 0; + for(const int32_t &i : stereoSampleBuf_32) + stereoSampleBuf[ssat++] = (int16_t)i; + } +#endif + + double mean = 0.0; + for(unsigned long c = 0; c < samples_per_interval; ++c) + mean += stereoSampleBuf[c * 2]; + mean /= samples_per_interval; + double std_deviation = 0; + for(unsigned long c = 0; c < samples_per_interval; ++c) + { + double diff = (stereoSampleBuf[c * 2] - mean); + std_deviation += diff * diff; + } + std_deviation = std::sqrt(std_deviation / samples_per_interval); + amplitudecurve_off.push_back(std_deviation); + + if(std_deviation < highest_sofar * 0.2) break; + } + + /* Analyze the results */ + double begin_amplitude = amplitudecurve_on[0]; + double peak_amplitude_value = begin_amplitude; + size_t peak_amplitude_time = 0; + size_t quarter_amplitude_time = amplitudecurve_on.size(); + size_t keyoff_out_time = 0; + + for(size_t a = 1; a < amplitudecurve_on.size(); ++a) + { + if(amplitudecurve_on[a] > peak_amplitude_value) + { + peak_amplitude_value = amplitudecurve_on[a]; + peak_amplitude_time = a; + } + } + for(size_t a = peak_amplitude_time; a < amplitudecurve_on.size(); ++a) + { + if(amplitudecurve_on[a] <= peak_amplitude_value * 0.2) + { + quarter_amplitude_time = a; + break; + } + } + for(size_t a = 0; a < amplitudecurve_off.size(); ++a) + { + if(amplitudecurve_off[a] <= peak_amplitude_value * 0.2) + { + keyoff_out_time = a; + break; + } + } + + if(keyoff_out_time == 0 && amplitudecurve_on.back() < peak_amplitude_value * 0.2) + keyoff_out_time = quarter_amplitude_time; + + DurationInfo result; + result.peak_amplitude_time = peak_amplitude_time; + result.peak_amplitude_value = peak_amplitude_value; + result.begin_amplitude = begin_amplitude; + result.quarter_amplitude_time = quarter_amplitude_time; + result.keyoff_out_time = keyoff_out_time; + + result.ms_sound_kon = (long)(quarter_amplitude_time * 1000.0 / interval); + result.ms_sound_koff = (long)(keyoff_out_time * 1000.0 / interval); + result.nosound = (peak_amplitude_value < 0.5); + return result; +} + +static const char* spinner = "-\\|/"; + +void MeasureThreaded::LoadCache(const char *fileName) +{ + FILE *in = std::fopen(fileName, "rb"); + if(!in) + { + printf("Failed to load cache: file is not exists.\n" + "Complete data will be generated from scratch.\n"); + fflush(stdout); + return; + } + + char magic[32]; + if(std::fread(magic, 1, 32, in) != 32) + { + std::fclose(in); + printf("Failed to load cache: can't read magic.\n" + "Complete data will be generated from scratch.\n"); + fflush(stdout); + return; + } + + if(memcmp(magic, "ADLMIDI-DURATION-CACHE-FILE-V1.0", 32) != 0) + { + std::fclose(in); + printf("Failed to load cache: magic missmatch.\n" + "Complete data will be generated from scratch.\n"); + fflush(stdout); + return; + } + + while(!std::feof(in)) + { + DurationInfo info; + ins inst; + //got by instrument + insdata id[2]; + size_t insNo[2] = {0, 0}; + bool found[2] = {false, false}; + //got from file + insdata id_f[2]; + bool found_f[2] = {false, false}; + bool isMatches = false; + + memset(id, 0, sizeof(insdata) * 2); + memset(id_f, 0, sizeof(insdata) * 2); + memset(&info, 0, sizeof(DurationInfo)); + memset(&inst, 0, sizeof(ins)); + + //Instrument + uint64_t inval; + if(std::fread(&inval, 1, sizeof(uint64_t), in) != sizeof(uint64_t)) + break; + inst.insno1 = inval; + if(std::fread(&inval, 1, sizeof(uint64_t), in) != sizeof(uint64_t)) + break; + inst.insno2 = inval; + if(std::fread(&inst.notenum, 1, 1, in) != 1) + break; + if(std::fread(&inst.pseudo4op, 1, 1, in) != 1) + break; + if(std::fread(&inst.fine_tune, sizeof(double), 1, in) != 1) + break; + + //Instrument data + if(fread(found_f, 1, 2 * sizeof(bool), in) != sizeof(bool) * 2) + break; + for(size_t i = 0; i < 2; i++) + { + if(fread(id_f[i].data, 1, 11, in) != 11) + break; + if(fread(&id_f[i].finetune, 1, 1, in) != 1) + break; + if(fread(&id_f[i].diff, 1, sizeof(bool), in) != sizeof(bool)) + break; + } + + if(found_f[0] || found_f[1]) + { + for(InstrumentDataTab::const_iterator j = insdatatab.begin(); j != insdatatab.end(); ++j) + { + if(j->second.first == inst.insno1) + { + id[0] = j->first; + found[0] = (id[0] == id_f[0]); + insNo[0] = inst.insno1; + if(found[1]) break; + } + if(j->second.first == inst.insno2) + { + id[1] = j->first; + found[1] = (id[1] == id_f[1]); + insNo[1] = inst.insno2; + if(found[0]) break; + } + } + + //Find instrument entries are matching + if((found[0] != found_f[0]) || (found[1] != found_f[1])) + { + for(InstrumentDataTab::const_iterator j = insdatatab.begin(); j != insdatatab.end(); ++j) + { + if(found_f[0] && (j->first == id_f[0])) + { + found[0] = true; + insNo[0] = j->second.first; + } + if(found_f[1] && (j->first == id_f[1])) + { + found[1] = true; + insNo[1] = j->second.first; + } + if(found[0] && !found_f[1]) + { + isMatches = true; + break; + } + if(found[0] && found[1]) + { + isMatches = true; + break; + } + } + } + else + { + isMatches = true; + } + + //Then find instrument entry that uses found instruments + if(isMatches) + { + inst.insno1 = insNo[0]; + inst.insno2 = insNo[1]; + InstrumentsData::iterator d = instab.find(inst); + if(d == instab.end()) + isMatches = false; + } + } + + //Duration data + if(std::fread(&info.peak_amplitude_time, 1, sizeof(uint64_t), in) != sizeof(uint64_t)) + break; + if(std::fread(&info.peak_amplitude_value, 1, sizeof(double), in) != sizeof(double)) + break; + if(std::fread(&info.quarter_amplitude_time, 1, sizeof(double), in) != sizeof(double)) + break; + if(std::fread(&info.begin_amplitude, 1, sizeof(double), in) != sizeof(double)) + break; + if(std::fread(&info.interval, 1, sizeof(double), in) != sizeof(double)) + break; + if(std::fread(&info.keyoff_out_time, 1, sizeof(double), in) != sizeof(double)) + break; + if(std::fread(&info.ms_sound_kon, 1, sizeof(int64_t), in) != sizeof(int64_t)) + break; + if(std::fread(&info.ms_sound_koff, 1, sizeof(int64_t), in) != sizeof(int64_t)) + break; + if(std::fread(&info.nosound, 1, sizeof(bool), in) != sizeof(bool)) + break; + + if(isMatches)//Store only if cached entry matches actual raw instrument data + m_durationInfo.insert({inst, info}); + } + + printf("Cache loaded!\n"); + fflush(stdout); + + std::fclose(in); +} + +void MeasureThreaded::SaveCache(const char *fileName) +{ + FILE *out = std::fopen(fileName, "wb"); + fprintf(out, "ADLMIDI-DURATION-CACHE-FILE-V1.0"); + for(DurationInfoCache::iterator it = m_durationInfo.begin(); it != m_durationInfo.end(); it++) + { + const ins &in = it->first; + insdata id[2]; + bool found[2] = {false, false}; + memset(id, 0, sizeof(insdata) * 2); + + uint64_t outval; + outval = in.insno1; + fwrite(&outval, 1, sizeof(uint64_t), out); + outval = in.insno2; + fwrite(&outval, 1, sizeof(uint64_t), out); + fwrite(&in.notenum, 1, 1, out); + fwrite(&in.pseudo4op, 1, 1, out); + fwrite(&in.fine_tune, sizeof(double), 1, out); + + for(InstrumentDataTab::const_iterator j = insdatatab.begin(); j != insdatatab.end(); ++j) + { + if(j->second.first == in.insno1) + { + id[0] = j->first; + found[0] = true; + if(found[1]) break; + } + if(j->second.first == in.insno2) + { + id[1] = j->first; + found[1] = true; + if(found[0]) break; + } + } + + fwrite(found, 1, 2 * sizeof(bool), out); + for(size_t i = 0; i < 2; i++) + { + fwrite(id[i].data, 1, 11, out); + fwrite(&id[i].finetune, 1, 1, out); + fwrite(&id[i].diff, 1, sizeof(bool), out); + } + + fwrite(&it->second.peak_amplitude_time, 1, sizeof(uint64_t), out); + fwrite(&it->second.peak_amplitude_value, 1, sizeof(double), out); + fwrite(&it->second.quarter_amplitude_time, 1, sizeof(double), out); + fwrite(&it->second.begin_amplitude, 1, sizeof(double), out); + fwrite(&it->second.interval, 1, sizeof(double), out); + fwrite(&it->second.keyoff_out_time, 1, sizeof(double), out); + fwrite(&it->second.ms_sound_kon, 1, sizeof(int64_t), out); + fwrite(&it->second.ms_sound_koff, 1, sizeof(int64_t), out); + fwrite(&it->second.nosound, 1, sizeof(bool), out); + } + std::fclose(out); +} + +void MeasureThreaded::printProgress() +{ + printf("Calculating measures... [%c %3u%% (%4u/%4u) Threads %3u, Matches %u] \r", + spinner[m_done.load() % 4], + (unsigned int)(((double)m_done.load() / (double)(m_total)) * 100), + (unsigned int)m_done.load(), + (unsigned int)m_total, + (unsigned int)m_threads.size(), + (unsigned int)m_cache_matches + ); + fflush(stdout); +} + +void MeasureThreaded::printFinal() +{ + printf("Calculating measures completed! [Total entries %4u with %u cache matches]\n", + (unsigned int)m_total, + (unsigned int)m_cache_matches); + fflush(stdout); +} + +void MeasureThreaded::run(InstrumentsData::const_iterator i) +{ + m_semaphore.wait(); + if(m_threads.size() > 0) + { + for(std::vector<destData *>::iterator it = m_threads.begin(); it != m_threads.end();) + { + if(!(*it)->m_works) + { + delete(*it); + it = m_threads.erase(it); + } + else + it++; + } + } + + destData *dd = new destData; + dd->i = i; + dd->myself = this; + dd->start(); + m_threads.push_back(dd); + printProgress(); +} + +void MeasureThreaded::waitAll() +{ + for(auto &th : m_threads) + { + printProgress(); + delete th; + } + m_threads.clear(); + printFinal(); +} + +void MeasureThreaded::destData::start() +{ + m_work = std::thread(&destData::callback, this); +} + +void MeasureThreaded::destData::callback(void *myself) +{ + destData *s = reinterpret_cast<destData *>(myself); + DurationInfo info; + DurationInfoCache::iterator cachedEntry = s->myself->m_durationInfo.find(s->i->first); + + if(cachedEntry != s->myself->m_durationInfo.end()) + { + s->myself->m_cache_matches++; + goto endWork; + } + + info = MeasureDurations(s->i->first); + s->myself->m_durationInfo_mx.lock(); + s->myself->m_durationInfo.insert({s->i->first, info}); + s->myself->m_durationInfo_mx.unlock(); + +endWork: + s->myself->m_semaphore.notify(); + s->myself->m_done++; + s->m_works = false; +} diff --git a/src/gen_adldata/measurer.h b/src/gen_adldata/measurer.h new file mode 100644 index 0000000..ba60ad7 --- /dev/null +++ b/src/gen_adldata/measurer.h @@ -0,0 +1,105 @@ +#ifndef MEASURER_H +#define MEASURER_H + +#include <atomic> +#include <map> +#include <mutex> +#include <condition_variable> +#include <thread> + +#include "progs_cache.h" + +struct DurationInfo +{ + uint64_t peak_amplitude_time; + double peak_amplitude_value; + double quarter_amplitude_time; + double begin_amplitude; + double interval; + double keyoff_out_time; + int64_t ms_sound_kon; + int64_t ms_sound_koff; + bool nosound; + uint8_t padding[7]; +}; + +class Semaphore +{ +public: + Semaphore(int count_ = 0) + : m_count(count_) {} + + inline void notify() + { + std::unique_lock<std::mutex> lock(mtx); + m_count++; + cv.notify_one(); + } + + inline void wait() + { + std::unique_lock<std::mutex> lock(mtx); + while(m_count == 0) + { + cv.wait(lock); + } + m_count--; + } + +private: + std::mutex mtx; + std::condition_variable cv; + std::atomic_int m_count; +}; + +struct MeasureThreaded +{ + typedef std::map<ins, DurationInfo> DurationInfoCache; + + MeasureThreaded() : + m_semaphore(9), + m_done(0), + m_cache_matches(0) + {} + + Semaphore m_semaphore; + std::mutex m_durationInfo_mx; + DurationInfoCache m_durationInfo; + std::atomic_bool m_delete_tail; + size_t m_total = 0; + std::atomic<size_t> m_done; + std::atomic<size_t> m_cache_matches; + + void LoadCache(const char *fileName); + void SaveCache(const char *fileName); + + struct destData + { + destData() + { + m_works = true; + } + ~destData() + { + m_work.join(); + } + MeasureThreaded *myself; + std::map<ins, std::pair<size_t, std::set<std::string> > >::const_iterator i; + std::thread m_work; + std::atomic_bool m_works; + + void start(); + static void callback(void *myself); + }; + + std::vector<destData *> m_threads; + + void printProgress(); + void printFinal(); + void run(InstrumentsData::const_iterator i); + void waitAll(); +}; + +extern DurationInfo MeasureDurations(const ins &in); + +#endif // MEASURER_H diff --git a/src/gen_adldata/midi_inst_list.h b/src/gen_adldata/midi_inst_list.h index f61b13d..61c9119 100644 --- a/src/gen_adldata/midi_inst_list.h +++ b/src/gen_adldata/midi_inst_list.h @@ -1,3 +1,6 @@ +#pragma once +#ifndef MIDI_INS_LIST +#define MIDI_INS_LIST static const char *const MidiInsName[] = { "AcouGrandPiano", @@ -178,3 +181,5 @@ static const char *const MidiInsName[] = { "Mute Triangle", "Open Triangle", "Shaker","Jingle Bell","Bell Tree","Castanets","Mute Surdu","Open Surdu",""}; + +#endif //MIDI_INS_LIST diff --git a/src/gen_adldata/progs_cache.cpp b/src/gen_adldata/progs_cache.cpp new file mode 100644 index 0000000..4aec9be --- /dev/null +++ b/src/gen_adldata/progs_cache.cpp @@ -0,0 +1,106 @@ +#include "progs_cache.h" + +InstrumentDataTab insdatatab; + +InstrumentsData instab; +InstProgsData progs; + +std::vector<std::string> banknames; + +//unsigned maxvalues[30] = { 0 }; + +void SetBank(unsigned bank, unsigned patch, size_t insno) +{ + progs[bank][patch] = insno + 1; +} + +size_t InsertIns( + const insdata &id, + const insdata &id2, + ins &in, + const std::string &name, + const std::string &name2) +{ + if(true) + { + InstrumentDataTab::iterator i = insdatatab.lower_bound(id); + + size_t insno = size_t(~0); + if(i == insdatatab.end() || i->first != id) + { + std::pair<insdata, std::pair<size_t, std::set<std::string> > > res; + res.first = id; + res.second.first = insdatatab.size(); + if(!name.empty()) res.second.second.insert(name); + if(!name2.empty()) res.second.second.insert(name2); + insdatatab.insert(i, res); + insno = res.second.first; + } + else + { + if(!name.empty()) i->second.second.insert(name); + if(!name2.empty()) i->second.second.insert(name2); + insno = i->second.first; + } + + in.insno1 = insno; + } + if(id != id2) + { + InstrumentDataTab::iterator i = insdatatab.lower_bound(id2); + + size_t insno2 = size_t(~0); + if(i == insdatatab.end() || i->first != id2) + { + std::pair<insdata, std::pair<size_t, std::set<std::string> > > res; + res.first = id2; + res.second.first = insdatatab.size(); + if(!name.empty()) res.second.second.insert(name); + if(!name2.empty()) res.second.second.insert(name2); + insdatatab.insert(i, res); + insno2 = res.second.first; + } + else + { + if(!name.empty()) i->second.second.insert(name); + if(!name2.empty()) i->second.second.insert(name2); + insno2 = i->second.first; + } + in.insno2 = insno2; + } + else + in.insno2 = in.insno1; + + + { + InstrumentsData::iterator i = instab.lower_bound(in); + + size_t resno = size_t(~0); + if(i == instab.end() || i->first != in) + { + std::pair<ins, std::pair<size_t, std::set<std::string> > > res; + res.first = in; + res.second.first = instab.size(); + if(!name.empty()) res.second.second.insert(name); + if(!name2.empty()) res.second.second.insert(name2); + instab.insert(i, res); + resno = res.second.first; + } + else + { + if(!name.empty()) i->second.second.insert(name); + if(!name2.empty()) i->second.second.insert(name2); + resno = i->second.first; + } + return resno; + } +} + +// Create silent 'nosound' instrument +size_t InsertNoSoundIns() +{ + // { 0x0F70700,0x0F70710, 0xFF,0xFF, 0x0,+0 }, + insdata tmp1 = { {0x00, 0x10, 0x07, 0x07, 0xF7, 0xF7, 0x00, 0x00, 0xFF, 0xFF, 0x00}, 0, 0 }; + struct ins tmp2 = { 0, 0, 0, false, 0.0 }; + return InsertIns(tmp1, tmp1, tmp2, "nosound"); +} diff --git a/src/gen_adldata/progs_cache.h b/src/gen_adldata/progs_cache.h new file mode 100644 index 0000000..6502925 --- /dev/null +++ b/src/gen_adldata/progs_cache.h @@ -0,0 +1,85 @@ +#ifndef PROGS_H +#define PROGS_H + +#include <map> +#include <set> +#include <utility> +#include <memory> +#include <cstring> +#include <cstdint> +#include <vector> + +struct insdata +{ + uint8_t data[11]; + int8_t finetune; + bool diff; + bool operator==(const insdata &b) const + { + return std::memcmp(data, b.data, 11) == 0 && finetune == b.finetune && diff == b.diff; + } + bool operator< (const insdata &b) const + { + int c = std::memcmp(data, b.data, 11); + if(c != 0) return c < 0; + if(finetune != b.finetune) return finetune < b.finetune; + if(diff != b.diff) return (!diff) == (b.diff); + return 0; + } + bool operator!=(const insdata &b) const + { + return !operator==(b); + } +}; + +struct ins +{ + size_t insno1, insno2; + unsigned char notenum; + bool pseudo4op; + double fine_tune; + + bool operator==(const ins &b) const + { + return notenum == b.notenum + && insno1 == b.insno1 + && insno2 == b.insno2 + && pseudo4op == b.pseudo4op + && fine_tune == b.fine_tune; + } + bool operator< (const ins &b) const + { + if(insno1 != b.insno1) return insno1 < b.insno1; + if(insno2 != b.insno2) return insno2 < b.insno2; + if(notenum != b.notenum) return notenum < b.notenum; + if(pseudo4op != b.pseudo4op) return pseudo4op < b.pseudo4op; + if(fine_tune != b.fine_tune) return fine_tune < b.fine_tune; + return 0; + } + bool operator!=(const ins &b) const + { + return !operator==(b); + } +}; + +typedef std::map<insdata, std::pair<size_t, std::set<std::string> > > InstrumentDataTab; +extern InstrumentDataTab insdatatab; + +typedef std::map<ins, std::pair<size_t, std::set<std::string> > > InstrumentsData; +extern InstrumentsData instab; + +typedef std::map<size_t, std::map<size_t, size_t> > InstProgsData; +extern InstProgsData progs; + +extern std::vector<std::string> banknames; + +//static std::map<unsigned, std::map<unsigned, unsigned> > Correlate; +//extern unsigned maxvalues[30]; + +void SetBank(unsigned bank, unsigned patch, size_t insno); + +size_t InsertIns(const insdata &id, const insdata &id2, ins &in, + const std::string &name, const std::string &name2 = ""); +size_t InsertNoSoundIns(); + +#endif // PROGS_H diff --git a/src/gen_adldata/scrapped.txt b/src/gen_adldata/scrapped.txt new file mode 100644 index 0000000..88bf4d9 --- /dev/null +++ b/src/gen_adldata/scrapped.txt @@ -0,0 +1,254 @@ + +#ifdef HARD_BANKS +static const char *const banknames[] = +{ + // 0 + "AIL (Star Control 3, Albion, Empire 2, Sensible Soccer, Settlers 2, many others)", + "Bisqwit (selection of 4op and 2op)", + "HMI (Descent, Asterix)", //melodic,drum + "HMI (Descent:: Int)", //intmelo,intdrum + "HMI (Descent:: Ham)", //hammelo,hamdrum + "HMI (Descent:: Rick)", //rickmelo,rickdrum + "HMI (Descent 2)", //d2melo,d2drum + "HMI (Normality)", //normmelo,normdrum + "HMI (Shattered Steel)", //ssmelo,ssdrum + "HMI (Theme Park)", // file131, file132 + // 10 + "HMI (3d Table Sports, Battle Arena Toshinden)", //file133, file134 + "HMI (Aces of the Deep)", //file142, file143 + "HMI (Earthsiege)", //file144, file145 + "HMI (Anvil of Dawn)", //file167,file168 + "DMX (Doom :: partially pseudo 4op)", + "DMX (Hexen, Heretic :: partially pseudo 4op)", + "DMX (MUS Play :: partially pseudo 4op)", + "AIL (Discworld, Grandest Fleet, Pocahontas, Slob Zone 3d, Ultima 4, Zorro)", // file17 + "AIL (Warcraft 2)", + "AIL (Syndicate)", // file19 + // 20 + "AIL (Guilty, Orion Conspiracy, Terra Nova Strike Force Centauri :: 4op)", // file20 + "AIL (Magic Carpet 2)", // file21 + "AIL (Nemesis)", + "AIL (Jagged Alliance)", //file23 + "AIL (When Two Worlds War :: 4op, MISSING INSTRUMENTS)", //file24 + "AIL (Bards Tale Construction :: MISSING INSTRUMENTS)", //file25 + "AIL (Return to Zork)", //file26 + "AIL (Theme Hospital)", //file27 + "AIL (National Hockey League PA)", + "AIL (Inherit The Earth)", //file29 + // 30 + "AIL (Inherit The Earth, file two)", //file30 + "AIL (Little Big Adventure :: 4op)", //file31 + "AIL (Wreckin Crew)", //file32 + "AIL (Death Gate)", // file13 + "AIL (FIFA International Soccer)", //file34 + "AIL (Starship Invasion)", //file35 + "AIL (Super Street Fighter 2 :: 4op)", //file36 + "AIL (Lords of the Realm :: MISSING INSTRUMENTS)", //file37 + "AIL (SimFarm, SimHealth :: 4op)", + "AIL (SimFarm, Settlers, Serf City)", + // 40 + "AIL (Caesar 2 :: partially 4op, MISSING INSTRUMENTS)", // file12 + "AIL (Syndicate Wars)", //file41 + "AIL (Bubble Bobble Feat. Rainbow Islands, Z)", //file42 + "AIL (Warcraft)", //file47 + "AIL (Terra Nova Strike Force Centuri :: partially 4op)", //file48 + "AIL (System Shock :: partially 4op)", //file49 + "AIL (Advanced Civilization)", //file50 + "AIL (Battle Chess 4000 :: partially 4op, melodic only)", //file53 + "AIL (Ultimate Soccer Manager :: partially 4op)", //file54 + "AIL (Air Bucks, Blue And The Gray, America Invades, Terminator 2029)", // sample.ad + // 50 + "AIL (Ultima Underworld 2)", // sample.opl + "AIL (Kasparov's Gambit)", // file15 + "AIL (High Seas Trader :: MISSING INSTRUMENTS)", // file16 + "AIL (Master of Magic, Master of Orion 2 :: 4op, std percussion)", //file159 + "AIL (Master of Magic, Master of Orion 2 :: 4op, orchestral percussion)", //file159 + "SB (Action Soccer)", + "SB (3d Cyberpuck :: melodic only)", + "SB (Simon the Sorcerer :: melodic only)", + "OP3 (The Fat Man 2op set)", + "OP3 (The Fat Man 4op set)", + // 60 + "OP3 (JungleVision 2op set :: melodic only)", + "OP3 (Wallace 2op set, Nitemare 3D :: melodic only)", + "TMB (Duke Nukem 3D)", + "TMB (Shadow Warrior)", + "DMX (Raptor)", + "OP3 (Modded GMOPL by Wohlstand)", + "SB (Jammey O'Connel's bank)", + "TMB (Default bank of Build Engine)", + "OP3 (4op bank by James Alan Nguyen)", + "TMB (Blood)", + // 70 + "TMB (Lee)", + "TMB (Nam)", + "DMX (Bank by Sneakernets)" +}; + +const char *prev = ""; +const char *prev_prefix = ""; +size_t prev_index = 0; + +enum IniType +{ + INI_Both, + INI_Melodic, + INI_Drums +}; + +void writeIni(const char * format, const char * filepath, const char * prefix, size_t index, int type, const char*filter_mel = 0, const char *filter_perc = 0) +{ + if(type == INI_Both) + { + FILE *ini = fopen("banks.ini", "a"); + fprintf(ini, "[bank-%lu]\n", index); + fprintf(ini, "name = \"%s\"\n", banknames[index]); + fprintf(ini, "format = %s\n", format); + fprintf(ini, "file = \"%s\"\n", filepath); + fprintf(ini, "prefix = \"%s\"\n", prefix); + if(filter_mel) fprintf(ini, "filter-m = \"%s\"\n", filter_mel); + if(filter_perc) fprintf(ini, "filter-p = \"%s\"\n", filter_perc); + fprintf(ini, "\n"); + fclose(ini); + } + + if(type == INI_Drums) + { + FILE *ini = fopen("banks.ini", "a"); + fprintf(ini, "[bank-%lu]\n", index); + fprintf(ini, "name = \"%s\"\n", banknames[index]); + fprintf(ini, "format = %s\n", format); + fprintf(ini, "file = \"%s\"\n", prev); + fprintf(ini, "file-p = \"%s\"\n", filepath); + fprintf(ini, "prefix = \"%s\"\n", prev_prefix); + fprintf(ini, "prefix-p = \"%s\"\n", prefix); + fprintf(ini, "\n"); + fclose(ini); + } + + prev = filepath; + prev_prefix = prefix; + prev_index = index; +} +#endif + + + +#ifdef HARD_BANKS +LoadMiles("fm_banks/opl_files/sc3.opl", 0, "G"); // Our "standard" bank! Same as file22.opl + +LoadBisqwit("fm_banks/op3_files/bisqwit.adlraw", 1, "Bisq"); + +LoadBNK("fm_banks/bnk_files/melodic.bnk", 2, "HMIGM", false, false); // same as file156.bnk +LoadBNK("fm_banks/bnk_files/drum.bnk", 2, "HMIGP", false, true); +LoadBNK("fm_banks/bnk_files/intmelo.bnk", 3, "intM", false, false); +LoadBNK("fm_banks/bnk_files/intdrum.bnk", 3, "intP", false, true); +LoadBNK("fm_banks/bnk_files/hammelo.bnk", 4, "hamM", false, false); +LoadBNK("fm_banks/bnk_files/hamdrum.bnk", 4, "hamP", false, true); +LoadBNK("fm_banks/bnk_files/rickmelo.bnk", 5, "rickM", false, false); +LoadBNK("fm_banks/bnk_files/rickdrum.bnk", 5, "rickP", false, true); + +LoadBNK("fm_banks/bnk_files/d2melo.bnk", 6, "b6M", false, false); +LoadBNK("fm_banks/bnk_files/d2drum.bnk", 6, "b6P", false, true); +LoadBNK("fm_banks/bnk_files/normmelo.bnk", 7, "b7M", false, false); +LoadBNK("fm_banks/bnk_files/normdrum.bnk", 7, "b7P", false, true); // same as file122.bnk +LoadBNK("fm_banks/bnk_files/ssmelo.bnk", 8, "b8M", false, false); +LoadBNK("fm_banks/bnk_files/ssdrum.bnk", 8, "b8P", false, true); + +LoadTMB("fm_banks/bnk_files/themepark.tmb", 9, "b9MP"); +//LoadBNK("fm_banks/bnk_files/file131.bnk", 9, "b9M", false, false); +//LoadBNK("fm_banks/bnk_files/file132.bnk", 9, "b9P", false, true); +LoadBNK("fm_banks/bnk_files/file133.bnk", 10, "b10P", false, true); +LoadBNK("fm_banks/bnk_files/file134.bnk", 10, "b10M", false, false); +LoadBNK("fm_banks/bnk_files/file142.bnk", 11, "b11P", false, true); +LoadBNK("fm_banks/bnk_files/file143.bnk", 11, "b11M", false, false); +LoadBNK("fm_banks/bnk_files/file145.bnk", 12, "b12M", false, false);//file145 is MELODIC +LoadBNK("fm_banks/bnk_files/file144.bnk", 12, "b12P", false, true);//file144 is DRUMS +LoadBNK("fm_banks/bnk_files/file167.bnk", 13, "b13P", false, true); +LoadBNK("fm_banks/bnk_files/file168.bnk", 13, "b13M", false, false); + +LoadDoom("fm_banks/doom2/genmidi.op2", 14, "dM"); +LoadDoom("fm_banks/doom2/genmidi.htc", 15, "hxM"); // same as genmidi.hxn +LoadDoom("fm_banks/doom2/default.op2", 16, "mus"); + +LoadMiles("fm_banks/opl_files/file17.opl", 17, "f17G"); +LoadMiles("fm_banks/opl_files/warcraft.ad", 18, "sG"); // same as file44, warcraft.opl +LoadMiles("fm_banks/opl_files/file19.opl", 19, "f19G"); +LoadMiles("fm_banks/opl_files/file20.opl", 20, "f20G"); +LoadMiles("fm_banks/opl_files/file21.opl", 21, "f21G"); +LoadMiles("fm_banks/opl_files/nemesis.opl", 22, "nem"); +LoadMiles("fm_banks/opl_files/file23.opl", 23, "f23G"); +LoadMiles("fm_banks/opl_files/file24.opl", 24, "f24G"); +LoadMiles("fm_banks/opl_files/file25.opl", 25, "f25G"); +LoadMiles("fm_banks/opl_files/file26.opl", 26, "f26G"); +LoadMiles("fm_banks/opl_files/file27.opl", 27, "f27G"); +LoadMiles("fm_banks/opl_files/nhlpa.opl", 28, "nhl"); +LoadMiles("fm_banks/opl_files/file29.opl", 29, "f29G"); +LoadMiles("fm_banks/opl_files/file30.opl", 30, "f30G"); +LoadMiles("fm_banks/opl_files/file31.opl", 31, "f31G"); +LoadMiles("fm_banks/opl_files/file32.opl", 32, "f32G"); +LoadMiles("fm_banks/opl_files/file13.opl", 33, "f13G"); +LoadMiles("fm_banks/opl_files/file34.opl", 34, "f34G"); +LoadMiles("fm_banks/opl_files/file35.opl", 35, "f35G"); +LoadMiles("fm_banks/opl_files/file36.opl", 36, "f36G"); +LoadMiles("fm_banks/opl_files/file37.opl", 37, "f37G"); +LoadMiles("fm_banks/opl_files/simfarm.opl", 38, "qG"); +LoadMiles("fm_banks/opl_files/simfarm.ad", 39, "mG"); // same as file18.opl +LoadMiles("fm_banks/opl_files/file12.opl", 40, "f12G"); +LoadMiles("fm_banks/opl_files/file41.opl", 41, "f41G"); +LoadMiles("fm_banks/opl_files/file42.opl", 42, "f42G"); +LoadMiles("fm_banks/opl_files/file47.opl", 43, "f47G"); +LoadMiles("fm_banks/opl_files/file48.opl", 44, "f48G"); +LoadMiles("fm_banks/opl_files/file49.opl", 45, "f49G"); +LoadMiles("fm_banks/opl_files/file50.opl", 46, "f50G"); +LoadMiles("fm_banks/opl_files/file53.opl", 47, "f53G"); +LoadBNK("fm_banks/bnk_files/file144.bnk", 47, "f53GD", false, true);//Attempt to append missing drums +LoadMiles("fm_banks/opl_files/file54.opl", 48, "f54G"); + +LoadMiles("fm_banks/opl_files/sample.ad", 49, "MG"); // same as file51.opl +LoadMiles("fm_banks/opl_files/sample.opl", 50, "oG"); // same as file40.opl +LoadMiles("fm_banks/opl_files/file15.opl", 51, "f15G"); +LoadMiles("fm_banks/opl_files/file16.opl", 52, "f16G"); + +LoadBNK2("fm_banks/bnk_files/file159.bnk", 53, "b50", "gm", "gps"); // fat-opl3 +LoadBNK2("fm_banks/bnk_files/file159.bnk", 54, "b51", "gm", "gpo"); + +LoadIBK("fm_banks/ibk_files/soccer-genmidi.ibk", 55, "b55M", false); +LoadIBK("fm_banks/ibk_files/soccer-percs.ibk", 55, "b55P", true); +LoadIBK("fm_banks/ibk_files/game.ibk", 56, "b56", false); +LoadIBK("fm_banks/ibk_files/mt_fm.ibk", 57, "b57", false); + +LoadJunglevision("fm_banks/op3_files/fat2.op3", 58, "fat2"); +LoadJunglevision("fm_banks/op3_files/fat4.op3", 59, "fat4"); +LoadJunglevision("fm_banks/op3_files/jv_2op.op3", 60, "b60"); +LoadJunglevision("fm_banks/op3_files/wallace.op3", 61, "b61"); + +LoadTMB("fm_banks/tmb_files/d3dtimbr.tmb", 62, "duke"); +LoadTMB("fm_banks/tmb_files/swtimbr.tmb", 63, "sw"); + +LoadDoom("fm_banks/raptor/genmidi.op2", 64, "rapt"); + +//LoadJunglevision("fm_banks/op3_files/fat2_modded.op3", 65, "b65M"); +LoadTMB("fm_banks/op3_files/gmopl_wohl_mod.tmb", 65, "b65"); + +//LoadIBK("fm_banks/ibk_files/JOconnel.IBK", 66, "b66M", false); +//LoadIBK("fm_banks/ibk_files/my-gmopldrums.IBK", 66, "b66P", true); +LoadTMB("fm_banks/op3_files/gmoconel.tmb", 66, "b66"); + +LoadTMB("fm_banks/tmb_files/default.tmb", 67, "3drm67"); +//LoadDoom("fm_banks/doom2/wolfinstein.op2", 67, "wolf"); //Small experiment! + +//LoadJunglevision("fm_banks/op3_files/2x2.op3", 68, "2x2byJAN"); +LoadMiles("fm_banks/op3_files/2x2.opl", 68, "2x2byJAN"); + +LoadTMB("fm_banks/tmb_files/bloodtmb.tmb", 69, "apgblood"); +LoadTMB("fm_banks/tmb_files/lee.tmb", 70, "apglee"); +LoadTMB("fm_banks/tmb_files/nam.tmb", 71, "apgnam"); + +LoadDoom("fm_banks/doom2/DMXOPL-by-sneakernets.op2", 72, "skeakernets"); + +//LoadBNK("bnk_files/grassman1.bnk", 63, "b63", false); +//LoadBNK("bnk_files/grassman2.bnk", 64, "b64", false); + +//LoadIBK("ibk_files/nitemare_3d.ibk", 65, "b65G", false); // Seems to be identical to wallace.op3 despite different format! +#endif |