+ unsigned char tag1[3];
+ unsigned char tag2[3];
+ unsigned char tag3[3];
+
+ if (is_bigendian) {
+ tag1[0] = tag2[0] = tag3[0] = '\x5a';
+ tag1[1] = tag2[1] = tag3[1] = '\x00';
+ tag1[2] = '\x01';
+ tag2[2] = '\x02';
+ tag3[2] = '\x03';
+ } else {
+ tag1[0] = tag2[0] = tag3[0] = '\x5a';
+ tag1[1] = '\x01';
+ tag2[1] = '\x02';
+ tag3[1] = '\x03';
+ tag1[2] = tag2[2] = tag3[2] = '\x00';
+ }
+
+ // Find Source->Region info
+ k = upto;
+ for (i = 0; i < 2; i++) {
+ jumpto(&k, ptfunxored, len, tag3, 3);
+ k++;
+ }
+ jumpto(&k, ptfunxored, len, tag2, 3);
+
+ numberofregions = u_endian_read4(&ptfunxored[k-13], is_bigendian);
+
+ i = k;
+ while (numberofregions > 0 && i < len) {
+ jumpto(&i, ptfunxored, len, tag2, 3);
+
+ uint32_t lengthofname = u_endian_read4(&ptfunxored[i+9], is_bigendian);
+
+ char name[256] = {0};
+ for (j = 0; j < lengthofname; j++) {
+ name[j] = ptfunxored[i+13+j];
+ }
+ name[j] = '\0';
+ j += i+13;
+ //uint8_t disabled = ptfunxored[j];
+
+ if (is_bigendian) {
+ offsetbytes = (ptfunxored[j+4] & 0xf0) >> 4;
+ lengthbytes = (ptfunxored[j+3] & 0xf0) >> 4;
+ startbytes = (ptfunxored[j+2] & 0xf0) >> 4;
+ somethingbytes = (ptfunxored[j+2] & 0xf);
+ skipbytes = ptfunxored[j+1];
+ } else {
+ offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4; //3
+ lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
+ startbytes = (ptfunxored[j+3] & 0xf0) >> 4; //1
+ somethingbytes = (ptfunxored[j+3] & 0xf);
+ skipbytes = ptfunxored[j+4];
+ }
+ findex = u_endian_read4(&ptfunxored[j+5
+ +startbytes
+ +lengthbytes
+ +offsetbytes
+ +somethingbytes
+ +skipbytes], is_bigendian);
+
+ uint32_t sampleoffset = 0;
+ switch (offsetbytes) {
+ case 4:
+ sampleoffset = u_endian_read4(&ptfunxored[j+5], false);
+ break;
+ case 3:
+ sampleoffset = u_endian_read3(&ptfunxored[j+5], false);
+ break;
+ case 2:
+ sampleoffset = (uint32_t)u_endian_read2(&ptfunxored[j+5], false);
+ break;
+ case 1:
+ sampleoffset = (uint32_t)(ptfunxored[j+5]);
+ break;
+ default:
+ break;
+ }
+ j+=offsetbytes;
+ uint32_t length = 0;
+ switch (lengthbytes) {
+ case 4:
+ length = u_endian_read4(&ptfunxored[j+5], false);
+ break;
+ case 3:
+ length = u_endian_read3(&ptfunxored[j+5], false);
+ break;
+ case 2:
+ length = (uint32_t)u_endian_read2(&ptfunxored[j+5], false);
+ break;
+ case 1:
+ length = (uint32_t)(ptfunxored[j+5]);
+ break;
+ default:
+ break;
+ }
+ j+=lengthbytes;
+ uint32_t start = 0;
+ switch (startbytes) {
+ case 4:
+ start = u_endian_read4(&ptfunxored[j+5], false);
+ break;
+ case 3:
+ start = u_endian_read3(&ptfunxored[j+5], false);
+ break;
+ case 2:
+ start = (uint32_t)u_endian_read2(&ptfunxored[j+5], false);
+ break;
+ case 1:
+ start = (uint32_t)(ptfunxored[j+5]);
+ break;
+ default:
+ break;
+ }
+ j+=startbytes;
+
+ std::string filename = string(name);
+ wav_t f = {
+ "",
+ (uint16_t)findex,
+ (int64_t)(start*ratefactor),
+ (int64_t)(length*ratefactor),
+ };
+
+ vector<wav_t>::iterator begin = actualwavs.begin();
+ vector<wav_t>::iterator finish = actualwavs.end();
+ vector<wav_t>::iterator found;
+ if ((found = std::find(begin, finish, f)) != finish) {
+ f.filename = (*found).filename;
+ }
+ std::vector<midi_ev_t> m;
+ region_t r = {
+ name,
+ rindex,
+ (int64_t)(start*ratefactor),
+ (int64_t)(sampleoffset*ratefactor),
+ (int64_t)(length*ratefactor),
+ f,
+ m
+ };
+ regions.push_back(r);
+ rindex++;
+ i = j + 1;
+ numberofregions--;
+ }