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.
17 #include "ptfformat.h"
26 static const uint32_t baselut[16] = {
27 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
28 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
29 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
30 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
33 static const uint32_t xorlut[16] = {
34 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
35 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
36 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
37 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
40 static uint32_t swapbytes32 (const uint32_t v) {
42 rv |= ((v >> 0) & 0xf) << 28;
43 rv |= ((v >> 4) & 0xf) << 24;
44 rv |= ((v >> 8) & 0xf) << 20;
45 rv |= ((v >> 12) & 0xf) << 16;
46 rv |= ((v >> 16) & 0xf) << 12;
47 rv |= ((v >> 20) & 0xf) << 8;
48 rv |= ((v >> 24) & 0xf) << 4;
49 rv |= ((v >> 28) & 0xf) << 0;
53 static uint64_t gen_secret (int i) {
54 assert (i > 0 && i < 256);
55 int iwrap = i & 0x7f; // wrap at 0x80;
56 uint32_t xor_lo = 0; // 0x40 flag
57 int idx; // mirror at 0x40;
66 int i16 = (idx >> 1) & 0xf;
71 uint32_t lo = baselut [i16];
72 uint32_t xk = xorlut [i16];
78 uint32_t hi = swapbytes32 (lo) ^ xk;
79 return ((uint64_t)hi << 32) | (lo ^ xor_lo);
82 PTFFormat::PTFFormat() {
85 PTFFormat::~PTFFormat() {
92 PTFFormat::foundin(std::string haystack, std::string needle) {
93 size_t found = haystack.find(needle);
94 if (found != std::string::npos) {
101 /* Return values: 0 success
102 0x01 to 0xff value of missing lut
103 -1 could not open file as ptf
106 PTFFormat::load(std::string path, int64_t targetsr) {
108 unsigned char xxor[256];
115 unsigned char message;
117 if (! (fp = fopen(path.c_str(), "rb"))) {
121 fseek(fp, 0, SEEK_END);
127 fseek(fp, 0x40, SEEK_SET);
128 fread(&c0, 1, 1, fp);
129 fread(&c1, 1, 1, fp);
131 // For version <= 7 support:
135 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
136 /* Silently fail -- out of memory*/
142 fseek(fp, 0x0, SEEK_SET);
149 //fprintf(stderr, "%02x %02x", c0, c1);
151 for (i = 2; i < 64; i++) {
152 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
153 //fprintf(stderr, "%02x ", xxor[i]);
156 fread(&ct, 1, 1, fp);
158 ptfunxored[0] = message;
160 fread(&ct, 1, 1, fp);
162 ptfunxored[1] = message;
165 while (fread(&ct, 1, 1, fp) != 0) {
169 message = xxor[i] ^ ct;
170 ptfunxored[j] = message;
179 for (i = 2; i < 256; i++) {
183 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
186 for (i = 0; i < 64; i++) {
189 for (i = 128; i < 192; i++) {
193 fread(&ct, 1, 1, fp);
195 ptfunxored[0] = message;
197 fread(&ct, 1, 1, fp);
199 ptfunxored[1] = message;
202 while (fread(&ct, 1, 1, fp) != 0) {
206 message = xxor[i] ^ ct;
207 ptfunxored[j] = message;
216 for (i = 2; i < 256; i++) {
220 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
224 key = gen_secret(c1);
225 for (i = 0; i < 64; i++) {
226 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
228 for (i = 128; i < 192; i++) {
229 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
230 xxor[i] ^= (inv * 0x40);
233 for (i = 192; i < 256; i++) {
237 fread(&ct, 1, 1, fp);
239 ptfunxored[0] = message;
241 fread(&ct, 1, 1, fp);
243 ptfunxored[1] = message;
246 while (fread(&ct, 1, 1, fp) != 0) {
250 message = xxor[i] ^ ct;
251 ptfunxored[j] = message;
258 //Should not happen, failed c[0] c[1]
263 targetrate = targetsr;
269 PTFFormat::unxor10(void)
271 key10a = ptfunxored[0x9f];
272 key10b = ptfunxored[0x9e] - ptfunxored[0x9b];
276 for (j = k; j < k + 0x1000 && j < len; j++) {
277 ptfunxored[j] ^= key10a;
280 for (j = k; j < k + 0x1000 && j < len; j++) {
281 ptfunxored[j] ^= key10b;
286 currkey = (key10a + currkey) & 0xff;
287 for (j = k; j < k + 0x1000 && j < len; j++) {
288 ptfunxored[j] ^= currkey;
294 PTFFormat::parse(void) {
295 version = (version == 0) ? ptfunxored[61] : version;
302 } else if (version == 7) {
307 } else if (version == 8) {
312 } else if (version == 9) {
317 } else if (version == 10) {
329 PTFFormat::setrates(void) {
331 if (sessionrate != 0) {
332 ratefactor = (float)targetrate / sessionrate;
337 PTFFormat::parse5header(void) {
340 // Find session sample rate
343 if ( (ptfunxored[k ] == 0x5a) &&
344 (ptfunxored[k+1] == 0x00) &&
345 (ptfunxored[k+2] == 0x02)) {
352 sessionrate |= ptfunxored[k+12] << 16;
353 sessionrate |= ptfunxored[k+13] << 8;
354 sessionrate |= ptfunxored[k+14];
358 PTFFormat::parse7header(void) {
361 // Find session sample rate
364 if ( (ptfunxored[k ] == 0x5a) &&
365 (ptfunxored[k+1] == 0x00) &&
366 (ptfunxored[k+2] == 0x05)) {
373 sessionrate |= ptfunxored[k+12] << 16;
374 sessionrate |= ptfunxored[k+13] << 8;
375 sessionrate |= ptfunxored[k+14];
379 PTFFormat::parse8header(void) {
382 // Find session sample rate
385 if ( (ptfunxored[k ] == 0x5a) &&
386 (ptfunxored[k+1] == 0x05)) {
393 sessionrate |= ptfunxored[k+11];
394 sessionrate |= ptfunxored[k+12] << 8;
395 sessionrate |= ptfunxored[k+13] << 16;
399 PTFFormat::parse9header(void) {
402 // Find session sample rate
405 if ( (ptfunxored[k ] == 0x5a) &&
406 (ptfunxored[k+1] == 0x06)) {
413 sessionrate |= ptfunxored[k+11];
414 sessionrate |= ptfunxored[k+12] << 8;
415 sessionrate |= ptfunxored[k+13] << 16;
419 PTFFormat::parse10header(void) {
422 // Find session sample rate
425 if ( (ptfunxored[k ] == 0x5a) &&
426 (ptfunxored[k+1] == 0x09)) {
433 sessionrate |= ptfunxored[k+11];
434 sessionrate |= ptfunxored[k+12] << 8;
435 sessionrate |= ptfunxored[k+13] << 16;
439 PTFFormat::parserest5(void) {
441 int regionspertrack, lengthofname;
442 int startbytes, lengthbytes, offsetbytes;
443 uint16_t tracknumber = 0;
448 for (i = 0; i < 5; i++) {
450 if ( (ptfunxored[k ] == 0x5a) &&
451 (ptfunxored[k+1] == 0x00) &&
452 (ptfunxored[k+2] == 0x03)) {
461 for (i = 0; i < 2; i++) {
463 if ( (ptfunxored[k ] == 0x5a) &&
464 (ptfunxored[k+1] == 0x00) &&
465 (ptfunxored[k+2] == 0x01)) {
476 if ( (ptfunxored[k ] == 0xff) &&
477 (ptfunxored[k+1] == 0xff)) {
481 if ( (ptfunxored[k ] == 0x5a) &&
482 (ptfunxored[k+1] == 0x00) &&
483 (ptfunxored[k+2] == 0x01)) {
489 lengthofname = ptfunxored[k+12];
490 if (ptfunxored[k+13] == 0x5a) {
494 char name[256] = {0};
495 for (j = 0; j < lengthofname; j++) {
496 name[j] = ptfunxored[k+13+j];
499 regionspertrack = ptfunxored[k+13+j+3];
500 for (i = 0; i < regionspertrack; i++) {
502 if ( (ptfunxored[k ] == 0x5a) &&
503 (ptfunxored[k+1] == 0x00) &&
504 (ptfunxored[k+2] == 0x03)) {
510 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
511 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
512 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
513 //somethingbytes = (ptfunxored[j+1] & 0xf);
514 findex = ptfunxored[k+14];
517 switch (startbytes) {
519 start |= (uint32_t)(ptfunxored[j+8] << 24);
521 start |= (uint32_t)(ptfunxored[j+7] << 16);
523 start |= (uint32_t)(ptfunxored[j+6] << 8);
525 start |= (uint32_t)(ptfunxored[j+5]);
531 switch (lengthbytes) {
533 length |= (uint32_t)(ptfunxored[j+8] << 24);
535 length |= (uint32_t)(ptfunxored[j+7] << 16);
537 length |= (uint32_t)(ptfunxored[j+6] << 8);
539 length |= (uint32_t)(ptfunxored[j+5]);
544 uint32_t sampleoffset = 0;
545 switch (offsetbytes) {
547 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
549 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
551 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
553 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
559 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
561 std::string filename = string(name) + extension;
565 (int64_t)(start*ratefactor),
566 (int64_t)(length*ratefactor),
569 vector<wav_t>::iterator begin = audiofiles.begin();
570 vector<wav_t>::iterator finish = audiofiles.end();
571 vector<wav_t>::iterator found;
573 if ((found = std::find(begin, finish, f)) != finish) {
577 (int64_t)(start*ratefactor),
578 (int64_t)(sampleoffset*ratefactor),
579 (int64_t)(length*ratefactor),
582 regions.push_back(r);
583 vector<track_t>::iterator ti;
584 vector<track_t>::iterator bt = tracks.begin();
585 vector<track_t>::iterator et = tracks.end();
586 track_t tr = { name, 0, 0, r };
587 if ((ti = std::find(bt, et, tr)) != et) {
588 tracknumber = (*ti).index;
590 tracknumber = tracks.size() + 1;
594 (uint16_t)tracknumber,
603 (int64_t)(start*ratefactor),
604 (int64_t)(sampleoffset*ratefactor),
605 (int64_t)(length*ratefactor),
608 regions.push_back(r);
609 vector<track_t>::iterator ti;
610 vector<track_t>::iterator bt = tracks.begin();
611 vector<track_t>::iterator et = tracks.end();
612 track_t tr = { name, 0, 0, r };
613 if ((ti = std::find(bt, et, tr)) != et) {
614 tracknumber = (*ti).index;
616 tracknumber = tracks.size() + 1;
620 (uint16_t)tracknumber,
634 PTFFormat::parseaudio5(void) {
636 int lengthofname, wavnumber;
638 // Find end of wav file list
641 if ( (ptfunxored[k ] == 0x5f) &&
642 (ptfunxored[k+1] == 0x50) &&
643 (ptfunxored[k+2] == 0x35)) {
650 if ( (ptfunxored[k ] == 0x5f) &&
651 (ptfunxored[k+1] == 0x50) &&
652 (ptfunxored[k+2] == 0x35)) {
658 // Find actual wav names
659 uint16_t numberofwavs = ptfunxored[k-23];
661 for (i = k; i < len; i++) {
662 if ( (ptfunxored[i ] == 'F') &&
663 (ptfunxored[i+1] == 'i') &&
664 (ptfunxored[i+2] == 'l') &&
665 (ptfunxored[i+3] == 'e') &&
666 (ptfunxored[i+4] == 's')) {
673 while (i < len && numberofwavs > 0) {
675 if ( (ptfunxored[i ] == 0x5a) &&
676 (ptfunxored[i+1] == 0x00) &&
677 (ptfunxored[i+2] == 0x05)) {
680 lengthofname = ptfunxored[i];
683 while (l < lengthofname) {
684 wavname[l] = ptfunxored[i+l];
689 if (foundin(wavname, ".wav")) {
690 extension = string(".wav");
691 } else if (foundin(wavname, ".aif")) {
692 extension = string(".aif");
694 extension = string("");
697 std::string wave = string(wavname);
698 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
700 if (foundin(wave, string(".grp"))) {
704 actualwavs.push_back(f);
705 audiofiles.push_back(f);
713 PTFFormat::parseaudio(void) {
716 // Find end of wav file list
719 if ( (ptfunxored[k ] == 0xff) &&
720 (ptfunxored[k+1] == 0xff) &&
721 (ptfunxored[k+2] == 0xff) &&
722 (ptfunxored[k+3] == 0xff)) {
728 // Find actual wav names
730 uint16_t numberofwavs;
732 for (i = k; i > 4; i--) {
733 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
734 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
735 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
736 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
739 while (ptfunxored[j] != '\0') {
740 wavname[l] = ptfunxored[j];
745 if (ptfunxored[i] == 'W') {
746 extension = string(".wav");
748 extension = string(".aif");
750 //uint8_t playlist = ptfunxored[j-8];
754 for (j = k; j > 4; j--) {
755 if ( (ptfunxored[j ] == 0x01) &&
756 (ptfunxored[j-1] == 0x5a)) {
759 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
760 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
761 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
762 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
763 //printf("%d wavs\n", numberofwavs);
770 std::string wave = string(wavname);
771 std::reverse(wave.begin(), wave.end());
772 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
774 if (foundin(wave, string(".grp"))) {
778 actualwavs.push_back(f);
781 if (numberofwavs <= 0)
788 PTFFormat::parserest89(void) {
791 uint8_t startbytes = 0;
792 uint8_t lengthbytes = 0;
793 uint8_t offsetbytes = 0;
794 uint8_t somethingbytes = 0;
795 uint8_t skipbytes = 0;
799 if ( (ptfunxored[k ] == 'S') &&
800 (ptfunxored[k+1] == 'n') &&
801 (ptfunxored[k+2] == 'a') &&
802 (ptfunxored[k+3] == 'p')) {
809 for (i = k; i < len-70; i++) {
810 if ( (ptfunxored[i ] == 0x5a) &&
811 (ptfunxored[i+1] == 0x0a)) {
814 if ( (ptfunxored[i ] == 0x5a) &&
815 (ptfunxored[i+1] == 0x0c)) {
817 uint8_t lengthofname = ptfunxored[i+9];
819 char name[256] = {0};
820 for (j = 0; j < lengthofname; j++) {
821 name[j] = ptfunxored[i+13+j];
825 //uint8_t disabled = ptfunxored[j];
827 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
828 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
829 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
830 somethingbytes = (ptfunxored[j+3] & 0xf);
831 skipbytes = ptfunxored[j+4];
832 findex = ptfunxored[j+5
839 /*rindex = ptfunxored[j+5
847 uint32_t sampleoffset = 0;
848 switch (offsetbytes) {
850 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
852 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
854 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
856 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
862 switch (lengthbytes) {
864 length |= (uint32_t)(ptfunxored[j+8] << 24);
866 length |= (uint32_t)(ptfunxored[j+7] << 16);
868 length |= (uint32_t)(ptfunxored[j+6] << 8);
870 length |= (uint32_t)(ptfunxored[j+5]);
876 switch (startbytes) {
878 start |= (uint32_t)(ptfunxored[j+8] << 24);
880 start |= (uint32_t)(ptfunxored[j+7] << 16);
882 start |= (uint32_t)(ptfunxored[j+6] << 8);
884 start |= (uint32_t)(ptfunxored[j+5]);
890 uint32_t something = 0;
891 switch (somethingbytes) {
893 something |= (uint32_t)(ptfunxored[j+8] << 24);
895 something |= (uint32_t)(ptfunxored[j+7] << 16);
897 something |= (uint32_t)(ptfunxored[j+6] << 8);
899 something |= (uint32_t)(ptfunxored[j+5]);
905 std::string filename = string(name) + extension;
909 (int64_t)(start*ratefactor),
910 (int64_t)(length*ratefactor),
914 //printf("something=%d\n", something);
916 vector<wav_t>::iterator begin = actualwavs.begin();
917 vector<wav_t>::iterator finish = actualwavs.end();
918 vector<wav_t>::iterator found;
919 // Add file to list only if it is an actual wav
920 if ((found = std::find(begin, finish, f)) != finish) {
921 audiofiles.push_back(f);
922 // Also add plain wav as region
926 (int64_t)(start*ratefactor),
927 (int64_t)(sampleoffset*ratefactor),
928 (int64_t)(length*ratefactor),
931 regions.push_back(r);
934 if (foundin(filename, string(".grp"))) {
940 (int64_t)(start*ratefactor),
941 (int64_t)(sampleoffset*ratefactor),
942 (int64_t)(length*ratefactor),
945 regions.push_back(r);
952 if ( (ptfunxored[k ] == 0x5a) &&
953 (ptfunxored[k+1] == 0x03)) {
959 if ( (ptfunxored[k ] == 0x5a) &&
960 (ptfunxored[k+1] == 0x02)) {
969 uint32_t tracknumber = 0;
970 uint32_t regionspertrack = 0;
971 for (;k < len; k++) {
972 if ( (ptfunxored[k ] == 0x5a) &&
973 (ptfunxored[k+1] == 0x04)) {
976 if ( (ptfunxored[k ] == 0x5a) &&
977 (ptfunxored[k+1] == 0x02)) {
979 uint8_t lengthofname = 0;
980 lengthofname = ptfunxored[k+9];
981 if (lengthofname == 0x5a) {
986 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
988 //printf("regions/track=%d\n", regionspertrack);
989 char name[256] = {0};
990 for (j = 0; j < lengthofname; j++) {
991 name[j] = ptfunxored[j+k+13];
994 tr.name = string(name);
995 tr.index = tracknumber++;
997 for (j = k; regionspertrack > 0 && j < len; j++) {
998 for (l = j; l < len; l++) {
999 if ( (ptfunxored[l ] == 0x5a) &&
1000 (ptfunxored[l+1] == 0x07)) {
1007 if (regionspertrack == 0) {
1008 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1012 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1013 vector<region_t>::iterator begin = regions.begin();
1014 vector<region_t>::iterator finish = regions.end();
1015 vector<region_t>::iterator found;
1016 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1021 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1022 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1023 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1024 offset |= (uint32_t)(ptfunxored[i]);
1025 tr.reg.startpos = (int64_t)(offset*ratefactor);
1026 if (tr.reg.length > 0) {
1027 tracks.push_back(tr);
1037 PTFFormat::parserest10(void) {
1040 uint8_t startbytes = 0;
1041 uint8_t lengthbytes = 0;
1042 uint8_t offsetbytes = 0;
1043 uint8_t somethingbytes = 0;
1044 uint8_t skipbytes = 0;
1048 if ( (ptfunxored[k ] == 'S') &&
1049 (ptfunxored[k+1] == 'n') &&
1050 (ptfunxored[k+2] == 'a') &&
1051 (ptfunxored[k+3] == 'p')) {
1056 for (i = k; i < len-70; i++) {
1057 if ( (ptfunxored[i ] == 0x5a) &&
1058 (ptfunxored[i+1] == 0x02)) {
1064 for (i = k; i < len-70; i++) {
1065 if ( (ptfunxored[i ] == 0x5a) &&
1066 (ptfunxored[i+1] == 0x02)) {
1072 uint16_t rindex = 0;
1073 uint32_t findex = 0;
1074 for (i = k; i < len-70; i++) {
1075 if ( (ptfunxored[i ] == 0x5a) &&
1076 (ptfunxored[i+1] == 0x08)) {
1079 if ( (ptfunxored[i ] == 0x5a) &&
1080 (ptfunxored[i+1] == 0x01)) {
1082 uint8_t lengthofname = ptfunxored[i+9];
1083 if (ptfunxored[i+13] == 0x5a) {
1086 char name[256] = {0};
1087 for (j = 0; j < lengthofname; j++) {
1088 name[j] = ptfunxored[i+13+j];
1092 //uint8_t disabled = ptfunxored[j];
1093 //printf("%s\n", name);
1095 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1096 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1097 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1098 somethingbytes = (ptfunxored[j+3] & 0xf);
1099 skipbytes = ptfunxored[j+4];
1100 findex = ptfunxored[j+5
1107 /*rindex = ptfunxored[j+5
1115 uint32_t sampleoffset = 0;
1116 switch (offsetbytes) {
1118 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1120 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1122 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1124 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1129 uint32_t length = 0;
1130 switch (lengthbytes) {
1132 length |= (uint32_t)(ptfunxored[j+8] << 24);
1134 length |= (uint32_t)(ptfunxored[j+7] << 16);
1136 length |= (uint32_t)(ptfunxored[j+6] << 8);
1138 length |= (uint32_t)(ptfunxored[j+5]);
1144 switch (startbytes) {
1146 start |= (uint32_t)(ptfunxored[j+8] << 24);
1148 start |= (uint32_t)(ptfunxored[j+7] << 16);
1150 start |= (uint32_t)(ptfunxored[j+6] << 8);
1152 start |= (uint32_t)(ptfunxored[j+5]);
1158 uint32_t something = 0;
1159 switch (somethingbytes) {
1161 something |= (uint32_t)(ptfunxored[j+8] << 24);
1163 something |= (uint32_t)(ptfunxored[j+7] << 16);
1165 something |= (uint32_t)(ptfunxored[j+6] << 8);
1167 something |= (uint32_t)(ptfunxored[j+5]);
1173 std::string filename = string(name) + extension;
1177 (int64_t)(start*ratefactor),
1178 (int64_t)(length*ratefactor),
1181 if (strlen(name) == 0) {
1188 //printf("something=%d\n", something);
1190 vector<wav_t>::iterator begin = actualwavs.begin();
1191 vector<wav_t>::iterator finish = actualwavs.end();
1192 vector<wav_t>::iterator found;
1193 // Add file to list only if it is an actual wav
1194 if ((found = std::find(begin, finish, f)) != finish) {
1195 audiofiles.push_back(f);
1196 // Also add plain wav as region
1200 (int64_t)(start*ratefactor),
1201 (int64_t)(sampleoffset*ratefactor),
1202 (int64_t)(length*ratefactor),
1205 regions.push_back(r);
1208 if (foundin(filename, string(".grp"))) {
1214 (int64_t)(start*ratefactor),
1215 (int64_t)(sampleoffset*ratefactor),
1216 (int64_t)(length*ratefactor),
1219 regions.push_back(r);
1222 //printf("%s\n", name);
1227 uint32_t tracknumber = 0;
1228 uint32_t regionspertrack = 0;
1229 for (;k < len; k++) {
1230 if ( (ptfunxored[k ] == 0x5a) &&
1231 (ptfunxored[k+1] == 0x08)) {
1236 for (;k < len; k++) {
1237 if ( (ptfunxored[k ] == 0x5a) &&
1238 (ptfunxored[k+1] == 0x04)) {
1241 if ( (ptfunxored[k ] == 0x5a) &&
1242 (ptfunxored[k+1] == 0x02)) {
1244 uint8_t lengthofname = 0;
1245 lengthofname = ptfunxored[k+9];
1246 if (lengthofname == 0x5a) {
1251 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1253 //printf("regions/track=%d\n", regionspertrack);
1254 char name[256] = {0};
1255 for (j = 0; j < lengthofname; j++) {
1256 name[j] = ptfunxored[j+k+13];
1259 tr.name = string(name);
1260 tr.index = tracknumber++;
1262 for (j = k; regionspertrack > 0 && j < len; j++) {
1263 for (l = j; l < len; l++) {
1264 if ( (ptfunxored[l ] == 0x5a) &&
1265 (ptfunxored[l+1] == 0x08)) {
1272 if (regionspertrack == 0) {
1273 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1277 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1278 vector<region_t>::iterator begin = regions.begin();
1279 vector<region_t>::iterator finish = regions.end();
1280 vector<region_t>::iterator found;
1281 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1286 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1287 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1288 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1289 offset |= (uint32_t)(ptfunxored[i]);
1290 tr.reg.startpos = (int64_t)(offset*ratefactor);
1291 if (tr.reg.length > 0) {
1292 tracks.push_back(tr);