Skip to content

Add transport_query() method on Client - #10

Closed
Vampouille wants to merge 7 commits into
spatialaudio:masterfrom
Vampouille:add_transport_query
Closed

Vampouille wants to merge 7 commits into
spatialaudio:masterfrom
Vampouille:add_transport_query

Conversation

@Vampouille

Copy link
Copy Markdown
Contributor

Hello,

I added transport_query() method in order to get current transport position and BPM.

I added several C types from /usr/include/jack/types.h
package : libjack-jackd2-dev 1.9.9.5+201306 (Linux Mint 17.1 64bit)
I also added a new attribute to Client class : 'position' to avoid malloc() in transport_query() method

@mgeier

mgeier commented Apr 13, 2015

Copy link
Copy Markdown
Member

Thanks a lot for this PR!

Can you please:

  • put the C function declarations in the same order as they are in the original header files?
  • remove the documentation from the C function declarations and move them to the appropriate Python function docstrings?
  • remove whitespace from the C declarations to match the rest of the cdef() string?

Comment thread jack.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to allocate the memory during initialization, but the attribute shouldn't be public, you should probably call it self._position or similar.

It would probably be in better company a few lines further down, e.g. after self._keepalive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change attribute name to '_position' and move initialization at the
end of constructor.

Le 13/04/2015 20:16, Matthias Geier a écrit :

In jack.py
#10 (comment):

