Repository navigation
Adding some things I needed #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a3da8ae
The server shouldn't start by default
amstan 4316c78
Ports now support is_physical
amstan 3b65749
Ports now can be of type midi
amstan c76eb06
Added jack_ringbuffer_t from ringbuffer.h
amstan 62f4caf
Changed cffi to use verify instead of dlopen
amstan 6a9b75d
Allow process callback to be a c function
amstan 1d1db6f
Rename keepalive to something easier to access
amstan 9002ac9
Added ringbuffer class
amstan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| __version__ = "0.1.0" | ||
|
|
||
| from cffi import FFI as _FFI | ||
| import types | ||
|
|
||
| _ffi = _FFI() | ||
| _ffi.cdef(""" | ||
|
|
@@ -187,6 +188,25 @@ | |
| void jack_set_info_function(void (*func)(const char*)); | ||
| void jack_free(void* ptr); | ||
|
|
||
| /* ringbuffer.h */ | ||
| typedef ... jack_ringbuffer_data_t ; | ||
| typedef ... jack_ringbuffer_t ; | ||
|
|
||
| jack_ringbuffer_t *jack_ringbuffer_create(size_t sz); | ||
| void jack_ringbuffer_free(jack_ringbuffer_t *rb); | ||
| /* not implemented: jack_ringbuffer_get_read_vector, really fast uses only */ | ||
| /* not implemented: jack_ringbuffer_get_write_vector, really fast uses only */ | ||
| size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt); | ||
| size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt); | ||
| void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt); | ||
| size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb); | ||
| int jack_ringbuffer_mlock(jack_ringbuffer_t *rb); | ||
| void jack_ringbuffer_reset(jack_ringbuffer_t *rb); | ||
| /* not implemented: jack_ringbuffer_reset_size */ | ||
| size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt); | ||
| /* not implemented: jack_ringbuffer_write_advance, really fast uses only */ | ||
| size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb); | ||
|
|
||
| /* transport.h */ | ||
|
|
||
| int jack_transport_locate(jack_client_t* client, jack_nframes_t frame); | ||
|
|
@@ -205,7 +225,14 @@ | |
| #define EEXIST 17 | ||
| """) | ||
|
|
||
| _lib = _ffi.dlopen("jack") | ||
|
|
||
| _verify_includes = """ | ||
| #include <jack/jack.h> | ||
| #include <jack/ringbuffer.h> | ||
| #include <jack/statistics.h> | ||
| #include <jack/types.h> | ||
| """ | ||
| _lib = _ffi.verify(_verify_includes, libraries = ["jack"]) | ||
|
|
||
| CALL_AGAIN = 0 | ||
| """Possible return value for process callback.""" | ||
|
|
@@ -254,7 +281,8 @@ def __iter__(self): | |
| def __repr__(self): | ||
| return self._portlist.__repr__() | ||
|
|
||
| def register(self, shortname, is_terminal=False): | ||
| def register(self, shortname, is_terminal=False, is_physical=False, | ||
| is_midi=False): | ||
| """Create a new input/output port. | ||
|
|
||
| The new :class:`OwnPort` object is automatically added to | ||
|
|
@@ -282,6 +310,11 @@ def register(self, shortname, is_terminal=False): | |
| Audio synthesizers, I/O hardware interface clients, HDR | ||
| systems are examples of clients that would set this flag for | ||
| their ports. | ||
| is_physical : bool | ||
| If ``True`` the port corresponds to some kind of physical | ||
| I/O connector. | ||
| is_midi : bool | ||
| If ``True`` the port will be a midi port, JACK_DEFAULT_MIDI_TYPE | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -290,7 +323,7 @@ def register(self, shortname, is_terminal=False): | |
|
|
||
| """ | ||
| port = self._client._register_port(shortname, is_terminal, | ||
| self._flag) | ||
| is_physical, is_midi, self._flag) | ||
| self._portlist.append(port) | ||
| return port | ||
|
|
||
|
|
@@ -305,7 +338,7 @@ def clear(self): | |
| while self._portlist: | ||
| self._portlist[0].unregister() | ||
|
|
||
| def __init__(self, name, use_exact_name=False, no_start_server=False, | ||
| def __init__(self, name, use_exact_name=False, no_start_server=True, | ||
| servername=None, session_id=None): | ||
| """Create a new JACK client. | ||
|
|
||
|
|
@@ -358,7 +391,7 @@ def __init__(self, name, use_exact_name=False, no_start_server=False, | |
|
|
||
| self._inports = self.Ports(self, _lib.JackPortIsInput) | ||
| self._outports = self.Ports(self, _lib.JackPortIsOutput) | ||
| self._keepalive = [] | ||
| self._callback_ptrs = {} | ||
|
|
||
| # Avoid confusion if something goes wrong before opening the client: | ||
| _ptr = _ffi.NULL | ||
|
|
@@ -736,13 +769,19 @@ def set_process_callback(self, callback, userdata=None): | |
| is called. | ||
|
|
||
| """ | ||
| @self._callback("JackProcessCallback", error=STOP_CALLING) | ||
| def callback_wrapper(frames, _): | ||
| return callback(frames, userdata) | ||
|
|
||
| _check(_lib.jack_set_process_callback( | ||
| self._ptr, callback_wrapper, _ffi.NULL), | ||
| "Error setting process callback") | ||
| if type(callback) == types.FunctionType: | ||
| @self._callback("JackProcessCallback", error=STOP_CALLING) | ||
| def callback_wrapper(frames, _): | ||
| return callback(frames, userdata) | ||
| _check(_lib.jack_set_process_callback( | ||
| self._ptr, callback_wrapper, _ffi.NULL), | ||
| "Error setting process callback") | ||
| else: | ||
| if userdata is None: | ||
| userdata = _ffi.NULL | ||
| _check(_lib.jack_set_process_callback( | ||
| self._ptr, callback, _ffi.cast("void *", userdata)), | ||
| "Error setting process callback") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't repeat the call to |
||
|
|
||
| def set_freewheel_callback(self, callback, userdata=None): | ||
| """Register freewheel callback. | ||
|
|
@@ -1213,17 +1252,20 @@ def _callback(self, cdecl, **kwargs): | |
| """Wrapper for ffi.callback() that keeps callback alive.""" | ||
| def callback_decorator(python_callable): | ||
| function_ptr = _ffi.callback(cdecl, python_callable, **kwargs) | ||
| self._keepalive.append(function_ptr) | ||
| self._callback_ptrs[cdecl] = function_ptr | ||
| return function_ptr | ||
| return callback_decorator | ||
|
|
||
| def _register_port(self, shortname, is_terminal, flags): | ||
| def _register_port(self, shortname, is_terminal, is_physical, | ||
| is_midi, flags): | ||
| """Create a new port.""" | ||
| if is_terminal: | ||
| flags |= _lib.JackPortIsTerminal | ||
| if is_physical: | ||
| flags |= _lib.JackPortIsPhysical | ||
| port_type = ["32 bit float mono audio", "8 bit raw midi"][is_midi].encode() | ||
| port_ptr = _lib.jack_port_register(self._ptr, shortname.encode(), | ||
| "32 bit float mono audio".encode(), | ||
| flags, 0) | ||
| port_type, flags, 0) | ||
| if not port_ptr: | ||
| raise JackError( | ||
| "{0!r}: port registration failed".format(shortname)) | ||
|
|
@@ -1514,6 +1556,69 @@ def get_array(self): | |
| return np.frombuffer(self.get_buffer(), dtype=np.float32) | ||
|
|
||
|
|
||
| def _maybe_bytes(data): | ||
| if type(data) is bytes: | ||
| data_ptr = _ffi.new("char[]", len(data)) | ||
| data_ptr[0:len(data)] = data | ||
| return data_ptr | ||
| return data | ||
|
|
||
| def _returns_bytes(fun): | ||
| def wrapper(self, cnt): | ||
| ret_cnt, data = fun(self, cnt) | ||
| return bytes(_ffi.buffer(data, min(cnt, ret_cnt))) | ||
| return wrapper | ||
|
|
||
| class RingBuffer(object): | ||
| def __init__(self, size=256): | ||
| ptr = _lib.jack_ringbuffer_create(size) | ||
| if not ptr: | ||
| raise JackError( | ||
| "Could not create ringbuffer of size {0}".format(size)) | ||
| self._ptr = _ffi.gc(ptr, _lib.jack_ringbuffer_free) | ||
| self._size = size | ||
|
|
||
| def _read(self, cnt): | ||
| data = _ffi.new("char[]", cnt) | ||
| ret_cnt = _lib.jack_ringbuffer_read(self._ptr, data, _ffi.sizeof(data)) | ||
| return ret_cnt, data | ||
| read = _returns_bytes(_read) | ||
|
|
||
| def _peek(self, cnt): | ||
| data = _ffi.new("char[]", cnt) | ||
| ret_cnt = _lib.jack_ringbuffer_peek(self._ptr, data, _ffi.sizeof(data)) | ||
| return ret_cnt, data | ||
| peek = _returns_bytes(_peek) | ||
|
|
||
| def write(self, data): | ||
| data = _maybe_bytes(data) | ||
| return _lib.jack_ringbuffer_write(self._ptr, data, _ffi.sizeof(data)) | ||
|
|
||
| def read_advance(self, cnt): | ||
| _lib.jack_ringbuffer_read_advance(self._ptr, cnt) | ||
|
|
||
| def mlock(self): | ||
| return _lib.jack_ringbuffer_mlock(self._ptr) | ||
|
|
||
| def reset(self): | ||
| _lib.jack_ringbuffer_reset(self._ptr) | ||
|
|
||
| @property | ||
| def read_space(self): | ||
| return _lib.jack_ringbuffer_read_space(self._ptr) | ||
|
|
||
| @property | ||
| def write_space(self): | ||
| return _lib.jack_ringbuffer_write_space(self._ptr) | ||
|
|
||
| @property | ||
| def size(self): | ||
| return self._size | ||
|
|
||
| def __repr__(self): | ||
| return "jack.{0.__class__.__name__}({0.read_space}/{0.size})".format(self) | ||
|
|
||
|
|
||
| class Status(object): | ||
|
|
||
| """Representation of the JACK status bits.""" | ||
|
|
@@ -1698,10 +1803,10 @@ def _set_some_function(callback, setter): | |
| def callback_wrapper(msg): | ||
| return callback(_ffi.string(msg).decode()) | ||
|
|
||
| _keepalive[setter] = callback_wrapper | ||
| _callback_ptrs[setter] = callback_wrapper | ||
| setter(callback_wrapper) | ||
|
|
||
| _keepalive = {} | ||
| _callback_ptrs = {} | ||
|
|
||
|
|
||
| def _check(error_code, msg): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is too strict. I'd like to support any Python callable, not just proper functions.
Can't you turn the logic around and check for a function pointer?
E.g. check for
CDataand then withtypeoflike injack.Port.__init__()?