Skip to main content

Async Python interface for Ableton Link

Project description

aalink is a Python wrapper for Ableton Link built for interactive applications using asyncio event loops.

It provides a simple programming interface for writing concurrent Python code synchronized to a beat. The beat can optionally be time-aligned with other peers in an Ableton Link session.

Installation

aalink requires at least Python 3.8. It can be installed using pip:

pip3 install aalink

It may be required to install the latest version of MSVC Runtime libraries on Windows to use the binary wheels currently hosted on PyPI.

Usage

aalink uses asyncio. To connect to a Link session, create a Link object, passing the asyncio event loop to the constructor, and await for Link.sync() as follows:

import asyncio

from aalink import Link

async def main():
    loop = asyncio.get_running_loop()

    link = Link(120, loop)
    link.enabled = True

    while True:
        await link.sync(1)
        print('bang!')

asyncio.run(main())

Link.sync(n) returns a Future scheduled to be done when Link time reaches next n-th beat on the timeline.

In the above example, awaiting for link.sync(1) will pause and resume the main coroutine at beats 1, 2, 3, and so on.

Keep in mind that awaiting for sync(n) does not cause a coroutine to sleep for the given number of beats. Regardless of the moment when the coroutine is suspended, it will resume when the next closest n-th beat is reached on the shared Link timeline, e.g. awaiting for sync(2) at beat 11.5 will resume at beat 12.

Non-integral beat syncing is supported. For example:

await link.sync(1/2) # resumes at beats 0.5, 1, 1.5...
await link.sync(3/2) # resumes at beats 1.5, 3, 4.5...

Sync events can be scheduled with an offset (also expressed in beats) by passing an offset argument to sync(). Use this to add groove to the coroutine rhythm.

async def arpeggiate():
    for i in range(16):
        swing = 0.25 if i % 2 == 1 else 0

        await link.sync(1/2, offset=swing)
        print('###', i)

        await link.sync(1/2, offset=0)
        print('@@@', i)

Combine synced coroutines to run in series or concurrently:

import asyncio
from aalink import Link

async def main():
    loop = asyncio.get_running_loop()

    link = Link(120, loop)
    link.enabled = True

    async def sequence(name):
        for i in range(4):
            await link.sync(1)
            print('bang!', name)

    await sequence('a')
    await sequence('b')

    await asyncio.gather(sequence('c'), sequence('d'))

asyncio.run(main())

Limitations

aalink aims to be punctual, but it is not 100% accurate due to the processing delay in the internal scheduler and the uncertainty of event loop iterations timing.

For convenience, the numerical values of futures returned from sync() aren’t equal to the exact beat time from the moment the futures are done. They correspond to the previously estimated resume times instead.

b = await link.sync(1) # b will be 1.0, returned at beat 1.00190
b = await link.sync(1) # b will be 2.0, returned at beat 2.00027
b = await link.sync(1) # b will be 3.0, returned at beat 3.00005

License

Copyright (c) 2023 Artem Popov <art@artfwo.net>

aalink is licensed under the GNU General Public License (GPL) version 3. You can find the full text of the GPL license in the LICENSE file included in this repository.

aalink includes code from pybind11 and Ableton Link.

pybind11

Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>, All rights reserved.

pybind11 license

Ableton Link

Copyright 2016, Ableton AG, Berlin. All rights reserved.

Ableton Link license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aalink-0.0.7.tar.gz (5.6 MB view details)

Uploaded Source

Built Distributions

aalink-0.0.7-cp313-cp313-win_amd64.whl (174.9 kB view details)

Uploaded CPython 3.13 Windows x86-64

aalink-0.0.7-cp313-cp313-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (234.1 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp313-cp313-macosx_11_0_x86_64.whl (193.9 kB view details)

Uploaded CPython 3.13 macOS 11.0+ x86-64

aalink-0.0.7-cp313-cp313-macosx_11_0_arm64.whl (182.2 kB view details)

Uploaded CPython 3.13 macOS 11.0+ ARM64

aalink-0.0.7-cp312-cp312-win_amd64.whl (174.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

aalink-0.0.7-cp312-cp312-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (234.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp312-cp312-macosx_11_0_x86_64.whl (193.8 kB view details)

Uploaded CPython 3.12 macOS 11.0+ x86-64

aalink-0.0.7-cp312-cp312-macosx_11_0_arm64.whl (182.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

aalink-0.0.7-cp311-cp311-win_amd64.whl (174.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

aalink-0.0.7-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (235.2 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp311-cp311-macosx_11_0_x86_64.whl (193.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ x86-64

aalink-0.0.7-cp311-cp311-macosx_11_0_arm64.whl (182.5 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

aalink-0.0.7-cp310-cp310-win_amd64.whl (173.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

aalink-0.0.7-cp310-cp310-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (233.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp310-cp310-macosx_11_0_x86_64.whl (192.2 kB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

aalink-0.0.7-cp310-cp310-macosx_11_0_arm64.whl (181.1 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

aalink-0.0.7-cp39-cp39-win_amd64.whl (172.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

aalink-0.0.7-cp39-cp39-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (234.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp39-cp39-macosx_11_0_x86_64.whl (192.5 kB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

aalink-0.0.7-cp39-cp39-macosx_11_0_arm64.whl (181.2 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

aalink-0.0.7-cp38-cp38-win_amd64.whl (173.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

aalink-0.0.7-cp38-cp38-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

aalink-0.0.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (233.4 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

aalink-0.0.7-cp38-cp38-macosx_11_0_x86_64.whl (192.1 kB view details)

Uploaded CPython 3.8 macOS 11.0+ x86-64

aalink-0.0.7-cp38-cp38-macosx_11_0_arm64.whl (181.0 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page