ptformat: Update the lib to 9d0b64f (upstream ptformat)
[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         std::vector<midi_ev_t> midi;
708         midi_ev_t m;
709         bool found = false;
710         int max_regions = regions.size();
711         char midiname[26] = { 0 };
712
713         // Find MdNLB
714         k = 0;
715
716         // Parse all midi tracks, treat each group of midi bytes as a track
717         while (k + 35 < len) {
718                 max_pos = 0;
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                         midi.push_back(m);
770
771                         //fprintf(stderr, "MIDI:  Note=%d Vel=%d Start=%d(samples) Len=%d(samples)\n", midi_note, midi_velocity, midi_pos, midi_len);
772                 }
773
774                 rsize = (uint16_t)regions.size();
775                 snprintf(midiname, 20, "MIDI-%d", rsize - max_regions + 1);
776                 wav_t w = { std::string(""), 0, 0, 0 };
777                 region_t r = {
778                         midiname,
779                         rsize,
780                         (int64_t)(0),
781                         (int64_t)(0),
782                         (int64_t)(max_pos*sessionrate*60/(960000*120)),
783                         w,
784                         midi,
785                 };
786                 regions.push_back(r);
787         }
788 }
789
790 void
791 PTFFormat::parseaudio(void) {
792         uint64_t i,j,k,l;
793
794         // Find end of wav file list
795         k = 0;
796         while (k < len) {
797                 if (            (ptfunxored[k  ] == 0xff) &&
798                                 (ptfunxored[k+1] == 0xff) &&
799                                 (ptfunxored[k+2] == 0xff) &&
800                                 (ptfunxored[k+3] == 0xff)) {
801                         break;
802                 }
803                 k++;
804         }
805
806         // Find actual wav names
807         bool first = true;
808         uint16_t numberofwavs;
809         char wavname[256];
810         for (i = k; i > 4; i--) {
811                 if (            ((ptfunxored[i  ] == 'W') || (ptfunxored[i  ] == 'A')) &&
812                                 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
813                                 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
814                                 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
815                         j = i-4;
816                         l = 0;
817                         while (ptfunxored[j] != '\0') {
818                                 wavname[l] = ptfunxored[j];
819                                 l++;
820                                 j--;
821                         }
822                         wavname[l] = 0;
823                         if (ptfunxored[i] == 'W') {
824                                 extension = string(".wav");
825                         } else {
826                                 extension = string(".aif");
827                         }
828                         //uint8_t playlist = ptfunxored[j-8];
829
830                         if (first) {
831                                 first = false;
832                                 for (j = k; j > 4; j--) {
833                                         if (    (ptfunxored[j  ] == 0x01) &&
834                                                 (ptfunxored[j-1] == 0x5a)) {
835
836                                                 numberofwavs = 0;
837                                                 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
838                                                 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
839                                                 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
840                                                 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
841                                                 //printf("%d wavs\n", numberofwavs);
842                                                 break;
843                                         }
844                                 k--;
845                                 }
846                         }
847
848                         std::string wave = string(wavname);
849                         std::reverse(wave.begin(), wave.end());
850                         wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
851
852                         if (foundin(wave, string(".grp"))) {
853                                 continue;
854                         }
855
856                         actualwavs.push_back(f);
857
858                         numberofwavs--;
859                         if (numberofwavs <= 0)
860                                 break;
861                 }
862         }
863 }
864
865 void
866 PTFFormat::parserest89(void) {
867         uint64_t i,j,k,l;
868         // Find Regions
869         uint8_t startbytes = 0;
870         uint8_t lengthbytes = 0;
871         uint8_t offsetbytes = 0;
872         uint8_t somethingbytes = 0;
873         uint8_t skipbytes = 0;
874
875         k = 0;
876         while (k < len) {
877                 if (            (ptfunxored[k  ] == 'S') &&
878                                 (ptfunxored[k+1] == 'n') &&
879                                 (ptfunxored[k+2] == 'a') &&
880                                 (ptfunxored[k+3] == 'p')) {
881                         break;
882                 }
883                 k++;
884         }
885         uint16_t rindex = 0;
886         uint32_t findex = 0;
887         for (i = k; i < len-70; i++) {
888                 if (            (ptfunxored[i  ] == 0x5a) &&
889                                 (ptfunxored[i+1] == 0x0a)) {
890                                 break;
891                 }
892                 if (            (ptfunxored[i  ] == 0x5a) &&
893                                 (ptfunxored[i+1] == 0x0c)) {
894
895                         uint8_t lengthofname = ptfunxored[i+9];
896
897                         char name[256] = {0};
898                         for (j = 0; j < lengthofname; j++) {
899                                 name[j] = ptfunxored[i+13+j];
900                         }
901                         name[j] = '\0';
902                         j += i+13;
903                         //uint8_t disabled = ptfunxored[j];
904
905                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
906                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
907                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
908                         somethingbytes = (ptfunxored[j+3] & 0xf);
909                         skipbytes = ptfunxored[j+4];
910                         findex = ptfunxored[j+5
911                                         +startbytes
912                                         +lengthbytes
913                                         +offsetbytes
914                                         +somethingbytes
915                                         +skipbytes
916                                         +40];
917                         /*rindex = ptfunxored[j+5
918                                         +startbytes
919                                         +lengthbytes
920                                         +offsetbytes
921                                         +somethingbytes
922                                         +skipbytes
923                                         +24];
924                         */
925                         uint32_t sampleoffset = 0;
926                         switch (offsetbytes) {
927                         case 4:
928                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
929                         case 3:
930                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
931                         case 2:
932                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
933                         case 1:
934                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
935                         default:
936                                 break;
937                         }
938                         j+=offsetbytes;
939                         uint32_t length = 0;
940                         switch (lengthbytes) {
941                         case 4:
942                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
943                         case 3:
944                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
945                         case 2:
946                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
947                         case 1:
948                                 length |= (uint32_t)(ptfunxored[j+5]);
949                         default:
950                                 break;
951                         }
952                         j+=lengthbytes;
953                         uint32_t start = 0;
954                         switch (startbytes) {
955                         case 4:
956                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
957                         case 3:
958                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
959                         case 2:
960                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
961                         case 1:
962                                 start |= (uint32_t)(ptfunxored[j+5]);
963                         default:
964                                 break;
965                         }
966                         j+=startbytes;
967                         /*
968                         uint32_t something = 0;
969                         switch (somethingbytes) {
970                         case 4:
971                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
972                         case 3:
973                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
974                         case 2:
975                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
976                         case 1:
977                                 something |= (uint32_t)(ptfunxored[j+5]);
978                         default:
979                                 break;
980                         }
981                         j+=somethingbytes;
982                         */
983                         std::string filename = string(name) + extension;
984                         wav_t f = {
985                                 filename,
986                                 0,
987                                 (int64_t)(start*ratefactor),
988                                 (int64_t)(length*ratefactor),
989                         };
990
991                         f.index = findex;
992                         //printf("something=%d\n", something);
993
994                         vector<wav_t>::iterator begin = actualwavs.begin();
995                         vector<wav_t>::iterator finish = actualwavs.end();
996                         vector<wav_t>::iterator found;
997                         // Add file to list only if it is an actual wav
998                         if ((found = std::find(begin, finish, f)) != finish) {
999                                 audiofiles.push_back(f);
1000                                 // Also add plain wav as region
1001                                 std::vector<midi_ev_t> m;
1002                                 region_t r = {
1003                                         name,
1004                                         rindex,
1005                                         (int64_t)(start*ratefactor),
1006                                         (int64_t)(sampleoffset*ratefactor),
1007                                         (int64_t)(length*ratefactor),
1008                                         f,
1009                                         m
1010                                 };
1011                                 regions.push_back(r);
1012                         // Region only
1013                         } else {
1014                                 if (foundin(filename, string(".grp"))) {
1015                                         continue;
1016                                 }
1017                                 std::vector<midi_ev_t> m;
1018                                 region_t r = {
1019                                         name,
1020                                         rindex,
1021                                         (int64_t)(start*ratefactor),
1022                                         (int64_t)(sampleoffset*ratefactor),
1023                                         (int64_t)(length*ratefactor),
1024                                         f,
1025                                         m
1026                                 };
1027                                 regions.push_back(r);
1028                         }
1029                         rindex++;
1030                 }
1031         }
1032
1033         while (k < len) {
1034                 if (            (ptfunxored[k  ] == 0x5a) &&
1035                                 (ptfunxored[k+1] == 0x03)) {
1036                                 break;
1037                 }
1038                 k++;
1039         }
1040         while (k < len) {
1041                 if (            (ptfunxored[k  ] == 0x5a) &&
1042                                 (ptfunxored[k+1] == 0x02)) {
1043                                 break;
1044                 }
1045                 k++;
1046         }
1047         k++;
1048
1049         //  Tracks
1050         uint32_t offset;
1051         uint32_t tracknumber = 0;
1052         uint32_t regionspertrack = 0;
1053         for (;k < len; k++) {
1054                 if (    (ptfunxored[k  ] == 0x5a) &&
1055                         (ptfunxored[k+1] == 0x04)) {
1056                         break;
1057                 }
1058                 if (    (ptfunxored[k  ] == 0x5a) &&
1059                         (ptfunxored[k+1] == 0x02)) {
1060
1061                         uint8_t lengthofname = 0;
1062                         lengthofname = ptfunxored[k+9];
1063                         if (lengthofname == 0x5a) {
1064                                 continue;
1065                         }
1066                         track_t tr;
1067
1068                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1069
1070                         //printf("regions/track=%d\n", regionspertrack);
1071                         char name[256] = {0};
1072                         for (j = 0; j < lengthofname; j++) {
1073                                 name[j] = ptfunxored[j+k+13];
1074                         }
1075                         name[j] = '\0';
1076                         tr.name = string(name);
1077                         tr.index = tracknumber++;
1078
1079                         for (j = k; regionspertrack > 0 && j < len; j++) {
1080                                 for (l = j; l < len; l++) {
1081                                         if (    (ptfunxored[l  ] == 0x5a) &&
1082                                                 (ptfunxored[l+1] == 0x07)) {
1083                                                 j = l;
1084                                                 break;
1085                                         }
1086                                 }
1087
1088
1089                                 if (regionspertrack == 0) {
1090                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1091                                         break;
1092                                 } else {
1093
1094                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1095                                         vector<region_t>::iterator begin = regions.begin();
1096                                         vector<region_t>::iterator finish = regions.end();
1097                                         vector<region_t>::iterator found;
1098                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1099                                                 tr.reg = (*found);
1100                                         }
1101                                         i = l+16;
1102                                         offset = 0;
1103                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1104                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1105                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1106                                         offset |= (uint32_t)(ptfunxored[i]);
1107                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1108                                         if (tr.reg.length > 0) {
1109                                                 tracks.push_back(tr);
1110                                         }
1111                                         regionspertrack--;
1112                                 }
1113                         }
1114                 }
1115         }
1116 }
1117
1118 void
1119 PTFFormat::parserest10(void) {
1120         uint64_t i,j,k,l;
1121         // Find Regions
1122         uint8_t startbytes = 0;
1123         uint8_t lengthbytes = 0;
1124         uint8_t offsetbytes = 0;
1125         uint8_t somethingbytes = 0;
1126         uint8_t skipbytes = 0;
1127
1128         k = 0;
1129         while (k < len) {
1130                 if (            (ptfunxored[k  ] == 'S') &&
1131                                 (ptfunxored[k+1] == 'n') &&
1132                                 (ptfunxored[k+2] == 'a') &&
1133                                 (ptfunxored[k+3] == 'p')) {
1134                         break;
1135                 }
1136                 k++;
1137         }
1138         for (i = k; i < len-70; i++) {
1139                 if (            (ptfunxored[i  ] == 0x5a) &&
1140                                 (ptfunxored[i+1] == 0x02)) {
1141                                 k = i;
1142                                 break;
1143                 }
1144         }
1145         k++;
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         uint16_t rindex = 0;
1155         uint32_t findex = 0;
1156         for (i = k; i < len-70; i++) {
1157                 if (            (ptfunxored[i  ] == 0x5a) &&
1158                                 (ptfunxored[i+1] == 0x08)) {
1159                                 break;
1160                 }
1161                 if (            (ptfunxored[i  ] == 0x5a) &&
1162                                 (ptfunxored[i+1] == 0x01)) {
1163
1164                         uint8_t lengthofname = ptfunxored[i+9];
1165                         if (ptfunxored[i+13] == 0x5a) {
1166                                 continue;
1167                         }
1168                         char name[256] = {0};
1169                         for (j = 0; j < lengthofname; j++) {
1170                                 name[j] = ptfunxored[i+13+j];
1171                         }
1172                         name[j] = '\0';
1173                         j += i+13;
1174                         //uint8_t disabled = ptfunxored[j];
1175                         //printf("%s\n", name);
1176
1177                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1178                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1179                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1180                         somethingbytes = (ptfunxored[j+3] & 0xf);
1181                         skipbytes = ptfunxored[j+4];
1182                         findex = ptfunxored[j+5
1183                                         +startbytes
1184                                         +lengthbytes
1185                                         +offsetbytes
1186                                         +somethingbytes
1187                                         +skipbytes
1188                                         +37];
1189                         /*rindex = ptfunxored[j+5
1190                                         +startbytes
1191                                         +lengthbytes
1192                                         +offsetbytes
1193                                         +somethingbytes
1194                                         +skipbytes
1195                                         +24];
1196                         */
1197                         uint32_t sampleoffset = 0;
1198                         switch (offsetbytes) {
1199                         case 4:
1200                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1201                         case 3:
1202                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1203                         case 2:
1204                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1205                         case 1:
1206                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1207                         default:
1208                                 break;
1209                         }
1210                         j+=offsetbytes;
1211                         uint32_t length = 0;
1212                         switch (lengthbytes) {
1213                         case 4:
1214                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
1215                         case 3:
1216                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
1217                         case 2:
1218                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
1219                         case 1:
1220                                 length |= (uint32_t)(ptfunxored[j+5]);
1221                         default:
1222                                 break;
1223                         }
1224                         j+=lengthbytes;
1225                         uint32_t start = 0;
1226                         switch (startbytes) {
1227                         case 4:
1228                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
1229                         case 3:
1230                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
1231                         case 2:
1232                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
1233                         case 1:
1234                                 start |= (uint32_t)(ptfunxored[j+5]);
1235                         default:
1236                                 break;
1237                         }
1238                         j+=startbytes;
1239                         /*
1240                         uint32_t something = 0;
1241                         switch (somethingbytes) {
1242                         case 4:
1243                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
1244                         case 3:
1245                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
1246                         case 2:
1247                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
1248                         case 1:
1249                                 something |= (uint32_t)(ptfunxored[j+5]);
1250                         default:
1251                                 break;
1252                         }
1253                         j+=somethingbytes;
1254                         */
1255                         std::string filename = string(name) + extension;
1256                         wav_t f = {
1257                                 filename,
1258                                 0,
1259                                 (int64_t)(start*ratefactor),
1260                                 (int64_t)(length*ratefactor),
1261                         };
1262
1263                         if (strlen(name) == 0) {
1264                                 continue;
1265                         }
1266                         if (length == 0) {
1267                                 continue;
1268                         }
1269                         f.index = findex;
1270                         //printf("something=%d\n", something);
1271
1272                         vector<wav_t>::iterator begin = actualwavs.begin();
1273                         vector<wav_t>::iterator finish = actualwavs.end();
1274                         vector<wav_t>::iterator found;
1275                         // Add file to list only if it is an actual wav
1276                         if ((found = std::find(begin, finish, f)) != finish) {
1277                                 audiofiles.push_back(f);
1278                                 // Also add plain wav as region
1279                                 std::vector<midi_ev_t> m;
1280                                 region_t r = {
1281                                         name,
1282                                         rindex,
1283                                         (int64_t)(start*ratefactor),
1284                                         (int64_t)(sampleoffset*ratefactor),
1285                                         (int64_t)(length*ratefactor),
1286                                         f,
1287                                         m
1288                                 };
1289                                 regions.push_back(r);
1290                         // Region only
1291                         } else {
1292                                 if (foundin(filename, string(".grp"))) {
1293                                         continue;
1294                                 }
1295                                 std::vector<midi_ev_t> m;
1296                                 region_t r = {
1297                                         name,
1298                                         rindex,
1299                                         (int64_t)(start*ratefactor),
1300                                         (int64_t)(sampleoffset*ratefactor),
1301                                         (int64_t)(length*ratefactor),
1302                                         f,
1303                                         m
1304                                 };
1305                                 regions.push_back(r);
1306                         }
1307                         rindex++;
1308                         //printf("%s\n", name);
1309                 }
1310         }
1311         //  Tracks
1312         uint32_t offset;
1313         uint32_t tracknumber = 0;
1314         uint32_t regionspertrack = 0;
1315         for (;k < len; k++) {
1316                 if (    (ptfunxored[k  ] == 0x5a) &&
1317                         (ptfunxored[k+1] == 0x08)) {
1318                         break;
1319                 }
1320         }
1321         k++;
1322         for (;k < len; k++) {
1323                 if (    (ptfunxored[k  ] == 0x5a) &&
1324                         (ptfunxored[k+1] == 0x04)) {
1325                         break;
1326                 }
1327                 if (    (ptfunxored[k  ] == 0x5a) &&
1328                         (ptfunxored[k+1] == 0x02)) {
1329
1330                         uint8_t lengthofname = 0;
1331                         lengthofname = ptfunxored[k+9];
1332                         if (lengthofname == 0x5a) {
1333                                 continue;
1334                         }
1335                         track_t tr;
1336
1337                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1338
1339                         //printf("regions/track=%d\n", regionspertrack);
1340                         char name[256] = {0};
1341                         for (j = 0; j < lengthofname; j++) {
1342                                 name[j] = ptfunxored[j+k+13];
1343                         }
1344                         name[j] = '\0';
1345                         tr.name = string(name);
1346                         tr.index = tracknumber++;
1347
1348                         for (j = k; regionspertrack > 0 && j < len; j++) {
1349                                 for (l = j; l < len; l++) {
1350                                         if (    (ptfunxored[l  ] == 0x5a) &&
1351                                                 (ptfunxored[l+1] == 0x08)) {
1352                                                 j = l+1;
1353                                                 break;
1354                                         }
1355                                 }
1356
1357
1358                                 if (regionspertrack == 0) {
1359                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1360                                         break;
1361                                 } else {
1362
1363                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1364                                         vector<region_t>::iterator begin = regions.begin();
1365                                         vector<region_t>::iterator finish = regions.end();
1366                                         vector<region_t>::iterator found;
1367                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1368                                                 tr.reg = (*found);
1369                                         }
1370                                         i = l+16;
1371                                         offset = 0;
1372                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1373                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1374                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1375                                         offset |= (uint32_t)(ptfunxored[i]);
1376                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1377                                         if (tr.reg.length > 0) {
1378                                                 tracks.push_back(tr);
1379                                         }
1380                                         regionspertrack--;
1381                                 }
1382                         }
1383                 }
1384         }
1385 }