Skip to content

Add examples for for JACK transport and timebase master - #74

Closed
SpotlightKid wants to merge 17 commits into
spatialaudio:masterfrom
SpotlightKid:enhancement/transport-examples
Closed

SpotlightKid wants to merge 17 commits into
spatialaudio:masterfrom
SpotlightKid:enhancement/transport-examples

Conversation

@SpotlightKid

Copy link
Copy Markdown
Contributor

Follow up to #72. Obsoletes #19.

Signed-off-by: Christopher Arndt chris@chrisarndt.de

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>

@mgeier mgeier left a comment

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.

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:

Comment thread examples/timebase_master.py Resolved
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
@SpotlightKid
SpotlightKid force-pushed the enhancement/transport-examples branch from c403db4 to d4e3852 Compare September 14, 2019 08:05
…g examples

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Comment thread examples/timebase_master.py Resolved
Comment thread examples/timebase_master.py Resolved
Comment thread examples/timebase_master.py Resolved
Comment thread examples/timebase_master.py Resolved
self.bpm = bpm
self.conditional = conditional
self.debug = debug
self.ticks_per_beat = int(ticks_per_beat)

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.

Why is this int() here?

According to http://jackaudio.org/files/docs/html/structjack__position__t.html, a double is expected.

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.

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

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.

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

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.

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.

ArgumentParser.error() terminates the script, which is unwanted here for reasons explained below your comment on the last line of the script.

Comment thread examples/timebase_master.py Outdated
try:
tbmaster = TimebaseMasterClient(
args.client_name,
bpm=max(0.0, min(300.0, args.tempo)),

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.

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 ...

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.

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.

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.

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 argparse argument 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.

Comment thread examples/timebase_master.py Outdated
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

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.

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.

Same reason not use it as for ArgumentParser.error()

Comment thread examples/timebase_master.py Resolved
Comment thread examples/timebase_master.py Outdated


if __name__ == '__main__':
sys.exit(main(sys.argv[1:]) or 0)

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.

Since this is the default behavior, you don't really have to pass anything to main(), right?

@SpotlightKid SpotlightKid Sep 22, 2019 •

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 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 to main()).
  • You can use the script as a console_script entry point (console_scripts = ['myscript = mypkg.myscript:main]) (main() is called without arguments and the command line args have to be retrieved via sys.argv).
  • You can import the script in another Python file and call the main() function manually, passing the command line as a string.
  • The sys.exit(main(...) or 0) allows the main() function to just return an exit code or an error string to set the program exit code or print an error code to stderr, but when you call main() manually, you can still use the return value (ArgumentParser.parse_args() may still terminate the program, so this isn't perfect). Returning None (the default with no return statement), sets the exit code to 0 (success).

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.

As discussed above ap.parse_args(args if args is not None else sys.argv[1:]) can be shortened to ap.parse_args(args).

@mgeier mgeier Sep 24, 2019 •

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 think there are two different things:

  • calling main() vs main(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?

@mgeier

mgeier commented Sep 15, 2019

Copy link
Copy Markdown
Member

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.

@SpotlightKid

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

I'm currently a bit busy, but I will try to address these issues / questions in a few days.

Comment thread examples/timebase_master.py Resolved
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.

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'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.

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.

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.

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.

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>
@SpotlightKid

Copy link
Copy Markdown
Contributor Author

@mgeier Please review again.

self.bpm = bpm
self.conditional = conditional
self.debug = debug
self.ticks_per_beat = int(ticks_per_beat)

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.

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):

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.

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?

Comment thread examples/timebase_master.py Outdated
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

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.

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?

Comment thread examples/timebase_master.py Resolved
Comment thread examples/timebase_master.py Resolved
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))

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.

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))

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.

BTW, this would be a perfect opportunity to use map():

map(int, args.meter.split('/'))

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.

... or of course with float, if we choose to allow that.

Comment thread examples/timebase_master.py Outdated
try:
tbmaster = TimebaseMasterClient(
args.client_name,
bpm=max(0.0, min(300.0, args.tempo)),

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.

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 argparse argument 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>
@mgeier

mgeier commented Oct 31, 2019

Copy link
Copy Markdown
Member

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 jack._lib.JackPositionBBT. Do you want to implement this or should I?
Do you have a suggestion how we should do this?

@SpotlightKid

Copy link
Copy Markdown
Contributor Author

Since jack_position_bits_t is a bit field, i.e. values are meant to be ORed together, I would just export these as module level constants:

JACK_POSITION_BBT = _lib.JacKPositionBBT
JACK_POSITION_TIMECODE = _lib.JackPositionTimecode
JACK_POSITION_BBT_FRAME_OFFSET _lib.JackBBTFrameOffset
JACK_POSITION_AUDIO_VIDEO_RATIO = _lib.JackAudioVideoRatio
JACK_POSITION_VIDEO_FRAME_OFFSET = _lib.JackVideoFrameOffset

@SpotlightKid

Copy link
Copy Markdown
Contributor Author

what do you think about my still open comments?

TBH, I don't really care too much, just change these things it in the way you like.

@mgeier mgeier mentioned this pull request Nov 7, 2019
@mgeier

mgeier commented Nov 7, 2019 •

Copy link
Copy Markdown
Member

what do you think about my still open comments?

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.
If needed, the other one could be added later.

@SpotlightKid

Copy link
Copy Markdown
Contributor Author

I personally would prefer JACK_POSITION_* constants. I find dict-based interfaces cumbersome and ugly to use, and there's the danger of mistyping dict keys.

@mgeier

mgeier commented Jan 24, 2020

Copy link
Copy Markdown
Member

I was also skeptical about #78 and created a branch with the JACK_POSITION_*. I just seem to have forgotten to make PR, which I've done just now: #82.

mgeier pushed a commit to mgeier/jackclient-python that referenced this pull request Jan 27, 2020
mgeier added a commit to mgeier/jackclient-python that referenced this pull request Jan 27, 2020
mgeier pushed a commit to mgeier/jackclient-python that referenced this pull request Jan 27, 2020
mgeier added a commit to mgeier/jackclient-python that referenced this pull request Jan 27, 2020
@mgeier

mgeier commented Jan 27, 2020

Copy link
Copy Markdown
Member

@SpotlightKid I've re-based and squashed your commits from this PR into #83 and added a few commits there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants