ptformat: Update lib to upstream 624671c
[ardour.git] / libs / ptformat / ptfformat.cc
1 /*
2     Copyright (C) 2015  Damien Zammit
3     Copyright (C) 2015  Robin Gareus
4
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.
9
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.
14
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string>
20 #include <string.h>
21 #include <assert.h>
22
23 #include <glib/gstdio.h>
24
25 #include "ptfformat.h"
26
27 using namespace std;
28
29 static void
30 hexdump(uint8_t *data, int len)
31 {
32         int i,j,end,step=16;
33
34         for (i = 0; i < len; i += step) {
35                 printf("0x%02X: ", i);
36                 end = i + step;
37                 if (end > len) end = len;
38                 for (j = i; j < end; j++) {
39                         printf("0x%02X ", data[j]);
40                 }
41                 for (j = i; j < end; j++) {
42                         if (data[j] < 128 && data[j] > 32)
43                                 printf("%c", data[j]);
44                         else
45                                 printf(".");
46                 }
47                 printf("\n");
48         }
49 }
50
51 PTFFormat::PTFFormat() : version(0), product(NULL) {
52 }
53
54 PTFFormat::~PTFFormat() {
55         if (ptfunxored) {
56                 free(ptfunxored);
57         }
58 }
59
60 bool
61 PTFFormat::foundin(std::string haystack, std::string needle) {
62         size_t found = haystack.find(needle);
63         if (found != std::string::npos) {
64                 return true;
65         } else {
66                 return false;
67         }
68 }
69
70 /* Return values:       0            success
71                         0x01 to 0xff value of missing lut
72                         -1           could not open file as ptf
73 */
74 int
75 PTFFormat::load(std::string path, int64_t targetsr) {
76         FILE *fp;
77         unsigned char xxor[256];
78         unsigned char ct;
79         uint64_t i;
80         uint8_t xor_type;
81         uint8_t xor_value;
82         uint8_t xor_delta;
83         uint16_t xor_len;
84         int err;
85
86         if (! (fp = g_fopen(path.c_str(), "rb"))) {
87                 return -1;
88         }
89
90         fseek(fp, 0, SEEK_END);
91         len = ftell(fp);
92         if (len < 0x14) {
93                 fclose(fp);
94                 return -1;
95         }
96
97         if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
98                 /* Silently fail -- out of memory*/
99                 fclose(fp);
100                 ptfunxored = 0;
101                 return -1;
102         }
103
104         /* The first 20 bytes are always unencrypted */
105         fseek(fp, 0x00, SEEK_SET);
106         i = fread(ptfunxored, 1, 0x14, fp);
107         if (i < 0x14) {
108                 fclose(fp);
109                 return -1;
110         }
111
112         xor_type = ptfunxored[0x12];
113         xor_value = ptfunxored[0x13];
114
115         // xor_type 0x01 = ProTools 5, 6, 7, 8 and 9
116         // xor_type 0x05 = ProTools 10, 11, 12
117         switch(xor_type) {
118         case 0x01:
119                 xor_delta = gen_xor_delta(xor_value, 53, false);
120                 xor_len = 256;
121                 break;
122         case 0x05:
123                 xor_delta = gen_xor_delta(xor_value, 11, true);
124                 xor_len = 128;
125                 break;
126         default:
127                 fclose(fp);
128                 return -1;
129         }
130
131         /* Generate the xor_key */
132         for (i=0; i < xor_len; i++)
133                 xxor[i] = (i * xor_delta) & 0xff;
134
135         /* hexdump(xxor, xor_len); */
136
137         /* Read file and decrypt rest of file */
138         i = 0x14;
139         fseek(fp, i, SEEK_SET);
140         while (fread(&ct, 1, 1, fp) != 0) {
141                 uint8_t xor_index = (xor_type == 0x01) ? i & 0xff : (i >> 12) & 0x7f;
142                 ptfunxored[i++] = ct ^ xxor[xor_index];
143         }
144         fclose(fp);
145
146         if (!parse_version())
147                 return -1;
148
149         if (version < 5 || version > 12)
150                 return -1;
151
152         targetrate = targetsr;
153         err = parse();
154         if (err)
155                 return -1;
156
157         return 0;
158 }
159
160 bool
161 PTFFormat::parse_version() {
162         uint32_t seg_len,str_len;
163         uint8_t *data = ptfunxored + 0x14;
164         uintptr_t data_end = ((uintptr_t)ptfunxored) + 0x100;
165         uint8_t seg_type; 
166         bool success = false;
167
168         while( ((uintptr_t)data < data_end) && (success == false) ) {
169
170                 if (data[0] != 0x5a) {
171                         success = false;
172                         break;
173                 }
174
175                 seg_type = data[1];
176                 /* Skip segment header */
177                 data += 3;
178                 if (data[0] == 0 && data[1] == 0) {
179                         /* LE */
180                         seg_len = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
181                 } else {
182                         /* BE */
183                         seg_len = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0];
184                 }
185                 /* Skip seg_len */
186                 data += 4;
187                 if (!(seg_type == 0x04 || seg_type == 0x03) || data[0] != 0x03) {
188                         /* Go to next segment */
189                         data += seg_len;
190                         continue;
191                 }
192                 /* Skip 0x03 0x00 0x00 */
193                 data += 3;
194                 seg_len -= 3;
195                 str_len = (*(uint8_t *)data);
196                 if (! (product = (uint8_t *)malloc((str_len+1) * sizeof(uint8_t)))) {
197                         success = false;
198                         break;
199                 }
200
201                 /* Skip str_len */
202                 data += 4;
203                 seg_len -= 4;
204
205                 memcpy(product, data, str_len);
206                 product[str_len] = 0;
207                 data += str_len;
208                 seg_len -= str_len;
209
210                 /* Skip 0x03 0x00 0x00 0x00 */
211                 data += 4;
212                 seg_len -= 4;
213
214                 version = data[0];
215                 if (version == 0) {
216                         version = data[3];
217                 }
218                 data += seg_len;
219                 success = true;
220         }
221
222         /* If the above does not work, assume old version 5,6,7 */
223         if ((uintptr_t)data >= data_end - seg_len) {
224                 version = ptfunxored[0x40];
225                 success = true;
226         }
227         return success;
228 }
229
230 uint8_t
231 PTFFormat::gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative) {
232         uint16_t i;
233         for (i = 0; i < 256; i++) {
234                 if (((i * mul) & 0xff) == xor_value) {
235                                 return (negative) ? i * (-1) : i;
236                 }
237         }
238         // Should not occur
239         return 0;
240 }
241
242 int
243 PTFFormat::parse(void) {
244         if (version == 5) {
245                 parse5header();
246                 setrates();
247                 if (sessionrate < 44100 || sessionrate > 192000)
248                   return -1;
249                 parseaudio5();
250                 parserest5();
251                 parsemidi();
252         } else if (version == 7) {
253                 parse7header();
254                 setrates();
255                 if (sessionrate < 44100 || sessionrate > 192000)
256                   return -1;
257                 parseaudio();
258                 parserest89();
259                 parsemidi();
260         } else if (version == 8) {
261                 parse8header();
262                 setrates();
263                 if (sessionrate < 44100 || sessionrate > 192000)
264                   return -1;
265                 parseaudio();
266                 parserest89();
267                 parsemidi();
268         } else if (version == 9) {
269                 parse9header();
270                 setrates();
271                 if (sessionrate < 44100 || sessionrate > 192000)
272                   return -1;
273                 parseaudio();
274                 parserest89();
275                 parsemidi();
276         } else if (version == 10 || version == 11 || version == 12) {
277                 parse10header();
278                 setrates();
279                 if (sessionrate < 44100 || sessionrate > 192000)
280                   return -1;
281                 parseaudio();
282                 parserest10();
283                 parsemidi();
284         } else {
285                 // Should not occur
286                 return -1;
287         }
288         return 0;
289 }
290
291 void
292 PTFFormat::setrates(void) {
293         ratefactor = 1.f;
294         if (sessionrate != 0) {
295                 ratefactor = (float)targetrate / sessionrate;
296         }
297 }
298
299 void
300 PTFFormat::parse5header(void) {
301         uint32_t k;
302
303         // Find session sample rate
304         k = 0x100;
305         while (k < len) {
306                 if (            (ptfunxored[k  ] == 0x5a) &&
307                                 (ptfunxored[k+1] == 0x00) &&
308                                 (ptfunxored[k+2] == 0x02)) {
309                         break;
310                 }
311                 k++;
312         }
313
314         sessionrate = 0;
315         sessionrate |= ptfunxored[k+12] << 16;
316         sessionrate |= ptfunxored[k+13] << 8;
317         sessionrate |= ptfunxored[k+14];
318 }
319
320 void
321 PTFFormat::parse7header(void) {
322         uint64_t k;
323
324         // Find session sample rate
325         k = 0x100;
326         while (k < len) {
327                 if (            (ptfunxored[k  ] == 0x5a) &&
328                                 (ptfunxored[k+1] == 0x00) &&
329                                 (ptfunxored[k+2] == 0x05)) {
330                         break;
331                 }
332                 k++;
333         }
334
335         sessionrate = 0;
336         sessionrate |= ptfunxored[k+12] << 16;
337         sessionrate |= ptfunxored[k+13] << 8;
338         sessionrate |= ptfunxored[k+14];
339 }
340
341 void
342 PTFFormat::parse8header(void) {
343         uint64_t k;
344
345         // Find session sample rate
346         k = 0;
347         while (k < len) {
348                 if (            (ptfunxored[k  ] == 0x5a) &&
349                                 (ptfunxored[k+1] == 0x05)) {
350                         break;
351                 }
352                 k++;
353         }
354
355         sessionrate = 0;
356         sessionrate |= ptfunxored[k+11];
357         sessionrate |= ptfunxored[k+12] << 8;
358         sessionrate |= ptfunxored[k+13] << 16;
359 }
360
361 void
362 PTFFormat::parse9header(void) {
363         uint64_t k;
364
365         // Find session sample rate
366         k = 0x100;
367         while (k < len) {
368                 if (            (ptfunxored[k  ] == 0x5a) &&
369                                 (ptfunxored[k+1] == 0x06)) {
370                         break;
371                 }
372                 k++;
373         }
374
375         sessionrate = 0;
376         sessionrate |= ptfunxored[k+11];
377         sessionrate |= ptfunxored[k+12] << 8;
378         sessionrate |= ptfunxored[k+13] << 16;
379 }
380
381 void
382 PTFFormat::parse10header(void) {
383         uint64_t k;
384
385         // Find session sample rate
386         k = 0x100;
387         while (k < len) {
388                 if (            (ptfunxored[k  ] == 0x5a) &&
389                                 (ptfunxored[k+1] == 0x09)) {
390                         break;
391                 }
392                 k++;
393         }
394
395         sessionrate = 0;
396         sessionrate |= ptfunxored[k+11];
397         sessionrate |= ptfunxored[k+12] << 8;
398         sessionrate |= ptfunxored[k+13] << 16;
399 }
400
401 void
402 PTFFormat::parserest5(void) {
403         uint64_t i, j, k;
404         uint64_t regionspertrack, lengthofname;
405         uint64_t startbytes, lengthbytes, offsetbytes;
406         uint16_t tracknumber = 0;
407         uint16_t findex;
408         uint16_t rindex;
409
410         k = 0;
411         for (i = 0; i < 5; i++) {
412                 while (k < len) {
413                         if (            (ptfunxored[k  ] == 0x5a) &&
414                                         (ptfunxored[k+1] == 0x00) &&
415                                         (ptfunxored[k+2] == 0x03)) {
416                                 break;
417                         }
418                         k++;
419                 }
420                 k++;
421         }
422         k--;
423
424         for (i = 0; i < 2; i++) {
425                 while (k) {
426                         if (            (ptfunxored[k  ] == 0x5a) &&
427                                         (ptfunxored[k+1] == 0x00) &&
428                                         (ptfunxored[k+2] == 0x01)) {
429                                 break;
430                         }
431                         k--;
432                 }
433                 if (k)
434                         k--;
435         }
436         k++;
437
438         rindex = 0;
439         while (k < len) {
440                 if (            (ptfunxored[k  ] == 0xff) &&
441                                 (ptfunxored[k+1] == 0xff)) {
442                         break;
443                 }
444                 while (k < len) {
445                         if (            (ptfunxored[k  ] == 0x5a) &&
446                                         (ptfunxored[k+1] == 0x00) &&
447                                         (ptfunxored[k+2] == 0x01)) {
448                                 break;
449                         }
450                         k++;
451                 }
452
453                 lengthofname = ptfunxored[k+12];
454                 if (ptfunxored[k+13] == 0x5a) {
455                         k++;
456                         break;
457                 }
458                 char name[256] = {0};
459                 for (j = 0; j < lengthofname; j++) {
460                         name[j] = ptfunxored[k+13+j];
461                 }
462                 name[j] = '\0';
463                 regionspertrack = ptfunxored[k+13+j+3];
464                 for (i = 0; i < regionspertrack; i++) {
465                         while (k < len) {
466                                 if (            (ptfunxored[k  ] == 0x5a) &&
467                                                 (ptfunxored[k+1] == 0x00) &&
468                                                 (ptfunxored[k+2] == 0x03)) {
469                                         break;
470                                 }
471                                 k++;
472                         }
473                         j = k+16;
474                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
475                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
476                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
477                         //somethingbytes = (ptfunxored[j+1] & 0xf);
478                         findex = ptfunxored[k+14];
479                         j--;
480                         uint32_t start = 0;
481                         switch (startbytes) {
482                         case 4:
483                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
484                         case 3:
485                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
486                         case 2:
487                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
488                         case 1:
489                                 start |= (uint32_t)(ptfunxored[j+5]);
490                         default:
491                                 break;
492                         }
493                         j+=startbytes;
494                         uint32_t length = 0;
495                         switch (lengthbytes) {
496                         case 4:
497                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
498                         case 3:
499                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
500                         case 2:
501                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
502                         case 1:
503                                 length |= (uint32_t)(ptfunxored[j+5]);
504                         default:
505                                 break;
506                         }
507                         j+=lengthbytes;
508                         uint32_t sampleoffset = 0;
509                         switch (offsetbytes) {
510                         case 4:
511                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
512                         case 3:
513                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
514                         case 2:
515                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
516                         case 1:
517                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
518                         default:
519                                 break;
520                         }
521                         j+=offsetbytes;
522
523                         //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
524
525                         std::string filename = string(name) + extension;
526                         wav_t f = {
527                                 filename,
528                                 findex,
529                                 (int64_t)(start*ratefactor),
530                                 (int64_t)(length*ratefactor),
531                         };
532
533                         vector<wav_t>::iterator begin = audiofiles.begin();
534                         vector<wav_t>::iterator finish = audiofiles.end();
535                         vector<wav_t>::iterator found;
536                         // Add file to lists
537                         if ((found = std::find(begin, finish, f)) != finish) {
538                                 std::vector<midi_ev_t> m;
539                                 region_t r = {
540                                         name,
541                                         rindex,
542                                         (int64_t)(start*ratefactor),
543                                         (int64_t)(sampleoffset*ratefactor),
544                                         (int64_t)(length*ratefactor),
545                                         *found,
546                                         m
547                                 };
548                                 regions.push_back(r);
549                                 vector<track_t>::iterator ti;
550                                 vector<track_t>::iterator bt = tracks.begin();
551                                 vector<track_t>::iterator et = tracks.end();
552                                 track_t tr = { name, 0, 0, r };
553                                 if ((ti = std::find(bt, et, tr)) != et) {
554                                         tracknumber = (*ti).index;
555                                 } else {
556                                         tracknumber = tracks.size() + 1;
557                                 }
558                                 track_t t = {
559                                         name,
560                                         (uint16_t)tracknumber,
561                                         uint8_t(0),
562                                         r
563                                 };
564                                 tracks.push_back(t);
565                         } else {
566                                 std::vector<midi_ev_t> m;
567                                 region_t r = {
568                                         name,
569                                         rindex,
570                                         (int64_t)(start*ratefactor),
571                                         (int64_t)(sampleoffset*ratefactor),
572                                         (int64_t)(length*ratefactor),
573                                         f,
574                                         m,
575                                 };
576                                 regions.push_back(r);
577                                 vector<track_t>::iterator ti;
578                                 vector<track_t>::iterator bt = tracks.begin();
579                                 vector<track_t>::iterator et = tracks.end();
580                                 track_t tr = { name, 0, 0, r };
581                                 if ((ti = std::find(bt, et, tr)) != et) {
582                                         tracknumber = (*ti).index;
583                                 } else {
584                                         tracknumber = tracks.size() + 1;
585                                 }
586                                 track_t t = {
587                                         name,
588                                         (uint16_t)tracknumber,
589                                         uint8_t(0),
590                                         r
591                                 };
592                                 tracks.push_back(t);
593                         }
594                         rindex++;
595                         k++;
596                 }
597                 k++;
598         }
599 }
600
601 void
602 PTFFormat::resort(std::vector<wav_t>& ws) {
603         int j = 0;
604         std::sort(ws.begin(), ws.end());
605         for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
606                 (*i).index = j;
607                 j++;
608         }
609 }
610
611 void
612 PTFFormat::parseaudio5(void) {
613         uint64_t i,k,l;
614         uint64_t lengthofname, wavnumber;
615
616         // Find end of wav file list
617         k = 0;
618         while (k < len) {
619                 if (            (ptfunxored[k  ] == 0x5f) &&
620                                 (ptfunxored[k+1] == 0x50) &&
621                                 (ptfunxored[k+2] == 0x35)) {
622                         break;
623                 }
624                 k++;
625         }
626         k++;
627         while (k < len) {
628                 if (            (ptfunxored[k  ] == 0x5f) &&
629                                 (ptfunxored[k+1] == 0x50) &&
630                                 (ptfunxored[k+2] == 0x35)) {
631                         break;
632                 }
633                 k++;
634         }
635
636         // Find actual wav names
637         uint16_t numberofwavs = ptfunxored[k-23];
638         char wavname[256];
639         for (i = k; i < len; i++) {
640                 if (            (ptfunxored[i  ] == 'F') &&
641                                 (ptfunxored[i+1] == 'i') &&
642                                 (ptfunxored[i+2] == 'l') &&
643                                 (ptfunxored[i+3] == 'e') &&
644                                 (ptfunxored[i+4] == 's')) {
645                         break;
646                 }
647         }
648
649         wavnumber = 0;
650         i+=16;
651         char ext[5];
652         while (i < len && numberofwavs > 0) {
653                 i++;
654                 if (            (ptfunxored[i  ] == 0x5a) &&
655                                 (ptfunxored[i+1] == 0x00) &&
656                                 (ptfunxored[i+2] == 0x05)) {
657                         break;
658                 }
659                 lengthofname = ptfunxored[i];
660                 i++;
661                 l = 0;
662                 while (l < lengthofname) {
663                         wavname[l] = ptfunxored[i+l];
664                         l++;
665                 }
666                 i+=lengthofname;
667                 ext[0] = ptfunxored[i++];
668                 ext[1] = ptfunxored[i++];
669                 ext[2] = ptfunxored[i++];
670                 ext[3] = ptfunxored[i++];
671                 ext[4] = '\0';
672
673                 wavname[l] = 0;
674                 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
675                         extension = string("");
676                 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
677                         extension = string(".wav");
678                 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
679                         extension = string(".aif");
680                 } else {
681                         extension = string("");
682                 }
683
684                 std::string wave = string(wavname);
685                 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
686
687                 if (foundin(wave, string(".grp"))) {
688                         continue;
689                 }
690
691                 actualwavs.push_back(f);
692                 audiofiles.push_back(f);
693                 //printf("done\n");
694                 numberofwavs--;
695                 i += 7;
696         }
697         resort(actualwavs);
698         resort(audiofiles);
699 }
700
701 void
702 PTFFormat::parsemidi(void) {
703         uint64_t i, k, n_midi_events, zero_ticks;
704         uint64_t midi_pos, midi_len, max_pos;
705         uint8_t midi_velocity, midi_note;
706         uint16_t rsize;
707         midi_ev_t m;
708         bool found = false;
709         int max_regions = regions.size();
710         char midiname[26] = { 0 };
711
712         // Find MdNLB
713         k = 0;
714
715         // Parse all midi tracks, treat each group of midi bytes as a track
716         while (k + 35 < len) {
717                 max_pos = 0;
718                 std::vector<midi_ev_t> midi;
719
720                 while (k < len) {
721                         if (            (ptfunxored[k  ] == 'M') &&
722                                         (ptfunxored[k+1] == 'd') &&
723                                         (ptfunxored[k+2] == 'N') &&
724                                         (ptfunxored[k+3] == 'L') &&
725                                         (ptfunxored[k+4] == 'B')) {
726                                 found = true;
727                                 break;
728                         }
729                         k++;
730                 }
731
732                 if (!found) {
733                         return;
734                 }
735
736                 k += 11;
737                 n_midi_events = ptfunxored[k] | ptfunxored[k+1] << 8 |
738                                 ptfunxored[k+2] << 16 | ptfunxored[k+3] << 24;
739
740                 k += 4;
741                 zero_ticks = (uint64_t)ptfunxored[k] |
742                                 (uint64_t)ptfunxored[k+1] << 8 |
743                                 (uint64_t)ptfunxored[k+2] << 16 |
744                                 (uint64_t)ptfunxored[k+3] << 24 |
745                                 (uint64_t)ptfunxored[k+4] << 32;
746                 for (i = 0; i < n_midi_events && k < len; i++, k += 35) {
747                         midi_pos = (uint64_t)ptfunxored[k] |
748                                 (uint64_t)ptfunxored[k+1] << 8 |
749                                 (uint64_t)ptfunxored[k+2] << 16 |
750                                 (uint64_t)ptfunxored[k+3] << 24 |
751                                 (uint64_t)ptfunxored[k+4] << 32;
752                         midi_pos -= zero_ticks;
753                         midi_note = ptfunxored[k+8];
754                         midi_len = (uint64_t)ptfunxored[k+9] |
755                                 (uint64_t)ptfunxored[k+10] << 8 |
756                                 (uint64_t)ptfunxored[k+11] << 16 |
757                                 (uint64_t)ptfunxored[k+12] << 24 |
758                                 (uint64_t)ptfunxored[k+13] << 32;
759                         midi_velocity = ptfunxored[k+17];
760
761                         if (midi_pos + midi_len > max_pos) {
762                                 max_pos = midi_pos + midi_len;
763                         }
764
765                         m.pos = midi_pos;
766                         m.length = midi_len;
767                         m.note = midi_note;
768                         m.velocity = midi_velocity;
769 #if 1
770 // stop gap measure to prevent crashes in ardour,
771 // remove when decryption is fully solved for .ptx
772                         if ((m.velocity & 0x80) || (m.note & 0x80) ||
773                                         (m.pos & 0xff00000000) || (m.length & 0xff00000000)) {
774                                 continue;
775                         }
776 #endif
777                         midi.push_back(m);
778
779                         //fprintf(stderr, "MIDI:  Note=%d Vel=%d Start=%d(samples) Len=%d(samples)\n", midi_note, midi_velocity, midi_pos, midi_len);
780                 }
781
782                 rsize = (uint16_t)regions.size();
783                 snprintf(midiname, 20, "MIDI-%d", rsize - max_regions + 1);
784                 wav_t w = { std::string(""), 0, 0, 0 };
785                 region_t r = {
786                         midiname,
787                         rsize,
788                         (int64_t)(0),
789                         (int64_t)(0),
790                         (int64_t)(max_pos*sessionrate*60/(960000*120)),
791                         w,
792                         midi,
793                 };
794                 regions.push_back(r);
795         }
796 }
797
798 void
799 PTFFormat::parseaudio(void) {
800         uint64_t i,j,k,l;
801
802         // Find end of wav file list
803         k = 0;
804         while (k < len) {
805                 if (            (ptfunxored[k  ] == 0xff) &&
806                                 (ptfunxored[k+1] == 0xff) &&
807                                 (ptfunxored[k+2] == 0xff) &&
808                                 (ptfunxored[k+3] == 0xff)) {
809                         break;
810                 }
811                 k++;
812         }
813
814         // Find actual wav names
815         bool first = true;
816         uint16_t numberofwavs;
817         char wavname[256];
818         for (i = k; i > 4; i--) {
819                 if (            ((ptfunxored[i  ] == 'W') || (ptfunxored[i  ] == 'A')) &&
820                                 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
821                                 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
822                                 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
823                         j = i-4;
824                         l = 0;
825                         while (ptfunxored[j] != '\0') {
826                                 wavname[l] = ptfunxored[j];
827                                 l++;
828                                 j--;
829                         }
830                         wavname[l] = 0;
831                         if (ptfunxored[i] == 'W') {
832                                 extension = string(".wav");
833                         } else {
834                                 extension = string(".aif");
835                         }
836                         //uint8_t playlist = ptfunxored[j-8];
837
838                         if (first) {
839                                 first = false;
840                                 for (j = k; j > 4; j--) {
841                                         if (    (ptfunxored[j  ] == 0x01) &&
842                                                 (ptfunxored[j-1] == 0x5a)) {
843
844                                                 numberofwavs = 0;
845                                                 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
846                                                 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
847                                                 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
848                                                 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
849                                                 //printf("%d wavs\n", numberofwavs);
850                                                 break;
851                                         }
852                                 k--;
853                                 }
854                         }
855
856                         std::string wave = string(wavname);
857                         std::reverse(wave.begin(), wave.end());
858                         wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
859
860                         if (foundin(wave, string(".grp"))) {
861                                 continue;
862                         }
863
864                         actualwavs.push_back(f);
865
866                         numberofwavs--;
867                         if (numberofwavs <= 0)
868                                 break;
869                 }
870         }
871 }
872
873 void
874 PTFFormat::parserest89(void) {
875         uint64_t i,j,k,l;
876         // Find Regions
877         uint8_t startbytes = 0;
878         uint8_t lengthbytes = 0;
879         uint8_t offsetbytes = 0;
880         uint8_t somethingbytes = 0;
881         uint8_t skipbytes = 0;
882
883         k = 0;
884         while (k < len) {
885                 if (            (ptfunxored[k  ] == 'S') &&
886                                 (ptfunxored[k+1] == 'n') &&
887                                 (ptfunxored[k+2] == 'a') &&
888                                 (ptfunxored[k+3] == 'p')) {
889                         break;
890                 }
891                 k++;
892         }
893         uint16_t rindex = 0;
894         uint32_t findex = 0;
895         for (i = k; i < len-70; i++) {
896                 if (            (ptfunxored[i  ] == 0x5a) &&
897                                 (ptfunxored[i+1] == 0x0a)) {
898                                 break;
899                 }
900                 if (            (ptfunxored[i  ] == 0x5a) &&
901                                 (ptfunxored[i+1] == 0x0c)) {
902
903                         uint8_t lengthofname = ptfunxored[i+9];
904
905                         char name[256] = {0};
906                         for (j = 0; j < lengthofname; j++) {
907                                 name[j] = ptfunxored[i+13+j];
908                         }
909                         name[j] = '\0';
910                         j += i+13;
911                         //uint8_t disabled = ptfunxored[j];
912
913                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
914                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
915                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
916                         somethingbytes = (ptfunxored[j+3] & 0xf);
917                         skipbytes = ptfunxored[j+4];
918                         findex = ptfunxored[j+5
919                                         +startbytes
920                                         +lengthbytes
921                                         +offsetbytes
922                                         +somethingbytes
923                                         +skipbytes
924                                         +40];
925                         /*rindex = ptfunxored[j+5
926                                         +startbytes
927                                         +lengthbytes
928                                         +offsetbytes
929                                         +somethingbytes
930                                         +skipbytes
931                                         +24];
932                         */
933                         uint32_t sampleoffset = 0;
934                         switch (offsetbytes) {
935                         case 4:
936                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
937                         case 3:
938                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
939                         case 2:
940                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
941                         case 1:
942                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
943                         default:
944                                 break;
945                         }
946                         j+=offsetbytes;
947                         uint32_t length = 0;
948                         switch (lengthbytes) {
949                         case 4:
950                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
951                         case 3:
952                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
953                         case 2:
954                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
955                         case 1:
956                                 length |= (uint32_t)(ptfunxored[j+5]);
957                         default:
958                                 break;
959                         }
960                         j+=lengthbytes;
961                         uint32_t start = 0;
962                         switch (startbytes) {
963                         case 4:
964                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
965                         case 3:
966                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
967                         case 2:
968                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
969                         case 1:
970                                 start |= (uint32_t)(ptfunxored[j+5]);
971                         default:
972                                 break;
973                         }
974                         j+=startbytes;
975                         /*
976                         uint32_t something = 0;
977                         switch (somethingbytes) {
978                         case 4:
979                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
980                         case 3:
981                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
982                         case 2:
983                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
984                         case 1:
985                                 something |= (uint32_t)(ptfunxored[j+5]);
986                         default:
987                                 break;
988                         }
989                         j+=somethingbytes;
990                         */
991                         std::string filename = string(name) + extension;
992                         wav_t f = {
993                                 filename,
994                                 0,
995                                 (int64_t)(start*ratefactor),
996                                 (int64_t)(length*ratefactor),
997                         };
998
999                         f.index = findex;
1000                         //printf("something=%d\n", something);
1001
1002                         vector<wav_t>::iterator begin = actualwavs.begin();
1003                         vector<wav_t>::iterator finish = actualwavs.end();
1004                         vector<wav_t>::iterator found;
1005                         // Add file to list only if it is an actual wav
1006                         if ((found = std::find(begin, finish, f)) != finish) {
1007                                 audiofiles.push_back(f);
1008                                 // Also add plain wav as region
1009                                 std::vector<midi_ev_t> m;
1010                                 region_t r = {
1011                                         name,
1012                                         rindex,
1013                                         (int64_t)(start*ratefactor),
1014                                         (int64_t)(sampleoffset*ratefactor),
1015                                         (int64_t)(length*ratefactor),
1016                                         f,
1017                                         m
1018                                 };
1019                                 regions.push_back(r);
1020                         // Region only
1021                         } else {
1022                                 if (foundin(filename, string(".grp"))) {
1023                                         continue;
1024                                 }
1025                                 std::vector<midi_ev_t> m;
1026                                 region_t r = {
1027                                         name,
1028                                         rindex,
1029                                         (int64_t)(start*ratefactor),
1030                                         (int64_t)(sampleoffset*ratefactor),
1031                                         (int64_t)(length*ratefactor),
1032                                         f,
1033                                         m
1034                                 };
1035                                 regions.push_back(r);
1036                         }
1037                         rindex++;
1038                 }
1039         }
1040
1041         while (k < len) {
1042                 if (            (ptfunxored[k  ] == 0x5a) &&
1043                                 (ptfunxored[k+1] == 0x03)) {
1044                                 break;
1045                 }
1046                 k++;
1047         }
1048         while (k < len) {
1049                 if (            (ptfunxored[k  ] == 0x5a) &&
1050                                 (ptfunxored[k+1] == 0x02)) {
1051                                 break;
1052                 }
1053                 k++;
1054         }
1055         k++;
1056
1057         //  Tracks
1058         uint32_t offset;
1059         uint32_t tracknumber = 0;
1060         uint32_t regionspertrack = 0;
1061         for (;k < len; k++) {
1062                 if (    (ptfunxored[k  ] == 0x5a) &&
1063                         (ptfunxored[k+1] == 0x04)) {
1064                         break;
1065                 }
1066                 if (    (ptfunxored[k  ] == 0x5a) &&
1067                         (ptfunxored[k+1] == 0x02)) {
1068
1069                         uint8_t lengthofname = 0;
1070                         lengthofname = ptfunxored[k+9];
1071                         if (lengthofname == 0x5a) {
1072                                 continue;
1073                         }
1074                         track_t tr;
1075
1076                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1077
1078                         //printf("regions/track=%d\n", regionspertrack);
1079                         char name[256] = {0};
1080                         for (j = 0; j < lengthofname; j++) {
1081                                 name[j] = ptfunxored[j+k+13];
1082                         }
1083                         name[j] = '\0';
1084                         tr.name = string(name);
1085                         tr.index = tracknumber++;
1086
1087                         for (j = k; regionspertrack > 0 && j < len; j++) {
1088                                 for (l = j; l < len; l++) {
1089                                         if (    (ptfunxored[l  ] == 0x5a) &&
1090                                                 (ptfunxored[l+1] == 0x07)) {
1091                                                 j = l;
1092                                                 break;
1093                                         }
1094                                 }
1095
1096
1097                                 if (regionspertrack == 0) {
1098                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1099                                         break;
1100                                 } else {
1101
1102                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1103                                         vector<region_t>::iterator begin = regions.begin();
1104                                         vector<region_t>::iterator finish = regions.end();
1105                                         vector<region_t>::iterator found;
1106                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1107                                                 tr.reg = (*found);
1108                                         }
1109                                         i = l+16;
1110                                         offset = 0;
1111                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1112                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1113                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1114                                         offset |= (uint32_t)(ptfunxored[i]);
1115                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1116                                         if (tr.reg.length > 0) {
1117                                                 tracks.push_back(tr);
1118                                         }
1119                                         regionspertrack--;
1120                                 }
1121                         }
1122                 }
1123         }
1124 }
1125
1126 void
1127 PTFFormat::parserest10(void) {
1128         uint64_t i,j,k,l;
1129         // Find Regions
1130         uint8_t startbytes = 0;
1131         uint8_t lengthbytes = 0;
1132         uint8_t offsetbytes = 0;
1133         uint8_t somethingbytes = 0;
1134         uint8_t skipbytes = 0;
1135
1136         k = 0;
1137         while (k < len) {
1138                 if (            (ptfunxored[k  ] == 'S') &&
1139                                 (ptfunxored[k+1] == 'n') &&
1140                                 (ptfunxored[k+2] == 'a') &&
1141                                 (ptfunxored[k+3] == 'p')) {
1142                         break;
1143                 }
1144                 k++;
1145         }
1146         for (i = k; i < len-70; i++) {
1147                 if (            (ptfunxored[i  ] == 0x5a) &&
1148                                 (ptfunxored[i+1] == 0x02)) {
1149                                 k = i;
1150                                 break;
1151                 }
1152         }
1153         k++;
1154         for (i = k; i < len-70; i++) {
1155                 if (            (ptfunxored[i  ] == 0x5a) &&
1156                                 (ptfunxored[i+1] == 0x02)) {
1157                                 k = i;
1158                                 break;
1159                 }
1160         }
1161         k++;
1162         uint16_t rindex = 0;
1163         uint32_t findex = 0;
1164         for (i = k; i < len-70; i++) {
1165                 if (            (ptfunxored[i  ] == 0x5a) &&
1166                                 (ptfunxored[i+1] == 0x08)) {
1167                                 break;
1168                 }
1169                 if (            (ptfunxored[i  ] == 0x5a) &&
1170                                 (ptfunxored[i+1] == 0x01)) {
1171
1172                         uint8_t lengthofname = ptfunxored[i+9];
1173                         if (ptfunxored[i+13] == 0x5a) {
1174                                 continue;
1175                         }
1176                         char name[256] = {0};
1177                         for (j = 0; j < lengthofname; j++) {
1178                                 name[j] = ptfunxored[i+13+j];
1179                         }
1180                         name[j] = '\0';
1181                         j += i+13;
1182                         //uint8_t disabled = ptfunxored[j];
1183                         //printf("%s\n", name);
1184
1185                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1186                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1187                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1188                         somethingbytes = (ptfunxored[j+3] & 0xf);
1189                         skipbytes = ptfunxored[j+4];
1190                         findex = ptfunxored[j+5
1191                                         +startbytes
1192                                         +lengthbytes
1193                                         +offsetbytes
1194                                         +somethingbytes
1195                                         +skipbytes
1196                                         +37];
1197                         /*rindex = ptfunxored[j+5
1198                                         +startbytes
1199                                         +lengthbytes
1200                                         +offsetbytes
1201                                         +somethingbytes
1202                                         +skipbytes
1203                                         +24];
1204                         */
1205                         uint32_t sampleoffset = 0;
1206                         switch (offsetbytes) {
1207                         case 4:
1208                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1209                         case 3:
1210                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1211                         case 2:
1212                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1213                         case 1:
1214                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1215                         default:
1216                                 break;
1217                         }
1218                         j+=offsetbytes;
1219                         uint32_t length = 0;
1220                         switch (lengthbytes) {
1221                         case 4:
1222                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
1223                         case 3:
1224                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
1225                         case 2:
1226                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
1227                         case 1:
1228                                 length |= (uint32_t)(ptfunxored[j+5]);
1229                         default:
1230                                 break;
1231                         }
1232                         j+=lengthbytes;
1233                         uint32_t start = 0;
1234                         switch (startbytes) {
1235                         case 4:
1236                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
1237                         case 3:
1238                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
1239                         case 2:
1240                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
1241                         case 1:
1242                                 start |= (uint32_t)(ptfunxored[j+5]);
1243                         default:
1244                                 break;
1245                         }
1246                         j+=startbytes;
1247                         /*
1248                         uint32_t something = 0;
1249                         switch (somethingbytes) {
1250                         case 4:
1251                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
1252                         case 3:
1253                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
1254                         case 2:
1255                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
1256                         case 1:
1257                                 something |= (uint32_t)(ptfunxored[j+5]);
1258                         default:
1259                                 break;
1260                         }
1261                         j+=somethingbytes;
1262                         */
1263                         std::string filename = string(name) + extension;
1264                         wav_t f = {
1265                                 filename,
1266                                 0,
1267                                 (int64_t)(start*ratefactor),
1268                                 (int64_t)(length*ratefactor),
1269                         };
1270
1271                         if (strlen(name) == 0) {
1272                                 continue;
1273                         }
1274                         if (length == 0) {
1275                                 continue;
1276                         }
1277                         f.index = findex;
1278                         //printf("something=%d\n", something);
1279
1280                         vector<wav_t>::iterator begin = actualwavs.begin();
1281                         vector<wav_t>::iterator finish = actualwavs.end();
1282                         vector<wav_t>::iterator found;
1283                         // Add file to list only if it is an actual wav
1284                         if ((found = std::find(begin, finish, f)) != finish) {
1285                                 audiofiles.push_back(f);
1286                                 // Also add plain wav as region
1287                                 std::vector<midi_ev_t> m;
1288                                 region_t r = {
1289                                         name,
1290                                         rindex,
1291                                         (int64_t)(start*ratefactor),
1292                                         (int64_t)(sampleoffset*ratefactor),
1293                                         (int64_t)(length*ratefactor),
1294                                         f,
1295                                         m
1296                                 };
1297                                 regions.push_back(r);
1298                         // Region only
1299                         } else {
1300                                 if (foundin(filename, string(".grp"))) {
1301                                         continue;
1302                                 }
1303                                 std::vector<midi_ev_t> m;
1304                                 region_t r = {
1305                                         name,
1306                                         rindex,
1307                                         (int64_t)(start*ratefactor),
1308                                         (int64_t)(sampleoffset*ratefactor),
1309                                         (int64_t)(length*ratefactor),
1310                                         f,
1311                                         m
1312                                 };
1313                                 regions.push_back(r);
1314                         }
1315                         rindex++;
1316                         //printf("%s\n", name);
1317                 }
1318         }
1319         //  Tracks
1320         uint32_t offset;
1321         uint32_t tracknumber = 0;
1322         uint32_t regionspertrack = 0;
1323         for (;k < len; k++) {
1324                 if (    (ptfunxored[k  ] == 0x5a) &&
1325                         (ptfunxored[k+1] == 0x08)) {
1326                         break;
1327                 }
1328         }
1329         k++;
1330         for (;k < len; k++) {
1331                 if (    (ptfunxored[k  ] == 0x5a) &&
1332                         (ptfunxored[k+1] == 0x04)) {
1333                         break;
1334                 }
1335                 if (    (ptfunxored[k  ] == 0x5a) &&
1336                         (ptfunxored[k+1] == 0x02)) {
1337
1338                         uint8_t lengthofname = 0;
1339                         lengthofname = ptfunxored[k+9];
1340                         if (lengthofname == 0x5a) {
1341                                 continue;
1342                         }
1343                         track_t tr;
1344
1345                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1346
1347                         //printf("regions/track=%d\n", regionspertrack);
1348                         char name[256] = {0};
1349                         for (j = 0; j < lengthofname; j++) {
1350                                 name[j] = ptfunxored[j+k+13];
1351                         }
1352                         name[j] = '\0';
1353                         tr.name = string(name);
1354                         tr.index = tracknumber++;
1355
1356                         for (j = k; regionspertrack > 0 && j < len; j++) {
1357                                 for (l = j; l < len; l++) {
1358                                         if (    (ptfunxored[l  ] == 0x5a) &&
1359                                                 (ptfunxored[l+1] == 0x08)) {
1360                                                 j = l+1;
1361                                                 break;
1362                                         }
1363                                 }
1364
1365
1366                                 if (regionspertrack == 0) {
1367                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1368                                         break;
1369                                 } else {
1370
1371                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1372                                         vector<region_t>::iterator begin = regions.begin();
1373                                         vector<region_t>::iterator finish = regions.end();
1374                                         vector<region_t>::iterator found;
1375                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1376                                                 tr.reg = (*found);
1377                                         }
1378                                         i = l+16;
1379                                         offset = 0;
1380                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1381                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1382                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1383                                         offset |= (uint32_t)(ptfunxored[i]);
1384                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1385                                         if (tr.reg.length > 0) {
1386                                                 tracks.push_back(tr);
1387                                         }
1388                                         regionspertrack--;
1389                                 }
1390                         }
1391                 }
1392         }
1393 }