/
hylia.cpp
157 lines (126 loc) · 3.52 KB
/
hylia.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
/*
* This file is part of Hylia.
*
* Hylia 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, either version 2 of the License, or
* (at your option) any later version.
*
* Hylia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Hylia. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hylia.h"
#if defined(__clang_major__) && __clang_major__ >= 4
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-local-typedef"
#endif
#include "AudioEngine.hpp"
#if defined(__clang_major__) && __clang_major__ >= 4
# pragma clang diagnostic pop
#endif
#include "ableton/link/HostTimeFilter.hpp"
#include <chrono>
class HyliaTransport {
public:
HyliaTransport()
: link(120.0),
engine(link),
outputLatency(0),
sampleTime(0)
{
}
void setEnabled(const bool enabled)
{
if (enabled)
sampleTime = 0;
link.enable(enabled);
}
void setQuantum(const double quantum)
{
engine.setQuantum(quantum);
}
void setTempo(const double tempo)
{
engine.setTempo(tempo);
}
void setOutputLatency(const uint32_t latency) noexcept
{
outputLatency = latency;
}
void setStartStopSyncEnabled(const bool enabled)
{
engine.setStartStopSyncEnabled(enabled);
}
void startPlaying()
{
engine.startPlaying();
}
void stopPlaying()
{
engine.stopPlaying();
}
void process(const uint32_t frames, LinkTimeInfo* const info)
{
const std::chrono::microseconds hostTime = hostTimeFilter.sampleTimeToHostTime(sampleTime)
+ std::chrono::microseconds(outputLatency);
engine.timelineCallback(hostTime, info);
sampleTime += frames;
}
private:
ableton::Link link;
ableton::link::AudioEngine engine;
ableton::link::HostTimeFilter<ableton::link::platform::Clock> hostTimeFilter;
uint32_t outputLatency, sampleTime;
};
hylia_t* hylia_create(void)
{
HyliaTransport* t;
try {
t = new HyliaTransport();
} catch (...) {
return nullptr;
}
return (hylia_t*)t;
}
void hylia_enable(hylia_t* link, bool on)
{
((HyliaTransport*)link)->setEnabled(on);
}
void hylia_set_beats_per_bar(hylia_t* link, double beatsPerBar)
{
((HyliaTransport*)link)->setQuantum(beatsPerBar);
}
void hylia_set_beats_per_minute(hylia_t* link, double beatsPerMinute)
{
((HyliaTransport*)link)->setTempo(beatsPerMinute);
}
void hylia_set_output_latency(hylia_t* link, uint32_t latency)
{
((HyliaTransport*)link)->setOutputLatency(latency);
}
void hylia_set_start_stop_sync_enabled(hylia_t* link, bool enabled)
{
((HyliaTransport*)link)->setStartStopSyncEnabled(enabled);
}
void hylia_start_playing(hylia_t* link)
{
((HyliaTransport*)link)->startPlaying();
}
void hylia_stop_playing(hylia_t* link)
{
((HyliaTransport*)link)->stopPlaying();
}
void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info)
{
((HyliaTransport*)link)->process(frames, (LinkTimeInfo*)info);
}
void hylia_cleanup(hylia_t* link)
{
delete (HyliaTransport*)link;
}
#include "AudioEngine.cpp"