Add Pianokeyboard + Velocity Control to PC Dialog
[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<track_t> tracks;
89
90         static bool regionexistsin(std::vector<region_t> reg, uint16_t index) {
91                 std::vector<region_t>::iterator begin = reg.begin();
92                 std::vector<region_t>::iterator finish = reg.end();
93                 std::vector<region_t>::iterator found;
94
95                 wav_t w = { std::string(""), 0, 0, 0 };
96                 std::vector<midi_ev_t> m;
97                 region_t r = { std::string(""), index, 0, 0, 0, w, m};
98
99                 if ((found = std::find(begin, finish, r)) != finish) {
100                         return true;
101                 }
102                 return false;
103         }
104
105         static bool wavexistsin(std::vector<wav_t> wv, uint16_t index) {
106                 std::vector<wav_t>::iterator begin = wv.begin();
107                 std::vector<wav_t>::iterator finish = wv.end();
108                 std::vector<wav_t>::iterator found;
109
110                 wav_t w = { std::string(""), index, 0, 0 };
111
112                 if ((found = std::find(begin, finish, w)) != finish) {
113                         return true;
114                 }
115                 return false;
116         }
117
118         int64_t sessionrate;
119         int64_t targetrate;
120         uint8_t version;
121         uint8_t *product;
122
123
124         unsigned char c0;
125         unsigned char c1;
126         unsigned char *ptfunxored;
127         uint64_t len;
128
129 private:
130         bool foundin(std::string haystack, std::string needle);
131         int parse(void);
132         bool parse_version();
133         uint8_t gen_xor_delta(uint8_t xor_value, uint8_t mul, bool negative);
134         void setrates(void);
135         void parse5header(void);
136         void parse7header(void);
137         void parse8header(void);
138         void parse9header(void);
139         void parse10header(void);
140         void parserest5(void);
141         void parserest89(void);
142         void parserest10(void);
143         void parseaudio5(void);
144         void parseaudio(void);
145         void parsemidi(void);
146         void resort(std::vector<wav_t>& ws);
147         std::vector<wav_t> actualwavs;
148         float ratefactor;
149         std::string extension;
150         unsigned char key10a;
151         unsigned char key10b;
152 };
153
154
155 #endif