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, somethingbytes;
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[j+4
522 switch (startbytes) {
524 start |= (uint32_t)(ptfunxored[j+8] << 24);
526 start |= (uint32_t)(ptfunxored[j+7] << 16);
528 start |= (uint32_t)(ptfunxored[j+6] << 8);
530 start |= (uint32_t)(ptfunxored[j+5]);
536 switch (lengthbytes) {
538 length |= (uint32_t)(ptfunxored[j+8] << 24);
540 length |= (uint32_t)(ptfunxored[j+7] << 16);
542 length |= (uint32_t)(ptfunxored[j+6] << 8);
544 length |= (uint32_t)(ptfunxored[j+5]);
549 uint32_t sampleoffset = 0;
550 switch (offsetbytes) {
552 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
554 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
556 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
558 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
564 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
566 std::string filename = string(name) + extension;
570 (int64_t)(start*ratefactor),
571 (int64_t)(length*ratefactor),
574 vector<wav_t>::iterator begin = audiofiles.begin();
575 vector<wav_t>::iterator finish = audiofiles.end();
576 vector<wav_t>::iterator found;
578 if ((found = std::find(begin, finish, f)) != finish) {
582 (int64_t)(start*ratefactor),
583 (int64_t)(sampleoffset*ratefactor),
584 (int64_t)(length*ratefactor),
587 regions.push_back(r);
588 vector<track_t>::iterator ti;
589 vector<track_t>::iterator bt = tracks.begin();
590 vector<track_t>::iterator et = tracks.end();
591 track_t tr = { name, 0, 0, r };
592 if ((ti = std::find(bt, et, tr)) != et) {
593 tracknumber = (*ti).index;
595 tracknumber = tracks.size() + 1;
599 (uint16_t)tracknumber,
605 audiofiles.push_back(f);
609 (int64_t)(start*ratefactor),
610 (int64_t)(sampleoffset*ratefactor),
611 (int64_t)(length*ratefactor),
614 regions.push_back(r);
615 vector<track_t>::iterator ti;
616 vector<track_t>::iterator bt = tracks.begin();
617 vector<track_t>::iterator et = tracks.end();
618 track_t tr = { name, 0, 0, r };
619 if ((ti = std::find(bt, et, tr)) != et) {
620 tracknumber = (*ti).index;
622 tracknumber = tracks.size() + 1;
626 (uint16_t)tracknumber,
640 PTFFormat::parseaudio5(void) {
643 // Find end of wav file list
646 if ( (ptfunxored[k ] == 0x5a) &&
647 (ptfunxored[k+1] == 0x00) &&
648 (ptfunxored[k+2] == 0x05)) {
654 // Find actual wav names
656 uint16_t numberofwavs;
658 for (i = k; i > 4; i--) {
659 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'F')) &&
660 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'F')) &&
661 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'I')) &&
662 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'A'))) {
665 while (ptfunxored[j] != '\0') {
666 wavname[l] = ptfunxored[j];
671 if (ptfunxored[i] == 'W') {
672 extension = string(".wav");
674 extension = string(".aif");
676 //uint8_t playlist = ptfunxored[j-8];
680 for (j = k; j > 4; j--) {
681 if ( (ptfunxored[j ] == 0x01) &&
682 (ptfunxored[j-1] == 0x00) &&
683 (ptfunxored[j-2] == 0x5a)) {
686 numberofwavs |= (uint32_t)(ptfunxored[j-6] << 24);
687 numberofwavs |= (uint32_t)(ptfunxored[j-5] << 16);
688 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
689 numberofwavs |= (uint32_t)(ptfunxored[j-3]);
690 //printf("%d wavs\n", numberofwavs);
697 std::string wave = string(wavname);
698 std::reverse(wave.begin(), wave.end());
699 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
701 if (foundin(wave, string(".grp"))) {
705 actualwavs.push_back(f);
708 if (numberofwavs <= 0)
715 PTFFormat::parseaudio(void) {
718 // Find end of wav file list
721 if ( (ptfunxored[k ] == 0xff) &&
722 (ptfunxored[k+1] == 0xff) &&
723 (ptfunxored[k+2] == 0xff) &&
724 (ptfunxored[k+3] == 0xff)) {
730 // Find actual wav names
732 uint16_t numberofwavs;
734 for (i = k; i > 4; i--) {
735 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
736 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
737 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
738 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
741 while (ptfunxored[j] != '\0') {
742 wavname[l] = ptfunxored[j];
747 if (ptfunxored[i] == 'W') {
748 extension = string(".wav");
750 extension = string(".aif");
752 //uint8_t playlist = ptfunxored[j-8];
756 for (j = k; j > 4; j--) {
757 if ( (ptfunxored[j ] == 0x01) &&
758 (ptfunxored[j-1] == 0x5a)) {
761 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
762 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
763 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
764 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
765 //printf("%d wavs\n", numberofwavs);
772 std::string wave = string(wavname);
773 std::reverse(wave.begin(), wave.end());
774 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
776 if (foundin(wave, string(".grp"))) {
780 actualwavs.push_back(f);
783 if (numberofwavs <= 0)
790 PTFFormat::parserest89(void) {
793 uint8_t startbytes = 0;
794 uint8_t lengthbytes = 0;
795 uint8_t offsetbytes = 0;
796 uint8_t somethingbytes = 0;
797 uint8_t skipbytes = 0;
801 if ( (ptfunxored[k ] == 'S') &&
802 (ptfunxored[k+1] == 'n') &&
803 (ptfunxored[k+2] == 'a') &&
804 (ptfunxored[k+3] == 'p')) {
811 for (i = k; i < len-70; i++) {
812 if ( (ptfunxored[i ] == 0x5a) &&
813 (ptfunxored[i+1] == 0x0a)) {
816 if ( (ptfunxored[i ] == 0x5a) &&
817 (ptfunxored[i+1] == 0x0c)) {
819 uint8_t lengthofname = ptfunxored[i+9];
821 char name[256] = {0};
822 for (j = 0; j < lengthofname; j++) {
823 name[j] = ptfunxored[i+13+j];
827 //uint8_t disabled = ptfunxored[j];
829 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
830 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
831 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
832 somethingbytes = (ptfunxored[j+3] & 0xf);
833 skipbytes = ptfunxored[j+4];
834 findex = ptfunxored[j+5
841 /*rindex = ptfunxored[j+5
849 uint32_t sampleoffset = 0;
850 switch (offsetbytes) {
852 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
854 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
856 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
858 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
864 switch (lengthbytes) {
866 length |= (uint32_t)(ptfunxored[j+8] << 24);
868 length |= (uint32_t)(ptfunxored[j+7] << 16);
870 length |= (uint32_t)(ptfunxored[j+6] << 8);
872 length |= (uint32_t)(ptfunxored[j+5]);
878 switch (startbytes) {
880 start |= (uint32_t)(ptfunxored[j+8] << 24);
882 start |= (uint32_t)(ptfunxored[j+7] << 16);
884 start |= (uint32_t)(ptfunxored[j+6] << 8);
886 start |= (uint32_t)(ptfunxored[j+5]);
892 uint32_t something = 0;
893 switch (somethingbytes) {
895 something |= (uint32_t)(ptfunxored[j+8] << 24);
897 something |= (uint32_t)(ptfunxored[j+7] << 16);
899 something |= (uint32_t)(ptfunxored[j+6] << 8);
901 something |= (uint32_t)(ptfunxored[j+5]);
907 std::string filename = string(name) + extension;
911 (int64_t)(start*ratefactor),
912 (int64_t)(length*ratefactor),
916 //printf("something=%d\n", something);
918 vector<wav_t>::iterator begin = actualwavs.begin();
919 vector<wav_t>::iterator finish = actualwavs.end();
920 vector<wav_t>::iterator found;
921 // Add file to list only if it is an actual wav
922 if ((found = std::find(begin, finish, f)) != finish) {
923 audiofiles.push_back(f);
924 // Also add plain wav as region
928 (int64_t)(start*ratefactor),
929 (int64_t)(sampleoffset*ratefactor),
930 (int64_t)(length*ratefactor),
933 regions.push_back(r);
936 if (foundin(filename, string(".grp"))) {
942 (int64_t)(start*ratefactor),
943 (int64_t)(sampleoffset*ratefactor),
944 (int64_t)(length*ratefactor),
947 regions.push_back(r);
954 if ( (ptfunxored[k ] == 0x5a) &&
955 (ptfunxored[k+1] == 0x03)) {
961 if ( (ptfunxored[k ] == 0x5a) &&
962 (ptfunxored[k+1] == 0x02)) {
971 uint32_t tracknumber = 0;
972 uint32_t regionspertrack = 0;
973 for (;k < len; k++) {
974 if ( (ptfunxored[k ] == 0x5a) &&
975 (ptfunxored[k+1] == 0x04)) {
978 if ( (ptfunxored[k ] == 0x5a) &&
979 (ptfunxored[k+1] == 0x02)) {
981 uint8_t lengthofname = 0;
982 lengthofname = ptfunxored[k+9];
983 if (lengthofname == 0x5a) {
988 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
990 //printf("regions/track=%d\n", regionspertrack);
991 char name[256] = {0};
992 for (j = 0; j < lengthofname; j++) {
993 name[j] = ptfunxored[j+k+13];
996 tr.name = string(name);
997 tr.index = tracknumber++;
999 for (j = k; regionspertrack > 0 && j < len; j++) {
1000 for (l = j; l < len; l++) {
1001 if ( (ptfunxored[l ] == 0x5a) &&
1002 (ptfunxored[l+1] == 0x07)) {
1009 if (regionspertrack == 0) {
1010 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1014 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1015 vector<region_t>::iterator begin = regions.begin();
1016 vector<region_t>::iterator finish = regions.end();
1017 vector<region_t>::iterator found;
1018 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1023 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1024 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1025 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1026 offset |= (uint32_t)(ptfunxored[i]);
1027 tr.reg.startpos = (int64_t)(offset*ratefactor);
1028 if (tr.reg.length > 0) {
1029 tracks.push_back(tr);
1039 PTFFormat::parserest10(void) {
1042 uint8_t startbytes = 0;
1043 uint8_t lengthbytes = 0;
1044 uint8_t offsetbytes = 0;
1045 uint8_t somethingbytes = 0;
1046 uint8_t skipbytes = 0;
1050 if ( (ptfunxored[k ] == 'S') &&
1051 (ptfunxored[k+1] == 'n') &&
1052 (ptfunxored[k+2] == 'a') &&
1053 (ptfunxored[k+3] == 'p')) {
1058 for (i = k; i < len-70; i++) {
1059 if ( (ptfunxored[i ] == 0x5a) &&
1060 (ptfunxored[i+1] == 0x02)) {
1066 for (i = k; i < len-70; i++) {
1067 if ( (ptfunxored[i ] == 0x5a) &&
1068 (ptfunxored[i+1] == 0x02)) {
1074 uint16_t rindex = 0;
1075 uint32_t findex = 0;
1076 for (i = k; i < len-70; i++) {
1077 if ( (ptfunxored[i ] == 0x5a) &&
1078 (ptfunxored[i+1] == 0x08)) {
1081 if ( (ptfunxored[i ] == 0x5a) &&
1082 (ptfunxored[i+1] == 0x01)) {
1084 uint8_t lengthofname = ptfunxored[i+9];
1085 if (ptfunxored[i+13] == 0x5a) {
1088 char name[256] = {0};
1089 for (j = 0; j < lengthofname; j++) {
1090 name[j] = ptfunxored[i+13+j];
1094 //uint8_t disabled = ptfunxored[j];
1095 //printf("%s\n", name);
1097 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1098 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1099 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1100 somethingbytes = (ptfunxored[j+3] & 0xf);
1101 skipbytes = ptfunxored[j+4];
1102 findex = ptfunxored[j+5
1109 /*rindex = ptfunxored[j+5
1117 uint32_t sampleoffset = 0;
1118 switch (offsetbytes) {
1120 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1122 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1124 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1126 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1131 uint32_t length = 0;
1132 switch (lengthbytes) {
1134 length |= (uint32_t)(ptfunxored[j+8] << 24);
1136 length |= (uint32_t)(ptfunxored[j+7] << 16);
1138 length |= (uint32_t)(ptfunxored[j+6] << 8);
1140 length |= (uint32_t)(ptfunxored[j+5]);
1146 switch (startbytes) {
1148 start |= (uint32_t)(ptfunxored[j+8] << 24);
1150 start |= (uint32_t)(ptfunxored[j+7] << 16);
1152 start |= (uint32_t)(ptfunxored[j+6] << 8);
1154 start |= (uint32_t)(ptfunxored[j+5]);
1160 uint32_t something = 0;
1161 switch (somethingbytes) {
1163 something |= (uint32_t)(ptfunxored[j+8] << 24);
1165 something |= (uint32_t)(ptfunxored[j+7] << 16);
1167 something |= (uint32_t)(ptfunxored[j+6] << 8);
1169 something |= (uint32_t)(ptfunxored[j+5]);
1175 std::string filename = string(name) + extension;
1179 (int64_t)(start*ratefactor),
1180 (int64_t)(length*ratefactor),
1183 if (strlen(name) == 0) {
1190 //printf("something=%d\n", something);
1192 vector<wav_t>::iterator begin = actualwavs.begin();
1193 vector<wav_t>::iterator finish = actualwavs.end();
1194 vector<wav_t>::iterator found;
1195 // Add file to list only if it is an actual wav
1196 if ((found = std::find(begin, finish, f)) != finish) {
1197 audiofiles.push_back(f);
1198 // Also add plain wav as region
1202 (int64_t)(start*ratefactor),
1203 (int64_t)(sampleoffset*ratefactor),
1204 (int64_t)(length*ratefactor),
1207 regions.push_back(r);
1210 if (foundin(filename, string(".grp"))) {
1216 (int64_t)(start*ratefactor),
1217 (int64_t)(sampleoffset*ratefactor),
1218 (int64_t)(length*ratefactor),
1221 regions.push_back(r);
1224 //printf("%s\n", name);
1229 uint32_t tracknumber = 0;
1230 uint32_t regionspertrack = 0;
1231 for (;k < len; k++) {
1232 if ( (ptfunxored[k ] == 0x5a) &&
1233 (ptfunxored[k+1] == 0x08)) {
1238 for (;k < len; k++) {
1239 if ( (ptfunxored[k ] == 0x5a) &&
1240 (ptfunxored[k+1] == 0x04)) {
1243 if ( (ptfunxored[k ] == 0x5a) &&
1244 (ptfunxored[k+1] == 0x02)) {
1246 uint8_t lengthofname = 0;
1247 lengthofname = ptfunxored[k+9];
1248 if (lengthofname == 0x5a) {
1253 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1255 //printf("regions/track=%d\n", regionspertrack);
1256 char name[256] = {0};
1257 for (j = 0; j < lengthofname; j++) {
1258 name[j] = ptfunxored[j+k+13];
1261 tr.name = string(name);
1262 tr.index = tracknumber++;
1264 for (j = k; regionspertrack > 0 && j < len; j++) {
1265 for (l = j; l < len; l++) {
1266 if ( (ptfunxored[l ] == 0x5a) &&
1267 (ptfunxored[l+1] == 0x08)) {
1274 if (regionspertrack == 0) {
1275 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1279 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1280 vector<region_t>::iterator begin = regions.begin();
1281 vector<region_t>::iterator finish = regions.end();
1282 vector<region_t>::iterator found;
1283 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1288 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1289 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1290 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1291 offset |= (uint32_t)(ptfunxored[i]);
1292 tr.reg.startpos = (int64_t)(offset*ratefactor);
1293 if (tr.reg.length > 0) {
1294 tracks.push_back(tr);