summaryrefslogtreecommitdiff
path: root/ptfformat.h
blob: dee8c1c48ec570ec3cb47f992976a8e40ceab33e (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
/*
    Copyright (C) 2015  Damien Zammit
    Copyright (C) 2015  Robin Gareus

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

*/
#ifndef PTFFORMAT_H
#define PTFFORMAT_H

#include <stdlib.h>
#include <string.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

static const uint32_t baselut[16] = {
	0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
	0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
	0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
	0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
};

static const uint32_t xorlut[16] = {
	0x00000000, 0x00b00000, 0x0b001000, 0x010b0b00,
	0x10b0b010, 0xb01b01b0, 0xb101bb10, 0xbbbb1110,
	0xbbbb1111, 0xbb01bbb1, 0xb0bb0bb1, 0xbab0b0bb,
	0xab0b0bab, 0xabaaba0b, 0xaabaa0ab, 0xaaaaaaab
};

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

	static const uint32_t swapbytes32 (const uint32_t v) {
		uint32_t rv = 0;
		rv |= ((v >>  0) & 0xf) << 28;
		rv |= ((v >>  4) & 0xf) << 24;
		rv |= ((v >>  8) & 0xf) << 20;
		rv |= ((v >> 12) & 0xf) << 16;
		rv |= ((v >> 16) & 0xf) << 12;
		rv |= ((v >> 20) & 0xf) <<  8;
		rv |= ((v >> 24) & 0xf) <<  4;
		rv |= ((v >> 28) & 0xf) <<  0;
		return rv;
	}

	static const uint64_t gen_secret (int i) {
		assert (i > 0 && i < 256);
		int iwrap = i & 0x7f; // wrap at 0x80;
		int idx;              // mirror at 0x40;
		uint32_t xor_lo = 0;  // 0x40 flag
		bool xor_20 = false;  // mirror at 0x20

		if (iwrap & 0x40) {
			xor_lo = 0x1;
			idx    = 0x80 - iwrap;
		} else {
			idx    = iwrap;
		}

		int i16 = (idx >> 1) & 0xf;
		if (idx & 0x20) {
			i16 = 15 - i16;
			xor_20 = true;
		}

		uint32_t lo = baselut [i16];
		uint32_t xk = xorlut  [i16];

		if (xor_20) {
			lo ^= 0xaaaaaaab;
			xk ^= 1;
		}
		uint32_t hi = swapbytes32 (lo) ^ swapbytes32 (xk);
		return  ((uint64_t)hi << 32) | (lo ^ xor_lo);
	}

	/* Return values:	0            success
				-1           could not open file as ptf
	*/
	int load(std::string path);

	typedef struct wav {
		std::string filename;
		uint16_t    index;

		int64_t     posabsolute;
		int64_t     length;

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

	} wav_t;

	typedef struct region {
		std::string name;
		uint16_t    index;
		int64_t     startpos;
		int64_t     sampleoffset;
		int64_t     length;
		wav_t       wave;

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

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

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

	std::vector<wav_t> audiofiles;
	std::vector<region_t> regions;
	std::vector<track_t> tracks;

	static bool trackexistsin(std::vector<track_t> tr, uint16_t index) {
		std::vector<track_t>::iterator begin = tr.begin();
		std::vector<track_t>::iterator finish = tr.end();
		std::vector<track_t>::iterator found;

		track_t f = { std::string(""), index };

		if ((found = std::find(begin, finish, f)) != finish) {
			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;

		region_t r = { std::string(""), index };

		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 };

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

	uint32_t sessionrate;
	uint8_t version;

	unsigned char c0;
	unsigned char c1;
	unsigned char *ptfunxored;
	int len;

private:
	bool foundin(std::string haystack, std::string needle);
	void parse(void);
	void parse8header(void);
	void parse9header(void);
	void parserest(void);
	std::vector<wav_t> actualwavs;
};


#endif