Skip to content
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

Less jitter with spin_sleep() #7

Open
ghost opened this issue Dec 24, 2020 · 1 comment
Open

Less jitter with spin_sleep() #7

ghost opened this issue Dec 24, 2020 · 1 comment

Comments

@ghost
Copy link

ghost commented Dec 24, 2020

For anyone who is interested. I built sequencer based on aiotone that is sync-master and drives my DAW. I had quite some jitter compared to other midi-sequencers. Using spin_sleep() in the main loop, there is much less jitter. Of course there is a tradeoff: higher CPU load.

sleep_resolution is platform dependent. On linux 0.001s should be a good value. I am not aware of a good way to detect sleep_resolution.

import asyncio
from time import time

sleep_resolution = 0.001


async def spin_sleep(sleep_time):
    deadline = sleep_time + time()
    await asyncio.sleep(sleep_time - sleep_resolution)
    while deadline > time():
        await asyncio.sleep(0)

Currently my code isn't in a state for sharing, but I will put it on github soonish.

@ghost
Copy link
Author

ghost commented Dec 24, 2020

Here it is: https://github.com/bltbitblt/mus

I guess the most interesting part is: https://github.com/bltbitblt/mus/commit/a38e3d26a36f6964954f9617b1c807a875293d00

pulses is now a float and it waits til the second last pulse and sleeps the rest using spin_sleep().

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

No branches or pull requests

0 participants