2 Copyright (C) 2015 Damien Zammit
3 Copyright (C) 2015 Robin Gareus
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
23 #include <glib/gstdio.h>
25 #include "ptfformat.h"
29 static const uint32_t baselut[16] = {
30 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
31 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
32 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
33 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
36 static const uint32_t xorlut[16] = {
37 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
38 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
39 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
40 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
43 static uint32_t swapbytes32 (const uint32_t v) {
45 rv |= ((v >> 0) & 0xf) << 28;
46 rv |= ((v >> 4) & 0xf) << 24;
47 rv |= ((v >> 8) & 0xf) << 20;
48 rv |= ((v >> 12) & 0xf) << 16;
49 rv |= ((v >> 16) & 0xf) << 12;
50 rv |= ((v >> 20) & 0xf) << 8;
51 rv |= ((v >> 24) & 0xf) << 4;
52 rv |= ((v >> 28) & 0xf) << 0;
56 static uint64_t gen_secret (int i) {
57 assert (i > 0 && i < 256);
58 int iwrap = i & 0x7f; // wrap at 0x80;
59 uint32_t xor_lo = 0; // 0x40 flag
60 int idx; // mirror at 0x40;
69 int i16 = (idx >> 1) & 0xf;
74 uint32_t lo = baselut [i16];
75 uint32_t xk = xorlut [i16];
81 uint32_t hi = swapbytes32 (lo) ^ xk;
82 return ((uint64_t)hi << 32) | (lo ^ xor_lo);
85 PTFFormat::PTFFormat() {
88 PTFFormat::~PTFFormat() {
95 PTFFormat::foundin(std::string haystack, std::string needle) {
96 size_t found = haystack.find(needle);
97 if (found != std::string::npos) {
104 /* Return values: 0 success
105 0x01 to 0xff value of missing lut
106 -1 could not open file as ptf
109 PTFFormat::load(std::string path, int64_t targetsr) {
111 unsigned char xxor[256];
121 if (! (fp = g_fopen(path.c_str(), "rb"))) {
125 fseek(fp, 0, SEEK_END);
131 fseek(fp, 0x40, SEEK_SET);
132 fread(&c0, 1, 1, fp);
133 fread(&c1, 1, 1, fp);
135 // For version <= 7 support:
139 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
140 /* Silently fail -- out of memory*/
151 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
153 for (i = 2; i < 256; i++) {
157 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
158 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
166 for (i = 2; i < 256; i++) {
170 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
173 for (i = 0; i < 64; i++) {
176 for (i = 128; i < 192; i++) {
184 for (i = 2; i < 256; i++) {
188 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
192 key = gen_secret(c1);
194 for (i = 0; i < 64; i++) {
195 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
197 for (i = 128; i < 192; i++) {
198 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
199 xxor[i] ^= (inv * 0x40);
202 for (i = 192; i < 256; i++) {
208 //Should not happen, failed c[0] c[1]
215 fseek(fp, 0, SEEK_SET);
216 while (fread(&ct, 1, 1, fp) != 0) {
217 ptfunxored[i++] = ct;
221 /* version detection */
223 v = ptfunxored[voff];
226 } else if (v == 0x03) {
231 v = ptfunxored[voff];
232 if (v == 10 || v == 11 || v == 12) {
237 // Special case when ptx is exported to ptf in PT
243 if (version == 0 || version == 5 || version == 7) {
244 /* Haven't detected version yet so decipher */
246 for (i = 0; i < len; i++) {
250 ptfunxored[i] ^= xxor[j];
254 /* version detection */
256 v = ptfunxored[voff];
259 } else if (v == 0x03) {
264 v = ptfunxored[voff];
265 if (v == 5 || v == 7 || v == 8 || v == 9) {
270 if (version < 5 || version > 12)
272 targetrate = targetsr;
280 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
282 uint32_t counts[256] = {0};
287 for (i = start; i < stop; i++) {
288 counts[ptfunxored[i]]++;
291 for (i = 0; i < 256; i++) {
292 if (counts[i] > max) {
301 PTFFormat::unxor10(void)
304 uint8_t x = mostfrequent(0x1000, 0x2000);
305 uint8_t dx = 0x100-x;
307 for (j = 0x1000; j < len; j++) {
308 if(j % 0x1000 == 0xfff) {
316 PTFFormat::unxor_ptx_to_ptf(void)
318 unsigned char keyy[16] = { 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,
319 0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0
324 for (i = 0, j = 0x10; j < len; j++,i++) {
325 ptfunxored[j] ^= keyy[i];
328 if (ptfunxored[j] % 2 == 0) {
338 PTFFormat::parse(void) {
342 if (sessionrate < 44100 || sessionrate > 192000)
346 } else if (version == 7) {
349 if (sessionrate < 44100 || sessionrate > 192000)
353 } else if (version == 8) {
356 if (sessionrate < 44100 || sessionrate > 192000)
360 } else if (version == 9) {
363 if (sessionrate < 44100 || sessionrate > 192000)
367 } else if (version == 10 || version == 11 || version == 12) {
370 if (sessionrate < 44100 || sessionrate > 192000)
382 PTFFormat::setrates(void) {
384 if (sessionrate != 0) {
385 ratefactor = (float)targetrate / sessionrate;
390 PTFFormat::parse5header(void) {
393 // Find session sample rate
396 if ( (ptfunxored[k ] == 0x5a) &&
397 (ptfunxored[k+1] == 0x00) &&
398 (ptfunxored[k+2] == 0x02)) {
405 sessionrate |= ptfunxored[k+12] << 16;
406 sessionrate |= ptfunxored[k+13] << 8;
407 sessionrate |= ptfunxored[k+14];
411 PTFFormat::parse7header(void) {
414 // Find session sample rate
417 if ( (ptfunxored[k ] == 0x5a) &&
418 (ptfunxored[k+1] == 0x00) &&
419 (ptfunxored[k+2] == 0x05)) {
426 sessionrate |= ptfunxored[k+12] << 16;
427 sessionrate |= ptfunxored[k+13] << 8;
428 sessionrate |= ptfunxored[k+14];
432 PTFFormat::parse8header(void) {
435 // Find session sample rate
438 if ( (ptfunxored[k ] == 0x5a) &&
439 (ptfunxored[k+1] == 0x05)) {
446 sessionrate |= ptfunxored[k+11];
447 sessionrate |= ptfunxored[k+12] << 8;
448 sessionrate |= ptfunxored[k+13] << 16;
452 PTFFormat::parse9header(void) {
455 // Find session sample rate
458 if ( (ptfunxored[k ] == 0x5a) &&
459 (ptfunxored[k+1] == 0x06)) {
466 sessionrate |= ptfunxored[k+11];
467 sessionrate |= ptfunxored[k+12] << 8;
468 sessionrate |= ptfunxored[k+13] << 16;
472 PTFFormat::parse10header(void) {
475 // Find session sample rate
478 if ( (ptfunxored[k ] == 0x5a) &&
479 (ptfunxored[k+1] == 0x09)) {
486 sessionrate |= ptfunxored[k+11];
487 sessionrate |= ptfunxored[k+12] << 8;
488 sessionrate |= ptfunxored[k+13] << 16;
492 PTFFormat::parserest5(void) {
494 uint64_t regionspertrack, lengthofname;
495 uint64_t startbytes, lengthbytes, offsetbytes;
496 uint16_t tracknumber = 0;
501 for (i = 0; i < 5; i++) {
503 if ( (ptfunxored[k ] == 0x5a) &&
504 (ptfunxored[k+1] == 0x00) &&
505 (ptfunxored[k+2] == 0x03)) {
514 for (i = 0; i < 2; i++) {
516 if ( (ptfunxored[k ] == 0x5a) &&
517 (ptfunxored[k+1] == 0x00) &&
518 (ptfunxored[k+2] == 0x01)) {
530 if ( (ptfunxored[k ] == 0xff) &&
531 (ptfunxored[k+1] == 0xff)) {
535 if ( (ptfunxored[k ] == 0x5a) &&
536 (ptfunxored[k+1] == 0x00) &&
537 (ptfunxored[k+2] == 0x01)) {
543 lengthofname = ptfunxored[k+12];
544 if (ptfunxored[k+13] == 0x5a) {
548 char name[256] = {0};
549 for (j = 0; j < lengthofname; j++) {
550 name[j] = ptfunxored[k+13+j];
553 regionspertrack = ptfunxored[k+13+j+3];
554 for (i = 0; i < regionspertrack; i++) {
556 if ( (ptfunxored[k ] == 0x5a) &&
557 (ptfunxored[k+1] == 0x00) &&
558 (ptfunxored[k+2] == 0x03)) {
564 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
565 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
566 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
567 //somethingbytes = (ptfunxored[j+1] & 0xf);
568 findex = ptfunxored[k+14];
571 switch (startbytes) {
573 start |= (uint32_t)(ptfunxored[j+8] << 24);
575 start |= (uint32_t)(ptfunxored[j+7] << 16);
577 start |= (uint32_t)(ptfunxored[j+6] << 8);
579 start |= (uint32_t)(ptfunxored[j+5]);
585 switch (lengthbytes) {
587 length |= (uint32_t)(ptfunxored[j+8] << 24);
589 length |= (uint32_t)(ptfunxored[j+7] << 16);
591 length |= (uint32_t)(ptfunxored[j+6] << 8);
593 length |= (uint32_t)(ptfunxored[j+5]);
598 uint32_t sampleoffset = 0;
599 switch (offsetbytes) {
601 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
603 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
605 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
607 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
613 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
615 std::string filename = string(name) + extension;
619 (int64_t)(start*ratefactor),
620 (int64_t)(length*ratefactor),
623 vector<wav_t>::iterator begin = audiofiles.begin();
624 vector<wav_t>::iterator finish = audiofiles.end();
625 vector<wav_t>::iterator found;
627 if ((found = std::find(begin, finish, f)) != finish) {
631 (int64_t)(start*ratefactor),
632 (int64_t)(sampleoffset*ratefactor),
633 (int64_t)(length*ratefactor),
636 regions.push_back(r);
637 vector<track_t>::iterator ti;
638 vector<track_t>::iterator bt = tracks.begin();
639 vector<track_t>::iterator et = tracks.end();
640 track_t tr = { name, 0, 0, r };
641 if ((ti = std::find(bt, et, tr)) != et) {
642 tracknumber = (*ti).index;
644 tracknumber = tracks.size() + 1;
648 (uint16_t)tracknumber,
657 (int64_t)(start*ratefactor),
658 (int64_t)(sampleoffset*ratefactor),
659 (int64_t)(length*ratefactor),
662 regions.push_back(r);
663 vector<track_t>::iterator ti;
664 vector<track_t>::iterator bt = tracks.begin();
665 vector<track_t>::iterator et = tracks.end();
666 track_t tr = { name, 0, 0, r };
667 if ((ti = std::find(bt, et, tr)) != et) {
668 tracknumber = (*ti).index;
670 tracknumber = tracks.size() + 1;
674 (uint16_t)tracknumber,
688 PTFFormat::resort(std::vector<wav_t>& ws) {
690 std::sort(ws.begin(), ws.end());
691 for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
698 PTFFormat::parseaudio5(void) {
700 uint64_t lengthofname, wavnumber;
702 // Find end of wav file list
705 if ( (ptfunxored[k ] == 0x5f) &&
706 (ptfunxored[k+1] == 0x50) &&
707 (ptfunxored[k+2] == 0x35)) {
714 if ( (ptfunxored[k ] == 0x5f) &&
715 (ptfunxored[k+1] == 0x50) &&
716 (ptfunxored[k+2] == 0x35)) {
722 // Find actual wav names
723 uint16_t numberofwavs = ptfunxored[k-23];
725 for (i = k; i < len; i++) {
726 if ( (ptfunxored[i ] == 'F') &&
727 (ptfunxored[i+1] == 'i') &&
728 (ptfunxored[i+2] == 'l') &&
729 (ptfunxored[i+3] == 'e') &&
730 (ptfunxored[i+4] == 's')) {
738 while (i < len && numberofwavs > 0) {
740 if ( (ptfunxored[i ] == 0x5a) &&
741 (ptfunxored[i+1] == 0x00) &&
742 (ptfunxored[i+2] == 0x05)) {
745 lengthofname = ptfunxored[i];
748 while (l < lengthofname) {
749 wavname[l] = ptfunxored[i+l];
753 ext[0] = ptfunxored[i++];
754 ext[1] = ptfunxored[i++];
755 ext[2] = ptfunxored[i++];
756 ext[3] = ptfunxored[i++];
760 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
761 extension = string("");
762 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
763 extension = string(".wav");
764 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
765 extension = string(".aif");
767 extension = string("");
770 std::string wave = string(wavname);
771 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
773 if (foundin(wave, string(".grp"))) {
777 actualwavs.push_back(f);
778 audiofiles.push_back(f);
788 PTFFormat::parseaudio(void) {
791 // Find end of wav file list
794 if ( (ptfunxored[k ] == 0xff) &&
795 (ptfunxored[k+1] == 0xff) &&
796 (ptfunxored[k+2] == 0xff) &&
797 (ptfunxored[k+3] == 0xff)) {
803 // Find actual wav names
805 uint16_t numberofwavs;
807 for (i = k; i > 4; i--) {
808 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
809 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
810 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
811 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
814 while (ptfunxored[j] != '\0') {
815 wavname[l] = ptfunxored[j];
820 if (ptfunxored[i] == 'W') {
821 extension = string(".wav");
823 extension = string(".aif");
825 //uint8_t playlist = ptfunxored[j-8];
829 for (j = k; j > 4; j--) {
830 if ( (ptfunxored[j ] == 0x01) &&
831 (ptfunxored[j-1] == 0x5a)) {
834 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
835 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
836 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
837 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
838 //printf("%d wavs\n", numberofwavs);
845 std::string wave = string(wavname);
846 std::reverse(wave.begin(), wave.end());
847 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
849 if (foundin(wave, string(".grp"))) {
853 actualwavs.push_back(f);
856 if (numberofwavs <= 0)
863 PTFFormat::parserest89(void) {
866 uint8_t startbytes = 0;
867 uint8_t lengthbytes = 0;
868 uint8_t offsetbytes = 0;
869 uint8_t somethingbytes = 0;
870 uint8_t skipbytes = 0;
874 if ( (ptfunxored[k ] == 'S') &&
875 (ptfunxored[k+1] == 'n') &&
876 (ptfunxored[k+2] == 'a') &&
877 (ptfunxored[k+3] == 'p')) {
884 for (i = k; i < len-70; i++) {
885 if ( (ptfunxored[i ] == 0x5a) &&
886 (ptfunxored[i+1] == 0x0a)) {
889 if ( (ptfunxored[i ] == 0x5a) &&
890 (ptfunxored[i+1] == 0x0c)) {
892 uint8_t lengthofname = ptfunxored[i+9];
894 char name[256] = {0};
895 for (j = 0; j < lengthofname; j++) {
896 name[j] = ptfunxored[i+13+j];
900 //uint8_t disabled = ptfunxored[j];
902 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
903 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
904 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
905 somethingbytes = (ptfunxored[j+3] & 0xf);
906 skipbytes = ptfunxored[j+4];
907 findex = ptfunxored[j+5
914 /*rindex = ptfunxored[j+5
922 uint32_t sampleoffset = 0;
923 switch (offsetbytes) {
925 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
927 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
929 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
931 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
937 switch (lengthbytes) {
939 length |= (uint32_t)(ptfunxored[j+8] << 24);
941 length |= (uint32_t)(ptfunxored[j+7] << 16);
943 length |= (uint32_t)(ptfunxored[j+6] << 8);
945 length |= (uint32_t)(ptfunxored[j+5]);
951 switch (startbytes) {
953 start |= (uint32_t)(ptfunxored[j+8] << 24);
955 start |= (uint32_t)(ptfunxored[j+7] << 16);
957 start |= (uint32_t)(ptfunxored[j+6] << 8);
959 start |= (uint32_t)(ptfunxored[j+5]);
965 uint32_t something = 0;
966 switch (somethingbytes) {
968 something |= (uint32_t)(ptfunxored[j+8] << 24);
970 something |= (uint32_t)(ptfunxored[j+7] << 16);
972 something |= (uint32_t)(ptfunxored[j+6] << 8);
974 something |= (uint32_t)(ptfunxored[j+5]);
980 std::string filename = string(name) + extension;
984 (int64_t)(start*ratefactor),
985 (int64_t)(length*ratefactor),
989 //printf("something=%d\n", something);
991 vector<wav_t>::iterator begin = actualwavs.begin();
992 vector<wav_t>::iterator finish = actualwavs.end();
993 vector<wav_t>::iterator found;
994 // Add file to list only if it is an actual wav
995 if ((found = std::find(begin, finish, f)) != finish) {
996 audiofiles.push_back(f);
997 // Also add plain wav as region
1001 (int64_t)(start*ratefactor),
1002 (int64_t)(sampleoffset*ratefactor),
1003 (int64_t)(length*ratefactor),
1006 regions.push_back(r);
1009 if (foundin(filename, string(".grp"))) {
1015 (int64_t)(start*ratefactor),
1016 (int64_t)(sampleoffset*ratefactor),
1017 (int64_t)(length*ratefactor),
1020 regions.push_back(r);
1027 if ( (ptfunxored[k ] == 0x5a) &&
1028 (ptfunxored[k+1] == 0x03)) {
1034 if ( (ptfunxored[k ] == 0x5a) &&
1035 (ptfunxored[k+1] == 0x02)) {
1044 uint32_t tracknumber = 0;
1045 uint32_t regionspertrack = 0;
1046 for (;k < len; k++) {
1047 if ( (ptfunxored[k ] == 0x5a) &&
1048 (ptfunxored[k+1] == 0x04)) {
1051 if ( (ptfunxored[k ] == 0x5a) &&
1052 (ptfunxored[k+1] == 0x02)) {
1054 uint8_t lengthofname = 0;
1055 lengthofname = ptfunxored[k+9];
1056 if (lengthofname == 0x5a) {
1061 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1063 //printf("regions/track=%d\n", regionspertrack);
1064 char name[256] = {0};
1065 for (j = 0; j < lengthofname; j++) {
1066 name[j] = ptfunxored[j+k+13];
1069 tr.name = string(name);
1070 tr.index = tracknumber++;
1072 for (j = k; regionspertrack > 0 && j < len; j++) {
1073 for (l = j; l < len; l++) {
1074 if ( (ptfunxored[l ] == 0x5a) &&
1075 (ptfunxored[l+1] == 0x07)) {
1082 if (regionspertrack == 0) {
1083 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1087 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1088 vector<region_t>::iterator begin = regions.begin();
1089 vector<region_t>::iterator finish = regions.end();
1090 vector<region_t>::iterator found;
1091 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1096 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1097 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1098 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1099 offset |= (uint32_t)(ptfunxored[i]);
1100 tr.reg.startpos = (int64_t)(offset*ratefactor);
1101 if (tr.reg.length > 0) {
1102 tracks.push_back(tr);
1112 PTFFormat::parserest10(void) {
1115 uint8_t startbytes = 0;
1116 uint8_t lengthbytes = 0;
1117 uint8_t offsetbytes = 0;
1118 uint8_t somethingbytes = 0;
1119 uint8_t skipbytes = 0;
1123 if ( (ptfunxored[k ] == 'S') &&
1124 (ptfunxored[k+1] == 'n') &&
1125 (ptfunxored[k+2] == 'a') &&
1126 (ptfunxored[k+3] == 'p')) {
1131 for (i = k; i < len-70; i++) {
1132 if ( (ptfunxored[i ] == 0x5a) &&
1133 (ptfunxored[i+1] == 0x02)) {
1139 for (i = k; i < len-70; i++) {
1140 if ( (ptfunxored[i ] == 0x5a) &&
1141 (ptfunxored[i+1] == 0x02)) {
1147 uint16_t rindex = 0;
1148 uint32_t findex = 0;
1149 for (i = k; i < len-70; i++) {
1150 if ( (ptfunxored[i ] == 0x5a) &&
1151 (ptfunxored[i+1] == 0x08)) {
1154 if ( (ptfunxored[i ] == 0x5a) &&
1155 (ptfunxored[i+1] == 0x01)) {
1157 uint8_t lengthofname = ptfunxored[i+9];
1158 if (ptfunxored[i+13] == 0x5a) {
1161 char name[256] = {0};
1162 for (j = 0; j < lengthofname; j++) {
1163 name[j] = ptfunxored[i+13+j];
1167 //uint8_t disabled = ptfunxored[j];
1168 //printf("%s\n", name);
1170 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1171 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1172 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1173 somethingbytes = (ptfunxored[j+3] & 0xf);
1174 skipbytes = ptfunxored[j+4];
1175 findex = ptfunxored[j+5
1182 /*rindex = ptfunxored[j+5
1190 uint32_t sampleoffset = 0;
1191 switch (offsetbytes) {
1193 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1195 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1197 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1199 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1204 uint32_t length = 0;
1205 switch (lengthbytes) {
1207 length |= (uint32_t)(ptfunxored[j+8] << 24);
1209 length |= (uint32_t)(ptfunxored[j+7] << 16);
1211 length |= (uint32_t)(ptfunxored[j+6] << 8);
1213 length |= (uint32_t)(ptfunxored[j+5]);
1219 switch (startbytes) {
1221 start |= (uint32_t)(ptfunxored[j+8] << 24);
1223 start |= (uint32_t)(ptfunxored[j+7] << 16);
1225 start |= (uint32_t)(ptfunxored[j+6] << 8);
1227 start |= (uint32_t)(ptfunxored[j+5]);
1233 uint32_t something = 0;
1234 switch (somethingbytes) {
1236 something |= (uint32_t)(ptfunxored[j+8] << 24);
1238 something |= (uint32_t)(ptfunxored[j+7] << 16);
1240 something |= (uint32_t)(ptfunxored[j+6] << 8);
1242 something |= (uint32_t)(ptfunxored[j+5]);
1248 std::string filename = string(name) + extension;
1252 (int64_t)(start*ratefactor),
1253 (int64_t)(length*ratefactor),
1256 if (strlen(name) == 0) {
1263 //printf("something=%d\n", something);
1265 vector<wav_t>::iterator begin = actualwavs.begin();
1266 vector<wav_t>::iterator finish = actualwavs.end();
1267 vector<wav_t>::iterator found;
1268 // Add file to list only if it is an actual wav
1269 if ((found = std::find(begin, finish, f)) != finish) {
1270 audiofiles.push_back(f);
1271 // Also add plain wav as region
1275 (int64_t)(start*ratefactor),
1276 (int64_t)(sampleoffset*ratefactor),
1277 (int64_t)(length*ratefactor),
1280 regions.push_back(r);
1283 if (foundin(filename, string(".grp"))) {
1289 (int64_t)(start*ratefactor),
1290 (int64_t)(sampleoffset*ratefactor),
1291 (int64_t)(length*ratefactor),
1294 regions.push_back(r);
1297 //printf("%s\n", name);
1302 uint32_t tracknumber = 0;
1303 uint32_t regionspertrack = 0;
1304 for (;k < len; k++) {
1305 if ( (ptfunxored[k ] == 0x5a) &&
1306 (ptfunxored[k+1] == 0x08)) {
1311 for (;k < len; k++) {
1312 if ( (ptfunxored[k ] == 0x5a) &&
1313 (ptfunxored[k+1] == 0x04)) {
1316 if ( (ptfunxored[k ] == 0x5a) &&
1317 (ptfunxored[k+1] == 0x02)) {
1319 uint8_t lengthofname = 0;
1320 lengthofname = ptfunxored[k+9];
1321 if (lengthofname == 0x5a) {
1326 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1328 //printf("regions/track=%d\n", regionspertrack);
1329 char name[256] = {0};
1330 for (j = 0; j < lengthofname; j++) {
1331 name[j] = ptfunxored[j+k+13];
1334 tr.name = string(name);
1335 tr.index = tracknumber++;
1337 for (j = k; regionspertrack > 0 && j < len; j++) {
1338 for (l = j; l < len; l++) {
1339 if ( (ptfunxored[l ] == 0x5a) &&
1340 (ptfunxored[l+1] == 0x08)) {
1347 if (regionspertrack == 0) {
1348 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1352 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1353 vector<region_t>::iterator begin = regions.begin();
1354 vector<region_t>::iterator finish = regions.end();
1355 vector<region_t>::iterator found;
1356 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1361 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1362 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1363 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1364 offset |= (uint32_t)(ptfunxored[i]);
1365 tr.reg.startpos = (int64_t)(offset*ratefactor);
1366 if (tr.reg.length > 0) {
1367 tracks.push_back(tr);