summaryrefslogtreecommitdiff
path: root/ptformat/ptfformat.h
blob: 136df5a629e0ebbf8b659fc9e0cf901b94204a92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * libptformat - a library to read ProTools sessions
 *
 * Copyright (C) 2015  Damien Zammit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#ifndef PTFFORMAT_H
#define PTFFORMAT_H

#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stdint.h>
#include "ptformat/visibility.h"

#define BITCODE			"0010111100101011"
#define ZMARK			'\x5a'
#define MAX_CONTENT_TYPE	0x3000
#define MAX_CHANNELS_PER_TRACK	8

class LIBPTFORMAT_API PTFFormat {
public:
	PTFFormat();
	~PTFFormat();

	/* Return values:	0            success
				-1           could not parse pt session
	*/
	int load(std::string path, int64_t targetsr);

	/* Return values:	0            success
				-1           could not decrypt pt session
	*/
	int unxor(std::string path);

	struct block_t;

	struct block_t {
		uint8_t zmark;			// 'Z'
		uint16_t block_type;		// type of block
		uint32_t block_size;		// size of block
		uint16_t content_type;		// type of content
		uint32_t offset;		// offset in file
		std::vector<block_t> child;	// vector of child blocks
	};

	struct wav_t {
		std::string filename;
		uint16_t    index;

		int64_t     posabsolute;
		int64_t     length;

		bool operator <(const struct wav_t& other) const {
			return (strcasecmp(this->filename.c_str(),
					other.filename.c_str()) < 0);
		}

		bool operator ==(const struct wav_t& other) const {
			return (this->filename == other.filename ||
				this->index == other.index);
		}

	};

	struct midi_ev_t {
		uint64_t pos;
		uint64_t length;
		uint8_t note;
		uint8_t velocity;
	};

	typedef struct region {
		std::string name;
		uint16_t    index;
		int64_t     startpos;
		int64_t     sampleoffset;
		int64_t     length;
		wav_t       wave;
		std::vector<midi_ev_t> midi;

		bool operator ==(const struct region& other) {
			return (this->index == other.index);
		}

		bool operator <(const struct region& other) const {
			return (strcasecmp(this->name.c_str(),
					other.name.c_str()) < 0);
		}
	} region_t;

	typedef struct compound {
		uint16_t curr_index;
		uint16_t unknown1;
		uint16_t level;
		uint16_t ontopof_index;
		uint16_t next_index;
		std::string name;
	} compound_t;

	typedef struct track {
		std::string name;
		uint16_t    index;
		uint8_t     playlist;
		region_t    reg;

		bool operator <(const struct track& other) const {
			return (this->index < other.index);
		}

		bool operator ==(const struct track& other) {
			return (this->index == other.index);
		}
	} track_t;

	std::vector<block_t> blocks;
	std::vector<wav_t> audiofiles;
	std::vector<region_t> regions;
	std::vector<region_t> midiregions;
	std::vector<compound_t> compounds;
	std::vector<track_t> tracks;
	std::vector<track_t> miditracks;

	bool find_track(uint16_t index, std::vector<track_t>::iterator& ti) {
		std::vector<track_t>::iterator begin = tracks.begin();
		std::vector<track_t>::iterator finish = tracks.end();
		std::vector<track_t>::iterator found;

		// Create dummy track with index
		wav_t w = { std::string(""), 0, 0, 0 };
		std::vector<midi_ev_t> m;
		region_t r = { std::string(""), 0, 0, 0, 0, w, m};
		track_t t = { std::string(""), index, 0, r};

		if ((found = std::find(begin, finish, t)) != finish) {
			ti = found;
			return true;
		}
		return false;
	}

	bool find_region(uint16_t index, std::vector<region_t>::iterator& ri) {
		std::vector<region_t>::iterator begin = regions.begin();
		std::vector<region_t>::iterator finish = regions.end();
		std::vector<region_t>::iterator found;

		wav_t w = { std::string(""), 0, 0, 0 };
		std::vector<midi_ev_t> m;
		region_t r = { std::string(""), index, 0, 0, 0, w, m};

		if ((found = std::find(begin, finish, r)) != finish) {
			ri = found;
			return true;
		}
		return false;
	}
	
