Skip to content

Commit ce009fe

Browse files
Vampouillemgeier
authored andcommitted
Add set_timebase_callback()
See spatialaudio#14.
1 parent 37f4a28 commit ce009fe

1 file changed

Lines changed: 94 additions & 2 deletions

File tree

‎jack.py‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
} jack_position_bits_t;
107107
/* _jack_position: see below in "packed" section */
108108
typedef struct _jack_position jack_position_t;
109+
typedef void (*JackTimebaseCallback)(jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg);
109110
/* deprecated: jack_transport_bits_t */
110111
/* deprecated: jack_transport_info_t */
111112
@@ -237,11 +238,11 @@
237238
/* TODO: jack_release_timebase */
238239
/* TODO: jack_set_sync_callback */
239240
/* TODO: jack_set_sync_timeout */
240-
/* TODO: jack_set_timebase_callback */
241+
int jack_set_timebase_callback(jack_client_t* client, int conditional, JackTimebaseCallback timebase_callback, void* arg);
241242
int jack_transport_locate(jack_client_t* client, jack_nframes_t frame);
242243
jack_transport_state_t jack_transport_query(const jack_client_t* client, jack_position_t* pos);
243244
jack_nframes_t jack_get_current_transport_frame(const jack_client_t* client);
244-
int jack_transport_reposition(jack_client_t* client, const jack_position_t* pos);
245+
/* TODO: jack_transport_reposition */
245246
void jack_transport_start(jack_client_t* client);
246247
void jack_transport_stop(jack_client_t* client);
247248
/* deprecated: jack_get_transport_info */
@@ -1263,6 +1264,97 @@ def callback_wrapper(_):
12631264
self._ptr, callback_wrapper, _ffi.NULL),
12641265
"Error setting xrun callback")
12651266

1267+
def set_timebase_callback(self, callback=None, conditional=False):
1268+
"""Register as timebase master for the JACK subsystem.
1269+
1270+
The timebase master registers a callback that updates extended
1271+
position information such as beats or timecode whenever
1272+
necessary. Without this extended information, there is no need
1273+
for this function.
1274+
1275+
There is never more than one master at a time. When a new client
1276+
takes over, the former callback is no longer called. Taking over
1277+
the timebase may be done conditionally, so it fails if there was
1278+
a master already.
1279+
1280+
The method may be called whether the client has been activated
1281+
or not.
1282+
1283+
Parameters
1284+
----------
1285+
callback : callable
1286+
Realtime function that returns extended position
1287+
information. Its output affects all of the following process
1288+
cycle. This realtime function must not wait.
1289+
1290+
This function is called immediately after `process()` in the
1291+
same thread whenever the transport is rolling, or when any
1292+
client has requested a new position in the previous
1293+
cycle. The first cycle after `set_timebase_callback()` is
1294+
also treated as a new position, or the first cycle after
1295+
`jack_activate()` if the client had been inactive.
1296+
1297+
The timebase master may not use its `pos` argument to set
1298+
`pos.frame`. To change position, use
1299+
`jack_transport_reposition()` or
1300+
`jack_transport_locate()`. These functions are
1301+
realtime-safe, the `timebase_callback` can call them
1302+
directly.
1303+
1304+
It must have this signature::
1305+
1306+
callback(state:int,
1307+
frames:int,
1308+
pos:CFFI_struct,
1309+
new_pos:bool) -> None
1310+
1311+
* state : int
1312+
current transport state.
1313+
* frames : int
1314+
number of frames in current period.
1315+
* pos : Position CFFI structure
1316+
position structure for the next cycle; `pos.frame` will
1317+
be its frame number. If `new_pos` is `False`, this
1318+
structure contains extended position information from
1319+
the current cycle. If `True`, it contains whatever was
1320+
set by the requester. The `timebase_callback`'s task is
1321+
to update the extended information here. See
1322+
:meth:`transport_query_struct` for the available fields.
1323+
* new_pos : bool
1324+
`True` (non-zero) for a newly requested `pos`, or for
1325+
the first cycle after the `timebase_callback` is
1326+
defined.
1327+
conditional : bool
1328+
set to `True` for a conditional request. If set to `True`
1329+
this call will fail if there is already a timebase master
1330+
1331+
Returns
1332+
-------
1333+
bool
1334+
`True` if new timebase master was set. `False` if
1335+
conditional request fail because another master is already
1336+
registered.
1337+
"""
1338+
if callback is None:
1339+
return lambda cb: self.set_timebase_callback(cb, conditional)
1340+
1341+
@self._callback("JackTimebaseCallback")
1342+
def callback_wrapper(state, frames, pos, new_pos, _):
1343+
return callback(state, frames, pos, bool(new_pos))
1344+
1345+
err = _lib.jack_set_timebase_callback(self._ptr,
1346+
conditional,
1347+
callback_wrapper,
1348+
_ffi.NULL)
1349+
1350+
# Because of a bug in JACK2 version <= 1.9.10, we also check
1351+
# for -1:
1352+
# See https://github.com/jackaudio/jack2/pull/123
1353+
if conditional and err in (_errno.EBUSY, -1):
1354+
return False
1355+
_check(err, "Error setting timebase callback")
1356+
return True
1357+
12661358
def get_uuid_for_client_name(self, name):
12671359
"""Get the session ID for a client name.
12681360

0 commit comments

Comments
 (0)