-
Notifications
You must be signed in to change notification settings - Fork 67
/
AbletonClipInfo.h
224 lines (184 loc) · 5.95 KB
/
AbletonClipInfo.h
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
#pragma once
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <cstring>
#include <portable_endian/include/portable_endian.h>
class AbletonClipInfo {
public:
double loop_start;
double loop_end;
double start_marker;
double hidden_loop_start;
double hidden_loop_end;
double end_marker;
bool loop_on = true;
bool warp_on = false;
// NB: a source of bugs (in the past only hopefully) has been overflow
// arithmetic mistakes related to loop_end/hidden_loop/end_marker. We
// initialize them with very high defaults.
AbletonClipInfo()
: loop_start(0),
loop_end(std::numeric_limits<double>::infinity()),
start_marker(0),
hidden_loop_start(0),
hidden_loop_end(std::numeric_limits<double>::infinity()),
end_marker(std::numeric_limits<double>::infinity()),
loop_on(true),
warp_on(false) {}
std::vector<std::pair<double, double>> warp_markers;
int beat_to_sample(double beat, double sr) {
double seconds;
double bpm;
beat_to_seconds(beat, seconds, bpm);
double samples = seconds * sr;
return samples;
}
void beat_to_seconds(double beat, double& seconds, double& bpm) {
// todo: better handle overflow cases where beat is very small or very large
if (warp_markers.size() < 2) {
bpm = 120.;
seconds = 60. * (beat / bpm);
return;
}
double p1, b1, p2, b2;
auto it = warp_markers.begin();
p1 = it->first;
b1 = it->second;
for (++it; it != warp_markers.end(); it++) {
if (it->second >= beat) {
p2 = it->first;
b2 = it->second;
bpm = (b2 - b1) / (p2 - p1) * 60.0;
// interpolate between the two warp markers
double x = (beat - b1) / (b2 - b1);
seconds = p1 + x * (p2 - p1);
return;
} else {
p1 = it->first;
b1 = it->second;
}
}
int last_index = (int)warp_markers.size() - 1;
p1 = warp_markers.at(last_index - 1).first;
b1 = warp_markers.at(last_index - 1).second;
p2 = warp_markers.at(last_index).first;
b2 = warp_markers.at(last_index).second;
bpm = (b2 - b1) / (p2 - p1) * 60.0;
// interpolate between the two warp markers
double x = (beat - b1) / (b2 - b1);
seconds = p1 + x * (p2 - p1);
return;
}
bool readWarpFile(const char* path) {
reset();
FILE* f = fopen(path, "rb");
if (!f) {
// Return because no warp file was found.
throw std::runtime_error("Error: Couldn't open file at path: " +
std::string(path));
}
if (!read_loop_info(f)) {
fclose(f);
throw std::runtime_error("Error: Couldn't find loop info in warp file: " +
std::string(path));
} else {
// printf("loop_start: %.17g loop_end: %.17g start_marker: %.17g
// hidden_loop_start: %.17g hidden_loop_end: %.17g end_marker: %.17g\n",
// loop_start, loop_end, start_marker, hidden_loop_start,
// hidden_loop_end, end_marker);
}
rewind(f);
double pos, beat;
bool found_one = false;
// the first appearance of "WarpMarker" isn't meaningful.
find_str(f, "WarpMarker");
long last_good_marker = 0;
// Subsequent "WarpMarkers" are meaningful
while (find_str(f, "WarpMarker")) {
if (!fseek(f, 4, SEEK_CUR) && read_double(f, &pos) &&
read_double(f, &beat)) {
found_one = true;
warp_markers.push_back(std::make_pair(pos, beat));
last_good_marker = ftell(f);
} else if (found_one) {
break;
}
}
if (!fseek(f, last_good_marker, SEEK_SET) && !fseek(f, 7, SEEK_CUR) &&
read_bool(f, &loop_on)) {
// Then we read the bool for loop_on
} else {
// Then we couldn't get to the byte for loop_on
fclose(f);
throw std::runtime_error("Error: Couldn't find loop on.");
}
fclose(f);
return true;
}
private:
void reset() {
warp_on = false;
warp_markers.clear();
}
int read_loop_info(FILE* f) {
double sample_offset;
// Assume it was saved with Ableton Live 10
if (find_str(f, "SampleOverViewLevel") &&
find_str(f, "SampleOverViewLevel") && !fseek(f, 71, SEEK_CUR) &&
read_double(f, &loop_start) && read_double(f, &loop_end) &&
read_double(f, &sample_offset) && read_double(f, &hidden_loop_start) &&
read_double(f, &hidden_loop_end) && read_double(f, &end_marker) &&
!fseek(f, 3, SEEK_CUR) && read_bool(f, &warp_on)) {
start_marker = loop_start + sample_offset;
return 1;
} else {
// Ableton Live 9?
rewind(f);
if (find_str(f, "SampleData") && find_str(f, "SampleData") &&
!fseek(f, 2702, SEEK_CUR) && read_double(f, &loop_start) &&
read_double(f, &loop_end) && read_double(f, &sample_offset) &&
read_double(f, &hidden_loop_start) &&
read_double(f, &hidden_loop_end) && read_double(f, &end_marker) &&
!fseek(f, 3, SEEK_CUR) && read_bool(f, &warp_on)) {
start_marker = loop_start + sample_offset;
return 1;
}
}
return 0;
}
int rot(int head, int size) {
if (++head >= size) return head - size;
return head;
}
int find_str(FILE* f, const char* string) {
const int size = (int)strlen(string);
// char buffer[size];
char* buffer = (char*)malloc(size * sizeof(char));
if (fread(buffer, 1, size, f) != size) return 0;
int head = 0, c;
for (;;) {
int rHead = head;
for (int i = 0; i < size; i++) {
if (buffer[rHead] != string[i]) goto next;
rHead = rot(rHead, size);
}
return 1;
next:
if ((c = getc(f)) == EOF) break;
buffer[head] = c;
head = rot(head, size);
}
return 0;
}
int read_double(FILE* f, double* x) {
if (fread(x, 1, 8, f) != 8) return 0;
*(uint64_t*)x = le64toh(*(uint64_t*)x);
return 1;
}
int read_bool(FILE* f, bool* b) {
if (fread(b, 1, 1, f) != 1) return 0;
return 1;
}
};