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