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];
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*/
147 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
149 for (i = 2; i < 256; i++) {
153 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
154 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
162 for (i = 2; i < 256; i++) {
166 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
169 for (i = 0; i < 64; i++) {
172 for (i = 128; i < 192; i++) {
180 for (i = 2; i < 256; i++) {
184 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
188 key = gen_secret(c1);
190 for (i = 0; i < 64; i++) {
191 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
193 for (i = 128; i < 192; i++) {
194 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
195 xxor[i] ^= (inv * 0x40);
198 for (i = 192; i < 256; i++) {
204 //Should not happen, failed c[0] c[1]
211 fseek(fp, 0, SEEK_SET);
212 while (fread(&ct, 1, 1, fp) != 0) {
213 ptfunxored[i++] = ct;
217 /* version detection */
219 v = ptfunxored[voff];
222 } else if (v == 0x03) {
227 v = ptfunxored[voff];
228 if (v == 10 || v == 11 || v == 12) {
233 if (version == 0 || version == 5 || version == 7) {
234 /* Haven't detected version yet so decipher */
236 for (i = 0; i < len; i++) {
240 ptfunxored[i] ^= xxor[j];
244 /* version detection */
246 v = ptfunxored[voff];
249 } else if (v == 0x03) {
254 v = ptfunxored[voff];
255 if (v == 5 || v == 7 || v == 8 || v == 9) {
260 targetrate = targetsr;
266 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
268 uint32_t counts[256] = {0};
273 for (i = start; i < stop; i++) {
274 counts[ptfunxored[i]]++;
277 for (i = 0; i < 256; i++) {
278 if (counts[i] > max) {
287 PTFFormat::unxor10(void)
290 uint8_t x = mostfrequent(0x1000, 0x2000);
291 uint8_t dx = 0x100-x;
293 for (j = 0x1000; j < len; j++) {
294 if(j % 0x1000 == 0xfff) {
302 PTFFormat::parse(void) {
308 } else if (version == 7) {
313 } else if (version == 8) {
318 } else if (version == 9) {
323 } else if (version == 10 || version == 11 || version == 12) {
334 PTFFormat::setrates(void) {
336 if (sessionrate != 0) {
337 ratefactor = (float)targetrate / sessionrate;
342 PTFFormat::parse5header(void) {
345 // Find session sample rate
348 if ( (ptfunxored[k ] == 0x5a) &&
349 (ptfunxored[k+1] == 0x00) &&
350 (ptfunxored[k+2] == 0x02)) {
357 sessionrate |= ptfunxored[k+12] << 16;
358 sessionrate |= ptfunxored[k+13] << 8;
359 sessionrate |= ptfunxored[k+14];
363 PTFFormat::parse7header(void) {
366 // Find session sample rate
369 if ( (ptfunxored[k ] == 0x5a) &&
370 (ptfunxored[k+1] == 0x00) &&
371 (ptfunxored[k+2] == 0x05)) {
378 sessionrate |= ptfunxored[k+12] << 16;
379 sessionrate |= ptfunxored[k+13] << 8;
380 sessionrate |= ptfunxored[k+14];
384 PTFFormat::parse8header(void) {
387 // Find session sample rate
390 if ( (ptfunxored[k ] == 0x5a) &&
391 (ptfunxored[k+1] == 0x05)) {
398 sessionrate |= ptfunxored[k+11];
399 sessionrate |= ptfunxored[k+12] << 8;
400 sessionrate |= ptfunxored[k+13] << 16;
404 PTFFormat::parse9header(void) {
407 // Find session sample rate
410 if ( (ptfunxored[k ] == 0x5a) &&
411 (ptfunxored[k+1] == 0x06)) {
418 sessionrate |= ptfunxored[k+11];
419 sessionrate |= ptfunxored[k+12] << 8;
420 sessionrate |= ptfunxored[k+13] << 16;
424 PTFFormat::parse10header(void) {
427 // Find session sample rate
430 if ( (ptfunxored[k ] == 0x5a) &&
431 (ptfunxored[k+1] == 0x09)) {
438 sessionrate |= ptfunxored[k+11];
439 sessionrate |= ptfunxored[k+12] << 8;
440 sessionrate |= ptfunxored[k+13] << 16;
444 PTFFormat::parserest5(void) {
446 uint64_t regionspertrack, lengthofname;
447 uint64_t startbytes, lengthbytes, offsetbytes;
448 uint16_t tracknumber = 0;
453 for (i = 0; i < 5; i++) {
455 if ( (ptfunxored[k ] == 0x5a) &&
456 (ptfunxored[k+1] == 0x00) &&
457 (ptfunxored[k+2] == 0x03)) {
466 for (i = 0; i < 2; i++) {
468 if ( (ptfunxored[k ] == 0x5a) &&
469 (ptfunxored[k+1] == 0x00) &&
470 (ptfunxored[k+2] == 0x01)) {
482 if ( (ptfunxored[k ] == 0xff) &&
483 (ptfunxored[k+1] == 0xff)) {
487 if ( (ptfunxored[k ] == 0x5a) &&
488 (ptfunxored[k+1] == 0x00) &&
489 (ptfunxored[k+2] == 0x01)) {
495 lengthofname = ptfunxored[k+12];
496 if (ptfunxored[k+13] == 0x5a) {
500 char name[256] = {0};
501 for (j = 0; j < lengthofname; j++) {
502 name[j] = ptfunxored[k+13+j];
505 regionspertrack = ptfunxored[k+13+j+3];
506 for (i = 0; i < regionspertrack; i++) {
508 if ( (ptfunxored[k ] == 0x5a) &&
509 (ptfunxored[k+1] == 0x00) &&
510 (ptfunxored[k+2] == 0x03)) {
516 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
517 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
518 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
519 //somethingbytes = (ptfunxored[j+1] & 0xf);
520 findex = ptfunxored[k+14];
523 switch (startbytes) {
525 start |= (uint32_t)(ptfunxored[j+8] << 24);
527 start |= (uint32_t)(ptfunxored[j+7] << 16);
529 start |= (uint32_t)(ptfunxored[j+6] << 8);
531 start |= (uint32_t)(ptfunxored[j+5]);
537 switch (lengthbytes) {
539 length |= (uint32_t)(ptfunxored[j+8] << 24);
541 length |= (uint32_t)(ptfunxored[j+7] << 16);
543 length |= (uint32_t)(ptfunxored[j+6] << 8);
545 length |= (uint32_t)(ptfunxored[j+5]);
550 uint32_t sampleoffset = 0;
551 switch (offsetbytes) {
553 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
555 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
557 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
559 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
565 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
567 std::string filename = string(name) + extension;
571 (int64_t)(start*ratefactor),
572 (int64_t)(length*ratefactor),
575 vector<wav_t>::iterator begin = audiofiles.begin();
576 vector<wav_t>::iterator finish = audiofiles.end();
577 vector<wav_t>::iterator found;
579 if ((found = std::find(begin, finish, f)) != finish) {
583 (int64_t)(start*ratefactor),
584 (int64_t)(sampleoffset*ratefactor),
585 (int64_t)(length*ratefactor),
588 regions.push_back(r);
589 vector<track_t>::iterator ti;
590 vector<track_t>::iterator bt = tracks.begin();
591 vector<track_t>::iterator et = tracks.end();
592 track_t tr = { name, 0, 0, r };
593 if ((ti = std::find(bt, et, tr)) != et) {
594 tracknumber = (*ti).index;
596 tracknumber = tracks.size() + 1;
600 (uint16_t)tracknumber,
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::resort(std::vector<wav_t> *ws) {
642 std::sort((*ws).begin(), (*ws).end());
643 for (std::vector<wav_t>::iterator i = (*ws).begin();
644 i != (*ws).end(); ++i) {
651 PTFFormat::parseaudio5(void) {
653 uint64_t lengthofname, wavnumber;
655 // Find end of wav file list
658 if ( (ptfunxored[k ] == 0x5f) &&
659 (ptfunxored[k+1] == 0x50) &&
660 (ptfunxored[k+2] == 0x35)) {
667 if ( (ptfunxored[k ] == 0x5f) &&
668 (ptfunxored[k+1] == 0x50) &&
669 (ptfunxored[k+2] == 0x35)) {
675 // Find actual wav names
676 uint16_t numberofwavs = ptfunxored[k-23];
678 for (i = k; i < len; i++) {
679 if ( (ptfunxored[i ] == 'F') &&
680 (ptfunxored[i+1] == 'i') &&
681 (ptfunxored[i+2] == 'l') &&
682 (ptfunxored[i+3] == 'e') &&
683 (ptfunxored[i+4] == 's')) {
691 while (i < len && numberofwavs > 0) {
693 if ( (ptfunxored[i ] == 0x5a) &&
694 (ptfunxored[i+1] == 0x00) &&
695 (ptfunxored[i+2] == 0x05)) {
698 lengthofname = ptfunxored[i];
701 while (l < lengthofname) {
702 wavname[l] = ptfunxored[i+l];
706 ext[0] = ptfunxored[i++];
707 ext[1] = ptfunxored[i++];
708 ext[2] = ptfunxored[i++];
709 ext[3] = ptfunxored[i++];
713 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
714 extension = string("");
715 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
716 extension = string(".wav");
717 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
718 extension = string(".aif");
720 extension = string("");
723 std::string wave = string(wavname);
724 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
726 if (foundin(wave, string(".grp"))) {
730 actualwavs.push_back(f);
731 audiofiles.push_back(f);
741 PTFFormat::parseaudio(void) {
744 // Find end of wav file list
747 if ( (ptfunxored[k ] == 0xff) &&
748 (ptfunxored[k+1] == 0xff) &&
749 (ptfunxored[k+2] == 0xff) &&
750 (ptfunxored[k+3] == 0xff)) {
756 // Find actual wav names
758 uint16_t numberofwavs;
760 for (i = k; i > 4; i--) {
761 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
762 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
763 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
764 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
767 while (ptfunxored[j] != '\0') {
768 wavname[l] = ptfunxored[j];
773 if (ptfunxored[i] == 'W') {
774 extension = string(".wav");
776 extension = string(".aif");
778 //uint8_t playlist = ptfunxored[j-8];
782 for (j = k; j > 4; j--) {
783 if ( (ptfunxored[j ] == 0x01) &&
784 (ptfunxored[j-1] == 0x5a)) {
787 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
788 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
789 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
790 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
791 //printf("%d wavs\n", numberofwavs);
798 std::string wave = string(wavname);
799 std::reverse(wave.begin(), wave.end());
800 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
802 if (foundin(wave, string(".grp"))) {
806 actualwavs.push_back(f);
809 if (numberofwavs <= 0)
816 PTFFormat::parserest89(void) {
819 uint8_t startbytes = 0;
820 uint8_t lengthbytes = 0;
821 uint8_t offsetbytes = 0;
822 uint8_t somethingbytes = 0;
823 uint8_t skipbytes = 0;
827 if ( (ptfunxored[k ] == 'S') &&
828 (ptfunxored[k+1] == 'n') &&
829 (ptfunxored[k+2] == 'a') &&
830 (ptfunxored[k+3] == 'p')) {
837 for (i = k; i < len-70; i++) {
838 if ( (ptfunxored[i ] == 0x5a) &&
839 (ptfunxored[i+1] == 0x0a)) {
842 if ( (ptfunxored[i ] == 0x5a) &&
843 (ptfunxored[i+1] == 0x0c)) {
845 uint8_t lengthofname = ptfunxored[i+9];
847 char name[256] = {0};
848 for (j = 0; j < lengthofname; j++) {
849 name[j] = ptfunxored[i+13+j];
853 //uint8_t disabled = ptfunxored[j];
855 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
856 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
857 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
858 somethingbytes = (ptfunxored[j+3] & 0xf);
859 skipbytes = ptfunxored[j+4];
860 findex = ptfunxored[j+5
867 /*rindex = ptfunxored[j+5
875 uint32_t sampleoffset = 0;
876 switch (offsetbytes) {
878 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
880 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
882 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
884 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
890 switch (lengthbytes) {
892 length |= (uint32_t)(ptfunxored[j+8] << 24);
894 length |= (uint32_t)(ptfunxored[j+7] << 16);
896 length |= (uint32_t)(ptfunxored[j+6] << 8);
898 length |= (uint32_t)(ptfunxored[j+5]);
904 switch (startbytes) {
906 start |= (uint32_t)(ptfunxored[j+8] << 24);
908 start |= (uint32_t)(ptfunxored[j+7] << 16);
910 start |= (uint32_t)(ptfunxored[j+6] << 8);
912 start |= (uint32_t)(ptfunxored[j+5]);
918 uint32_t something = 0;
919 switch (somethingbytes) {
921 something |= (uint32_t)(ptfunxored[j+8] << 24);
923 something |= (uint32_t)(ptfunxored[j+7] << 16);
925 something |= (uint32_t)(ptfunxored[j+6] << 8);
927 something |= (uint32_t)(ptfunxored[j+5]);
933 std::string filename = string(name) + extension;
937 (int64_t)(start*ratefactor),
938 (int64_t)(length*ratefactor),
942 //printf("something=%d\n", something);
944 vector<wav_t>::iterator begin = actualwavs.begin();
945 vector<wav_t>::iterator finish = actualwavs.end();
946 vector<wav_t>::iterator found;
947 // Add file to list only if it is an actual wav
948 if ((found = std::find(begin, finish, f)) != finish) {
949 audiofiles.push_back(f);
950 // Also add plain wav as region
954 (int64_t)(start*ratefactor),
955 (int64_t)(sampleoffset*ratefactor),
956 (int64_t)(length*ratefactor),
959 regions.push_back(r);
962 if (foundin(filename, string(".grp"))) {
968 (int64_t)(start*ratefactor),
969 (int64_t)(sampleoffset*ratefactor),
970 (int64_t)(length*ratefactor),
973 regions.push_back(r);
980 if ( (ptfunxored[k ] == 0x5a) &&
981 (ptfunxored[k+1] == 0x03)) {
987 if ( (ptfunxored[k ] == 0x5a) &&
988 (ptfunxored[k+1] == 0x02)) {
997 uint32_t tracknumber = 0;
998 uint32_t regionspertrack = 0;
999 for (;k < len; k++) {
1000 if ( (ptfunxored[k ] == 0x5a) &&
1001 (ptfunxored[k+1] == 0x04)) {
1004 if ( (ptfunxored[k ] == 0x5a) &&
1005 (ptfunxored[k+1] == 0x02)) {
1007 uint8_t lengthofname = 0;
1008 lengthofname = ptfunxored[k+9];
1009 if (lengthofname == 0x5a) {
1014 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1016 //printf("regions/track=%d\n", regionspertrack);
1017 char name[256] = {0};
1018 for (j = 0; j < lengthofname; j++) {
1019 name[j] = ptfunxored[j+k+13];
1022 tr.name = string(name);
1023 tr.index = tracknumber++;
1025 for (j = k; regionspertrack > 0 && j < len; j++) {
1026 for (l = j; l < len; l++) {
1027 if ( (ptfunxored[l ] == 0x5a) &&
1028 (ptfunxored[l+1] == 0x07)) {
1035 if (regionspertrack == 0) {
1036 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1040 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1041 vector<region_t>::iterator begin = regions.begin();
1042 vector<region_t>::iterator finish = regions.end();
1043 vector<region_t>::iterator found;
1044 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1049 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1050 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1051 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1052 offset |= (uint32_t)(ptfunxored[i]);
1053 tr.reg.startpos = (int64_t)(offset*ratefactor);
1054 if (tr.reg.length > 0) {
1055 tracks.push_back(tr);
1065 PTFFormat::parserest10(void) {
1068 uint8_t startbytes = 0;
1069 uint8_t lengthbytes = 0;
1070 uint8_t offsetbytes = 0;
1071 uint8_t somethingbytes = 0;
1072 uint8_t skipbytes = 0;
1076 if ( (ptfunxored[k ] == 'S') &&
1077 (ptfunxored[k+1] == 'n') &&
1078 (ptfunxored[k+2] == 'a') &&
1079 (ptfunxored[k+3] == 'p')) {
1084 for (i = k; i < len-70; i++) {
1085 if ( (ptfunxored[i ] == 0x5a) &&
1086 (ptfunxored[i+1] == 0x02)) {
1092 for (i = k; i < len-70; i++) {
1093 if ( (ptfunxored[i ] == 0x5a) &&
1094 (ptfunxored[i+1] == 0x02)) {
1100 uint16_t rindex = 0;
1101 uint32_t findex = 0;
1102 for (i = k; i < len-70; i++) {
1103 if ( (ptfunxored[i ] == 0x5a) &&
1104 (ptfunxored[i+1] == 0x08)) {
1107 if ( (ptfunxored[i ] == 0x5a) &&
1108 (ptfunxored[i+1] == 0x01)) {
1110 uint8_t lengthofname = ptfunxored[i+9];
1111 if (ptfunxored[i+13] == 0x5a) {
1114 char name[256] = {0};
1115 for (j = 0; j < lengthofname; j++) {
1116 name[j] = ptfunxored[i+13+j];
1120 //uint8_t disabled = ptfunxored[j];
1121 //printf("%s\n", name);
1123 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1124 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1125 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1126 somethingbytes = (ptfunxored[j+3] & 0xf);
1127 skipbytes = ptfunxored[j+4];
1128 findex = ptfunxored[j+5
1135 /*rindex = ptfunxored[j+5
1143 uint32_t sampleoffset = 0;
1144 switch (offsetbytes) {
1146 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1148 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1150 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1152 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1157 uint32_t length = 0;
1158 switch (lengthbytes) {
1160 length |= (uint32_t)(ptfunxored[j+8] << 24);
1162 length |= (uint32_t)(ptfunxored[j+7] << 16);
1164 length |= (uint32_t)(ptfunxored[j+6] << 8);
1166 length |= (uint32_t)(ptfunxored[j+5]);
1172 switch (startbytes) {
1174 start |= (uint32_t)(ptfunxored[j+8] << 24);
1176 start |= (uint32_t)(ptfunxored[j+7] << 16);
1178 start |= (uint32_t)(ptfunxored[j+6] << 8);
1180 start |= (uint32_t)(ptfunxored[j+5]);
1186 uint32_t something = 0;
1187 switch (somethingbytes) {
1189 something |= (uint32_t)(ptfunxored[j+8] << 24);
1191 something |= (uint32_t)(ptfunxored[j+7] << 16);
1193 something |= (uint32_t)(ptfunxored[j+6] << 8);
1195 something |= (uint32_t)(ptfunxored[j+5]);
1201 std::string filename = string(name) + extension;
1205 (int64_t)(start*ratefactor),
1206 (int64_t)(length*ratefactor),
1209 if (strlen(name) == 0) {
1216 //printf("something=%d\n", something);
1218 vector<wav_t>::iterator begin = actualwavs.begin();
1219 vector<wav_t>::iterator finish = actualwavs.end();
1220 vector<wav_t>::iterator found;
1221 // Add file to list only if it is an actual wav
1222 if ((found = std::find(begin, finish, f)) != finish) {
1223 audiofiles.push_back(f);
1224 // Also add plain wav as region
1228 (int64_t)(start*ratefactor),
1229 (int64_t)(sampleoffset*ratefactor),
1230 (int64_t)(length*ratefactor),
1233 regions.push_back(r);
1236 if (foundin(filename, string(".grp"))) {
1242 (int64_t)(start*ratefactor),
1243 (int64_t)(sampleoffset*ratefactor),
1244 (int64_t)(length*ratefactor),
1247 regions.push_back(r);
1250 //printf("%s\n", name);
1255 uint32_t tracknumber = 0;
1256 uint32_t regionspertrack = 0;
1257 for (;k < len; k++) {
1258 if ( (ptfunxored[k ] == 0x5a) &&
1259 (ptfunxored[k+1] == 0x08)) {
1264 for (;k < len; k++) {
1265 if ( (ptfunxored[k ] == 0x5a) &&
1266 (ptfunxored[k+1] == 0x04)) {
1269 if ( (ptfunxored[k ] == 0x5a) &&
1270 (ptfunxored[k+1] == 0x02)) {
1272 uint8_t lengthofname = 0;
1273 lengthofname = ptfunxored[k+9];
1274 if (lengthofname == 0x5a) {
1279 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1281 //printf("regions/track=%d\n", regionspertrack);
1282 char name[256] = {0};
1283 for (j = 0; j < lengthofname; j++) {
1284 name[j] = ptfunxored[j+k+13];
1287 tr.name = string(name);
1288 tr.index = tracknumber++;
1290 for (j = k; regionspertrack > 0 && j < len; j++) {
1291 for (l = j; l < len; l++) {
1292 if ( (ptfunxored[l ] == 0x5a) &&
1293 (ptfunxored[l+1] == 0x08)) {
1300 if (regionspertrack == 0) {
1301 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1305 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1306 vector<region_t>::iterator begin = regions.begin();
1307 vector<region_t>::iterator finish = regions.end();
1308 vector<region_t>::iterator found;
1309 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1314 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1315 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1316 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1317 offset |= (uint32_t)(ptfunxored[i]);
1318 tr.reg.startpos = (int64_t)(offset*ratefactor);
1319 if (tr.reg.length > 0) {
1320 tracks.push_back(tr);