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
225
226
227
228
use crate::{rust_bindings::*, session_state::SessionState, split};
use std::os::raw::c_void;
/// The representation of an abl_link instance.
pub struct AblLink {
pub(crate) link: abl_link,
}
unsafe impl Send for AblLink {}
unsafe impl Sync for AblLink {}
impl Drop for AblLink {
fn drop(&mut self) {
unsafe { abl_link_destroy(self.link) }
}
}
impl AblLink {
/// Construct a new AblLink instance with an initial tempo.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn new(bpm: f64) -> AblLink {
AblLink {
link: unsafe { abl_link_create(bpm) },
}
}
/// Is Link currently enabled?
///
/// Thread-safe: yes
///
/// Realtime-safe: yes
pub fn is_enabled(&self) -> bool {
unsafe { abl_link_is_enabled(self.link) }
}
/// Enable/disable Link.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn enable(&self, enable: bool) {
unsafe { abl_link_enable(self.link, enable) }
}
/// Is start/stop synchronization enabled?
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn is_start_stop_sync_enabled(&self) -> bool {
unsafe { abl_link_is_start_stop_sync_enabled(self.link) }
}
/// Enable start/stop synchronization.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn enable_start_stop_sync(&self, enable: bool) {
unsafe { abl_link_enable_start_stop_sync(self.link, enable) }
}
/// How many peers are currently connected in a Link session?
///
/// Thread-safe: yes
///
/// Realtime-safe: yes
pub fn num_peers(&self) -> u64 {
unsafe { abl_link_num_peers(self.link) }
}
/// Get the current link clock time in microseconds.
///
/// Thread-safe: yes
///
/// Realtime-safe: yes
pub fn clock_micros(&self) -> i64 {
unsafe { abl_link_clock_micros(self.link) }
}
/// Capture the current Link Session State from the audio thread.
///
/// Thread-safe: no
///
/// Realtime-safe: yes
///
/// This function should ONLY be called in the audio thread and must not be
/// accessed from any other threads. After capturing the SessionState holds a snapshot
/// of the current Link Session State, so it should be used in a local scope. The
/// session_state should not be created on the audio thread.
pub fn capture_audio_session_state(&self, session_state: &mut SessionState) {
unsafe { abl_link_capture_audio_session_state(self.link, session_state.session_state) }
}
/// Capture the current Link Session State from an application thread.
///
/// Thread-safe: no
///
/// Realtime-safe: yes
///
/// Provides a mechanism for capturing the Link Session State from an
/// application thread (other than the audio thread). After capturing the session_state
/// contains a snapshot of the current Link state, so it should be used in a local
/// scope.
pub fn capture_app_session_state(&self, session_state: &mut SessionState) {
unsafe { abl_link_capture_app_session_state(self.link, session_state.session_state) };
}
/// Commit the given Session State to the Link session from the audio thread.
///
/// Thread-safe: no
///
/// Realtime-safe: yes
///
/// This function should ONLY be called in the audio thread. The given
/// session_state will replace the current Link state. Modifications will be
/// communicated to other peers in the session.
pub fn commit_audio_session_state(&self, session_state: &SessionState) {
unsafe { abl_link_commit_audio_session_state(self.link, session_state.session_state) };
}
/// Commit the given Session State to the Link session from an application thread.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
///
/// The given SessionState will replace the current Link Session State.
/// Modifications of the Session State will be communicated to other peers in the
/// session.
pub fn commit_app_session_state(&self, session_state: &SessionState) {
unsafe { abl_link_commit_app_session_state(self.link, session_state.session_state) };
}
/// Register a callback to be notified when the number of
/// peers in the Link session changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
///
/// The callback is invoked on a Link-managed thread.
pub fn set_num_peers_callback<C: FnMut(u64) + Send + 'static>(&self, mut closure: C) {
unsafe {
let (state, callback) = split::split_closure_trailing_data(&mut closure);
abl_link_set_num_peers_callback(self.link, Some(callback), state);
};
}
/// Register a callback to be notified when the session tempo changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
///
/// The callback is invoked on a Link-managed thread.
pub fn set_tempo_callback<C: FnMut(f64) + Send + 'static>(&self, mut closure: C) {
unsafe {
let (state, callback) = split::split_closure_trailing_data(&mut closure);
abl_link_set_tempo_callback(self.link, Some(callback), state);
}
}
/// Register a callback to be notified when the state of start/stop isPlaying changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
///
/// The callback is invoked on a Link-managed thread.
pub fn set_start_stop_callback<C: FnMut(bool) + Send + 'static>(&self, mut closure: C) {
unsafe {
let (state, callback) = split::split_closure_trailing_data(&mut closure);
abl_link_set_start_stop_callback(self.link, Some(callback), state);
}
}
/// Delete the callback which notifies when the number of peers in the Link session changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn delete_num_peers_callback(&self) {
extern "C" fn empty_fn(_: u64, _: *mut c_void) {}
unsafe {
abl_link_set_num_peers_callback(
self.link,
Some(empty_fn),
std::ptr::null_mut() as *mut c_void,
);
}
}
/// Delete the callback which notifies when the session tempo changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn delete_tempo_callback(&self) {
extern "C" fn empty_fn(_: f64, _: *mut c_void) {}
unsafe {
abl_link_set_tempo_callback(
self.link,
Some(empty_fn),
std::ptr::null_mut() as *mut c_void,
);
}
}
/// Delete the callback which notifies when the state of start/stop isPlaying changes.
///
/// Thread-safe: yes
///
/// Realtime-safe: no
pub fn delete_start_stop_callback(&self) {
extern "C" fn empty_fn(_: bool, _: *mut c_void) {}
unsafe {
abl_link_set_start_stop_callback(
self.link,
Some(empty_fn),
std::ptr::null_mut() as *mut c_void,
);
}
}
}