	bool find_miditrack(uint16_t index, std::vector<track_t>::iterator& ti) {
		std::vector<track_t>::iterator begin = miditracks.begin();
		std::vector<track_t>::iterator finish = miditracks.end();
		std::vector<track_t>::iterator found;

		// Create dummy track with index
		wav_t w = { std::string(""), 0, 0, 0 };
		std::vector<midi_ev_t> m;
		region_t r = { std::string(""), 0, 0, 0, 0, w, m};
		track_t t = { std::string(""), index, 0, r};

		if ((found = std::find(begin, finish, t)) != finish) {
			ti = found;
			return true;
		}
		return false;
	}

	bool find_midiregion(uint16_t index, std::vector<region_t>::iterator& ri) {
		std::vector<region_t>::iterator begin = midiregions.begin();
		std::vector<region_t>::iterator finish = midiregions.end();
		std::vector<region_t>::iterator found;

		wav_t w = { std::string(""), 0, 0, 0 };
		std::vector<midi_ev_t> m;
		region_t r = { std::string(""), index, 0, 0, 0, w, m};

		if ((found = std::find(begin, finish, r)) != finish) {
			ri = found;
			return true;
		}
		return false;
	}

	bool find_wav(uint16_t index, std::vector<wav_t>::iterator& wi) {
		std::vector<wav_t>::iterator begin = audiofiles.begin();
		std::vector<wav_t>::iterator finish = audiofiles.end();
		std::vector<wav_t>::iterator found;

		wav_t w = { std::string(""), index, 0, 0 };

		if ((found = std::find(begin, finish, w)) != finish) {
			wi = found;
			return true;
		}
		return false;
	}

	static bool regionexistsin(std::vector<region_t> reg, uint16_t index) {
		std::vector<region_t>::iterator begin = reg.begin();
		std::vector<region_t>::iterator finish = reg.end();
		std::vector<region_t>::iterator found;

		wav_t w = { std::string(""), 0, 0, 0 };
		std::vector<midi_ev_t> m;
		region_t r = { std::string(""), index, 0, 0, 0, w, m};

		if ((found = std::find(begin, finish, r)) != finish) {
			return true;
		}
		return false;
	}

	static bool wavexistsin(std::vector<wav_t> wv, uint16_t index) {
		std::vector<wav_t>::iterator begin = wv.begin();
		std::vector<wav_t>::iterator finish = wv.end();
		std::vector<wav_t>::iterator found;

		wav_t w = { std::string(""), index, 0, 0 };

		if ((found = std::find(begin, finish, w)) != finish) {
			return true;
		}
		return false;
	}

	int64_t sessionrate;
	int64_t targetrate;
	uint8_t version;
	uint8_t *product;
	std::string path;

	unsigned char c0;
	unsigned char c1;
	unsigned char *ptfunxored;
	uint64_t len;
	bool is_bigendian;

private:
	bool jumpback(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
	bool jumpto(uint32_t *currpos, unsigned char *buf, const uint32_t maxoffset, const unsigned char *needle, const uint32_t needlelen);
	bool foundin(std::string haystack, std::string needle);
	int64_t foundat(unsigned char *haystack, uint64_t n, const char *needle);
	uint16_t u_endian_read2(unsigned char *buf, bool);
	uint32_t u_endian_read3(unsigned char *buf, bool);
	uint32_t u_endian_read4(unsigned char *buf, bool);
	uint64_t u_endian_read5(unsigned char *buf, bool);
	uint64_t u_endian_read8(unsigned char *buf, bool);


	char *parsestring(uint32_t pos);
	std::string get_content_description(uint16_t ctype);
	int parse(void);
	void parseblocks(void);
	bool parseheader(void);
	bool parserest(void);
	bool parseaudio(void);
	bool parsemidi(void);
	void dump(void);
	bool parse_block_at(uint32_t pos, struct block_t *b, int level);
	void dump_block(struct block_t& b, int level);
	bool parse_version();
	void parse_region_info(uint32_t j, block_t& blk, region_t& r);
	void parse_three_point(uint32_t j, uint64_t& start, uint64_t& offset, uint64_t& length);
	uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative);
	void setrates(void);
	void cleanup(void);
	void parse5header(void);
	void parse7header(void);
	void parse8header(void);
	void parse9header(void);
	void parse10header(void);
	void parserest5(void);
	void parserest89(void);
	void parserest12(void);
	void parseaudio5(void);
	void parsemidi12(void);
	void resort(std::vector<wav_t>& ws);
	void resort(std::vector<region_t>& rs);
	void filter(std::vector<region_t>& rs);
	std::vector<wav_t> actualwavs;
	float ratefactor;
	std::string extension;
	uint32_t upto;
};


#endif