933d2be56e69d2bb308f57f01380c4868a36838a
[ardour.git] / libs / ptformat / ptformat / ptfformat.h
1 /*
2  * libptformat - a library to read ProTools sessions
3  *
4  * Copyright (C) 2015  Damien Zammit
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 #ifndef PTFFORMAT_H
22 #define PTFFORMAT_H
23
24 #include <string>
25 #include <cstring>
26 #include <algorithm>
27 #include <vector>
28 #include <stdint.h>
29 #include "ptformat/visibility.h"
30
31 class LIBPTFORMAT_API PTFFormat {
32 public:
33         PTFFormat();
34         ~PTFFormat();
35
36         /* Return values:       0            success
37                                 -1           could not parse pt session
38         */
39         int load(std::string path, int64_t targetsr);
40
41         /* Return values:       0            success
42                                 -1           could not decrypt pt session
43         */
44         int unxor(std::string path);
45
46         struct wav_t {
47                 std::string filename;
48                 uint16_t    index;
49
50                 int64_t     posabsolute;
51                 int64_t     length;
52
53                 bool operator <(const struct wav_t& other) const {
54                         return (strcasecmp(this->filename.c_str(),
55                                         other.filename.c_str()) < 0);
56                 }
57
58                 bool operator ==(const struct wav_t& other) const {
59                         return (this->filename == other.filename ||
60                                 this->index == other.index);
61                 }
62
63         };
64
65         struct midi_ev_t {
66                 uint64_t pos;
67                 uint64_t length;
68                 uint8_t note;
69                 uint8_t velocity;
70         };
71
72         typedef struct region {
73                 std::string name;
74                 uint16_t    index;
75                 int64_t     startpos;
76                 int64_t     sampleoffset;
77                 int64_t     length;
78                 wav_t       wave;
79                 std::vector<midi_ev_t> midi;
80
81                 bool operator ==(const struct region& other) {
82                         return (this->index == other.index);
83                 }
84
85                 bool operator <(const struct region& other) const {
86                         return (strcasecmp(this->name.c_str(),
87                                         other.name.c_str()) < 0);
88                 }
89         } region_t;
90
91         typedef struct compound {
92                 uint16_t curr_index;
93                 uint16_t unknown1;
94                 uint16_t level;
95                 uint16_t ontopof_index;
96                 uint16_t next_index;
97                 std::string name;
98         } compound_t;
99
100         typedef struct track {
101                 std::string name;
102                 uint16_t    index;
103                 uint8_t     playlist;
104                 region_t    reg;
105
106                 bool operator ==(const struct track& other) {
107                         return (this->name == other.name);
108                 }
109         } track_t;
110
111         std::vector<wav_t> audiofiles;
112         std::vector<region_t> regions;
113         std::vector<region_t> midiregions;
114         std::vector<compound_t> compounds;
115         std::vector<track_t> tracks;
116         std::vector<track_t> miditracks;
117
118         static bool regionexistsin(std::vector<region_t> reg, uint16_t index) {
119                 std::vector<region_t>::iterator begin = reg.begin();
120                 std::vector<region_t>::iterator finish = reg.end();
121                 std::vector<region_t>::iterator found;
122
123                 wav_t w = { std::string(""), 0, 0, 0 };
124                 std::vector<midi_ev_t> m;
125                 region_t r = { std::string(""), index, 0, 0, 0, w, m};
126
127                 if ((found = std::find(begin, finish, r)) != finish) {
128                         return true;
129                 }
130                 return false;
131         }
132
133         static bool wavexistsin(std::vector<wav_t> wv, uint16_t index) {
134                 std::vector<wav_t>::iterator begin = wv.begin();
135                 std::vector<wav_t>::iterator finish = wv.end();
136                 std::vector<wav_t>::iterator found;
137
138                 wav_t w = { std::string(""), index, 0, 0 };
139
140                 if ((found = std::find(begin, finish, w)) != finish) {
141                         return true;
142                 }
143                 return false;
144         }
145
146         int64_t sessionrate;
147         int64_t targetrate;
148         uint8_t version;
149         uint8_t *product;
150         std::string path;
151
152         unsigned char c0;
153         unsigned char c1;
154         unsigned char *ptfunxored;
155         uint64_t len;
156
157 private:
158         bool jumpback(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
159         bool jumpto(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
160         bool foundin(std::string haystack, std::string needle);
161         int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle);
162         int parse(void);
163         bool parse_version();
164         uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative);
165         void setrates(void);
166         void cleanup(void);
167         void parse5header(void);
168         void parse7header(void);
169         void parse8header(void);
170         void parse9header(void);
171         void parse10header(void);
172         void parserest5(void);
173         void parserest89(void);
174         void parserest12(void);
175         void parseaudio5(void);
176         void parseaudio(void);
177         void parsemidi(void);
178         void parsemidi12(void);
179         void resort(std::vector<wav_t>& ws);
180         void resort(std::vector<region_t>& rs);
181         void filter(std::vector<region_t>& rs);
182         std::vector<wav_t> actualwavs;
183         float ratefactor;
184         std::string extension;
185 };
186
187
188 #endif