Intelligent-Instruments-Lab / iil-python-tools

Central repo for python projects including: iipyper (easy OSC and MIDI handling) and notochord (a probabilistic model for real-time MIDI performance built in pytorch)
MIT License
33 stars 4 forks source link

iipyper: @repeat should provide accurate timing and warnings #11

Open jarmitage opened 2 years ago

jarmitage commented 2 years ago

e.g.

def repeat(_time):
    # close the decorator over time argument
    def decorator(f):
        # define the coroutine
        async def g():
            # call `f` every `time` seconds
            while True:
                t1 = time.time()
                f()
                t2 = time.time()
                delta_t = t2 - t1
                sleep_time = _time-delta_t

                if (sleep_time < 0):
                    print("Warning: repeat@ sleep time < 0")
                    await asyncio.sleep(0)
                else:
                    await asyncio.sleep(sleep_time)

        # track the coroutine in a global list
        _loop_fns.append(g)

    return decorator
jarmitage commented 1 year ago

With _time < 0.01, it seems that having an OSC handler function

@osc.args("/*")
def _(address, *args):
    # handle osc

Will slow down @repeat by a significant factor, >2x for more than a few messages received per second

jarmitage commented 1 year ago

It might be worth creating a branch of iipyper that uses osc4py3 under the hood, to see if the throttling is coming from python-osc or not