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];
115 unsigned char message;
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*/
142 fseek(fp, 0x0, SEEK_SET);
149 //fprintf(stderr, "%02x %02x", c0, c1);
151 for (i = 2; i < 64; i++) {
152 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
153 //fprintf(stderr, "%02x ", xxor[i]);
156 fread(&ct, 1, 1, fp);
158 ptfunxored[0] = message;
160 fread(&ct, 1, 1, fp);
162 ptfunxored[1] = message;
165 while (fread(&ct, 1, 1, fp) != 0) {
169 message = xxor[i] ^ ct;
170 ptfunxored[j] = message;
179 for (i = 2; i < 256; i++) {
183 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
186 for (i = 0; i < 64; i++) {
189 for (i = 128; i < 192; i++) {
193 fread(&ct, 1, 1, fp);
195 ptfunxored[0] = message;
197 fread(&ct, 1, 1, fp);
199 ptfunxored[1] = message;
202 while (fread(&ct, 1, 1, fp) != 0) {
206 message = xxor[i] ^ ct;
207 ptfunxored[j] = message;
216 for (i = 2; i < 256; i++) {
220 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
224 key = gen_secret(c1);
225 for (i = 0; i < 64; i++) {
226 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
228 for (i = 128; i < 192; i++) {
229 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
230 xxor[i] ^= (inv * 0x40);
233 for (i = 192; i < 256; i++) {
237 fread(&ct, 1, 1, fp);
239 ptfunxored[0] = message;
241 fread(&ct, 1, 1, fp);
243 ptfunxored[1] = message;
246 while (fread(&ct, 1, 1, fp) != 0) {
250 message = xxor[i] ^ ct;
251 ptfunxored[j] = message;
258 //Should not happen, failed c[0] c[1]
263 targetrate = targetsr;
269 PTFFormat::unxor10(void)
271 key10a = ptfunxored[0x9f];
272 key10b = ptfunxored[0x9e] - ptfunxored[0x9b];
276 for (j = k; j < k + 0x1000 && j < len; j++) {
277 ptfunxored[j] ^= key10a;
280 for (j = k; j < k + 0x1000 && j < len; j++) {
281 ptfunxored[j] ^= key10b;
286 currkey = (key10a + currkey) & 0xff;
287 for (j = k; j < k + 0x1000 && j < len; j++) {
288 ptfunxored[j] ^= currkey;
294 PTFFormat::parse(void) {
295 version = (version == 0) ? ptfunxored[61] : version;
302 } else if (version == 7) {
307 } else if (version == 8) {
312 } else if (version == 9) {
317 } else if (version == 10) {
329 PTFFormat::setrates(void) {
331 if (sessionrate != 0) {
332 ratefactor = (float)targetrate / sessionrate;
337 PTFFormat::parse5header(void) {
340 // Find session sample rate
343 if ( (ptfunxored[k ] == 0x5a) &&
344 (ptfunxored[k+1] == 0x00) &&
345 (ptfunxored[k+2] == 0x02)) {
352 sessionrate |= ptfunxored[k+12] << 16;
353 sessionrate |= ptfunxored[k+13] << 8;
354 sessionrate |= ptfunxored[k+14];
358 PTFFormat::parse7header(void) {
361 // Find session sample rate
364 if ( (ptfunxored[k ] == 0x5a) &&
365 (ptfunxored[k+1] == 0x00) &&
366 (ptfunxored[k+2] == 0x05)) {
373 sessionrate |= ptfunxored[k+12] << 16;
374 sessionrate |= ptfunxored[k+13] << 8;
375 sessionrate |= ptfunxored[k+14];
379 PTFFormat::parse8header(void) {
382 // Find session sample rate
385 if ( (ptfunxored[k ] == 0x5a) &&
386 (ptfunxored[k+1] == 0x05)) {
393 sessionrate |= ptfunxored[k+11];
394 sessionrate |= ptfunxored[k+12] << 8;
395 sessionrate |= ptfunxored[k+13] << 16;
399 PTFFormat::parse9header(void) {
402 // Find session sample rate
405 if ( (ptfunxored[k ] == 0x5a) &&
406 (ptfunxored[k+1] == 0x06)) {
413 sessionrate |= ptfunxored[k+11];
414 sessionrate |= ptfunxored[k+12] << 8;
415 sessionrate |= ptfunxored[k+13] << 16;
419 PTFFormat::parse10header(void) {
422 // Find session sample rate
425 if ( (ptfunxored[k ] == 0x5a) &&
426 (ptfunxored[k+1] == 0x09)) {
433 sessionrate |= ptfunxored[k+11];
434 sessionrate |= ptfunxored[k+12] << 8;
435 sessionrate |= ptfunxored[k+13] << 16;
439 PTFFormat::parserest5(void) {
441 int regionspertrack, lengthofname;
442 int startbytes, lengthbytes, offsetbytes;
443 uint16_t tracknumber = 0;
448 for (i = 0; i < 5; i++) {
450 if ( (ptfunxored[k ] == 0x5a) &&
451 (ptfunxored[k+1] == 0x00) &&
452 (ptfunxored[k+2] == 0x03)) {
461 for (i = 0; i < 2; i++) {
463 if ( (ptfunxored[k ] == 0x5a) &&
464 (ptfunxored[k+1] == 0x00) &&
465 (ptfunxored[k+2] == 0x01)) {
476 if ( (ptfunxored[k ] == 0xff) &&
477 (ptfunxored[k+1] == 0xff)) {
481 if ( (ptfunxored[k ] == 0x5a) &&
482 (ptfunxored[k+1] == 0x00) &&
483 (ptfunxored[k+2] == 0x01)) {
489 lengthofname = ptfunxored[k+12];
490 if (ptfunxored[k+13] == 0x5a) {
494 char name[256] = {0};
495 for (j = 0; j < lengthofname; j++) {
496 name[j] = ptfunxored[k+13+j];
499 regionspertrack = ptfunxored[k+13+j+3];
500 for (i = 0; i < regionspertrack; i++) {
502 if ( (ptfunxored[k ] == 0x5a) &&
503 (ptfunxored[k+1] == 0x00) &&
504 (ptfunxored[k+2] == 0x03)) {
510 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
511 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
512 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
513 //somethingbytes = (ptfunxored[j+1] & 0xf);
514 findex = ptfunxored[k+14];
517 switch (startbytes) {
519 start |= (uint32_t)(ptfunxored[j+8] << 24);
521 start |= (uint32_t)(ptfunxored[j+7] << 16);
523 start |= (uint32_t)(ptfunxored[j+6] << 8);
525 start |= (uint32_t)(ptfunxored[j+5]);
531 switch (lengthbytes) {
533 length |= (uint32_t)(ptfunxored[j+8] << 24);
535 length |= (uint32_t)(ptfunxored[j+7] << 16);
537 length |= (uint32_t)(ptfunxored[j+6] << 8);
539 length |= (uint32_t)(ptfunxored[j+5]);
544 uint32_t sampleoffset = 0;
545 switch (offsetbytes) {
547 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
549 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
551 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
553 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
559 //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
561 std::string filename = string(name) + extension;
565 (int64_t)(start*ratefactor),
566 (int64_t)(length*ratefactor),
569 vector<wav_t>::iterator begin = audiofiles.begin();
570 vector<wav_t>::iterator finish = audiofiles.end();
571 vector<wav_t>::iterator found;
573 if ((found = std::find(begin, finish, f)) != finish) {
577 (int64_t)(start*ratefactor),
578 (int64_t)(sampleoffset*ratefactor),
579 (int64_t)(length*ratefactor),
582 regions.push_back(r);
583 vector<track_t>::iterator ti;
584 vector<track_t>::iterator bt = tracks.begin();
585 vector<track_t>::iterator et = tracks.end();
586 track_t tr = { name, 0, 0, r };
587 if ((ti = std::find(bt, et, tr)) != et) {
588 tracknumber = (*ti).index;
590 tracknumber = tracks.size() + 1;
594 (uint16_t)tracknumber,
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,
634 PTFFormat::resort(std::vector<wav_t> *ws) {
636 std::sort((*ws).begin(), (*ws).end());
637 for (std::vector<wav_t>::iterator i = (*ws).begin();
638 i != (*ws).end(); ++i) {
645 PTFFormat::parseaudio5(void) {
647 int lengthofname, wavnumber;
649 // Find end of wav file list
652 if ( (ptfunxored[k ] == 0x5f) &&
653 (ptfunxored[k+1] == 0x50) &&
654 (ptfunxored[k+2] == 0x35)) {
661 if ( (ptfunxored[k ] == 0x5f) &&
662 (ptfunxored[k+1] == 0x50) &&
663 (ptfunxored[k+2] == 0x35)) {
669 // Find actual wav names
670 uint16_t numberofwavs = ptfunxored[k-23];
672 for (i = k; i < len; i++) {
673 if ( (ptfunxored[i ] == 'F') &&
674 (ptfunxored[i+1] == 'i') &&
675 (ptfunxored[i+2] == 'l') &&
676 (ptfunxored[i+3] == 'e') &&
677 (ptfunxored[i+4] == 's')) {
685 while (i < len && numberofwavs > 0) {
687 if ( (ptfunxored[i ] == 0x5a) &&
688 (ptfunxored[i+1] == 0x00) &&
689 (ptfunxored[i+2] == 0x05)) {
692 lengthofname = ptfunxored[i];
695 while (l < lengthofname) {
696 wavname[l] = ptfunxored[i+l];
700 ext[0] = ptfunxored[i++];
701 ext[1] = ptfunxored[i++];
702 ext[2] = ptfunxored[i++];
703 ext[3] = ptfunxored[i++];
707 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
708 extension = string("");
709 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
710 extension = string(".wav");
711 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
712 extension = string(".aif");
714 extension = string("");
717 std::string wave = string(wavname);
718 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
720 if (foundin(wave, string(".grp"))) {
724 actualwavs.push_back(f);
725 audiofiles.push_back(f);
735 PTFFormat::parseaudio(void) {
738 // Find end of wav file list
741 if ( (ptfunxored[k ] == 0xff) &&
742 (ptfunxored[k+1] == 0xff) &&
743 (ptfunxored[k+2] == 0xff) &&
744 (ptfunxored[k+3] == 0xff)) {
750 // Find actual wav names
752 uint16_t numberofwavs;
754 for (i = k; i > 4; i--) {
755 if ( ((ptfunxored[i ] == 'W') || (ptfunxored[i ] == 'A')) &&
756 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
757 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
758 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
761 while (ptfunxored[j] != '\0') {
762 wavname[l] = ptfunxored[j];
767 if (ptfunxored[i] == 'W') {
768 extension = string(".wav");
770 extension = string(".aif");
772 //uint8_t playlist = ptfunxored[j-8];
776 for (j = k; j > 4; j--) {
777 if ( (ptfunxored[j ] == 0x01) &&
778 (ptfunxored[j-1] == 0x5a)) {
781 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
782 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
783 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
784 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
785 //printf("%d wavs\n", numberofwavs);
792 std::string wave = string(wavname);
793 std::reverse(wave.begin(), wave.end());
794 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
796 if (foundin(wave, string(".grp"))) {
800 actualwavs.push_back(f);
803 if (numberofwavs <= 0)
810 PTFFormat::parserest89(void) {
813 uint8_t startbytes = 0;
814 uint8_t lengthbytes = 0;
815 uint8_t offsetbytes = 0;
816 uint8_t somethingbytes = 0;
817 uint8_t skipbytes = 0;
821 if ( (ptfunxored[k ] == 'S') &&
822 (ptfunxored[k+1] == 'n') &&
823 (ptfunxored[k+2] == 'a') &&
824 (ptfunxored[k+3] == 'p')) {
831 for (i = k; i < len-70; i++) {
832 if ( (ptfunxored[i ] == 0x5a) &&
833 (ptfunxored[i+1] == 0x0a)) {
836 if ( (ptfunxored[i ] == 0x5a) &&
837 (ptfunxored[i+1] == 0x0c)) {
839 uint8_t lengthofname = ptfunxored[i+9];
841 char name[256] = {0};
842 for (j = 0; j < lengthofname; j++) {
843 name[j] = ptfunxored[i+13+j];
847 //uint8_t disabled = ptfunxored[j];
849 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
850 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
851 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
852 somethingbytes = (ptfunxored[j+3] & 0xf);
853 skipbytes = ptfunxored[j+4];
854 findex = ptfunxored[j+5
861 /*rindex = ptfunxored[j+5
869 uint32_t sampleoffset = 0;
870 switch (offsetbytes) {
872 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
874 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
876 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
878 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
884 switch (lengthbytes) {
886 length |= (uint32_t)(ptfunxored[j+8] << 24);
888 length |= (uint32_t)(ptfunxored[j+7] << 16);
890 length |= (uint32_t)(ptfunxored[j+6] << 8);
892 length |= (uint32_t)(ptfunxored[j+5]);
898 switch (startbytes) {
900 start |= (uint32_t)(ptfunxored[j+8] << 24);
902 start |= (uint32_t)(ptfunxored[j+7] << 16);
904 start |= (uint32_t)(ptfunxored[j+6] << 8);
906 start |= (uint32_t)(ptfunxored[j+5]);
912 uint32_t something = 0;
913 switch (somethingbytes) {
915 something |= (uint32_t)(ptfunxored[j+8] << 24);
917 something |= (uint32_t)(ptfunxored[j+7] << 16);
919 something |= (uint32_t)(ptfunxored[j+6] << 8);
921 something |= (uint32_t)(ptfunxored[j+5]);
927 std::string filename = string(name) + extension;
931 (int64_t)(start*ratefactor),
932 (int64_t)(length*ratefactor),
936 //printf("something=%d\n", something);
938 vector<wav_t>::iterator begin = actualwavs.begin();
939 vector<wav_t>::iterator finish = actualwavs.end();
940 vector<wav_t>::iterator found;
941 // Add file to list only if it is an actual wav
942 if ((found = std::find(begin, finish, f)) != finish) {
943 audiofiles.push_back(f);
944 // Also add plain wav as region
948 (int64_t)(start*ratefactor),
949 (int64_t)(sampleoffset*ratefactor),
950 (int64_t)(length*ratefactor),
953 regions.push_back(r);
956 if (foundin(filename, string(".grp"))) {
962 (int64_t)(start*ratefactor),
963 (int64_t)(sampleoffset*ratefactor),
964 (int64_t)(length*ratefactor),
967 regions.push_back(r);
974 if ( (ptfunxored[k ] == 0x5a) &&
975 (ptfunxored[k+1] == 0x03)) {
981 if ( (ptfunxored[k ] == 0x5a) &&
982 (ptfunxored[k+1] == 0x02)) {
991 uint32_t tracknumber = 0;
992 uint32_t regionspertrack = 0;
993 for (;k < len; k++) {
994 if ( (ptfunxored[k ] == 0x5a) &&
995 (ptfunxored[k+1] == 0x04)) {
998 if ( (ptfunxored[k ] == 0x5a) &&
999 (ptfunxored[k+1] == 0x02)) {
1001 uint8_t lengthofname = 0;
1002 lengthofname = ptfunxored[k+9];
1003 if (lengthofname == 0x5a) {
1008 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1010 //printf("regions/track=%d\n", regionspertrack);
1011 char name[256] = {0};
1012 for (j = 0; j < lengthofname; j++) {
1013 name[j] = ptfunxored[j+k+13];
1016 tr.name = string(name);
1017 tr.index = tracknumber++;
1019 for (j = k; regionspertrack > 0 && j < len; j++) {
1020 for (l = j; l < len; l++) {
1021 if ( (ptfunxored[l ] == 0x5a) &&
1022 (ptfunxored[l+1] == 0x07)) {
1029 if (regionspertrack == 0) {
1030 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1034 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1035 vector<region_t>::iterator begin = regions.begin();
1036 vector<region_t>::iterator finish = regions.end();
1037 vector<region_t>::iterator found;
1038 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1043 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1044 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1045 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1046 offset |= (uint32_t)(ptfunxored[i]);
1047 tr.reg.startpos = (int64_t)(offset*ratefactor);
1048 if (tr.reg.length > 0) {
1049 tracks.push_back(tr);
1059 PTFFormat::parserest10(void) {
1062 uint8_t startbytes = 0;
1063 uint8_t lengthbytes = 0;
1064 uint8_t offsetbytes = 0;
1065 uint8_t somethingbytes = 0;
1066 uint8_t skipbytes = 0;
1070 if ( (ptfunxored[k ] == 'S') &&
1071 (ptfunxored[k+1] == 'n') &&
1072 (ptfunxored[k+2] == 'a') &&
1073 (ptfunxored[k+3] == 'p')) {
1078 for (i = k; i < len-70; i++) {
1079 if ( (ptfunxored[i ] == 0x5a) &&
1080 (ptfunxored[i+1] == 0x02)) {
1086 for (i = k; i < len-70; i++) {
1087 if ( (ptfunxored[i ] == 0x5a) &&
1088 (ptfunxored[i+1] == 0x02)) {
1094 uint16_t rindex = 0;
1095 uint32_t findex = 0;
1096 for (i = k; i < len-70; i++) {
1097 if ( (ptfunxored[i ] == 0x5a) &&
1098 (ptfunxored[i+1] == 0x08)) {
1101 if ( (ptfunxored[i ] == 0x5a) &&
1102 (ptfunxored[i+1] == 0x01)) {
1104 uint8_t lengthofname = ptfunxored[i+9];
1105 if (ptfunxored[i+13] == 0x5a) {
1108 char name[256] = {0};
1109 for (j = 0; j < lengthofname; j++) {
1110 name[j] = ptfunxored[i+13+j];
1114 //uint8_t disabled = ptfunxored[j];
1115 //printf("%s\n", name);
1117 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1118 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1119 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1120 somethingbytes = (ptfunxored[j+3] & 0xf);
1121 skipbytes = ptfunxored[j+4];
1122 findex = ptfunxored[j+5
1129 /*rindex = ptfunxored[j+5
1137 uint32_t sampleoffset = 0;
1138 switch (offsetbytes) {
1140 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1142 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1144 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1146 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1151 uint32_t length = 0;
1152 switch (lengthbytes) {
1154 length |= (uint32_t)(ptfunxored[j+8] << 24);
1156 length |= (uint32_t)(ptfunxored[j+7] << 16);
1158 length |= (uint32_t)(ptfunxored[j+6] << 8);
1160 length |= (uint32_t)(ptfunxored[j+5]);
1166 switch (startbytes) {
1168 start |= (uint32_t)(ptfunxored[j+8] << 24);
1170 start |= (uint32_t)(ptfunxored[j+7] << 16);
1172 start |= (uint32_t)(ptfunxored[j+6] << 8);
1174 start |= (uint32_t)(ptfunxored[j+5]);
1180 uint32_t something = 0;
1181 switch (somethingbytes) {
1183 something |= (uint32_t)(ptfunxored[j+8] << 24);
1185 something |= (uint32_t)(ptfunxored[j+7] << 16);
1187 something |= (uint32_t)(ptfunxored[j+6] << 8);
1189 something |= (uint32_t)(ptfunxored[j+5]);
1195 std::string filename = string(name) + extension;
1199 (int64_t)(start*ratefactor),
1200 (int64_t)(length*ratefactor),
1203 if (strlen(name) == 0) {
1210 //printf("something=%d\n", something);
1212 vector<wav_t>::iterator begin = actualwavs.begin();
1213 vector<wav_t>::iterator finish = actualwavs.end();
1214 vector<wav_t>::iterator found;
1215 // Add file to list only if it is an actual wav
1216 if ((found = std::find(begin, finish, f)) != finish) {
1217 audiofiles.push_back(f);
1218 // Also add plain wav as region
1222 (int64_t)(start*ratefactor),
1223 (int64_t)(sampleoffset*ratefactor),
1224 (int64_t)(length*ratefactor),
1227 regions.push_back(r);
1230 if (foundin(filename, string(".grp"))) {
1236 (int64_t)(start*ratefactor),
1237 (int64_t)(sampleoffset*ratefactor),
1238 (int64_t)(length*ratefactor),
1241 regions.push_back(r);
1244 //printf("%s\n", name);
1249 uint32_t tracknumber = 0;
1250 uint32_t regionspertrack = 0;
1251 for (;k < len; k++) {
1252 if ( (ptfunxored[k ] == 0x5a) &&
1253 (ptfunxored[k+1] == 0x08)) {
1258 for (;k < len; k++) {
1259 if ( (ptfunxored[k ] == 0x5a) &&
1260 (ptfunxored[k+1] == 0x04)) {
1263 if ( (ptfunxored[k ] == 0x5a) &&
1264 (ptfunxored[k+1] == 0x02)) {
1266 uint8_t lengthofname = 0;
1267 lengthofname = ptfunxored[k+9];
1268 if (lengthofname == 0x5a) {
1273 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1275 //printf("regions/track=%d\n", regionspertrack);
1276 char name[256] = {0};
1277 for (j = 0; j < lengthofname; j++) {
1278 name[j] = ptfunxored[j+k+13];
1281 tr.name = string(name);
1282 tr.index = tracknumber++;
1284 for (j = k; regionspertrack > 0 && j < len; j++) {
1285 for (l = j; l < len; l++) {
1286 if ( (ptfunxored[l ] == 0x5a) &&
1287 (ptfunxored[l+1] == 0x08)) {
1294 if (regionspertrack == 0) {
1295 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1299 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1300 vector<region_t>::iterator begin = regions.begin();
1301 vector<region_t>::iterator finish = regions.end();
1302 vector<region_t>::iterator found;
1303 if ((found = std::find(begin, finish, tr.reg)) != finish) {
1308 offset |= (uint32_t)(ptfunxored[i+3] << 24);
1309 offset |= (uint32_t)(ptfunxored[i+2] << 16);
1310 offset |= (uint32_t)(ptfunxored[i+1] << 8);
1311 offset |= (uint32_t)(ptfunxored[i]);
1312 tr.reg.startpos = (int64_t)(offset*ratefactor);
1313 if (tr.reg.length > 0) {
1314 tracks.push_back(tr);