+
+ if (offsetbytes == 5)
+ sampleoffset -= 1000000000000ULL;
+ if (startbytes == 5)
+ start -= 1000000000000ULL;
+
+ std::string filename = string(name);
+ wav_t f = {
+ filename,
+ (uint16_t)findex,
+ (int64_t)(start*ratefactor),
+ (int64_t)(length*ratefactor),
+ };
+
+ if (strlen(name) == 0) {
+ continue;
+ }
+ if (length == 0) {
+ continue;
+ }
+ //if (foundin(filename, string(".grp")) && !regionsingroup && !findex) {
+ // // Empty region group
+ // verbose_printf(" EMPTY: %s\n", name);
+ // continue;
+ if (regionsingroup) {
+ // Active region grouping
+ // Iterate parsing all the regions in the group
+ verbose_printf("\nGROUP\t%d %s\n", groupcount, name);
+ m = j;
+ n = j+16;
+
+ for (l = 0; l < regionsingroup; l++) {
+ if (!jumpto(&n, ptfunxored, len, (const unsigned char *)"\x5a\x02", 2)) {
+ return;
+ }
+ n++;
+ }
+ n--;
+ //printf("n=0x%x\n", n+112);
+ //findex = ptfunxored[n+112] | (ptfunxored[n+113] << 8);
+ findex = u_endian_read2(&ptfunxored[i-11], is_bigendian);
+ findex2 = u_endian_read2(&ptfunxored[n+108], is_bigendian);
+ //findex2= rindex; //XXX
+ // Find wav with correct findex
+ vector<wav_t>::iterator wave = actualwavs.end();
+ for (vector<wav_t>::iterator aw = actualwavs.begin();
+ aw != actualwavs.end(); ++aw) {
+ if (aw->index == findex) {
+ wave = aw;
+ }
+ }
+ if (wave == actualwavs.end())
+ return;
+
+ if (!jumpto(&n, ptfunxored, len, (const unsigned char *)"\x5a\x02", 2))
+ return;
+ n += 37;
+ //rindex = ptfunxored[n] | (ptfunxored[n+1] << 8);
+ for (l = 0; l < regionsingroup; l++) {
+ if (!jumpto(&m, ptfunxored, len, (const unsigned char *)"\x5a\x02", 2))
+ return;
+
+ m += 37;
+ rindex = u_endian_read2(&ptfunxored[m], is_bigendian);
+
+ m += 12;
+ sampleoffset = 0;
+ switch (offsetbytes) {
+ case 5:
+ sampleoffset = u_endian_read5(&ptfunxored[m], false);
+ break;
+ case 4:
+ sampleoffset = (uint64_t)u_endian_read4(&ptfunxored[m], false);
+ break;
+ case 3:
+ sampleoffset = (uint64_t)u_endian_read3(&ptfunxored[m], false);
+ break;
+ case 2:
+ sampleoffset = (uint64_t)u_endian_read2(&ptfunxored[m], false);
+ break;
+ case 1:
+ sampleoffset = (uint64_t)(ptfunxored[m]);
+ break;
+ default:
+ break;
+ }
+ m+=offsetbytes+3;
+ start = 0;
+ switch (offsetbytes) {
+ case 5:
+ start = u_endian_read5(&ptfunxored[m], false);
+ break;
+ case 4:
+ start = (uint64_t)u_endian_read4(&ptfunxored[m], false);
+ break;
+ case 3:
+ start = (uint64_t)u_endian_read3(&ptfunxored[m], false);
+ break;
+ case 2:
+ start = (uint64_t)u_endian_read2(&ptfunxored[m], false);
+ break;
+ case 1:
+ start = (uint64_t)(ptfunxored[m]);
+ break;
+ default:
+ break;
+ }
+ m+=offsetbytes+3;
+ length = 0;
+ switch (lengthbytes) {
+ case 5:
+ length = u_endian_read5(&ptfunxored[m], false);
+ break;
+ case 4:
+ length = (uint64_t)u_endian_read4(&ptfunxored[m], false);
+ break;
+ case 3:
+ length = (uint64_t)u_endian_read3(&ptfunxored[m], false);
+ break;
+ case 2:
+ length = (uint64_t)u_endian_read2(&ptfunxored[m], false);
+ break;
+ case 1:
+ length = (uint64_t)(ptfunxored[m]);
+ break;
+ default:
+ break;
+ }
+ m+=8;
+ findex3 = ptfunxored[m] | (ptfunxored[m+1] << 8);
+ sampleoffset -= 1000000000000ULL;
+ start -= 1000000000000ULL;
+
+ /*
+ // Find wav with correct findex
+ vector<wav_t>::iterator wave = actualwavs.end();
+ for (vector<wav_t>::iterator aw = actualwavs.begin();
+ aw != actualwavs.end(); ++aw) {
+ if (aw->index == (glookup.begin()+findex2)->startpos) {
+ wave = aw;
+ }
+ }
+ if (wave == actualwavs.end())
+ return;
+ // findex is the true source
+ std::vector<midi_ev_t> md;
+ region_t r = {
+ name,
+ (uint16_t)rindex,
+ (int64_t)findex, //(start*ratefactor),
+ (int64_t)findex2, //(sampleoffset*ratefactor),
+ (int64_t)findex3, //(length*ratefactor),
+ *wave,
+ md
+ };
+ groups.push_back(r);
+ */
+ vector<compound_t>::iterator g = groupmap.begin()+findex2;
+ if (g >= groupmap.end())
+ continue;
+ compound_t c;
+ c.name = string(g->name);
+ c.curr_index = compoundcount;
+ c.level = findex;
+ c.ontopof_index = findex3;
+ c.next_index = g->next_index;
+ c.unknown1 = g->unknown1;
+ compounds.push_back(c);
+ verbose_printf("COMPOUND\tc(%d) %s (%d %d) -> c(%u) %s\n", c.curr_index, c.name.c_str(), c.level, c.ontopof_index, c.next_index, name);
+ compoundcount++;
+ }
+ groupcount++;
+ }
+ }
+ }
+ j = 0;
+
+ // Start pure regions
+ k = m != 0 ? m : k - 1;
+ if (!jumpto(&k, ptfunxored, k+64, (const unsigned char *)"\x5a\x05", 2))
+ jumpto(&k, ptfunxored, k+0x400, (const unsigned char *)"\x5a\x02", 2);
+
+ verbose_printf("pure regions k=0x%x\n", k);
+
+ maxregions = u_endian_read4(&ptfunxored[k-4], is_bigendian);
+
+ verbose_printf("maxregions=%u\n", maxregions);
+ rindex = 0;
+ for (i = k; rindex < maxregions && i < len; i++) {
+ if ( (ptfunxored[i ] == 0xff) &&
+ (ptfunxored[i+1] == 0x5a) &&
+ (ptfunxored[i+2] == 0x01)) {
+ break;
+ }
+ //if ( (ptfunxored[i ] == 0x5a) &&
+ // (ptfunxored[i+1] == 0x03)) {
+ // break;
+ //}
+ if ( (ptfunxored[i ] == 0x5a) &&
+ ((ptfunxored[i+1] == 0x01) || (ptfunxored[i+1] == 0x02))) {
+
+ //findex = ptfunxored[i-48] | ptfunxored[i-47] << 8;
+ //rindex = ptfunxored[i+3] | ptfunxored[i+4] << 8;
+
+ uint8_t lengthofname = ptfunxored[i+9];
+ if (ptfunxored[i+13] == 0x5a) {
+ continue;
+ }
+ char name[256] = {0};
+ for (j = 0; j < lengthofname; j++) {
+ name[j] = ptfunxored[i+13+j];
+ }
+ name[j] = '\0';
+ j += i+13;
+
+ offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
+ lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
+ startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
+ somethingbytes = (ptfunxored[j+3] & 0xf);
+ skipbytes = ptfunxored[j+4];
+ findex = ptfunxored[j+5
+ +startbytes
+ +lengthbytes
+ +offsetbytes
+ +somethingbytes
+ +skipbytes
+ +37]
+ | ptfunxored[j+5
+ +startbytes
+ +lengthbytes
+ +offsetbytes
+ +somethingbytes
+ +skipbytes
+ +38] << 8;
+
+ uint64_t sampleoffset = 0;
+ switch (offsetbytes) {
+ case 5:
+ sampleoffset = u_endian_read5(&ptfunxored[j+5], false);
+ break;