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];
118 if (! (fp = fopen(path.c_str(), "rb"))) {
122 fseek(fp, 0, SEEK_END);
128 fseek(fp, 0x40, SEEK_SET);
129 fread(&c0, 1, 1, fp);
130 fread(&c1, 1, 1, fp);
132 // For version <= 7 support:
136 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
137 /* Silently fail -- out of memory*/
148 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
150 for (i = 2; i < 256; i++) {
154 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
155 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
163 for (i = 2; i < 256; i++) {
167 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
170 for (i = 0; i < 64; i++) {
173 for (i = 128; i < 192; i++) {
181 for (i = 2; i < 256; i++) {
185 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
189 key = gen_secret(c1);
191 for (i = 0; i < 64; i++) {
192 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
194 for (i = 128; i < 192; i++) {
195 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
196 xxor[i] ^= (inv * 0x40);
199 for (i = 192; i < 256; i++) {
205 //Should not happen, failed c[0] c[1]
212 fseek(fp, 0, SEEK_SET);
213 while (fread(&ct, 1, 1, fp) != 0) {
214 ptfunxored[i++] = ct;
218 /* version detection */
220 v = ptfunxored[voff];
223 } else if (v == 0x03) {
228 v = ptfunxored[voff];
229 if (v == 10 || v == 11 || v == 12) {
234 if (version == 0 || version == 5 || version == 7) {
235 /* Haven't detected version yet so decipher */
237 for (i = 0; i < len; i++) {
241 ptfunxored[i] ^= xxor[j];
245 /* version detection */
247 v = ptfunxored[voff];
250 } else if (v == 0x03) {
255 v = ptfunxored[voff];
256 if (v == 5 || v == 7 || v == 8 || v == 9) {
261 if (version < 5 || version > 12)
263 targetrate = targetsr;
271 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
273 uint32_t counts[256] = {0};
278 for (i = start; i < stop; i++) {
279 counts[ptfunxored[i]]++;
282 for (i = 0; i < 256; i++) {
283 if (counts[i] > max) {
292 PTFFormat::unxor10(void)
295 uint8_t x = mostfrequent(0x1000, 0x2000);
296 uint8_t dx = 0x100-x;
298 for (j = 0x1000; j < len; j++) {
299 if(j % 0x1000 == 0xfff) {
307 PTFFormat::parse(void) {
311 if (sessionrate < 44100 || sessionrate > 192000)
315 } else if (version == 7) {
318 if (sessionrate < 44100 || sessionrate > 192000)
322 } else if (version == 8) {
325 if (sessionrate < 44100 || sessionrate > 192000)
329 } else if (version == 9) {
332 if (sessionrate < 44100 || sessionrate > 192000)
336 } else if (version == 10 || version == 11 || version == 12) {
339 if (sessionrate < 44100 || sessionrate > 192000)
351 PTFFormat::setrates(void) {
353 if (sessionrate != 0) {
354 ratefactor = (float)targetrate / sessionrate;
359 PTFFormat::parse5header(void) {
362 // Find session sample rate
365 if ( (ptfunxored[k ] == 0x5a) &&
366 (ptfunxored[k+1] == 0x00) &&
367 (ptfunxored[k+2] == 0x02)) {
374 sessionrate |= ptfunxored[k+12] << 16;
375 sessionrate |= ptfunxored[k+13] << 8;
376 sessionrate |= ptfunxored[k+14];
380 PTFFormat::parse7header(void) {
383 // Find session sample rate
386 if ( (ptfunxored[k ] == 0x5a) &&
387 (ptfunxored[k+1] == 0x00) &&
388 (ptfunxored[k+2] == 0x05)) {
395 sessionrate |= ptfunxored[k+12] << 16;
396 sessionrate |= ptfunxored[k+13] << 8;
397 sessionrate |= ptfunxored[k+14];
401 PTFFormat::parse8header(void) {
404 // Find session sample rate
407 if ( (ptfunxored[k ] == 0x5a) &&
408 (ptfunxored[k+1] == 0x05)) {
415 sessionrate |= ptfunxored[k+11];
416 sessionrate |= ptfunxored[k+12] << 8;
417 sessionrate |= ptfunxored[k+13] << 16;
421 PTFFormat::parse9header(void) {
424 // Find session sample rate
427 if ( (ptfunxored[k ] == 0x5a) &&
428 (ptfunxored[k+1] == 0x06)) {
435 sessionrate |= ptfunxored[k+11];
436 sessionrate |= ptfunxored[k+12] << 8;
437 sessionrate |= ptfunxored[k+13] << 16;
441 PTFFormat::parse10header(void) {
444 // Find session sample rate
447 if ( (ptfunxored[k ] == 0x5a) &&
448 (ptfunxored[k+1] == 0x09)) {
455 sessionrate |= ptfunxored[k+11];
456 sessionrate |= ptfunxored[k+12] << 8;
457 sessionrate |= ptfunxored[k+13] << 16;
461 PTFFormat::parserest5(void) {
463 uint64_t regionspertrack, lengthofname;
464 uint64_t startbytes, lengthbytes, offsetbytes;
465 uint16_t tracknumber = 0;
470 for (i = 0; i < 5; i++) {
472 if ( (ptfunxored[k ] == 0x5a) &&
473 (ptfunxored[k+1] == 0x00) &&
474 (ptfunxored[k+2] == 0x03)) {
483 for (i = 0; i < 2; i++) {
485 if ( (ptfunxored[k ] == 0x5a) &&
486 (ptfunxored[k+1] == 0x00) &&
487 (ptfunxored[k+2] == 0x01)) {
499 if ( (ptfunxored[k ] == 0xff) &&
500 (ptfunxored[k+1] == 0xff)) {
504 if ( (ptfunxored[k ] == 0x5a) &&
505 (ptfunxored[k+1] == 0x00) &&
506 (ptfunxored[k+2] == 0x01)) {
512 lengthofname = ptfunxored[k+12];
513 if (ptfunxored[k+13] == 0x5a) {
517 char name[256] = {0};
518 for (j = 0; j < lengthofname; j++) {
519 name[j] = ptfunxored[k+13+j];
522 regionspertrack = ptfunxored[k+13+j+3];
523 for (i = 0; i < regionspertrack; i++) {
525 if ( (ptfunxored[k ] == 0x5a) &&
526 (ptfunxored[k+1] == 0x00) &&
527 (ptfunxored[k+2] == 0x03)) {
533 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
534 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
535 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
536 //somethingbytes = (ptfunxored[j+1] & 0xf);
537 findex = ptfunxored[k+14];
540 switch (startbytes) {
542 start |= (uint32_t)(ptfunxored[j+8] << 24);
544 start |= (uint32_t)(ptfunxored[j+7] << 16);
546 start |= (uint32_t)(ptfunxored[j+6] << 8);
548 start |= (uint32_t)(ptfunxored[j+5]);
554 switch (lengthbytes) {
556 length |= (uint32_t)(ptfunxored[j+8] << 24);
558 length |= (uint32_t)(ptfunxored[j+7] << 16);
560 length |= (uint32_t)(ptfunxored[j+6] << 8);
562 length |= (uint32_t)(ptfunxored[j+5]);
567 uint32_t sampleoffset = 0;
568 switch (offsetbytes) {
570 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
572 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
574 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
576 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
582 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
584 std::string filename = string(name) + extension;
588 (int64_t)(start*ratefactor),
589 (int64_t)(length*ratefactor),
592 vector<wav_t>::iterator begin = audiofiles.begin();
593 vector<wav_t>::iterator finish = audiofiles.end();
594 vector<wav_t>::iterator found;
596 if ((found = std::find(begin, finish, f)) != finish) {
600 (int64_t)(start*ratefactor),
601 (int64_t)(sampleoffset*ratefactor),
602 (int64_t)(length*ratefactor),
605 regions.push_back(r);
606 vector<track_t>::iterator ti;
607 vector<track_t>::iterator bt = tracks.begin();
608 vector<track_t>::iterator et = tracks.end();
609 track_t tr = { name, 0, 0, r };
610 if ((ti = std::find(bt, et, tr)) != et) {
611 tracknumber = (*ti).index;
613 tracknumber = tracks.size() + 1;
617 (uint16_t)tracknumber,
626 (int64_t)(start*ratefactor),
627 (int64_t)(sampleoffset*ratefactor),
628 (int64_t)(length*ratefactor),
631 regions.push_back(r);
632 vector<track_t>::iterator ti;
633 vector<track_t>::iterator bt = tracks.begin();
634 vector<track_t>::iterator et = tracks.end();
635 track_t tr = { name, 0, 0, r };
636 if ((ti = std::find(bt, et, tr)) != et) {
637 tracknumber = (*ti).index;
639 tracknumber = tracks.size() + 1;
643 (uint16_t)tracknumber,
657 PTFFormat::resort(std::vector<wav_t>& ws) {
659 std::sort(ws.begin(), ws.end());
660 for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
667 PTFFormat::parseaudio5(void) {
669 uint64_t lengthofname, wavnumber;
671 // Find end of wav file list
674 if ( (ptfunxored[k ] == 0x5f) &&
675 (ptfunxored[k+1] == 0x50) &&
676 (ptfunxored[k+2] == 0x35)) {
683 if ( (ptfunxored[k ] == 0x5f) &&
684 (ptfunxored[k+1] == 0x50) &&
685 (ptfunxored[k+2] == 0x35)) {
691 // Find actual wav names
692 uint16_t numberofwavs = ptfunxored[k-23];
694 for (i = k; i < len; i++) {
695 if ( (ptfunxored[i ] == 'F') &&
696 (ptfunxored[i+1] == 'i') &&
697 (ptfunxored[i+2] == 'l') &&
698 (ptfunxored[i+3] == 'e') &&
699 (ptfunxored[i+4] == 's')) {
707 while (i < len && numberofwavs > 0) {
709 if ( (ptfunxored[i ] == 0x5a) &&
710 (ptfunxored[i+1] == 0x00) &&
711 (ptfunxored[i+2] == 0x05)) {
714 lengthofname = ptfunxored[i];
717 while (l < lengthofname) {
718 wavname[l] = ptfunxored[i+l];
722 ext[0] = ptfunxored[i++];
723 ext[1] = ptfunxored[i++];
724 ext[2] = ptfunxored[i++];
725 ext[3] = ptfunxored[i++];
729 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
730 extension = string("");
731 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
732 extension = string(".wav");
733 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
734 extension = string(".aif");
736 extension = string("");
739 std::string wave = string(wavname);
740 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
742 if (foundin(wave, string(".grp"))) {
746 actualwavs.push_back(f);
747 audiofiles.push_back(f);
757 PTFFormat::parseaudio(void) {
760 // Find end of wav file list
763 if ( (ptfunxored[k ] == 0xff) &&
764 (ptfunxored[k+1] == 0xff) &&
765 (ptfunxored[k+2] == 0xff) &&
766 (ptfunxored[k+3] == 0xff)) {
772 // Find actual wav names
774 uint16_t numberofwavs;
776 for (i = k; i > 4; i--) {
777 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
778 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
779 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
780 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
783 while (ptfunxored[j] != '\0') {
784 wavname[l] = ptfunxored[j];
789 if (ptfunxored[i] == 'W') {
790 extension = string(".wav");
792 extension = string(".aif");
794 //uint8_t playlist = ptfunxored[j-8];
798 for (j = k; j > 4; j--) {
799 if ( (ptfunxored[j ] == 0x01) &&
800 (ptfunxored[j-1] == 0x5a)) {
803 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
804 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
805 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
806 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
807 //printf("%d wavs\n", numberofwavs);
814 std::string wave = string(wavname);
815 std::reverse(wave.begin(), wave.end());
816 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
818 if (foundin(wave, string(".grp"))) {
822 actualwavs.push_back(f);
825 if (numberofwavs <= 0)
832 PTFFormat::parserest89(void) {
835 uint8_t startbytes = 0;
836 uint8_t lengthbytes = 0;
837 uint8_t offsetbytes = 0;
838 uint8_t somethingbytes = 0;
839 uint8_t skipbytes = 0;
843 if ( (ptfunxored[k ] == 'S') &&
844 (ptfunxored[k+1] == 'n') &&
845 (ptfunxored[k+2] == 'a') &&
846 (ptfunxored[k+3] == 'p')) {
853 for (i = k; i < len-70; i++) {
854 if ( (ptfunxored[i ] == 0x5a) &&
855 (ptfunxored[i+1] == 0x0a)) {
858 if ( (ptfunxored[i ] == 0x5a) &&
859 (ptfunxored[i+1] == 0x0c)) {
861 uint8_t lengthofname = ptfunxored[i+9];
863 char name[256] = {0};
864 for (j = 0; j < lengthofname; j++) {
865 name[j] = ptfunxored[i+13+j];
869 //uint8_t disabled = ptfunxored[j];
871 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
872 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
873 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
874 somethingbytes = (ptfunxored[j+3] & 0xf);
875 skipbytes = ptfunxored[j+4];
876 findex = ptfunxored[j+5
883 /*rindex = ptfunxored[j+5
891 uint32_t sampleoffset = 0;
892 switch (offsetbytes) {
894 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
896 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
898 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
900 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
906 switch (lengthbytes) {
908 length |= (uint32_t)(ptfunxored[j+8] << 24);
910 length |= (uint32_t)(ptfunxored[j+7] << 16);
912 length |= (uint32_t)(ptfunxored[j+6] << 8);
914 length |= (uint32_t)(ptfunxored[j+5]);
920 switch (startbytes) {
922 start |= (uint32_t)(ptfunxored[j+8] << 24);
924 start |= (uint32_t)(ptfunxored[j+7] << 16);
926 start |= (uint32_t)(ptfunxored[j+6] << 8);
928 start |= (uint32_t)(ptfunxored[j+5]);
934 uint32_t something = 0;
935 switch (somethingbytes) {
937 something |= (uint32_t)(ptfunxored[j+8] << 24);
939 something |= (uint32_t)(ptfunxored[j+7] << 16);
941 something |= (uint32_t)(ptfunxored[j+6] << 8);
943 something |= (uint32_t)(ptfunxored[j+5]);
949 std::string filename = string(name) + extension;
953 (int64_t)(start*ratefactor),
954 (int64_t)(length*ratefactor),
958 //printf("something=%d\n", something);
960 vector<wav_t>::iterator begin = actualwavs.begin();
961 vector<wav_t>::iterator finish = actualwavs.end();
962 vector<wav_t>::iterator found;
963 // Add file to list only if it is an actual wav
964 if ((found = std::find(begin, finish, f)) != finish) {
965 audiofiles.push_back(f);
966 // Also add plain wav as region
970 (int64_t)(start*ratefactor),
971 (int64_t)(sampleoffset*ratefactor),
972 (int64_t)(length*ratefactor),
975 regions.push_back(r);
978 if (foundin(filename, string(".grp"))) {
984 (int64_t)(start*ratefactor),
985 (int64_t)(sampleoffset*ratefactor),
986 (int64_t)(length*ratefactor),
989 regions.push_back(r);
996 if ( (ptfunxored[k ] == 0x5a) &&
997 (ptfunxored[k+1] == 0x03)) {
1003 if ( (ptfunxored[k ] == 0x5a) &&
1004 (ptfunxored[k+1] == 0x02)) {
1013 uint32_t tracknumber = 0;
1014 uint32_t regionspertrack = 0;
1015 for (;k < len; k++) {
1016 if ( (ptfunxored[k ] == 0x5a) &&
1017 (ptfunxored[k+1] == 0x04)) {
1020 if ( (ptfunxored[k ] == 0x5a) &&
1021 (ptfunxored[k+1] == 0x02)) {
1023 uint8_t lengthofname = 0;
1024 lengthofname = ptfunxored[k+9];
1025 if (lengthofname == 0x5a) {
1030 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1032 //printf("regions/track=%d\n", regionspertrack);
1033 char name[256] = {0};
1034 for (j = 0; j < lengthofname; j++) {
1035 name[j] = ptfunxored[j+k+13];
1038 tr.name = string(name);
1039 tr.index = tracknumber++;
1041 for (j = k; regionspertrack > 0 && j < len; j++) {
1042 for (l = j; l < len; l++) {
1043 if ( (ptfunxored[l ] == 0x5a) &&
1044 (ptfunxored[l+1] == 0x07)) {
1051 if (regionspertrack == 0) {
1052 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1056 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1057 vector<region_t>::iterator begin = regions.begin();
1058 vector<region_t>::iterator finish = regions.end();
1059 vector<region_t>::iterator found;
1060 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1065 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1066 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1067 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1068 offset |= (uint32_t)(ptfunxored[i]);
1069 tr.reg.startpos = (int64_t)(offset*ratefactor);
1070 if (tr.reg.length > 0) {
1071 tracks.push_back(tr);
1081 PTFFormat::parserest10(void) {
1084 uint8_t startbytes = 0;
1085 uint8_t lengthbytes = 0;
1086 uint8_t offsetbytes = 0;
1087 uint8_t somethingbytes = 0;
1088 uint8_t skipbytes = 0;
1092 if ( (ptfunxored[k ] == 'S') &&
1093 (ptfunxored[k+1] == 'n') &&
1094 (ptfunxored[k+2] == 'a') &&
1095 (ptfunxored[k+3] == 'p')) {
1100 for (i = k; i < len-70; i++) {
1101 if ( (ptfunxored[i ] == 0x5a) &&
1102 (ptfunxored[i+1] == 0x02)) {
1108 for (i = k; i < len-70; i++) {
1109 if ( (ptfunxored[i ] == 0x5a) &&
1110 (ptfunxored[i+1] == 0x02)) {
1116 uint16_t rindex = 0;
1117 uint32_t findex = 0;
1118 for (i = k; i < len-70; i++) {
1119 if ( (ptfunxored[i ] == 0x5a) &&
1120 (ptfunxored[i+1] == 0x08)) {
1123 if ( (ptfunxored[i ] == 0x5a) &&
1124 (ptfunxored[i+1] == 0x01)) {
1126 uint8_t lengthofname = ptfunxored[i+9];
1127 if (ptfunxored[i+13] == 0x5a) {
1130 char name[256] = {0};
1131 for (j = 0; j < lengthofname; j++) {
1132 name[j] = ptfunxored[i+13+j];
1136 //uint8_t disabled = ptfunxored[j];
1137 //printf("%s\n", name);
1139 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1140 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1141 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1142 somethingbytes = (ptfunxored[j+3] & 0xf);
1143 skipbytes = ptfunxored[j+4];
1144 findex = ptfunxored[j+5
1151 /*rindex = ptfunxored[j+5
1159 uint32_t sampleoffset = 0;
1160 switch (offsetbytes) {
1162 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1164 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1166 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1168 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1173 uint32_t length = 0;
1174 switch (lengthbytes) {
1176 length |= (uint32_t)(ptfunxored[j+8] << 24);
1178 length |= (uint32_t)(ptfunxored[j+7] << 16);
1180 length |= (uint32_t)(ptfunxored[j+6] << 8);
1182 length |= (uint32_t)(ptfunxored[j+5]);
1188 switch (startbytes) {
1190 start |= (uint32_t)(ptfunxored[j+8] << 24);
1192 start |= (uint32_t)(ptfunxored[j+7] << 16);
1194 start |= (uint32_t)(ptfunxored[j+6] << 8);
1196 start |= (uint32_t)(ptfunxored[j+5]);
1202 uint32_t something = 0;
1203 switch (somethingbytes) {
1205 something |= (uint32_t)(ptfunxored[j+8] << 24);
1207 something |= (uint32_t)(ptfunxored[j+7] << 16);
1209 something |= (uint32_t)(ptfunxored[j+6] << 8);
1211 something |= (uint32_t)(ptfunxored[j+5]);
1217 std::string filename = string(name) + extension;
1221 (int64_t)(start*ratefactor),
1222 (int64_t)(length*ratefactor),
1225 if (strlen(name) == 0) {
1232 //printf("something=%d\n", something);
1234 vector<wav_t>::iterator begin = actualwavs.begin();
1235 vector<wav_t>::iterator finish = actualwavs.end();
1236 vector<wav_t>::iterator found;
1237 // Add file to list only if it is an actual wav
1238 if ((found = std::find(begin, finish, f)) != finish) {
1239 audiofiles.push_back(f);
1240 // Also add plain wav as region
1244 (int64_t)(start*ratefactor),
1245 (int64_t)(sampleoffset*ratefactor),
1246 (int64_t)(length*ratefactor),
1249 regions.push_back(r);
1252 if (foundin(filename, string(".grp"))) {
1258 (int64_t)(start*ratefactor),
1259 (int64_t)(sampleoffset*ratefactor),
1260 (int64_t)(length*ratefactor),
1263 regions.push_back(r);
1266 //printf("%s\n", name);
1271 uint32_t tracknumber = 0;
1272 uint32_t regionspertrack = 0;
1273 for (;k < len; k++) {
1274 if ( (ptfunxored[k ] == 0x5a) &&
1275 (ptfunxored[k+1] == 0x08)) {
1280 for (;k < len; k++) {
1281 if ( (ptfunxored[k ] == 0x5a) &&
1282 (ptfunxored[k+1] == 0x04)) {
1285 if ( (ptfunxored[k ] == 0x5a) &&
1286 (ptfunxored[k+1] == 0x02)) {
1288 uint8_t lengthofname = 0;
1289 lengthofname = ptfunxored[k+9];
1290 if (lengthofname == 0x5a) {
1295 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1297 //printf("regions/track=%d\n", regionspertrack);
1298 char name[256] = {0};
1299 for (j = 0; j < lengthofname; j++) {
1300 name[j] = ptfunxored[j+k+13];
1303 tr.name = string(name);
1304 tr.index = tracknumber++;
1306 for (j = k; regionspertrack > 0 && j < len; j++) {
1307 for (l = j; l < len; l++) {
1308 if ( (ptfunxored[l ] == 0x5a) &&
1309 (ptfunxored[l+1] == 0x08)) {
1316 if (regionspertrack == 0) {
1317 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1321 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1322 vector<region_t>::iterator begin = regions.begin();
1323 vector<region_t>::iterator finish = regions.end();
1324 vector<region_t>::iterator found;
1325 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1330 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1331 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1332 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1333 offset |= (uint32_t)(ptfunxored[i]);
1334 tr.reg.startpos = (int64_t)(offset*ratefactor);
1335 if (tr.reg.length > 0) {
1336 tracks.push_back(tr);