@@ -276,6 +355,7 @@ def init(self, name, use_exact_name=False, no_start_server=False,

     """
     status = _ffi.new("jack_status_t*")
  •    self.position = _ffi.new("jack_position_t*")
    

It's a good idea to allocate the memory during initialization, but the
attribute shouldn't be public, you should probably call it
|self._position| or similar.

It would probably be in better company a few lines further down, e.g.
after |self._keepalive|?

—
Reply to this email directly or view it on GitHub
https://github.com/spatialaudio/jackclient-python/pull/10/files#r28263559.

@mgeier

mgeier commented Apr 13, 2015

Copy link
Copy Markdown
Member

What about returning something more Pythonic by default (e.g. a dict), and providing an argument (e.g. return_dict=False) to return the CFFI struct for realtime usage?

The former could also do the consistency check unique_1 == unique_2.

@Vampouille

Copy link
Copy Markdown
Contributor Author

Le 13/04/2015 20:22, Matthias Geier a écrit :

What about returning something more Pythonic by default (e.g. a
|dict|), and providing an argument (e.g. |return_dict=False|) to
return the CFFI struct for realtime usage?

I just start python some days ago so I'm sure my code can be more
Pythonic. You're help is welcome!

About dealing with CFFI struct for realtime usage :

My first idea was to hide CFFI struct with a class suitable for realtime
usage that take CFFI struct as constructor argument. This class will
have methods to access each fields (or group of fields) of the CFFI struct :

class Position():
def init(self,cffi_data):
self._data = cffi_data

 def get_bar_beat_tick(self):
     return (self._data.bar, self._data.beat, self._data.tick)

 def get_frame(self):
     return self._data.frame
 ...

constructor only store reference to the CFFI struct so it should be ok
for realtime?
This class will avoid copy of all data ? (and it can be documented)

As you suggest, I will add a method to return a complete dict with all
values in correct python type.

The former could also do the consistency check |unique_1 == unique_2|.

Should I check consistency (|unique_1 == unique_2|) before returning
position ?

Should I send you another pull request or can you check my fork ? I
don't known if I can make modification on the current pull request.

Best,
Julien

@mgeier

mgeier commented Apr 14, 2015

Copy link
Copy Markdown
Member

First of all, Python isn't really reliable for realtime stuff.
Many things may work quite well, but there isn't really a guarantee for anything.

I made these Python bindings mainly to be able to do the non-realtime stuff (connecting ports and so on).
I also implemented some of the realtime stuff, but mostly out of curiosity.

If you want a more reliable system, you should use C or C++.

That said, I still want to try to make the library as useful, reliable and efficient as is possible.

My first idea was to hide CFFI struct with a class suitable for realtime
usage that take CFFI struct as constructor argument.
This class will
have methods to access each fields (or group of fields) of the CFFI struct :

class Position():
     def __init__(self,cffi_data):
         self._data = cffi_data

     def get_bar_beat_tick(self):
         return (self._data.bar, self._data.beat, self._data.tick)

     def get_frame(self):
         return self._data.frame
     ...

Getter methods are un-Pythonic, you would use properties here:

    @property
    def bar_beat_tick(self):
        return self._data.bar, self._data.beat, self._data.tick

constructor only store reference to the CFFI struct so it should be ok
for realtime?
This class will avoid copy of all data ? (and it can be documented)

It doesn't really make a difference if you create a class instance or a dict, because the class instance also holds a dict (or at least something similar).

So no, a class isn't better than a dict, because both will have to be allocated.

OTOH, Python uses some kind of memory pool for small objects, but I don't really know any details and I don't know if we should count on this.

As you suggest, I will add a method to return a complete dict with all
values in correct python type.

The former could also do the consistency check |unique_1 == unique_2|.

Should I check consistency (|unique_1 == unique_2|) before returning position ?

I think this would be good, since that's what those strange fields are there for.

But if you make two different options - one for efficiency and one for Pythonicity - you should probably do this only on the latter.

Should I send you another pull request or can you check my fork ?

You can make another pull request if you want, but normally you would just add more commits to your branch "add_transport_query".
When you push those commits to your repository, they will automagically be added to this PR.
But I guess you've found that out already ...

@mgeier

mgeier commented Apr 14, 2015

Copy link
Copy Markdown
Member

BTW, you'll probably have to put some of the structs into a separate cdef() and use the packed argument on it, see https://cffi.readthedocs.org/.

@mgeier

mgeier commented Apr 18, 2015

Copy link
Copy Markdown
Member

I think the Transport class you added in 7b6dfe9 adds far too much code for this little functionality.
There is a lot of boiler plate just to replicate the attributes which are already available on the CFFI struct object.

What about creating a dict by means of a for loop iterating over a list of attributes specified by you?

Or probably it's better to make a much simpler function (similar to what you had before) right now that only returns the CFFI struct (and the integer representing the transport state) and later, in a separate PR we try to make this more Pythonic?

@Vampouille

Copy link
Copy Markdown
Contributor Author

I just make a new commit without Transport class.

Le 18/04/2015 20:10, Matthias Geier a écrit :

I think the |Transport| class you added in 7b6dfe9
7b6dfe9
adds far too much code for this little functionality.
There is a lot of boiler plate just to replicate the attributes which
are already available on the CFFI struct object.

What about creating a |dict| by means of a |for| loop iterating over a
list of attributes specified by you?

Or probably it's better to make a much simpler function (similar to
what you had before) right now that only returns the CFFI struct (and
the integer representing the transport state) and later, in a separate
PR we try to make this more Pythonic?

—
Reply to this email directly or view it on GitHub
#10 (comment).

Comment thread jack.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the declarations in the same order as in the original header files.
This avoids repeated declarations, like for example jack_nframes_t.

@mgeier

mgeier commented May 4, 2015

Copy link
Copy Markdown
Member

What about #10 (comment)?

I think we should use the packed argument for the structs which have PRE_PACKED_STRUCTURE and POST_PACKED_STRUCTURE, right?

Comment thread jack.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a single space around the equals sign and a comma at the end of the last line.

@Vampouille

Copy link
Copy Markdown
Contributor Author

I add another cdef() with packed argument.

mgeier pushed a commit that referenced this pull request May 10, 2015
@mgeier

mgeier commented May 10, 2015

Copy link
Copy Markdown
Member

I squashed your commits into f8a21a8 and a pushed a few follow-up commits.

Thanks a lot for this PR!

@mgeier mgeier closed this May 10, 2015
@Vampouille
Vampouille deleted the add_transport_query branch May 11, 2015 14:03
@mgeier mgeier added this to the 0.3.0 milestone May 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants