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.
23 #include <glib/gstdio.h>
25 #include "ptfformat.h"
29 static const uint32_t baselut[16] = {
30 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
31 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
32 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
33 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
36 static const uint32_t xorlut[16] = {
37 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
38 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
39 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
40 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
43 static uint32_t swapbytes32 (const uint32_t v) {
45 rv |= ((v >> 0) & 0xf) << 28;
46 rv |= ((v >> 4) & 0xf) << 24;
47 rv |= ((v >> 8) & 0xf) << 20;
48 rv |= ((v >> 12) & 0xf) << 16;
49 rv |= ((v >> 16) & 0xf) << 12;
50 rv |= ((v >> 20) & 0xf) << 8;
51 rv |= ((v >> 24) & 0xf) << 4;
52 rv |= ((v >> 28) & 0xf) << 0;
56 static uint64_t gen_secret (int i) {
57 assert (i > 0 && i < 256);
58 int iwrap = i & 0x7f; // wrap at 0x80;
59 uint32_t xor_lo = 0; // 0x40 flag
60 int idx; // mirror at 0x40;
69 int i16 = (idx >> 1) & 0xf;
74 uint32_t lo = baselut [i16];
75 uint32_t xk = xorlut [i16];
81 uint32_t hi = swapbytes32 (lo) ^ xk;
82 return ((uint64_t)hi << 32) | (lo ^ xor_lo);
85 PTFFormat::PTFFormat() {
88 PTFFormat::~PTFFormat() {
95 PTFFormat::foundin(std::string haystack, std::string needle) {
96 size_t found = haystack.find(needle);
97 if (found != std::string::npos) {
104 /* Return values: 0 success
105 0x01 to 0xff value of missing lut
106 -1 could not open file as ptf
109 PTFFormat::load(std::string path, int64_t targetsr) {
111 unsigned char xxor[256];
121 if (! (fp = g_fopen(path.c_str(), "rb"))) {
125 fseek(fp, 0, SEEK_END);
131 fseek(fp, 0x40, SEEK_SET);
132 fread(&c0, 1, 1, fp);
133 fread(&c1, 1, 1, fp);
135 // For version <= 7 support:
139 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
140 /* Silently fail -- out of memory*/
151 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
153 for (i = 2; i < 256; i++) {
157 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
158 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
166 for (i = 2; i < 256; i++) {
170 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
173 for (i = 0; i < 64; i++) {
176 for (i = 128; i < 192; i++) {
184 for (i = 2; i < 256; i++) {
188 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
192 key = gen_secret(c1);
194 for (i = 0; i < 64; i++) {
195 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
197 for (i = 128; i < 192; i++) {
198 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
199 xxor[i] ^= (inv * 0x40);
202 for (i = 192; i < 256; i++) {
208 //Should not happen, failed c[0] c[1]
215 fseek(fp, 0, SEEK_SET);
216 while (fread(&ct, 1, 1, fp) != 0) {
217 ptfunxored[i++] = ct;
221 /* version detection */
223 v = ptfunxored[voff];
226 } else if (v == 0x03) {
231 v = ptfunxored[voff];
232 if (v == 10 || v == 11 || v == 12) {
237 if (version == 0 || version == 5 || version == 7) {
238 /* Haven't detected version yet so decipher */
240 for (i = 0; i < len; i++) {
244 ptfunxored[i] ^= xxor[j];
248 /* version detection */
250 v = ptfunxored[voff];
253 } else if (v == 0x03) {
258 v = ptfunxored[voff];
259 if (v == 5 || v == 7 || v == 8 || v == 9) {
264 if (version < 5 || version > 12)
266 targetrate = targetsr;
274 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
276 uint32_t counts[256] = {0};
281 for (i = start; i < stop; i++) {
282 counts[ptfunxored[i]]++;
285 for (i = 0; i < 256; i++) {
286 if (counts[i] > max) {
295 PTFFormat::unxor10(void)
298 uint8_t x = mostfrequent(0x1000, 0x2000);
299 uint8_t dx = 0x100-x;
301 for (j = 0x1000; j < len; j++) {
302 if(j % 0x1000 == 0xfff) {
310 PTFFormat::parse(void) {
314 if (sessionrate < 44100 || sessionrate > 192000)
318 } else if (version == 7) {
321 if (sessionrate < 44100 || sessionrate > 192000)
325 } else if (version == 8) {
328 if (sessionrate < 44100 || sessionrate > 192000)
332 } else if (version == 9) {
335 if (sessionrate < 44100 || sessionrate > 192000)
339 } else if (version == 10 || version == 11 || version == 12) {
342 if (sessionrate < 44100 || sessionrate > 192000)
354 PTFFormat::setrates(void) {
356 if (sessionrate != 0) {
357 ratefactor = (float)targetrate / sessionrate;
362 PTFFormat::parse5header(void) {
365 // Find session sample rate
368 if ( (ptfunxored[k ] == 0x5a) &&
369 (ptfunxored[k+1] == 0x00) &&
370 (ptfunxored[k+2] == 0x02)) {
377 sessionrate |= ptfunxored[k+12] << 16;
378 sessionrate |= ptfunxored[k+13] << 8;
379 sessionrate |= ptfunxored[k+14];
383 PTFFormat::parse7header(void) {
386 // Find session sample rate
389 if ( (ptfunxored[k ] == 0x5a) &&
390 (ptfunxored[k+1] == 0x00) &&
391 (ptfunxored[k+2] == 0x05)) {
398 sessionrate |= ptfunxored[k+12] << 16;
399 sessionrate |= ptfunxored[k+13] << 8;
400 sessionrate |= ptfunxored[k+14];
404 PTFFormat::parse8header(void) {
407 // Find session sample rate
410 if ( (ptfunxored[k ] == 0x5a) &&
411 (ptfunxored[k+1] == 0x05)) {
418 sessionrate |= ptfunxored[k+11];
419 sessionrate |= ptfunxored[k+12] << 8;
420 sessionrate |= ptfunxored[k+13] << 16;
424 PTFFormat::parse9header(void) {
427 // Find session sample rate
430 if ( (ptfunxored[k ] == 0x5a) &&
431 (ptfunxored[k+1] == 0x06)) {
438 sessionrate |= ptfunxored[k+11];
439 sessionrate |= ptfunxored[k+12] << 8;
440 sessionrate |= ptfunxored[k+13] << 16;
444 PTFFormat::parse10header(void) {
447 // Find session sample rate
450 if ( (ptfunxored[k ] == 0x5a) &&
451 (ptfunxored[k+1] == 0x09)) {
458 sessionrate |= ptfunxored[k+11];
459 sessionrate |= ptfunxored[k+12] << 8;
460 sessionrate |= ptfunxored[k+13] << 16;
464 PTFFormat::parserest5(void) {
466 uint64_t regionspertrack, lengthofname;
467 uint64_t startbytes, lengthbytes, offsetbytes;
468 uint16_t tracknumber = 0;
473 for (i = 0; i < 5; i++) {
475 if ( (ptfunxored[k ] == 0x5a) &&
476 (ptfunxored[k+1] == 0x00) &&
477 (ptfunxored[k+2] == 0x03)) {
486 for (i = 0; i < 2; i++) {
488 if ( (ptfunxored[k ] == 0x5a) &&
489 (ptfunxored[k+1] == 0x00) &&
490 (ptfunxored[k+2] == 0x01)) {
502 if ( (ptfunxored[k ] == 0xff) &&
503 (ptfunxored[k+1] == 0xff)) {
507 if ( (ptfunxored[k ] == 0x5a) &&
508 (ptfunxored[k+1] == 0x00) &&
509 (ptfunxored[k+2] == 0x01)) {
515 lengthofname = ptfunxored[k+12];
516 if (ptfunxored[k+13] == 0x5a) {
520 char name[256] = {0};
521 for (j = 0; j < lengthofname; j++) {
522 name[j] = ptfunxored[k+13+j];
525 regionspertrack = ptfunxored[k+13+j+3];
526 for (i = 0; i < regionspertrack; i++) {
528 if ( (ptfunxored[k ] == 0x5a) &&
529 (ptfunxored[k+1] == 0x00) &&
530 (ptfunxored[k+2] == 0x03)) {
536 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
537 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
538 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
539 //somethingbytes = (ptfunxored[j+1] & 0xf);
540 findex = ptfunxored[k+14];
543 switch (startbytes) {
545 start |= (uint32_t)(ptfunxored[j+8] << 24);
547 start |= (uint32_t)(ptfunxored[j+7] << 16);
549 start |= (uint32_t)(ptfunxored[j+6] << 8);
551 start |= (uint32_t)(ptfunxored[j+5]);
557 switch (lengthbytes) {
559 length |= (uint32_t)(ptfunxored[j+8] << 24);
561 length |= (uint32_t)(ptfunxored[j+7] << 16);
563 length |= (uint32_t)(ptfunxored[j+6] << 8);
565 length |= (uint32_t)(ptfunxored[j+5]);
570 uint32_t sampleoffset = 0;
571 switch (offsetbytes) {
573 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
575 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
577 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
579 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
585 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
587 std::string filename = string(name) + extension;
591 (int64_t)(start*ratefactor),
592 (int64_t)(length*ratefactor),
595 vector<wav_t>::iterator begin = audiofiles.begin();
596 vector<wav_t>::iterator finish = audiofiles.end();
597 vector<wav_t>::iterator found;
599 if ((found = std::find(begin, finish, f)) != finish) {
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,
629 (int64_t)(start*ratefactor),
630 (int64_t)(sampleoffset*ratefactor),
631 (int64_t)(length*ratefactor),
634 regions.push_back(r);
635 vector<track_t>::iterator ti;
636 vector<track_t>::iterator bt = tracks.begin();
637 vector<track_t>::iterator et = tracks.end();
638 track_t tr = { name, 0, 0, r };
639 if ((ti = std::find(bt, et, tr)) != et) {
640 tracknumber = (*ti).index;
642 tracknumber = tracks.size() + 1;
646 (uint16_t)tracknumber,
660 PTFFormat::resort(std::vector<wav_t>& ws) {
662 std::sort(ws.begin(), ws.end());
663 for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
670 PTFFormat::parseaudio5(void) {
672 uint64_t lengthofname, wavnumber;
674 // Find end of wav file list
677 if ( (ptfunxored[k ] == 0x5f) &&
678 (ptfunxored[k+1] == 0x50) &&
679 (ptfunxored[k+2] == 0x35)) {
686 if ( (ptfunxored[k ] == 0x5f) &&
687 (ptfunxored[k+1] == 0x50) &&
688 (ptfunxored[k+2] == 0x35)) {
694 // Find actual wav names
695 uint16_t numberofwavs = ptfunxored[k-23];
697 for (i = k; i < len; i++) {
698 if ( (ptfunxored[i ] == 'F') &&
699 (ptfunxored[i+1] == 'i') &&
700 (ptfunxored[i+2] == 'l') &&
701 (ptfunxored[i+3] == 'e') &&
702 (ptfunxored[i+4] == 's')) {
710 while (i < len && numberofwavs > 0) {
712 if ( (ptfunxored[i ] == 0x5a) &&
713 (ptfunxored[i+1] == 0x00) &&
714 (ptfunxored[i+2] == 0x05)) {
717 lengthofname = ptfunxored[i];
720 while (l < lengthofname) {
721 wavname[l] = ptfunxored[i+l];
725 ext[0] = ptfunxored[i++];
726 ext[1] = ptfunxored[i++];
727 ext[2] = ptfunxored[i++];
728 ext[3] = ptfunxored[i++];
732 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
733 extension = string("");
734 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
735 extension = string(".wav");
736 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
737 extension = string(".aif");
739 extension = string("");
742 std::string wave = string(wavname);
743 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
745 if (foundin(wave, string(".grp"))) {
749 actualwavs.push_back(f);
750 audiofiles.push_back(f);
760 PTFFormat::parseaudio(void) {
763 // Find end of wav file list
766 if ( (ptfunxored[k ] == 0xff) &&
767 (ptfunxored[k+1] == 0xff) &&
768 (ptfunxored[k+2] == 0xff) &&
769 (ptfunxored[k+3] == 0xff)) {
775 // Find actual wav names
777 uint16_t numberofwavs;
779 for (i = k; i > 4; i--) {
780 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
781 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
782 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
783 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
786 while (ptfunxored[j] != '\0') {
787 wavname[l] = ptfunxored[j];
792 if (ptfunxored[i] == 'W') {
793 extension = string(".wav");
795 extension = string(".aif");
797 //uint8_t playlist = ptfunxored[j-8];
801 for (j = k; j > 4; j--) {
802 if ( (ptfunxored[j ] == 0x01) &&
803 (ptfunxored[j-1] == 0x5a)) {
806 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
807 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
808 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
809 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
810 //printf("%d wavs\n", numberofwavs);
817 std::string wave = string(wavname);
818 std::reverse(wave.begin(), wave.end());
819 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
821 if (foundin(wave, string(".grp"))) {
825 actualwavs.push_back(f);
828 if (numberofwavs <= 0)
835 PTFFormat::parserest89(void) {
838 uint8_t startbytes = 0;
839 uint8_t lengthbytes = 0;
840 uint8_t offsetbytes = 0;
841 uint8_t somethingbytes = 0;
842 uint8_t skipbytes = 0;
846 if ( (ptfunxored[k ] == 'S') &&
847 (ptfunxored[k+1] == 'n') &&
848 (ptfunxored[k+2] == 'a') &&
849 (ptfunxored[k+3] == 'p')) {
856 for (i = k; i < len-70; i++) {
857 if ( (ptfunxored[i ] == 0x5a) &&
858 (ptfunxored[i+1] == 0x0a)) {
861 if ( (ptfunxored[i ] == 0x5a) &&
862 (ptfunxored[i+1] == 0x0c)) {
864 uint8_t lengthofname = ptfunxored[i+9];
866 char name[256] = {0};
867 for (j = 0; j < lengthofname; j++) {
868 name[j] = ptfunxored[i+13+j];
872 //uint8_t disabled = ptfunxored[j];
874 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
875 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
876 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
877 somethingbytes = (ptfunxored[j+3] & 0xf);
878 skipbytes = ptfunxored[j+4];
879 findex = ptfunxored[j+5
886 /*rindex = ptfunxored[j+5
894 uint32_t sampleoffset = 0;
895 switch (offsetbytes) {
897 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
899 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
901 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
903 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
909 switch (lengthbytes) {
911 length |= (uint32_t)(ptfunxored[j+8] << 24);
913 length |= (uint32_t)(ptfunxored[j+7] << 16);
915 length |= (uint32_t)(ptfunxored[j+6] << 8);
917 length |= (uint32_t)(ptfunxored[j+5]);
923 switch (startbytes) {
925 start |= (uint32_t)(ptfunxored[j+8] << 24);
927 start |= (uint32_t)(ptfunxored[j+7] << 16);
929 start |= (uint32_t)(ptfunxored[j+6] << 8);
931 start |= (uint32_t)(ptfunxored[j+5]);
937 uint32_t something = 0;
938 switch (somethingbytes) {
940 something |= (uint32_t)(ptfunxored[j+8] << 24);
942 something |= (uint32_t)(ptfunxored[j+7] << 16);
944 something |= (uint32_t)(ptfunxored[j+6] << 8);
946 something |= (uint32_t)(ptfunxored[j+5]);
952 std::string filename = string(name) + extension;
956 (int64_t)(start*ratefactor),
957 (int64_t)(length*ratefactor),
961 //printf("something=%d\n", something);
963 vector<wav_t>::iterator begin = actualwavs.begin();
964 vector<wav_t>::iterator finish = actualwavs.end();
965 vector<wav_t>::iterator found;
966 // Add file to list only if it is an actual wav
967 if ((found = std::find(begin, finish, f)) != finish) {
968 audiofiles.push_back(f);
969 // Also add plain wav as region
973 (int64_t)(start*ratefactor),
974 (int64_t)(sampleoffset*ratefactor),
975 (int64_t)(length*ratefactor),
978 regions.push_back(r);
981 if (foundin(filename, string(".grp"))) {
987 (int64_t)(start*ratefactor),
988 (int64_t)(sampleoffset*ratefactor),
989 (int64_t)(length*ratefactor),
992 regions.push_back(r);
999 if ( (ptfunxored[k ] == 0x5a) &&
1000 (ptfunxored[k+1] == 0x03)) {
1006 if ( (ptfunxored[k ] == 0x5a) &&
1007 (ptfunxored[k+1] == 0x02)) {
1016 uint32_t tracknumber = 0;
1017 uint32_t regionspertrack = 0;
1018 for (;k < len; k++) {
1019 if ( (ptfunxored[k ] == 0x5a) &&
1020 (ptfunxored[k+1] == 0x04)) {
1023 if ( (ptfunxored[k ] == 0x5a) &&
1024 (ptfunxored[k+1] == 0x02)) {
1026 uint8_t lengthofname = 0;
1027 lengthofname = ptfunxored[k+9];
1028 if (lengthofname == 0x5a) {
1033 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1035 //printf("regions/track=%d\n", regionspertrack);
1036 char name[256] = {0};
1037 for (j = 0; j < lengthofname; j++) {
1038 name[j] = ptfunxored[j+k+13];
1041 tr.name = string(name);
1042 tr.index = tracknumber++;
1044 for (j = k; regionspertrack > 0 && j < len; j++) {
1045 for (l = j; l < len; l++) {
1046 if ( (ptfunxored[l ] == 0x5a) &&
1047 (ptfunxored[l+1] == 0x07)) {
1054 if (regionspertrack == 0) {
1055 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1059 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1060 vector<region_t>::iterator begin = regions.begin();
1061 vector<region_t>::iterator finish = regions.end();
1062 vector<region_t>::iterator found;
1063 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1068 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1069 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1070 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1071 offset |= (uint32_t)(ptfunxored[i]);
1072 tr.reg.startpos = (int64_t)(offset*ratefactor);
1073 if (tr.reg.length > 0) {
1074 tracks.push_back(tr);
1084 PTFFormat::parserest10(void) {
1087 uint8_t startbytes = 0;
1088 uint8_t lengthbytes = 0;
1089 uint8_t offsetbytes = 0;
1090 uint8_t somethingbytes = 0;
1091 uint8_t skipbytes = 0;
1095 if ( (ptfunxored[k ] == 'S') &&
1096 (ptfunxored[k+1] == 'n') &&
1097 (ptfunxored[k+2] == 'a') &&
1098 (ptfunxored[k+3] == 'p')) {
1103 for (i = k; i < len-70; i++) {
1104 if ( (ptfunxored[i ] == 0x5a) &&
1105 (ptfunxored[i+1] == 0x02)) {
1111 for (i = k; i < len-70; i++) {
1112 if ( (ptfunxored[i ] == 0x5a) &&
1113 (ptfunxored[i+1] == 0x02)) {
1119 uint16_t rindex = 0;
1120 uint32_t findex = 0;
1121 for (i = k; i < len-70; i++) {
1122 if ( (ptfunxored[i ] == 0x5a) &&
1123 (ptfunxored[i+1] == 0x08)) {
1126 if ( (ptfunxored[i ] == 0x5a) &&
1127 (ptfunxored[i+1] == 0x01)) {
1129 uint8_t lengthofname = ptfunxored[i+9];
1130 if (ptfunxored[i+13] == 0x5a) {
1133 char name[256] = {0};
1134 for (j = 0; j < lengthofname; j++) {
1135 name[j] = ptfunxored[i+13+j];
1139 //uint8_t disabled = ptfunxored[j];
1140 //printf("%s\n", name);
1142 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1143 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1144 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1145 somethingbytes = (ptfunxored[j+3] & 0xf);
1146 skipbytes = ptfunxored[j+4];
1147 findex = ptfunxored[j+5
1154 /*rindex = ptfunxored[j+5
1162 uint32_t sampleoffset = 0;
1163 switch (offsetbytes) {
1165 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1167 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1169 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1171 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1176 uint32_t length = 0;
1177 switch (lengthbytes) {
1179 length |= (uint32_t)(ptfunxored[j+8] << 24);
1181 length |= (uint32_t)(ptfunxored[j+7] << 16);
1183 length |= (uint32_t)(ptfunxored[j+6] << 8);
1185 length |= (uint32_t)(ptfunxored[j+5]);
1191 switch (startbytes) {
1193 start |= (uint32_t)(ptfunxored[j+8] << 24);
1195 start |= (uint32_t)(ptfunxored[j+7] << 16);
1197 start |= (uint32_t)(ptfunxored[j+6] << 8);
1199 start |= (uint32_t)(ptfunxored[j+5]);
1205 uint32_t something = 0;
1206 switch (somethingbytes) {
1208 something |= (uint32_t)(ptfunxored[j+8] << 24);
1210 something |= (uint32_t)(ptfunxored[j+7] << 16);
1212 something |= (uint32_t)(ptfunxored[j+6] << 8);
1214 something |= (uint32_t)(ptfunxored[j+5]);
1220 std::string filename = string(name) + extension;
1224 (int64_t)(start*ratefactor),
1225 (int64_t)(length*ratefactor),
1228 if (strlen(name) == 0) {
1235 //printf("something=%d\n", something);
1237 vector<wav_t>::iterator begin = actualwavs.begin();
1238 vector<wav_t>::iterator finish = actualwavs.end();
1239 vector<wav_t>::iterator found;
1240 // Add file to list only if it is an actual wav
1241 if ((found = std::find(begin, finish, f)) != finish) {
1242 audiofiles.push_back(f);
1243 // Also add plain wav as region
1247 (int64_t)(start*ratefactor),
1248 (int64_t)(sampleoffset*ratefactor),
1249 (int64_t)(length*ratefactor),
1252 regions.push_back(r);
1255 if (foundin(filename, string(".grp"))) {
1261 (int64_t)(start*ratefactor),
1262 (int64_t)(sampleoffset*ratefactor),
1263 (int64_t)(length*ratefactor),
1266 regions.push_back(r);
1269 //printf("%s\n", name);
1274 uint32_t tracknumber = 0;
1275 uint32_t regionspertrack = 0;
1276 for (;k < len; k++) {
1277 if ( (ptfunxored[k ] == 0x5a) &&
1278 (ptfunxored[k+1] == 0x08)) {
1283 for (;k < len; k++) {
1284 if ( (ptfunxored[k ] == 0x5a) &&
1285 (ptfunxored[k+1] == 0x04)) {
1288 if ( (ptfunxored[k ] == 0x5a) &&
1289 (ptfunxored[k+1] == 0x02)) {
1291 uint8_t lengthofname = 0;
1292 lengthofname = ptfunxored[k+9];
1293 if (lengthofname == 0x5a) {
1298 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1300 //printf("regions/track=%d\n", regionspertrack);
1301 char name[256] = {0};
1302 for (j = 0; j < lengthofname; j++) {
1303 name[j] = ptfunxored[j+k+13];
1306 tr.name = string(name);
1307 tr.index = tracknumber++;
1309 for (j = k; regionspertrack > 0 && j < len; j++) {
1310 for (l = j; l < len; l++) {
1311 if ( (ptfunxored[l ] == 0x5a) &&
1312 (ptfunxored[l+1] == 0x08)) {
1319 if (regionspertrack == 0) {
1320 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1324 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1325 vector<region_t>::iterator begin = regions.begin();
1326 vector<region_t>::iterator finish = regions.end();
1327 vector<region_t>::iterator found;
1328 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1333 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1334 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1335 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1336 offset |= (uint32_t)(ptfunxored[i]);
1337 tr.reg.startpos = (int64_t)(offset*ratefactor);
1338 if (tr.reg.length > 0) {
1339 tracks.push_back(tr);