Repository navigation
Add examples for for JACK transport and timebase master - #74
SpotlightKid wants to merge 17 commits into
Conversation
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
mgeier
left a comment
There was a problem hiding this comment.
That looks great, thanks!
I've just tried it quickly and it works nicely, but I'd like to have a closer look at the code within the next few days.
For now, I've only found one small bug:
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
c403db4 to
d4e3852
Compare
…g examples Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
| self.bpm = bpm | ||
| self.conditional = conditional | ||
| self.debug = debug | ||
| self.ticks_per_beat = int(ticks_per_beat) |
There was a problem hiding this comment.
Why is this int() here?
According to http://jackaudio.org/files/docs/html/structjack__position__t.html, a double is expected.
There was a problem hiding this comment.
That doesn't make much sense to me, musically, but I can convert the instance attribute via float() when setting the pos attribute.
Also, since tick is an int32_t, we need to be careful when assigning expressions to it that involve ticks_per_beat, that the type of the expression result is an int, otherwise cffi will throw an error.
I noticed that beats_per_bar and beat_type are also of type float (again, makes no sense to me, musically), so I changed the code to convert these as well when assigning to pos, while keeping the instance attributes as type int: e5c018e
There was a problem hiding this comment.
That doesn't make much sense to me, musically
Probably not, probably it does?
I would simply allow float values because JACK allows them.
we need to be careful when assigning expressions to it
Yes, but it doesn't help if we assign an int to ticks_per_beat, because it will be immediately converted to a double.
again, makes no sense to me, musically
Again, I don't know, I would still allow it if JACK thinks that's a good idea.
keeping the instance attributes as type
int
I would just pass it through to the JACK functions, CFFI will check it and complain if it isn't right.
I think converting the inputs with int() is not a good idea, because it silently converts user-provided float values.
If you really want to require ints (which I think you shouldn't), you should at least fail loudly when the uses tries to use a float value.
Also, it's very confusing if your local copy of the values have different data types than the values in the pos struct, isn't it?
| except (TypeError, ValueError): | ||
| print("Error: invalid meter: %s\n" % args.meter) | ||
| ap.print_help() | ||
| return 2 |
There was a problem hiding this comment.
I would use ap.error(...) for this.
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.error
There was a problem hiding this comment.
ArgumentParser.error() terminates the script, which is unwanted here for reasons explained below your comment on the last line of the script.
| try: | ||
| tbmaster = TimebaseMasterClient( | ||
| args.client_name, | ||
| bpm=max(0.0, min(300.0, args.tempo)), |
There was a problem hiding this comment.
Why are you silently limiting this instead of raising an error?
Why are you limiting it at all?
I didn't know that, but JACK seems to allow negative BPM values ...
There was a problem hiding this comment.
Because BPM values above 300 aren't very musically useful, IMHO.
I could have implemented a custom argparse argument checker, but it seems overkill here. It's just an example, after all.
And how would a negative BPM work? Come to think of it, 0 BPM makes no sense either.
I changed the clamping to the range 0.1 - 300.0 and updated the param help to show the min/max value in da75599.
There was a problem hiding this comment.
aren't very musically useful
maybe, maybe not.
I think it is much more interesting to see how JACK reacts to certain values, than what restrictions were added in this example.
I could have implemented a custom
argparseargument checker
I wouldn't check the range at all.
And how would a negative BPM work? Come to think of it, 0 BPM makes no sense either.
I don't know any of this.
But I would certainly be interested how JACK handles those values, and if you are limiting them in the example, I can't easily find out.
| ticks_per_beat=args.ticks_per_beat, | ||
| debug=args.debug) | ||
| except jack.JackError as exc: | ||
| return "Could not create timebase master JACK client: %s" % exc |
There was a problem hiding this comment.
Here you could use ap.exit(...).
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.exit
There was a problem hiding this comment.
Same reason not use it as for ArgumentParser.error()
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main(sys.argv[1:]) or 0) |
There was a problem hiding this comment.
Since this is the default behavior, you don't really have to pass anything to main(), right?
There was a problem hiding this comment.
I use the following strategy for all my command line scripts (imports not shown):
def main(args=None):
ap = argparse.ArgumentParser()
args = ap.parse_args(args if args is not None else sys.argv[1:])
if __name == '__main__':
sys.exit(main(sys.argv[1:]) or 0)This allows the following:
- You can call the script directly with
python myscript.py(args are passed via the call tomain()). - You can use the script as a
console_scriptentry point (console_scripts = ['myscript = mypkg.myscript:main]) (main()is called without arguments and the command line args have to be retrieved viasys.argv). - You can
importthe script in another Python file and call themain()function manually, passing the command line as a string. - The
sys.exit(main(...) or 0)allows themain()function to just return an exit code or an error string to set the program exit code or print an error code tostderr, but when you callmain()manually, you can still use the return value (ArgumentParser.parse_args()may still terminate the program, so this isn't perfect). ReturningNone(the default with noreturnstatement), sets the exit code to 0 (success).
There was a problem hiding this comment.
As discussed above ap.parse_args(args if args is not None else sys.argv[1:]) can be shortened to ap.parse_args(args).
There was a problem hiding this comment.
I think there are two different things:
- calling
main()vsmain(sys.argv[1:])(your first 3 points) - terminating on error or returning something (your last point)
AFAICT, there is no disadvantage in calling main() without arguments here, it works exactly the same in all mentioned cases.
Your last point is a separate thing.
I guess it's possible to do it that way, but I'm not sure if you are gaining anything. It probably even makes things worse.
On one hand, it might be a YAGNI situation just adding useless noise.
Did you ever hear of a usage where this mattered?
On the other hand, I'm not sure what you would even do when you get an "error value" except for terminating the program.
Is there a conceivable situation where you wouldn't want to terminate the program?
And even if there is a very teeny tiny chance that somebody reeeeally doesn't want to terminate, they could still catch SystemExit, right?
(
ArgumentParser.parse_args()may still terminate the program, so this isn't perfect)
Exactly, far from it.
It's actually an extremely common case.
And I think this is the main argument here.
Why should a wrong --tempo argument take a completely different code path than a wrong --meter argument?
All in all, it sounds like much noise for little (or no?) gain, doesn't it?
|
You have already changed a few double quotes to single quotes, but for my taste you could change even more. I haven't been completely consistent TBH, but #77 should improve this. |
|
Thanks for the review! I'm currently a bit busy, but I will try to address these issues / questions in a few days. |
| pos.bar_start_tick = pos.bar * pos.beats_per_bar * pos.ticks_per_beat | ||
| pos.bar += 1 # adjust start to bar 1 | ||
| else: | ||
| # Compute BBT info based on previous period. |
There was a problem hiding this comment.
I'm not sure, but don't you have to check pos.valid here?
If it's guaranteed to be valid, you should probably add an assert here.
There was a problem hiding this comment.
My interpretation of http://jackaudio.org/api/structjack__position__t.html is that pos.valid is only set by the timebase master. I'll ask around whether that's true.
There was a problem hiding this comment.
OK, sounds interesting.
What about slapping an assert in there for now, just to see if it ever happens?
We can remove it if it turns out to be wrong.
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
…onvert to float when assigning to transport position Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
…Kid/jackclient-python into enhancement/transport-examples Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
… already Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
|
@mgeier Please review again. |
| self.bpm = bpm | ||
| self.conditional = conditional | ||
| self.debug = debug | ||
| self.ticks_per_beat = int(ticks_per_beat) |
There was a problem hiding this comment.
That doesn't make much sense to me, musically
Probably not, probably it does?
I would simply allow float values because JACK allows them.
we need to be careful when assigning expressions to it
Yes, but it doesn't help if we assign an int to ticks_per_beat, because it will be immediately converted to a double.
again, makes no sense to me, musically
Again, I don't know, I would still allow it if JACK thinks that's a good idea.
keeping the instance attributes as type
int
I would just pass it through to the JACK functions, CFFI will check it and complain if it isn't right.
I think converting the inputs with int() is not a good idea, because it silently converts user-provided float values.
If you really want to require ints (which I think you shouldn't), you should at least fail loudly when the uses tries to use a float value.
Also, it's very confusing if your local copy of the values have different data types than the values in the pos struct, isn't it?
| self.stop_event = Event() | ||
| self.set_shutdown_callback(self.shutdown) | ||
|
|
||
| def shutdown(self, status, reason): |
There was a problem hiding this comment.
That indeed sounds a bit much.
Strictly speaking, all instance variables should have an underscore, but I think this would be distracting in this simple example.
But I still think it would make sense for the two callback functions to have the same private-ness, wouldn't it?
| pos.beats_per_minute = self.bpm | ||
| pos.beat_type = self.beat_type | ||
| pos.ticks_per_beat = self.ticks_per_beat | ||
| pos.valid |= jack._lib.JackPositionBBT |
There was a problem hiding this comment.
The more I think about it, the less I understand ...
I have the feeling that the JACK API isn't 100% consistent in itself and this isn't possible to get right without some additional assumptions about the behavior of other clients.
I would keep it the same as the JACK example, but if you are confident |= is the right thing to do, that's also fine by me.
I think that should be in a separate PR.
Yes, sure.
But that PR would have to be merged before this PR?
| args = ap.parse_args(args if args is not None else sys.argv[1:]) | ||
|
|
||
| try: | ||
| beats_per_bar, beat_type = (int(x) for x in args.meter.split('/', 1)) |
There was a problem hiding this comment.
You get a better error
ValueError: invalid literal for int() with base 10
vs.
ValueError: too many values to unpack (expected 2)
I would argue that the second is better, especially since the error for too few / characters is very similar in both situations.
But it doesn't matter at all, since you are catching this exception, and nobody ever sees it, right?
| args = ap.parse_args(args) | ||
|
|
||
| try: | ||
| beats_per_bar, beat_type = (int(x) for x in args.meter.split('/', 1)) |
There was a problem hiding this comment.
BTW, this would be a perfect opportunity to use map():
map(int, args.meter.split('/'))There was a problem hiding this comment.
... or of course with float, if we choose to allow that.
| try: | ||
| tbmaster = TimebaseMasterClient( | ||
| args.client_name, | ||
| bpm=max(0.0, min(300.0, args.tempo)), |
There was a problem hiding this comment.
aren't very musically useful
maybe, maybe not.
I think it is much more interesting to see how JACK reacts to certain values, than what restrictions were added in this example.
I could have implemented a custom
argparseargument checker
I wouldn't check the range at all.
And how would a negative BPM work? Come to think of it, 0 BPM makes no sense either.
I don't know any of this.
But I would certainly be interested how JACK handles those values, and if you are limiting them in the example, I can't easily find out.
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
|
Sorry @SpotlightKid, I kinda forgot about this PR ... Thanks for your recent changes, what do you think about my still open comments? There is still a lack of an official way to use |
|
Since |
TBH, I don't really care too much, just change these things it in the way you like. |
OK, cool, will do. But I'd first like to hear your opinion on which of those options is the best:
I have the feeling that adding only one of them would suffice for now. |
|
I personally would prefer |
Squashed from spatialaudio#74. Closes spatialaudio#19.
Has been squashed from and closes spatialaudio#74. Closes spatialaudio#19.
|
@SpotlightKid I've re-based and squashed your commits from this PR into #83 and added a few commits there. |
Follow up to #72. Obsoletes #19.
Signed-off-by: Christopher Arndt chris@chrisarndt.de