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.
22 #include <glib/gstdio.h>
24 #include "ptfformat.h"
28 static const uint32_t baselut[16] = {
29 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
30 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
31 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
32 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
35 static const uint32_t xorlut[16] = {
36 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
37 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
38 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
39 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
42 static uint32_t swapbytes32 (const uint32_t v) {
44 rv |= ((v >> 0) & 0xf) << 28;
45 rv |= ((v >> 4) & 0xf) << 24;
46 rv |= ((v >> 8) & 0xf) << 20;
47 rv |= ((v >> 12) & 0xf) << 16;
48 rv |= ((v >> 16) & 0xf) << 12;
49 rv |= ((v >> 20) & 0xf) << 8;
50 rv |= ((v >> 24) & 0xf) << 4;
51 rv |= ((v >> 28) & 0xf) << 0;
55 static uint64_t gen_secret (int i) {
56 assert (i > 0 && i < 256);
57 int iwrap = i & 0x7f; // wrap at 0x80;
58 uint32_t xor_lo = 0; // 0x40 flag
59 int idx; // mirror at 0x40;
68 int i16 = (idx >> 1) & 0xf;
73 uint32_t lo = baselut [i16];
74 uint32_t xk = xorlut [i16];
80 uint32_t hi = swapbytes32 (lo) ^ xk;
81 return ((uint64_t)hi << 32) | (lo ^ xor_lo);
84 PTFFormat::PTFFormat() {
87 PTFFormat::~PTFFormat() {
94 PTFFormat::foundin(std::string haystack, std::string needle) {
95 size_t found = haystack.find(needle);
96 if (found != std::string::npos) {
103 /* Return values: 0 success
104 0x01 to 0xff value of missing lut
105 -1 could not open file as ptf
108 PTFFormat::load(std::string path, int64_t targetsr) {
110 unsigned char xxor[256];
120 if (! (fp = g_fopen(path.c_str(), "rb"))) {
124 fseek(fp, 0, SEEK_END);
130 fseek(fp, 0x40, SEEK_SET);
131 fread(&c0, 1, 1, fp);
132 fread(&c1, 1, 1, fp);
134 // For version <= 7 support:
138 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
139 /* Silently fail -- out of memory*/
150 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
152 for (i = 2; i < 256; i++) {
156 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
157 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
165 for (i = 2; i < 256; i++) {
169 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
172 for (i = 0; i < 64; i++) {
175 for (i = 128; i < 192; i++) {
183 for (i = 2; i < 256; i++) {
187 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
191 key = gen_secret(c1);
193 for (i = 0; i < 64; i++) {
194 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
196 for (i = 128; i < 192; i++) {
197 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
198 xxor[i] ^= (inv * 0x40);
201 for (i = 192; i < 256; i++) {
207 //Should not happen, failed c[0] c[1]
214 fseek(fp, 0, SEEK_SET);
215 while (fread(&ct, 1, 1, fp) != 0) {
216 ptfunxored[i++] = ct;
220 /* version detection */
222 v = ptfunxored[voff];
225 } else if (v == 0x03) {
230 v = ptfunxored[voff];
231 if (v == 10 || v == 11 || v == 12) {
236 if (version == 0 || version == 5 || version == 7) {
237 /* Haven't detected version yet so decipher */
239 for (i = 0; i < len; i++) {
243 ptfunxored[i] ^= xxor[j];
247 /* version detection */
249 v = ptfunxored[voff];
252 } else if (v == 0x03) {
257 v = ptfunxored[voff];
258 if (v == 5 || v == 7 || v == 8 || v == 9) {
263 if (version < 5 || version > 12)
265 targetrate = targetsr;
273 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
275 uint32_t counts[256] = {0};
280 for (i = start; i < stop; i++) {
281 counts[ptfunxored[i]]++;
284 for (i = 0; i < 256; i++) {
285 if (counts[i] > max) {
294 PTFFormat::unxor10(void)
297 uint8_t x = mostfrequent(0x1000, 0x2000);
298 uint8_t dx = 0x100-x;
300 for (j = 0x1000; j < len; j++) {
301 if(j % 0x1000 == 0xfff) {
309 PTFFormat::parse(void) {
313 if (sessionrate < 44100 || sessionrate > 192000)
317 } else if (version == 7) {
320 if (sessionrate < 44100 || sessionrate > 192000)
324 } else if (version == 8) {
327 if (sessionrate < 44100 || sessionrate > 192000)
331 } else if (version == 9) {
334 if (sessionrate < 44100 || sessionrate > 192000)
338 } else if (version == 10 || version == 11 || version == 12) {
341 if (sessionrate < 44100 || sessionrate > 192000)
353 PTFFormat::setrates(void) {
355 if (sessionrate != 0) {
356 ratefactor = (float)targetrate / sessionrate;
361 PTFFormat::parse5header(void) {
364 // Find session sample rate
367 if ( (ptfunxored[k ] == 0x5a) &&
368 (ptfunxored[k+1] == 0x00) &&
369 (ptfunxored[k+2] == 0x02)) {
376 sessionrate |= ptfunxored[k+12] << 16;
377 sessionrate |= ptfunxored[k+13] << 8;
378 sessionrate |= ptfunxored[k+14];
382 PTFFormat::parse7header(void) {
385 // Find session sample rate
388 if ( (ptfunxored[k ] == 0x5a) &&
389 (ptfunxored[k+1] == 0x00) &&
390 (ptfunxored[k+2] == 0x05)) {
397 sessionrate |= ptfunxored[k+12] << 16;
398 sessionrate |= ptfunxored[k+13] << 8;
399 sessionrate |= ptfunxored[k+14];
403 PTFFormat::parse8header(void) {
406 // Find session sample rate
409 if ( (ptfunxored[k ] == 0x5a) &&
410 (ptfunxored[k+1] == 0x05)) {
417 sessionrate |= ptfunxored[k+11];
418 sessionrate |= ptfunxored[k+12] << 8;
419 sessionrate |= ptfunxored[k+13] << 16;
423 PTFFormat::parse9header(void) {
426 // Find session sample rate
429 if ( (ptfunxored[k ] == 0x5a) &&
430 (ptfunxored[k+1] == 0x06)) {
437 sessionrate |= ptfunxored[k+11];
438 sessionrate |= ptfunxored[k+12] << 8;
439 sessionrate |= ptfunxored[k+13] << 16;
443 PTFFormat::parse10header(void) {
446 // Find session sample rate
449 if ( (ptfunxored[k ] == 0x5a) &&
450 (ptfunxored[k+1] == 0x09)) {
457 sessionrate |= ptfunxored[k+11];
458 sessionrate |= ptfunxored[k+12] << 8;
459 sessionrate |= ptfunxored[k+13] << 16;
463 PTFFormat::parserest5(void) {
465 uint64_t regionspertrack, lengthofname;
466 uint64_t startbytes, lengthbytes, offsetbytes;
467 uint16_t tracknumber = 0;
472 for (i = 0; i < 5; i++) {
474 if ( (ptfunxored[k ] == 0x5a) &&
475 (ptfunxored[k+1] == 0x00) &&
476 (ptfunxored[k+2] == 0x03)) {
485 for (i = 0; i < 2; i++) {
487 if ( (ptfunxored[k ] == 0x5a) &&
488 (ptfunxored[k+1] == 0x00) &&
489 (ptfunxored[k+2] == 0x01)) {
501 if ( (ptfunxored[k ] == 0xff) &&
502 (ptfunxored[k+1] == 0xff)) {
506 if ( (ptfunxored[k ] == 0x5a) &&
507 (ptfunxored[k+1] == 0x00) &&
508 (ptfunxored[k+2] == 0x01)) {
514 lengthofname = ptfunxored[k+12];
515 if (ptfunxored[k+13] == 0x5a) {
519 char name[256] = {0};
520 for (j = 0; j < lengthofname; j++) {
521 name[j] = ptfunxored[k+13+j];
524 regionspertrack = ptfunxored[k+13+j+3];
525 for (i = 0; i < regionspertrack; i++) {
527 if ( (ptfunxored[k ] == 0x5a) &&
528 (ptfunxored[k+1] == 0x00) &&
529 (ptfunxored[k+2] == 0x03)) {
535 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
536 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
537 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
538 //somethingbytes = (ptfunxored[j+1] & 0xf);
539 findex = ptfunxored[k+14];
542 switch (startbytes) {
544 start |= (uint32_t)(ptfunxored[j+8] << 24);
546 start |= (uint32_t)(ptfunxored[j+7] << 16);
548 start |= (uint32_t)(ptfunxored[j+6] << 8);
550 start |= (uint32_t)(ptfunxored[j+5]);
556 switch (lengthbytes) {
558 length |= (uint32_t)(ptfunxored[j+8] << 24);
560 length |= (uint32_t)(ptfunxored[j+7] << 16);
562 length |= (uint32_t)(ptfunxored[j+6] << 8);
564 length |= (uint32_t)(ptfunxored[j+5]);
569 uint32_t sampleoffset = 0;
570 switch (offsetbytes) {
572 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
574 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
576 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
578 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
584 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
586 std::string filename = string(name) + extension;
590 (int64_t)(start*ratefactor),
591 (int64_t)(length*ratefactor),
594 vector<wav_t>::iterator begin = audiofiles.begin();
595 vector<wav_t>::iterator finish = audiofiles.end();
596 vector<wav_t>::iterator found;
598 if ((found = std::find(begin, finish, f)) != finish) {
602 (int64_t)(start*ratefactor),
603 (int64_t)(sampleoffset*ratefactor),
604 (int64_t)(length*ratefactor),
607 regions.push_back(r);
608 vector<track_t>::iterator ti;
609 vector<track_t>::iterator bt = tracks.begin();
610 vector<track_t>::iterator et = tracks.end();
611 track_t tr = { name, 0, 0, r };
612 if ((ti = std::find(bt, et, tr)) != et) {
613 tracknumber = (*ti).index;
615 tracknumber = tracks.size() + 1;
619 (uint16_t)tracknumber,
628 (int64_t)(start*ratefactor),
629 (int64_t)(sampleoffset*ratefactor),
630 (int64_t)(length*ratefactor),
633 regions.push_back(r);
634 vector<track_t>::iterator ti;
635 vector<track_t>::iterator bt = tracks.begin();
636 vector<track_t>::iterator et = tracks.end();
637 track_t tr = { name, 0, 0, r };
638 if ((ti = std::find(bt, et, tr)) != et) {
639 tracknumber = (*ti).index;
641 tracknumber = tracks.size() + 1;
645 (uint16_t)tracknumber,
659 PTFFormat::resort(std::vector<wav_t>& ws) {
661 std::sort(ws.begin(), ws.end());
662 for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
669 PTFFormat::parseaudio5(void) {
671 uint64_t lengthofname, wavnumber;
673 // Find end of wav file list
676 if ( (ptfunxored[k ] == 0x5f) &&
677 (ptfunxored[k+1] == 0x50) &&
678 (ptfunxored[k+2] == 0x35)) {
685 if ( (ptfunxored[k ] == 0x5f) &&
686 (ptfunxored[k+1] == 0x50) &&
687 (ptfunxored[k+2] == 0x35)) {
693 // Find actual wav names
694 uint16_t numberofwavs = ptfunxored[k-23];
696 for (i = k; i < len; i++) {
697 if ( (ptfunxored[i ] == 'F') &&
698 (ptfunxored[i+1] == 'i') &&
699 (ptfunxored[i+2] == 'l') &&
700 (ptfunxored[i+3] == 'e') &&
701 (ptfunxored[i+4] == 's')) {
709 while (i < len && numberofwavs > 0) {
711 if ( (ptfunxored[i ] == 0x5a) &&
712 (ptfunxored[i+1] == 0x00) &&
713 (ptfunxored[i+2] == 0x05)) {
716 lengthofname = ptfunxored[i];
719 while (l < lengthofname) {
720 wavname[l] = ptfunxored[i+l];
724 ext[0] = ptfunxored[i++];
725 ext[1] = ptfunxored[i++];
726 ext[2] = ptfunxored[i++];
727 ext[3] = ptfunxored[i++];
731 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
732 extension = string("");
733 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
734 extension = string(".wav");
735 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
736 extension = string(".aif");
738 extension = string("");
741 std::string wave = string(wavname);
742 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
744 if (foundin(wave, string(".grp"))) {
748 actualwavs.push_back(f);
749 audiofiles.push_back(f);
759 PTFFormat::parseaudio(void) {
762 // Find end of wav file list
765 if ( (ptfunxored[k ] == 0xff) &&
766 (ptfunxored[k+1] == 0xff) &&
767 (ptfunxored[k+2] == 0xff) &&
768 (ptfunxored[k+3] == 0xff)) {
774 // Find actual wav names
776 uint16_t numberofwavs;
778 for (i = k; i > 4; i--) {
779 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
780 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
781 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
782 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
785 while (ptfunxored[j] != '\0') {
786 wavname[l] = ptfunxored[j];
791 if (ptfunxored[i] == 'W') {
792 extension = string(".wav");
794 extension = string(".aif");
796 //uint8_t playlist = ptfunxored[j-8];
800 for (j = k; j > 4; j--) {
801 if ( (ptfunxored[j ] == 0x01) &&
802 (ptfunxored[j-1] == 0x5a)) {
805 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
806 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
807 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
808 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
809 //printf("%d wavs\n", numberofwavs);
816 std::string wave = string(wavname);
817 std::reverse(wave.begin(), wave.end());
818 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
820 if (foundin(wave, string(".grp"))) {
824 actualwavs.push_back(f);
827 if (numberofwavs <= 0)
834 PTFFormat::parserest89(void) {
837 uint8_t startbytes = 0;
838 uint8_t lengthbytes = 0;
839 uint8_t offsetbytes = 0;
840 uint8_t somethingbytes = 0;
841 uint8_t skipbytes = 0;
845 if ( (ptfunxored[k ] == 'S') &&
846 (ptfunxored[k+1] == 'n') &&
847 (ptfunxored[k+2] == 'a') &&
848 (ptfunxored[k+3] == 'p')) {
855 for (i = k; i < len-70; i++) {
856 if ( (ptfunxored[i ] == 0x5a) &&
857 (ptfunxored[i+1] == 0x0a)) {
860 if ( (ptfunxored[i ] == 0x5a) &&
861 (ptfunxored[i+1] == 0x0c)) {
863 uint8_t lengthofname = ptfunxored[i+9];
865 char name[256] = {0};
866 for (j = 0; j < lengthofname; j++) {
867 name[j] = ptfunxored[i+13+j];
871 //uint8_t disabled = ptfunxored[j];
873 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
874 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
875 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
876 somethingbytes = (ptfunxored[j+3] & 0xf);
877 skipbytes = ptfunxored[j+4];
878 findex = ptfunxored[j+5
885 /*rindex = ptfunxored[j+5
893 uint32_t sampleoffset = 0;
894 switch (offsetbytes) {
896 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
898 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
900 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
902 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
908 switch (lengthbytes) {
910 length |= (uint32_t)(ptfunxored[j+8] << 24);
912 length |= (uint32_t)(ptfunxored[j+7] << 16);
914 length |= (uint32_t)(ptfunxored[j+6] << 8);
916 length |= (uint32_t)(ptfunxored[j+5]);
922 switch (startbytes) {
924 start |= (uint32_t)(ptfunxored[j+8] << 24);
926 start |= (uint32_t)(ptfunxored[j+7] << 16);
928 start |= (uint32_t)(ptfunxored[j+6] << 8);
930 start |= (uint32_t)(ptfunxored[j+5]);
936 uint32_t something = 0;
937 switch (somethingbytes) {
939 something |= (uint32_t)(ptfunxored[j+8] << 24);
941 something |= (uint32_t)(ptfunxored[j+7] << 16);
943 something |= (uint32_t)(ptfunxored[j+6] << 8);
945 something |= (uint32_t)(ptfunxored[j+5]);
951 std::string filename = string(name) + extension;
955 (int64_t)(start*ratefactor),
956 (int64_t)(length*ratefactor),
960 //printf("something=%d\n", something);
962 vector<wav_t>::iterator begin = actualwavs.begin();
963 vector<wav_t>::iterator finish = actualwavs.end();
964 vector<wav_t>::iterator found;
965 // Add file to list only if it is an actual wav
966 if ((found = std::find(begin, finish, f)) != finish) {
967 audiofiles.push_back(f);
968 // Also add plain wav as region
972 (int64_t)(start*ratefactor),
973 (int64_t)(sampleoffset*ratefactor),
974 (int64_t)(length*ratefactor),
977 regions.push_back(r);
980 if (foundin(filename, string(".grp"))) {
986 (int64_t)(start*ratefactor),
987 (int64_t)(sampleoffset*ratefactor),
988 (int64_t)(length*ratefactor),
991 regions.push_back(r);
998 if ( (ptfunxored[k ] == 0x5a) &&
999 (ptfunxored[k+1] == 0x03)) {
1005 if ( (ptfunxored[k ] == 0x5a) &&
1006 (ptfunxored[k+1] == 0x02)) {
1015 uint32_t tracknumber = 0;
1016 uint32_t regionspertrack = 0;
1017 for (;k < len; k++) {
1018 if ( (ptfunxored[k ] == 0x5a) &&
1019 (ptfunxored[k+1] == 0x04)) {
1022 if ( (ptfunxored[k ] == 0x5a) &&
1023 (ptfunxored[k+1] == 0x02)) {
1025 uint8_t lengthofname = 0;
1026 lengthofname = ptfunxored[k+9];
1027 if (lengthofname == 0x5a) {
1032 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1034 //printf("regions/track=%d\n", regionspertrack);
1035 char name[256] = {0};
1036 for (j = 0; j < lengthofname; j++) {
1037 name[j] = ptfunxored[j+k+13];
1040 tr.name = string(name);
1041 tr.index = tracknumber++;
1043 for (j = k; regionspertrack > 0 && j < len; j++) {
1044 for (l = j; l < len; l++) {
1045 if ( (ptfunxored[l ] == 0x5a) &&
1046 (ptfunxored[l+1] == 0x07)) {
1053 if (regionspertrack == 0) {
1054 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1058 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1059 vector<region_t>::iterator begin = regions.begin();
1060 vector<region_t>::iterator finish = regions.end();
1061 vector<region_t>::iterator found;
1062 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1067 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1068 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1069 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1070 offset |= (uint32_t)(ptfunxored[i]);
1071 tr.reg.startpos = (int64_t)(offset*ratefactor);
1072 if (tr.reg.length > 0) {
1073 tracks.push_back(tr);
1083 PTFFormat::parserest10(void) {
1086 uint8_t startbytes = 0;
1087 uint8_t lengthbytes = 0;
1088 uint8_t offsetbytes = 0;
1089 uint8_t somethingbytes = 0;
1090 uint8_t skipbytes = 0;
1094 if ( (ptfunxored[k ] == 'S') &&
1095 (ptfunxored[k+1] == 'n') &&
1096 (ptfunxored[k+2] == 'a') &&
1097 (ptfunxored[k+3] == 'p')) {
1102 for (i = k; i < len-70; i++) {
1103 if ( (ptfunxored[i ] == 0x5a) &&
1104 (ptfunxored[i+1] == 0x02)) {
1110 for (i = k; i < len-70; i++) {
1111 if ( (ptfunxored[i ] == 0x5a) &&
1112 (ptfunxored[i+1] == 0x02)) {
1118 uint16_t rindex = 0;
1119 uint32_t findex = 0;
1120 for (i = k; i < len-70; i++) {
1121 if ( (ptfunxored[i ] == 0x5a) &&
1122 (ptfunxored[i+1] == 0x08)) {
1125 if ( (ptfunxored[i ] == 0x5a) &&
1126 (ptfunxored[i+1] == 0x01)) {
1128 uint8_t lengthofname = ptfunxored[i+9];
1129 if (ptfunxored[i+13] == 0x5a) {
1132 char name[256] = {0};
1133 for (j = 0; j < lengthofname; j++) {
1134 name[j] = ptfunxored[i+13+j];
1138 //uint8_t disabled = ptfunxored[j];
1139 //printf("%s\n", name);
1141 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1142 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1143 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1144 somethingbytes = (ptfunxored[j+3] & 0xf);
1145 skipbytes = ptfunxored[j+4];
1146 findex = ptfunxored[j+5
1153 /*rindex = ptfunxored[j+5
1161 uint32_t sampleoffset = 0;
1162 switch (offsetbytes) {
1164 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1166 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1168 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1170 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1175 uint32_t length = 0;
1176 switch (lengthbytes) {
1178 length |= (uint32_t)(ptfunxored[j+8] << 24);
1180 length |= (uint32_t)(ptfunxored[j+7] << 16);
1182 length |= (uint32_t)(ptfunxored[j+6] << 8);
1184 length |= (uint32_t)(ptfunxored[j+5]);
1190 switch (startbytes) {
1192 start |= (uint32_t)(ptfunxored[j+8] << 24);
1194 start |= (uint32_t)(ptfunxored[j+7] << 16);
1196 start |= (uint32_t)(ptfunxored[j+6] << 8);
1198 start |= (uint32_t)(ptfunxored[j+5]);
1204 uint32_t something = 0;
1205 switch (somethingbytes) {
1207 something |= (uint32_t)(ptfunxored[j+8] << 24);
1209 something |= (uint32_t)(ptfunxored[j+7] << 16);
1211 something |= (uint32_t)(ptfunxored[j+6] << 8);
1213 something |= (uint32_t)(ptfunxored[j+5]);
1219 std::string filename = string(name) + extension;
1223 (int64_t)(start*ratefactor),
1224 (int64_t)(length*ratefactor),
1227 if (strlen(name) == 0) {
1234 //printf("something=%d\n", something);
1236 vector<wav_t>::iterator begin = actualwavs.begin();
1237 vector<wav_t>::iterator finish = actualwavs.end();
1238 vector<wav_t>::iterator found;
1239 // Add file to list only if it is an actual wav
1240 if ((found = std::find(begin, finish, f)) != finish) {
1241 audiofiles.push_back(f);
1242 // Also add plain wav as region
1246 (int64_t)(start*ratefactor),
1247 (int64_t)(sampleoffset*ratefactor),
1248 (int64_t)(length*ratefactor),
1251 regions.push_back(r);
1254 if (foundin(filename, string(".grp"))) {
1260 (int64_t)(start*ratefactor),
1261 (int64_t)(sampleoffset*ratefactor),
1262 (int64_t)(length*ratefactor),
1265 regions.push_back(r);
1268 //printf("%s\n", name);
1273 uint32_t tracknumber = 0;
1274 uint32_t regionspertrack = 0;
1275 for (;k < len; k++) {
1276 if ( (ptfunxored[k ] == 0x5a) &&
1277 (ptfunxored[k+1] == 0x08)) {
1282 for (;k < len; k++) {
1283 if ( (ptfunxored[k ] == 0x5a) &&
1284 (ptfunxored[k+1] == 0x04)) {
1287 if ( (ptfunxored[k ] == 0x5a) &&
1288 (ptfunxored[k+1] == 0x02)) {
1290 uint8_t lengthofname = 0;
1291 lengthofname = ptfunxored[k+9];
1292 if (lengthofname == 0x5a) {
1297 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1299 //printf("regions/track=%d\n", regionspertrack);
1300 char name[256] = {0};
1301 for (j = 0; j < lengthofname; j++) {
1302 name[j] = ptfunxored[j+k+13];
1305 tr.name = string(name);
1306 tr.index = tracknumber++;
1308 for (j = k; regionspertrack > 0 && j < len; j++) {
1309 for (l = j; l < len; l++) {
1310 if ( (ptfunxored[l ] == 0x5a) &&
1311 (ptfunxored[l+1] == 0x08)) {
1318 if (regionspertrack == 0) {
1319 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1323 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1324 vector<region_t>::iterator begin = regions.begin();
1325 vector<region_t>::iterator finish = regions.end();
1326 vector<region_t>::iterator found;
1327 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1332 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1333 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1334 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1335 offset |= (uint32_t)(ptfunxored[i]);
1336 tr.reg.startpos = (int64_t)(offset*ratefactor);
1337 if (tr.reg.length > 0) {
1338 tracks.push_back(tr);