forked from albertz/music-player-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFader.hpp
70 lines (59 loc) · 1.38 KB
/
Fader.hpp
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
#ifndef MP_FADER_HPP
#define MP_FADER_HPP
#include <stdint.h>
#include <boost/atomic.hpp>
struct PlayerObject;
class Fader {
private:
boost::atomic<uint16_t> cur;
boost::atomic<uint16_t> limit;
boost::atomic<int8_t> inc; // -1 or 1 or 0
public:
Fader();
// If samplerate differs, it resets.
void change(int8_t inc /* 1 for fading in, -1 for fading out */, int Samplerate);
void finish();
void wait(PlayerObject* player);
double sampleFactor() const {
if(limit == 0) return 1;
return double(cur) / limit;
}
bool finished() const {
int8_t incCpy(inc);
if(incCpy == 0) return true;
if(incCpy > 0) return cur >= limit;
if(incCpy < 0) return cur == 0;
assert(false); return false;
}
struct Scope {
Fader& fader;
uint16_t cur, limit;
int8_t inc;
Scope(Fader& _fader) : fader(_fader) {
cur = fader.cur;
limit = fader.limit;
inc = fader.inc;
}
~Scope() {
if(inc == 0) return; // no change
if(inc != fader.inc) return; // changed meanwhile
if(limit != fader.limit) return; // changed meanwhile
fader.cur = cur;
}
double sampleFactor() const {
if(limit == 0) return 1;
return double(cur) / limit;
}
void frameTick() {
if(finished()) return;
cur += inc;
}
bool finished() {
if(inc == 0) return true;
if(inc > 0) return cur >= limit;
if(inc < 0) return cur == 0;
assert(false); return false;
}
};
};
#endif // FADER_HPP