forked from guciek/midialign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracktempo.cpp
183 lines (164 loc) · 4.98 KB
/
tracktempo.cpp
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
/**************************************************************************
* 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, version 2 or 3. *
* Authors: *
* - Lupus Nocawy *
**************************************************************************/
#include "tracktempo.hpp"
#include <vector>
#include <list>
#ifdef DEBUG
#include <cstdio>
#endif
using namespace std;
typedef pair<tick_t, double> tTempoMark;
class ptracktempo {
public:
ptracktempo(double seconds_per_tick) {
tempoList.push_back(tTempoMark(0,seconds_per_tick)); // tick 0
}
void addTempoMark(tick_t tick, double seconds_per_tick) {
list<tTempoMark>::iterator it = tempoList.end();
it--;
while(it->first > tick) {
it--;
}
if(it->first != tick) {
it++;
tempoList.insert(it,tTempoMark(tick,seconds_per_tick)); //~ add element
}
else {
it->second = seconds_per_tick;
}
#ifdef DEBUG
//~ for(it = tempoList.begin(); it!=tempoList.end(); it++){
//~ printf("%llu %lf\n", it->first, it->second);
//~ } printf("\n");
#endif
}
void delTempoMark(tick_t tick) {
if (tick == 0) {
return;
}
list<tTempoMark>::iterator it = tempoList.end();
it--;
while(it->first > tick) {
it--;
}
if(it->first == tick) {
tempoList.erase(it); //~ remove element
}
#ifdef DEBUG
//~ for(it = tempoList.begin(); it!=tempoList.end(); it++){
//~ printf("%llu %lf\n", it->first, it->second);
//~ } printf("\n");
#endif
}
double getTickTime(tick_t tick) {
double total = 0.0;
list<tTempoMark>::iterator it = tempoList.begin();
tick_t prev_tick = 0;
double prev_tempo = it->second;
it++;
while (it != tempoList.end() && it->first < tick) {
total += (it->first - prev_tick)*(prev_tempo);
prev_tick = it->first;
prev_tempo = it->second;
it++;
}
total += (tick - prev_tick)*(prev_tempo);
#ifdef DEBUG
//~ printf("%3llu %10lf\n", tick, total);
#endif
return total;
}
tick_t nextTempoMarkAfter(tick_t tick) const {
//~ list<tTempoMark>::const_iterator it = tempoList.begin();
__typeof__(tempoList.begin()) it = tempoList.begin();
while(it != tempoList.end() && it->first <= tick) {
it++;
}
if(it != tempoList.end()) {
return it->first;
}
else return 0;
}
double readTempoMark(tick_t tick) const {
//~ list<tTempoMark>::const_iterator it = tempoList.end();
__typeof__(tempoList.end()) it = tempoList.end();
do {
it--;
} while (it->first > tick);
return it->second;
}
bool operator==(const ptracktempo& b) const {
#ifdef DEBUG
//~ printf("#ptracktempo::operator== tempoList.size(): %d\n", (int)b.tempoList.size());
//~ printf("
#endif
__typeof__(tempoList.begin()) ita = tempoList.begin();
__typeof__(b.tempoList.begin()) itb = b.tempoList.begin();
double da=0.0, db=0.0;
while (ita != this->tempoList.end() || itb != b.tempoList.end() ) {
if ( da!=db ) return 0;
if ( ita == this->tempoList.end() ) {
db = itb->second;
itb++;
}
else if ( itb == b.tempoList.end() ) {
da = ita->second;
ita++;
}
else if( ita->first > itb->first ) {
db = itb->second;
itb++;
}
else if( ita->first < itb->first ) {
da = ita->second;
ita++;
}
else {
da = ita->second;
ita++;
db = itb->second;
itb++;
}
}
if ( da!=db ) return 0;
return 1;
}
bool operator!=(const ptracktempo& b) const {
return ( !(*this==b));
}
//~ TODO destructor if needed
//~ ~ptracktempo(){};
private:
//~ TODO replace list with vector, for linear insertions, but getTickTime() in O(lg)
list<tTempoMark> tempoList;
};
tracktempo::tracktempo(double seconds_per_tick)
{ p = new ptracktempo(seconds_per_tick); }
tracktempo::tracktempo(const tracktempo & rhs)
{ p = new ptracktempo(*((ptracktempo *)rhs.p)); }
tracktempo::~tracktempo() { delete ((ptracktempo *)p); }
tracktempo & tracktempo::operator=(const tracktempo & rhs) {
if (this == &rhs) return *this;
delete ((ptracktempo *)p);
p = new ptracktempo(*((ptracktempo *)rhs.p));
return *this;
}
void tracktempo::addTempoMark(tick_t tick, double seconds_per_tick)
{ return ((ptracktempo *)p)->addTempoMark(tick, seconds_per_tick); }
void tracktempo::delTempoMark(tick_t tick)
{ return ((ptracktempo *)p)->delTempoMark(tick); }
double tracktempo::getTickTime(tick_t tick) const
{ return ((ptracktempo *)p)->getTickTime(tick); }
tick_t tracktempo::nextTempoMarkAfter(tick_t tick) const
{ return ((ptracktempo *)p)->nextTempoMarkAfter(tick); }
double tracktempo::readTempoMark(tick_t tick) const
{ return ((ptracktempo *)p)->readTempoMark(tick); }
bool tracktempo::operator==(const tracktempo& b) const
{ return ((ptracktempo *)p)->operator==( (*(ptracktempo *)b.p) ); }
bool tracktempo::operator!=(const tracktempo& b) const
{ return ((ptracktempo *)p)->operator!=( (*(ptracktempo *)b.p) ); }