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;
598 track_t t ( name, (uint16_t)tracknumber, uint8_t(0), &r);
604 (int64_t)(start*ratefactor),
605 (int64_t)(sampleoffset*ratefactor),
606 (int64_t)(length*ratefactor),
609 regions.push_back(r);
610 vector<track_t>::iterator ti;
611 vector<track_t>::iterator bt = tracks.begin();
612 vector<track_t>::iterator et = tracks.end();
613 track_t tr ( name, 0, 0, &r );
614 if ((ti = std::find(bt, et, tr)) != et) {
615 tracknumber = (*ti).index;
617 tracknumber = tracks.size() + 1;
619 track_t t ( name, (uint16_t)tracknumber, uint8_t(0), &r);
630 PTFFormat::resort(std::vector<wav_t> *ws) {
632 std::sort((*ws).begin(), (*ws).end());
633 for (std::vector<wav_t>::iterator i = (*ws).begin();
634 i != (*ws).end(); ++i) {
641 PTFFormat::parseaudio5(void) {
643 uint64_t lengthofname, wavnumber;
645 // Find end of wav file list
648 if ( (ptfunxored[k ] == 0x5f) &&
649 (ptfunxored[k+1] == 0x50) &&
650 (ptfunxored[k+2] == 0x35)) {
657 if ( (ptfunxored[k ] == 0x5f) &&
658 (ptfunxored[k+1] == 0x50) &&
659 (ptfunxored[k+2] == 0x35)) {
665 // Find actual wav names
666 uint16_t numberofwavs = ptfunxored[k-23];
668 for (i = k; i < len; i++) {
669 if ( (ptfunxored[i ] == 'F') &&
670 (ptfunxored[i+1] == 'i') &&
671 (ptfunxored[i+2] == 'l') &&
672 (ptfunxored[i+3] == 'e') &&
673 (ptfunxored[i+4] == 's')) {
681 while (i < len && numberofwavs > 0) {
683 if ( (ptfunxored[i ] == 0x5a) &&
684 (ptfunxored[i+1] == 0x00) &&
685 (ptfunxored[i+2] == 0x05)) {
688 lengthofname = ptfunxored[i];
691 while (l < lengthofname) {
692 wavname[l] = ptfunxored[i+l];
696 ext[0] = ptfunxored[i++];
697 ext[1] = ptfunxored[i++];
698 ext[2] = ptfunxored[i++];
699 ext[3] = ptfunxored[i++];
703 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
704 extension = string("");
705 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
706 extension = string(".wav");
707 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
708 extension = string(".aif");
710 extension = string("");
713 std::string wave = string(wavname);
714 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
716 if (foundin(wave, string(".grp"))) {
720 actualwavs.push_back(f);
721 audiofiles.push_back(f);
731 PTFFormat::parseaudio(void) {
734 // Find end of wav file list
737 if ( (ptfunxored[k ] == 0xff) &&
738 (ptfunxored[k+1] == 0xff) &&
739 (ptfunxored[k+2] == 0xff) &&
740 (ptfunxored[k+3] == 0xff)) {
746 // Find actual wav names
748 uint16_t numberofwavs;
750 for (i = k; i > 4; i--) {
751 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
752 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
753 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
754 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
757 while (ptfunxored[j] != '\0') {
758 wavname[l] = ptfunxored[j];
763 if (ptfunxored[i] == 'W') {
764 extension = string(".wav");
766 extension = string(".aif");
768 //uint8_t playlist = ptfunxored[j-8];
772 for (j = k; j > 4; j--) {
773 if ( (ptfunxored[j ] == 0x01) &&
774 (ptfunxored[j-1] == 0x5a)) {
777 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
778 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
779 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
780 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
781 //printf("%d wavs\n", numberofwavs);
788 std::string wave = string(wavname);
789 std::reverse(wave.begin(), wave.end());
790 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
792 if (foundin(wave, string(".grp"))) {
796 actualwavs.push_back(f);
799 if (numberofwavs <= 0)
806 PTFFormat::parserest89(void) {
809 uint8_t startbytes = 0;
810 uint8_t lengthbytes = 0;
811 uint8_t offsetbytes = 0;
812 uint8_t somethingbytes = 0;
813 uint8_t skipbytes = 0;
817 if ( (ptfunxored[k ] == 'S') &&
818 (ptfunxored[k+1] == 'n') &&
819 (ptfunxored[k+2] == 'a') &&
820 (ptfunxored[k+3] == 'p')) {
827 for (i = k; i < len-70; i++) {
828 if ( (ptfunxored[i ] == 0x5a) &&
829 (ptfunxored[i+1] == 0x0a)) {
832 if ( (ptfunxored[i ] == 0x5a) &&
833 (ptfunxored[i+1] == 0x0c)) {
835 uint8_t lengthofname = ptfunxored[i+9];
837 char name[256] = {0};
838 for (j = 0; j < lengthofname; j++) {
839 name[j] = ptfunxored[i+13+j];
843 //uint8_t disabled = ptfunxored[j];
845 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
846 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
847 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
848 somethingbytes = (ptfunxored[j+3] & 0xf);
849 skipbytes = ptfunxored[j+4];
850 findex = ptfunxored[j+5
857 /*rindex = ptfunxored[j+5
865 uint32_t sampleoffset = 0;
866 switch (offsetbytes) {
868 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
870 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
872 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
874 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
880 switch (lengthbytes) {
882 length |= (uint32_t)(ptfunxored[j+8] << 24);
884 length |= (uint32_t)(ptfunxored[j+7] << 16);
886 length |= (uint32_t)(ptfunxored[j+6] << 8);
888 length |= (uint32_t)(ptfunxored[j+5]);
894 switch (startbytes) {
896 start |= (uint32_t)(ptfunxored[j+8] << 24);
898 start |= (uint32_t)(ptfunxored[j+7] << 16);
900 start |= (uint32_t)(ptfunxored[j+6] << 8);
902 start |= (uint32_t)(ptfunxored[j+5]);
908 uint32_t something = 0;
909 switch (somethingbytes) {
911 something |= (uint32_t)(ptfunxored[j+8] << 24);
913 something |= (uint32_t)(ptfunxored[j+7] << 16);
915 something |= (uint32_t)(ptfunxored[j+6] << 8);
917 something |= (uint32_t)(ptfunxored[j+5]);
923 std::string filename = string(name) + extension;
927 (int64_t)(start*ratefactor),
928 (int64_t)(length*ratefactor),
932 //printf("something=%d\n", something);
934 vector<wav_t>::iterator begin = actualwavs.begin();
935 vector<wav_t>::iterator finish = actualwavs.end();
936 vector<wav_t>::iterator found;
937 // Add file to list only if it is an actual wav
938 if ((found = std::find(begin, finish, f)) != finish) {
939 audiofiles.push_back(f);
940 // Also add plain wav as region
944 (int64_t)(start*ratefactor),
945 (int64_t)(sampleoffset*ratefactor),
946 (int64_t)(length*ratefactor),
949 regions.push_back(r);
952 if (foundin(filename, string(".grp"))) {
958 (int64_t)(start*ratefactor),
959 (int64_t)(sampleoffset*ratefactor),
960 (int64_t)(length*ratefactor),
963 regions.push_back(r);
970 if ( (ptfunxored[k ] == 0x5a) &&
971 (ptfunxored[k+1] == 0x03)) {
977 if ( (ptfunxored[k ] == 0x5a) &&
978 (ptfunxored[k+1] == 0x02)) {
987 uint32_t tracknumber = 0;
988 uint32_t regionspertrack = 0;
989 for (;k < len; k++) {
990 if ( (ptfunxored[k ] == 0x5a) &&
991 (ptfunxored[k+1] == 0x04)) {
994 if ( (ptfunxored[k ] == 0x5a) &&
995 (ptfunxored[k+1] == 0x02)) {
997 uint8_t lengthofname = 0;
998 lengthofname = ptfunxored[k+9];
999 if (lengthofname == 0x5a) {
1004 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1006 //printf("regions/track=%d\n", regionspertrack);
1007 char name[256] = {0};
1008 for (j = 0; j < lengthofname; j++) {
1009 name[j] = ptfunxored[j+k+13];
1012 tr.name = string(name);
1013 tr.index = tracknumber++;
1015 for (j = k; regionspertrack > 0 && j < len; j++) {
1016 for (l = j; l < len; l++) {
1017 if ( (ptfunxored[l ] == 0x5a) &&
1018 (ptfunxored[l+1] == 0x07)) {
1025 if (regionspertrack == 0) {
1026 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1030 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1031 vector<region_t>::iterator begin = regions.begin();
1032 vector<region_t>::iterator finish = regions.end();
1033 vector<region_t>::iterator found;
1034 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1039 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1040 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1041 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1042 offset |= (uint32_t)(ptfunxored[i]);
1043 tr.reg.startpos = (int64_t)(offset*ratefactor);
1044 if (tr.reg.length > 0) {
1045 tracks.push_back(tr);
1055 PTFFormat::parserest10(void) {
1058 uint8_t startbytes = 0;
1059 uint8_t lengthbytes = 0;
1060 uint8_t offsetbytes = 0;
1061 uint8_t somethingbytes = 0;
1062 uint8_t skipbytes = 0;
1066 if ( (ptfunxored[k ] == 'S') &&
1067 (ptfunxored[k+1] == 'n') &&
1068 (ptfunxored[k+2] == 'a') &&
1069 (ptfunxored[k+3] == 'p')) {
1074 for (i = k; i < len-70; i++) {
1075 if ( (ptfunxored[i ] == 0x5a) &&
1076 (ptfunxored[i+1] == 0x02)) {
1082 for (i = k; i < len-70; i++) {
1083 if ( (ptfunxored[i ] == 0x5a) &&
1084 (ptfunxored[i+1] == 0x02)) {
1090 uint16_t rindex = 0;
1091 uint32_t findex = 0;
1092 for (i = k; i < len-70; i++) {
1093 if ( (ptfunxored[i ] == 0x5a) &&
1094 (ptfunxored[i+1] == 0x08)) {
1097 if ( (ptfunxored[i ] == 0x5a) &&
1098 (ptfunxored[i+1] == 0x01)) {
1100 uint8_t lengthofname = ptfunxored[i+9];
1101 if (ptfunxored[i+13] == 0x5a) {
1104 char name[256] = {0};
1105 for (j = 0; j < lengthofname; j++) {
1106 name[j] = ptfunxored[i+13+j];
1110 //uint8_t disabled = ptfunxored[j];
1111 //printf("%s\n", name);
1113 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1114 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1115 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1116 somethingbytes = (ptfunxored[j+3] & 0xf);
1117 skipbytes = ptfunxored[j+4];
1118 findex = ptfunxored[j+5
1125 /*rindex = ptfunxored[j+5
1133 uint32_t sampleoffset = 0;
1134 switch (offsetbytes) {
1136 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1138 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1140 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1142 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1147 uint32_t length = 0;
1148 switch (lengthbytes) {
1150 length |= (uint32_t)(ptfunxored[j+8] << 24);
1152 length |= (uint32_t)(ptfunxored[j+7] << 16);
1154 length |= (uint32_t)(ptfunxored[j+6] << 8);
1156 length |= (uint32_t)(ptfunxored[j+5]);
1162 switch (startbytes) {
1164 start |= (uint32_t)(ptfunxored[j+8] << 24);
1166 start |= (uint32_t)(ptfunxored[j+7] << 16);
1168 start |= (uint32_t)(ptfunxored[j+6] << 8);
1170 start |= (uint32_t)(ptfunxored[j+5]);
1176 uint32_t something = 0;
1177 switch (somethingbytes) {
1179 something |= (uint32_t)(ptfunxored[j+8] << 24);
1181 something |= (uint32_t)(ptfunxored[j+7] << 16);
1183 something |= (uint32_t)(ptfunxored[j+6] << 8);
1185 something |= (uint32_t)(ptfunxored[j+5]);
1191 std::string filename = string(name) + extension;
1195 (int64_t)(start*ratefactor),
1196 (int64_t)(length*ratefactor),
1199 if (strlen(name) == 0) {
1206 //printf("something=%d\n", something);
1208 vector<wav_t>::iterator begin = actualwavs.begin();
1209 vector<wav_t>::iterator finish = actualwavs.end();
1210 vector<wav_t>::iterator found;
1211 // Add file to list only if it is an actual wav
1212 if ((found = std::find(begin, finish, f)) != finish) {
1213 audiofiles.push_back(f);
1214 // Also add plain wav as region
1218 (int64_t)(start*ratefactor),
1219 (int64_t)(sampleoffset*ratefactor),
1220 (int64_t)(length*ratefactor),
1223 regions.push_back(r);
1226 if (foundin(filename, string(".grp"))) {
1232 (int64_t)(start*ratefactor),
1233 (int64_t)(sampleoffset*ratefactor),
1234 (int64_t)(length*ratefactor),
1237 regions.push_back(r);
1240 //printf("%s\n", name);
1245 uint32_t tracknumber = 0;
1246 uint32_t regionspertrack = 0;
1247 for (;k < len; k++) {
1248 if ( (ptfunxored[k ] == 0x5a) &&
1249 (ptfunxored[k+1] == 0x08)) {
1254 for (;k < len; k++) {
1255 if ( (ptfunxored[k ] == 0x5a) &&
1256 (ptfunxored[k+1] == 0x04)) {
1259 if ( (ptfunxored[k ] == 0x5a) &&
1260 (ptfunxored[k+1] == 0x02)) {
1262 uint8_t lengthofname = 0;
1263 lengthofname = ptfunxored[k+9];
1264 if (lengthofname == 0x5a) {
1269 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1271 //printf("regions/track=%d\n", regionspertrack);
1272 char name[256] = {0};
1273 for (j = 0; j < lengthofname; j++) {
1274 name[j] = ptfunxored[j+k+13];
1277 tr.name = string(name);
1278 tr.index = tracknumber++;
1280 for (j = k; regionspertrack > 0 && j < len; j++) {
1281 for (l = j; l < len; l++) {
1282 if ( (ptfunxored[l ] == 0x5a) &&
1283 (ptfunxored[l+1] == 0x08)) {
1290 if (regionspertrack == 0) {
1291 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1295 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1296 vector<region_t>::iterator begin = regions.begin();
1297 vector<region_t>::iterator finish = regions.end();
1298 vector<region_t>::iterator found;
1299 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1300 tr.set_region (&(*found));
1304 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1305 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1306 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1307 offset |= (uint32_t)(ptfunxored[i]);
1308 tr.reg.startpos = (int64_t)(offset*ratefactor);
1309 if (tr.reg.length > 0) {
1310 tracks.push_back(tr);