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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::{ffi, ptr};
use jack_sys as j;
use callbacks;
use port;
use enums::*;
use flags::*;
use port::*;
use utils;
use callbacks::JackHandler;
#[derive(Clone, Copy, Debug)]
pub struct CycleTimes {
pub current_frames: u32,
pub current_usecs: u64,
pub next_usecs: u64,
pub period_usecs: f32,
}
#[derive(Debug)]
pub struct Client<T: JackHandler> {
client: *mut j::jack_client_t,
handler: *mut T,
status: ClientStatus,
}
impl<T: JackHandler> Client<T> {
pub fn name_size() -> usize {
let s = unsafe { j::jack_client_name_size() - 1 };
s as usize
}
pub unsafe fn type_buffer_size(&self, port_type: &str) -> usize {
let port_type = ffi::CString::new(port_type).unwrap();
let n = j::jack_port_type_get_buffer_size(self.client, port_type.as_ptr());
n
}
pub fn open(client_name: &str, options: ClientOptions) -> Result<Self, JackErr> {
let mut status_bits = 0;
let client = unsafe {
let client_name = ffi::CString::new(client_name).unwrap();
j::jack_client_open(ffi::CString::new(client_name).unwrap().as_ptr(),
options.bits(),
&mut status_bits)
};
let status = ClientStatus::from_bits(status_bits).unwrap_or(UNKNOWN_ERROR);
if client.is_null() {
Err(JackErr::ClientError(status))
} else {
Ok(Client {
client: client,
handler: ptr::null_mut(),
status: status,
})
}
}
pub fn close(self) {}
pub fn status(&self) -> ClientStatus {
self.status
}
pub fn name<'a>(&'a self) -> &'a str {
unsafe {
let ptr = j::jack_get_client_name(self.client);
let cstr = ffi::CStr::from_ptr(ptr);
cstr.to_str().unwrap()
}
}
pub fn uuid<'a>(&'a self) -> &'a str {
self.uuid_by_name(self.name()).unwrap()
}
pub fn thread_id<P>(&self) -> P {
unimplemented!();
}
pub fn name_by_uuid<'a>(&'a self, uuid: &str) -> Option<&'a str> {
unsafe {
let uuid = ffi::CString::new(uuid).unwrap();
let name_ptr = j::jack_get_client_name_by_uuid(self.client, uuid.as_ptr());
if name_ptr.is_null() {
None
} else {
Some(ffi::CStr::from_ptr(name_ptr).to_str().unwrap())
}
}
}
pub fn uuid_by_name<'a>(&'a self, name: &str) -> Option<&'a str> {
unsafe {
let name = ffi::CString::new(name).unwrap();
let uuid_ptr = j::jack_get_client_name_by_uuid(self.client, name.as_ptr());
if uuid_ptr.is_null() {
None
} else {
Some(ffi::CStr::from_ptr(uuid_ptr).to_str().unwrap())
}
}
}
pub fn ports(&self,
port_name_pattern: Option<&str>,
type_name_pattern: Option<&str>,
flags: PortFlags)
-> Vec<String> {
let pnp = ffi::CString::new(port_name_pattern.unwrap_or("")).unwrap();
let tnp = ffi::CString::new(type_name_pattern.unwrap_or("")).unwrap();
let flags = flags.bits() as u64;
unsafe {
utils::collect_strs(j::jack_get_ports(self.client, pnp.as_ptr(), tnp.as_ptr(), flags))
}
}
pub fn port_by_name(&self, port_name: &str) -> Option<Port> {
let port_name = ffi::CString::new(port_name).unwrap();
unsafe {
ptrs_to_port(self.client,
j::jack_port_by_name(self.client, port_name.as_ptr()))
}
}
pub fn port_by_id(&self, port_id: u32) -> Option<Port> {
unsafe { ptrs_to_port(self.client, j::jack_port_by_id(self.client, port_id)) }
}
pub fn activate(&mut self, handler: T) -> Result<(), JackErr> {
let handler = try!(unsafe { callbacks::register_callbacks(self.client, handler) });
if handler.is_null() {
Err(JackErr::CallbackRegistrationError)
} else {
let res = unsafe { j::jack_activate(self.client) };
match res {
0 => {
self.handler = handler;
Ok(())
}
_ => {
unsafe { Box::from_raw(handler) };
Err(JackErr::ClientActivationError)
}
}
}
}
pub fn deactivate(&mut self) -> Result<Box<T>, JackErr> {
if self.handler.is_null() {
return Err(JackErr::InvalidDeactivation);
}
let res = unsafe { j::jack_deactivate(self.client) };
let handler_ptr = self.handler;
self.handler = ptr::null_mut();
try!(unsafe { callbacks::clear_callbacks(self.client) });
match res {
0 => Ok(unsafe { Box::from_raw(handler_ptr) }),
_ => Err(JackErr::ClientDeactivationError),
}
}
pub fn register_port(&mut self,
port_name: &str,
port_type: &str,
flags: PortFlags,
buffer_size: Option<usize>)
-> Result<Port, JackErr> {
let port_name = ffi::CString::new(port_name).unwrap();
let port_type = ffi::CString::new(port_type).unwrap();
let port_flags = flags.bits() as u64;
let buffer_size = buffer_size.unwrap_or(0) as u64;
let port = unsafe {
let ptr = j::jack_port_register(self.client,
port_name.as_ptr(),
port_type.as_ptr(),
port_flags,
buffer_size);
ptrs_to_port(self.client, ptr)
};
match port {
Some(p) => Ok(p),
None => Err(JackErr::PortRegistrationError),
}
}
pub fn is_mine(&self, port: &Port) -> bool {
match unsafe { j::jack_port_is_mine(self.client, port::port_pointer(port)) } {
0 => false,
_ => true,
}
}
pub fn request_monitor(&self, port_name: &str, enable_monitor: bool) -> Result<(), JackErr> {
let port_name = ffi::CString::new(port_name).unwrap();
let onoff = match enable_monitor {
true => 1,
false => 0,
};
let res =
unsafe { j::jack_port_request_monitor_by_name(self.client, port_name.as_ptr(), onoff) };
match res {
0 => Ok(()),
_ => Err(JackErr::PortMonitorError),
}
}
pub fn connect_ports(&self, source_port: &str, destination_port: &str) -> Result<(), JackErr> {
let source_port = ffi::CString::new(source_port).unwrap();
let destination_port = ffi::CString::new(destination_port).unwrap();
let res = unsafe {
j::jack_connect(self.client, source_port.as_ptr(), destination_port.as_ptr())
};
match res {
0 => Ok(()),
::libc::EEXIST => Err(JackErr::PortNotFound),
_ => Err(JackErr::PortConnectionError),
}
}
pub fn disconnect_ports(&self,
source_port: &str,
destination_port: &str)
-> Result<(), JackErr> {
let source_port = ffi::CString::new(source_port).unwrap();
let destination_port = ffi::CString::new(destination_port).unwrap();
let res = unsafe {
j::jack_disconnect(self.client, source_port.as_ptr(), destination_port.as_ptr())
};
match res {
0 => Ok(()),
_ => Err(JackErr::PortDisconnectionError),
}
}
pub fn sample_rate(&self) -> usize {
let srate = unsafe { j::jack_get_sample_rate(self.client) };
srate as usize
}
pub fn buffer_size(&self) -> usize {
let bsize = unsafe { j::jack_get_buffer_size(self.client) };
bsize as usize
}
pub fn cpu_load(&self) -> f32 {
let load = unsafe { j::jack_cpu_load(self.client) };
load
}
pub fn set_freewheel(&self, enable: bool) -> Result<(), JackErr> {
let onoff = match enable {
true => 0,
false => 1,
};
match unsafe { j::jack_set_freewheel(self.client, onoff) } {
0 => Ok(()),
_ => Err(JackErr::FreewheelError),
}
}
pub fn set_buffer_size(&self, n_frames: usize) -> Result<(), JackErr> {
let n_frames = n_frames as u32;
let res = unsafe { j::jack_set_buffer_size(self.client, n_frames) };
match res {
0 => Ok(()),
_ => Err(JackErr::SetBufferSizeError),
}
}
pub fn frames_since_cycle_start(&self) -> u32 {
unsafe { j::jack_frames_since_cycle_start(self.client) }
}
pub fn frame_time(&self) -> u32 {
unsafe { j::jack_frame_time(self.client) }
}
pub fn last_frame_time(&self) -> u32 {
unsafe { j::jack_last_frame_time(self.client) }
}
pub fn cycle_times(&self) -> Result<CycleTimes, JackErr> {
let mut current_frames: u32 = 0;
let mut current_usecs: u64 = 0;
let mut next_usecs: u64 = 0;
let mut period_usecs: f32 = 0.0;
let res = unsafe {
j::jack_get_cycle_times(self.client,
&mut current_frames,
&mut current_usecs,
&mut next_usecs,
&mut period_usecs)
};
match res {
0 => {
Ok(CycleTimes {
current_frames: current_frames,
current_usecs: current_usecs,
next_usecs: next_usecs,
period_usecs: period_usecs,
})
},
_ => Err(JackErr::TimeError),
}
}
pub fn frames_to_time(&self, n_frames: u32) -> u64 {
unsafe { j::jack_frames_to_time(self.client, n_frames) }
}
pub fn time_to_frames(&self, t: u64) -> u32 {
unsafe { j::jack_time_to_frames(self.client, t) }
}
}
impl<T: JackHandler> Drop for Client<T> {
fn drop(&mut self) {
let _ = self.deactivate();
if !self.client.is_null() {
let res = unsafe { j::jack_client_close(self.client) };
assert_eq!(res, 0);
self.client = ptr::null_mut();
}
}
}